<template>
  <div ref='difference'>
    <a-modal
      title='差异处理'
      width='100%'
      :visible='differenceVisible'
      :destroyOnClose='true'
      :confirm-loading='confirmLoading'
      @ok='handleOk'
      @cancel='handleCancel'
    >
      <div class='btn_div'>
        <p-button @click='btnEvent(1)' class='btn_class' type='primary'>新增</p-button>
        <p-button @click='btnEvent(2)' type='primary'>删除</p-button>
      </div>
      <p-table-sheet
        ref='tableSheet'
        rowKey=''
        @onSelect='onSelect'
        :dataSource='differenceData'
        :columns='columns'
        :operations='operations'
      />
    </a-modal>
    <PModalTable
      v-model='visible'
      :tableColumns='item.defTableCol'
      :fromItem='item.defToolBar.items'
      :uniqueKey='uniqueKey'
      :remoteMethod='item.remoteUrl.method'
      :remoteUrl='item.remoteUrl.url'
      :selectType='1'
      :autoLoad='true'
      :colNum='3'
      @handleOk='itemsHandleOk'
    />
  </div>
</template>

<script>
import { differentColumns } from './config.js'
import { item } from './item.js'
import { batchAdd } from '../api'

export default {
  name: 'difference',
  model: {
    prop: 'differenceVisible',
    event: 'change'
  },
  props: {
    differenceVisible: {
      type: Boolean,
      default: false
    },
    differenceData: {
      type: Array,
      default() {
        return []
      }
    },
    differenceObj: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 18 }
      },
      confirmLoading: false,
      refsss: null,
      columns: differentColumns(this),
      operations: [],
      uniqueKey: 'itemId',
      visible: false,
      item: item,
      selectedRows: []
    }
  },
  methods: {
    onSelect(data) {
      this.selectedRows = data.selectedRows
    },
    btnEvent(data) {
      if (data == 1) {
        this.visible = true
      } else {
        this.$refs.tableSheet.removeSelectedRow()
      }
    },
    itemsHandleOk(data) {
      if (data.length > 0) {
        data.forEach(item => {
          this.$refs.tableSheet.addRow(1, '', '', item)
        })
      }
      this.visible = false
    },
    handleOk() {
      if (this.selectedRows.length < 1) {
        this.$message.error('请至少选择一条数据')
        return
      }
      this.$refs.tableSheet.getDataSource().then((data) => {
        this.selectedRows.forEach(item => {
          item.manufacturer = item.manufacturerId
          item.containerCode = this.differenceObj.containerCode
          item.pickTaskId = this.differenceObj.pickTaskId
        })
        batchAdd(this.selectedRows).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.$emit('change', false)
          } else {
            this.$message.success(res.message)
          }
        })
      }).catch((error) => {
        console.log(error)
      })
    },
    handleCancel() {
      this.$emit('change', false)
    }
  }
}
</script>

<style>
.btn_class {
  margin-right: 10px
}

.btn_div {
  text-align: right;
  /* display: flex; */
  /* justify-content: end; */
  margin-bottom: 10px;
}
</style>
