<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper" v-if="hasPerm('partnerIndex:page')">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="名称">
              <a-input v-model="queryParam.name" allow-clear placeholder="请输入名称搜索"/>
            </a-form-item>
          </a-col>
          <a-col :md="16" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button type="primary" style="margin-left: 8px" v-if="hasPerm('partnerIndex:add')" icon="plus" @click="$refs.addForm.add()">新增</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.partnerId"
    >
      <span slot="partnerName" slot-scope="text">
        <ellipsis :length="30" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="partnerImg" slot-scope="src">
        <img :src="src | getImgUrl" width="60" height="45" @click="showPreviewImg(src)" style="cursor: pointer;"/>
      </span>
      <span slot="action" slot-scope="text, record">
        <a-popconfirm placement="top" :title="record.partnerIsLine===1? '是否确认下线该条目？':'是否确认上线该条目？'" @confirm="() => editjobStatusStatus(record.partnerIsLine,record)">
          <a>{{ statusFilter(record.partnerIsLine) }}</a>
        </a-popconfirm>
        <a-divider type="vertical"/>
        <a v-if="hasPerm('partnerIndex:edit')" @click="$refs.editForm.edit(record)">编辑</a>
        <a-divider type="vertical" v-if="hasPerm('partnerIndex:edit') && hasPerm('partnerIndex:delete')"/>
        <a-popconfirm v-if="hasPerm('partnerIndex:delete')" placement="topRight" title="是否确认删除该条目？" @confirm="() => partnerIndexDelete(record)">
          <a>删除</a>
        </a-popconfirm>
      </span>
    </s-table>
    <add-form ref="addForm" @ok="handleOk" />
    <edit-form ref="editForm" @ok="handleOk" />
    <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel" title="预览">
      <img alt="example" style="width: 100%" :src="previewImage" />
    </a-modal>
  </a-card>
</template>
<script>
  import { STable, Ellipsis } from '@/components'
  import homeApi from '@/api/homeApi'
  import { setImgUrl } from '@/utils/img'
  import addForm from './addPartnerForm'
  import editForm from './editPartnerForm'

  export default {
    name: 'PartnerIndex',
    components: {
      STable,
      Ellipsis,
      addForm,
      editForm
    },
    data () {
      return {
        // 查询参数
        queryParam: { id: '' },
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: 'dataIndex',
            customRender: (text, row, index) => {
              return index + 1
            },
            align: 'center',
            width: '90px'
          },
          {
            title: '名称',
            dataIndex: 'partnerName',
            scopedSlots: { customRender: 'partnerName' }
          },
          {
            title: '图片',
            dataIndex: 'partnerImg',
            scopedSlots: { customRender: 'partnerImg' },
            align: 'center'
          },
          {
            title: '排序',
            dataIndex: 'partnerSort',
            align: 'center'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return homeApi.findPartenrById(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        statusDictTypeDropDown: [{ code: 0, name: '下线' }, { code: 1, name: '上线' }],
        previewVisible: false,
        previewImage: ''
      }
    },
    created () {
      if (this.hasPerm('partnerIndex:edit') || this.hasPerm('partnerIndex:delete') || hasPerm('partnerIndex:start') || hasPerm('partnerIndex:stop')) {
        this.columns.push({
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        })
      }
    },
    filters: {
      getImgUrl (v) {
        return setImgUrl(v)
      }
    },
    methods: {
      statusFilter (status) {
        const values = this.statusDictTypeDropDown.filter(item => item.code === status)
        if (values.length > 0) {
          return values[0].name
        } else {
          return '上线'
        }
      },
      /**
       * 启动停止
       */
      editjobStatusStatus (code, record) {
        if (code === 1) {
          homeApi.updateIsLinePartner({ id: record.partnerId, isLine: false }).then(res => {
            if (res.status === 200) {
              if (res.data) {
                this.$message.success('下线成功')
                this.$refs.table.refresh()
              } else {
                this.$message.error('下线失败')
              }
            } else {
              this.$message.error('下线失败：' + res.message)
            }
          })
        } else if (code === 2) {
          homeApi.updateIsLinePartner({ id: record.partnerId, isLine: true }).then(res => {
            if (res.status === 200) {
              if (res.data) {
                this.$message.success('上线成功')
                this.$refs.table.refresh()
              } else {
                this.$message.error('上线失败')
              }
            } else {
              this.$message.error('上线失败：' + res.message)
            }
          })
        }
      },
      partnerIndexDelete (record) {
        homeApi.deletePartner({ id: record.partnerId }).then((res) => {
          if (res.status === 200) {
            if (res.data) {
              this.$message.success('删除成功')
              this.$refs.table.refresh()
            } else {
              this.$message.error('删除失败')
            }
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      showPreviewImg (src) {
        this.previewImage = setImgUrl(src)
        this.previewVisible = true
      },
      handleCancel () {
        this.previewVisible = false
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
