<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 #default="scope">
            <span>
              {{
                scope && scope.row && scope.row.type === 0 ? '普通分销商'
                : scope && scope.row && scope.row.type === 1 ? '商户'
                : scope && scope.row && 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
          label="操作"
          fixed="right"
          width="160">
          <template #default="scope">
            <el-button
              type="text"
              :data-index="scope && scope.$index"
              @click="handleCopyClick">快捷复制</el-button>
            <el-button
              type="text"
              :data-index="scope && scope.$index"
              @click="handleEditClick">编辑</el-button>
            <el-button
              type="text"
              :data-index="scope && scope.$index"
              @click="handleDeleteClick">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分销商弹框 -->
    <ElDialog
      title="添加分销商"
      width="860px"
      v-model="addVisible">
      <AddDialog
        @close-dialog="addVisible = false"
        @updateList="getOtaSourceList"></AddDialog>
    </ElDialog>

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

    <el-dialog
      title="提示"
      width="420px"
      :visible.sync="deleteConfirmVisible">
      <div style="padding: 10px 0 18px;">此操作将永久删除，是否继续？</div>
      <div class="dialog-btn-wrap">
        <el-button @click="deleteConfirmVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmDeleteOtaSource">确定</el-button>
      </div>
    </el-dialog>

    <el-pagination
      background
      :hide-on-single-page="false"
      :page-size="10"
      :current-page="form.pageNum"
      @current-change="handlePage"
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getOtaSourceList, deleteOtaSource as deleteOtaSourceApi } 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: {},
      deleteConfirmVisible: false,
      pendingDeleteId: ''
    }
  },
  methods: {
    getRowByEvent (event) {
      const indexText = event && event.currentTarget && event.currentTarget.dataset
        ? event.currentTarget.dataset.index
        : ''
      const index = Number(indexText)
      if (Number.isNaN(index) || !this.tableData[index]) {
        return null
      }
      return this.tableData[index]
    },
    handleCopyClick (event) {
      const row = this.getRowByEvent(event)
      if (!row) return
      this.copyInfo(row)
    },
    handleEditClick (event) {
      const row = this.getRowByEvent(event)
      if (!row) return
      this.showDialog('edit', row)
    },
    handleDeleteClick (event) {
      const row = this.getRowByEvent(event)
      if (!row || !row.id) return
      this.deleteOtaSource(row.id)
    },
    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, index) {
      const sourceItem = item || {}
      let fullItem = sourceItem
      if (typeof index === 'number' && this.tableData[index]) {
        fullItem = this.tableData[index]
      } else if (sourceItem.id) {
        fullItem = this.tableData.find(row => String(row.id) === String(sourceItem.id)) || sourceItem
      }
      this.currentItem = { ...fullItem }
      this[`${type}Visible`] = true
    },
    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.pendingDeleteId = id
      this.deleteConfirmVisible = true
    },
    confirmDeleteOtaSource () {
      if (!this.pendingDeleteId) {
        this.deleteConfirmVisible = false
        return
      }
      deleteOtaSourceApi(this.pendingDeleteId).then(() => {
        this.$message.success('删除成功')
        this.deleteConfirmVisible = false
        this.pendingDeleteId = ''
        this.getOtaSourceList(true)
      })
    },
    handlePage (pageNum) {
      this.form.pageNum = pageNum
      this.getOtaSourceList()
    }
  }
}
</script>

<style scoped>

</style>
