<template>
  <a-modal
    centered
    class="chooseImport-modal"
    :footer="null"
    :maskClosable="false"
    title="导入"
    v-model="isShow"
    @cancel="isShow=false"
    :width="900">
    <div class="chooseImport-con">
      <!-- 可导入数据 -->
      <div class="chooseImport-head">
        <p class="text-overflow">可导入数据{{ loadData.length }}条</p>
        <p>
          <a-tooltip placement="rightTop">
            <template slot="title">
              <div v-for="(con,i) in countData" :key="i"><strong>业务单号</strong>（{{ con.bizCode }}）<span v-for="(item,j) in con.info" :key="j">{{ item.productCode }}（<span class="red">{{ item.productNum }}</span>/{{ item.bizProductNum }}）；</span></div>
            </template>
            <div class="text-overflows2" v-if="countData && countData.length>0">
              可导入产品数量与实际销售数量有差异：<span v-for="(con,i) in countData" :key="i"><strong>业务单号</strong>（{{ con.bizCode }}）<span v-for="(item,j) in con.info" :key="j">{{ item.productCode }}（<span class="red">{{ item.productNum }}</span>/{{ item.bizProductNum }}）；</span></span>
            </div>
          </a-tooltip>
        </p>
      </div>
      <a-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.no"
        rowKeyName="no"
        :columns="nowColumns"
        :dataSource="loadData"
        :loading="loading"
        :scroll="{ y: 200 }"
        :pagination="false"
        bordered>
      </a-table>
      <a-divider />
      <!-- 不可导入数据 -->
      <p class="red">不可导入数据{{ unLoadData.length }}条</p>
      <a-table
        class="unTable"
        ref="unTable"
        size="small"
        :rowKey="(record) => record.no"
        rowKeyName="no"
        :columns="nowUnColumns"
        :dataSource="unLoadData"
        :loading="loading"
        :scroll="{ y: 200 }"
        :pagination="false"
        bordered>
      </a-table>
      <!-- 按钮 -->
      <div class="btn-con">
        <a-button
          type="primary"
          id="chooseImport-submit"
          size="large"
          :class="loadData.length==0?'button-grey':'button-primary'"
          @click="handleSubmit"
          style="padding: 0 40px;">确认导入</a-button>
        <a-button
          id="chooseImport-cancel"
          size="large"
          class="button-cancel"
          @click="isShow=false"
          style="padding: 0 40px;margin-left: 15px;">取消</a-button>
        <a-button
          type="primary"
          id="chooseImport-error"
          size="large"
          class="button-error"
          @click="handleError"
          style="padding: 0 40px;margin-left: 15px;">导出错误项</a-button>
      </div>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { hdExportExcel } from '@/libs/exportExcel'
// 接口
import { codeExportImportError } from '@/api/reportData'
export default {
  name: 'ChooseImportModal',
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    paramsData: {// 导入数据
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      nowColumns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '21%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '唯一码', dataIndex: 'traceCode', width: '21%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务单号', dataIndex: 'bizCode', width: '21%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '工厂出库单号', dataIndex: 'factoryOutNo', width: '26%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      loadData: [], // 可导入信息
      nowUnColumns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '17%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '唯一码', dataIndex: 'traceCode', width: '17%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务单号', dataIndex: 'bizCode', width: '17%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '工厂出库单号', dataIndex: 'factoryOutNo', width: '17%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备注', dataIndex: 'errorDesc', width: '27%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      unLoadData: [], // 不可导入信息
      loading: false, // 表格加载状态
      countData: []// 导入条数统计
    }
  },
  methods: {
    // 获取导入列表
    getData () {
      const _this = this
      const paramsData = JSON.parse(JSON.stringify(_this.paramsData))
      this.loadData = paramsData.rightList || []
      this.unLoadData = paramsData.errorList || []
      this.loadData.map((item, index) => {
        item.no = index + 1
      })
      this.unLoadData.map((item, index) => {
        item.no = index + 1
      })
      if (paramsData.billList && paramsData.billList.length > 0) {
        const newCountData = paramsData.billList || []
        newCountData.map(item => {
          item.info = [{ productCode: item.productCode,
            productNum: item.productNum,
            bizProductNum: item.bizProductNum }]
        })
        const mergedObjects = newCountData.reduce((acc, obj) => {
          const index = acc.findIndex(o => { return o.bizCode == obj.bizCode })
          if (index != -1) {
            acc[index].info.push({ productCode: obj.productCode,
              productNum: obj.productNum,
              bizProductNum: obj.bizProductNum })
          } else {
            acc.push(obj)
          }
          return acc
        }, [])
        this.countData = mergedObjects
      }
    },
    // 获取 重复导入数据 有重复true  没有返回false
    compareObjects (obj1, obj2) {
      return Object.keys(obj1).every(key => obj1[key] === obj2[key])
    },
    // 确认导入
    handleSubmit () {
      if (this.loadData.length == 0) {
        this.$message.warning('无可导入数据！')
      } else {
        this.$emit('ok', this.loadData)
        this.isShow = false
      }
    },
    // 导出 错误项
    handleError () {
      const _this = this
      if (_this.unLoadData.length < 1) {
        _this.$message.info('暂无可导出的错误项~')
        return
      }
      _this.spinning = true
      hdExportExcel(codeExportImportError, _this.unLoadData, '唯一码错误项', function () {
        _this.spinning = false
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.loadData = []
        this.unLoadData = []
        this.countData = []
      } else {
        this.getData()
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .chooseImport-modal{
    .chooseImport-con{
      .red{
        color: #f30;
      }
      .btn-con{
        text-align: center;
        margin: 30px 0 20px;
        .button-cancel,.button-error{
          font-size: 12px;
        }
      }
      .chooseImport-head{
        display:flex;
        align-items:center;
        p:first-child{
          max-width:12%;
        }
        p:last-child{
          margin-left:10px;
          max-width:88%;
        }
      }
    }
  }
</style>
