<template>
  <div class="form-wrap">
    <div class="searchBox">
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        label-width="100px">
        <el-form-item
          label="分销商名称"
          prop="otaSourceName">
          <el-input v-model="form.otaSourceName"></el-input>
        </el-form-item>
        <el-form-item
          label="分销商编码"
          prop="otaSourceCode">
          <el-input v-model="form.otaSourceCode"></el-input>
        </el-form-item>
        <el-form-item
          label="描述"
          prop="otaSourceDesc">
          <el-input v-model="form.otaSourceDesc"></el-input>
        </el-form-item>
      </el-form>
      <div class="btn-wrap">
        <el-button
          type="primary"
          @click="getOtaSourceList">搜索</el-button>
        <el-button
          type="primary"
          v-if="canAddUsers"
          @click="showDialog('add')">新增分销商</el-button>
      </div>
    </div>
    <div class="tableBox">
      <!-- <div class="block-title">
        分销商列表
      </div> -->

      <el-table
        v-loading="loading"
        row-class-name="is-center"
        :data="tableData">
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="160">
        </el-table-column>
        <el-table-column
          prop="id"
          label="分销商ID"
          min-width="200">
        </el-table-column>
        <el-table-column
          prop="otaSourceName"
          label="分销商名称"
          min-width="140">
        </el-table-column>
        <el-table-column
          prop="otaSourceCode"
          label="分销商编码"
          min-width="140">
        </el-table-column>
        <el-table-column
          prop="type"
          label="分销商类型"
          width="120">
          <template slot-scope="scope">
            <span>
              {{
                scope.row.type === 0 ? '普通分销商'
                : scope.row.type === 1 ? '商户'
                : scope.row.type === 2 ? '总代'
                : '未知'
              }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="otaSourceDesc"
          label="分销商描述"
          min-width="180">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="otaSourceSecret"
          label="分销商密钥"
          width="180">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="createCallbackUrl"
          label="下单回调地址"
          min-width="200">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="checkCallbackUrl"
          label="核销回调地址"
          min-width="200">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="cancelCallbackUrl"
          label="退单回调地址"
          min-width="200">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="param"
          label="分销商参数"
          min-width="180">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="160">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="copyInfo(scope.row)">快捷复制</el-button>
            <el-button
              type="text"
              @click="showDialog('edit',scope.row)">编辑</el-button>
            <el-button
              type="text"
              @click="deleteOtaSource(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分销商弹框 -->
    <ElDialog
      title="添加分销商"
      width="860px"
      v-model="addVisible">
      <AddDialog @updateList="getOtaSourceList"></AddDialog>
    </ElDialog>

    <ElDialog
      title="编辑分销商"
      width="860px"
      v-model="editVisible">
      <EditDialog
        :current-item="currentItem"
        @updateList="getOtaSourceList"></EditDialog>
    </ElDialog>

    <el-pagination
      background
      :page-size="10"
      @current-change="handlePage"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getOtaSourceList, deleteOtaSource } from '@/api/platform'
import ElDialog from '@/components/Dialog'
import AddDialog from './otaSource/AddDialog'
import EditDialog from './otaSource/EditDialog'

export default {
  components: {
    ElDialog,
    AddDialog,
    EditDialog
  },
  computed: {
    canAddUsers () {
      return true
      // return this.$store.state.user.permissionList.includes('system_settings:add_users')
    }
  },

  created () {
    this.getOtaSourceList()
  },
  data () {
    return {
      loading: false,
      addVisible: false,
      editVisible: false,
      tableData: [],
      total: 0,
      form: {
        otaSourceName: '', // 分销商名
        otaSourceDesc: '', // 描述
        otaSourceCode: '', // 分销商编码
        pageNum: 1,
        pageSize: 10
      },
      // 当前分销商对象
      currentItem: {}
    }
  },
  methods: {
    copyInfo (row) {
      const info = `合作方名称: ${row.otaSourceName}\n合作方ID: ${row.id}\n合作方密钥: ${row.otaSourceSecret}\nhttp请求地址: ${window.location.origin}\nhttps请求地址: https://ota.hhtrip.com.cn/`
      this.copyTextToClipboard(info)
    },
    showDialog (type, item) {
      this[`${type}Visible`] = true
      this.currentItem = item
    },
    getOtaSourceList (goFirst) {
      this.loading = true
      if (goFirst) {
        this.form.pageNum = 1
      }
      getOtaSourceList(this.form).then(res => {
        this.loading = false
        this.tableData = res.data.records.map(item => {
          item.permissionList = (item.roleList || []).reduce((acc, role) => {
            return acc.concat(role.resourceList || [])
          }, [])
          return item
        }) || []
        this.total = res.data.total
      })
    },
    deleteOtaSource (id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteOtaSource(id).then(res => {
          this.$message.success('删除成功')
          this.getOtaSourceList(true)
        })
      })
    },
    handlePage (pageNum) {
      this.form.pageNum = pageNum
      this.getOtaSourceList()
    }
  }
}
</script>

<style scoped>

</style>
