<template>
  <a-modal
    centered
    class="chooseImport-modal"
    :footer="null"
    :maskClosable="false"
    title="确认导入"
    v-model="isShow"
    @cancel="isShow=false"
    width="80%">
    <div class="chooseImport-con">
      <!-- 可导入数据 -->
      <p class="">可导入数据{{ loadData.length }}条</p>
      <a-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.allocateSn"
        :columns="nowColumns"
        :dataSource="loadData"
        :loading="loading"
        :scroll="{ y: 200 }"
        :pagination="false"
        bordered>
        <template slot="unitTypeShow" slot-scope="text,record">
          {{ record.unitType==='SL'?'按数量设置':'按整箱设置' }}
        </template>
      </a-table>
      <a-divider />
      <!-- 不可导入数据 -->
      <p class="red">不可导入数据{{ unLoadData.length }}条</p>
      <a-table
        class="unTable"
        ref="unTable"
        size="small"
        :rowKey="(record) => record.errorDesc"
        :columns="nowUnColumns"
        :dataSource="unLoadData"
        :loading="loading"
        :scroll="{ y: 200 }"
        :pagination="false"
        bordered>
        <template slot="unitTypeShow" slot-scope="text,record">
          {{ record.unitType==='SL'?'按数量设置':'按整箱设置' }}
        </template>
        <!-- <template slot="errorMsg" slot-scope="text,record">
          <a-popover placement="topRight">
            <template slot="content">
              <p v-for="d in record.importErrorMsgSet">{{ d }}</p>
            </template>
            <a-button type="link">
              查看
            </a-button>
          </a-popover>
        </template> -->
      </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 { downNormalFail, downSpecialFail } from '@/api/promotion'
export default {
  name: 'ChooseImportModal',
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    paramsData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      loadData: [], // 可导入数据
      unLoadData: [], // 不可导入数据
      loading: false, // 列表加载状态
      goodsFlag: '', // 1特价产品商品 0买产品送产品 买产品送采购额页面
      discountType: ''// 折扣类型
    }
  },
  computed: {
    nowColumns () {
      const _this = this
      let columnsArr = [
        { title: '序号', dataIndex: 'no', width: '10%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', width: '30%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '起订类型', scopedSlots: { customRender: 'unitTypeShow' }, dataIndex: 'unitType', width: '20%', align: 'center' },
        { title: '起订数量', dataIndex: 'unitQty', width: '20%', align: 'center', customRender: function (text) { return (text || text == 0) ? text : '--' } }
      ]
      if (_this.goodsFlag == '0' && _this.discountType == '0') {
        const arr = [
          { title: '省级特价', dataIndex: 'provinceDiscountPriceText', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
          { title: '市级特价', dataIndex: 'cityDiscountPriceText', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
          { title: '特约特价', dataIndex: 'specialDiscountPriceText', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } }
        ]
        columnsArr = columnsArr.concat(arr)
      }
      return columnsArr
    },
    nowUnColumns () {
      const _this = this
      const unColumnsArr = [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '起订类型', scopedSlots: { customRender: 'unitTypeShow' }, dataIndex: 'unitType', width: '10%', align: 'center' },
        { title: '起订数量', dataIndex: 'unitQty', width: '10%', align: 'center', customRender: function (text) { return (text || text == 0) ? text : '--' } },
        { title: '错误说明', dataIndex: 'remarks', width: '30%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true }
      ]
      if (_this.goodsFlag == '0' && _this.discountType == '0') {
        unColumnsArr.splice(3, 0, { title: '省级特价', dataIndex: 'provinceDiscountPriceText', width: '10%', align: 'right', customRender: function (text) { return text || '--' } },
          { title: '市级特价', dataIndex: 'cityDiscountPriceText', width: '10%', align: 'right', customRender: function (text) { return text || '--' } },
          { title: '特约特价', dataIndex: 'specialDiscountPriceText', width: '10%', align: 'right', customRender: function (text) { return text || '--' } })
      }
      return unColumnsArr
    }
  },
  methods: {
    // 判断页面来源
    setFlag (val) {
      this.goodsFlag = val.goodFlag
      this.discountType = val.discountType
    },
    // 获取导入数据
    getData () {
      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
        item.importErrorMsgSet = item.remarks.split(',')
      })
    },
    // 确认导入
    handleSubmit () {
      if (this.loadData.length == 0) {
        this.$message.warning('没有可导入的数据！')
      } else {
        const data = this.paramsData.rightList
        this.$emit('ok', data)
      }
    },
    // 导出错误项
    handleError () {
      const _this = this
      if (_this.unLoadData.length < 1) {
        _this.$message.info('暂无可导出的错误项~')
        return
      }
      _this.spinning = true
      const ajaxName = _this.goodsFlag == '0' ? downSpecialFail : downNormalFail
      var ajaxData = {}
      ajaxData.unLoadData = _this.unLoadData
      if (_this.goodsFlag == '0') {
        ajaxData.discountType = _this.discountType
      }
      hdExportExcel(ajaxName, ajaxData, '导出产品错误项', function () {
        _this.spinning = false
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.loadData = []
        this.unLoadData = []
      } 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;
        }
      }
    }
  }
</style>
