<template>
  <a-modal
    centered
    wrapClassName="chooseImport-modal"
    :footer="null"
    :maskClosable="false"
    title="确认导入"
    v-model="isShow"
    @cancel="isShow=false"
    width="80%">
    <div class="chooseImport-con">
      <!-- 可导入数据 -->
      <div style="display:flex;justify-content: space-between;align-items: center;padding:10px 0;">
        <div style="display:flex;align-items: center;">
          可导入数据{{ loadData.length }}条
          <div v-if="disableProduct.length">
            ,其中<span style="color: red;">{{ disableProduct.length }}个禁用产品</span>,请确认已禁用的产品是否导入？
            <a-radio-group v-model="importDisabled">
              <a-radio :value="1">
                导入
              </a-radio>
              <a-radio :value="0">
                不导入
              </a-radio>
            </a-radio-group>
          </div>
        </div>
        <div v-if="disableProduct.length">
          <a-checkbox v-model="showDisable">
            仅显示已禁用产品
          </a-checkbox>
        </div>
      </div>
      <a-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.productCode"
        :rowClassName="(record, index) => record.productEnableFlag == 0 ? 'redBg-row':''"
        :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.productCode"
        :columns="nowUnColumns"
        :dataSource="unLoadData"
        :loading="loading"
        :scroll="{ y: 200 }"
        :pagination="false"
        bordered>
        <template slot="errorMsg" slot-scope="text,record">
          <a-popover placement="topRight">
            <template slot="content">
              <p v-for="d in record.errorMsgList">{{ 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 { hdExportExcel } from '@/libs/exportExcel'
import { sparePartsPurExportErrorLine } from '@/api/sparePartsPur.js'
export default {
  name: 'ChooseImportModal',
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    paramsData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      nowColumns: [
        { title: '序号', dataIndex: 'no', width: '8%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '25%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '25%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '成本价', dataIndex: 'putCost', width: '14%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '数量', dataIndex: 'putQty', width: '14%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '仓库', dataIndex: 'warehouseName', width: '14%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: '14%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'productUnit', align: 'center', width: '15%', customRender: function (text) { return text || '--' } }
      ],
      nowUnColumns: [
        { title: '序号', dataIndex: 'no', width: '9%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '成本价', dataIndex: 'putCost', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '数量', dataIndex: 'putQty', width: '20%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '仓库', dataIndex: 'warehouseName', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '错误说明', scopedSlots: { customRender: 'errorMsg' }, width: '10%', align: 'center' }
      ],
      unLoadData: [],
      loading: false,
      importDisabled: undefined,
      showDisable: false,
      rightData: []
    }
  },
  computed: {
    // 启用产品
    enableProduct () {
      return this.rightData.filter(item => item.productEnableFlag == 1)
    },
    // 禁用产品
    disableProduct () {
      return this.rightData.filter(item => item.productEnableFlag == 0)
    },
    loadData () {
      return this.showDisable ? this.rightData.filter(item => item.productEnableFlag == 0) : this.rightData
    }
  },
  methods: {
    getData () {
      const paramsData = JSON.parse(JSON.stringify(this.paramsData))
      this.rightData = paramsData.okList || []
      this.rightData.map((item, index) => {
        item.no = index + 1
        item.sparePartsPurchaseSn = paramsData.sparePartsPurchaseSn
      })
      this.unLoadData = paramsData.errorList || []
      this.unLoadData.map((item, index) => {
        item.no = index + 1
        item.sparePartsPurchaseSn = paramsData.sparePartsPurchaseSn
      })
    },
    // 确认导入
    handleSubmit () {
      if (this.loadData.length == 0) {
        this.$message.warning('无可导入的产品！')
      } else {
        // 有禁用产品
        if (this.disableProduct.length > 0) {
          if (this.importDisabled == undefined) {
            this.$message.warning('请先确认已禁用的产品是否导入?')
          } else {
            this.$emit('ok', this.importDisabled == 1 ? this.rightData : this.enableProduct)
            this.isShow = false
          }
        } else {
          this.$emit('ok', this.rightData)
          this.isShow = false
        }
      }
    },
    // 导出
    handleError () {
      const _this = this
      if (_this.paramsData.errorList.length < 1) {
        _this.$message.info('暂无可导出错误项~')
        return
      }
      _this.spinning = true
      hdExportExcel(sparePartsPurExportErrorLine, _this.paramsData.errorList, '散件入库导出错误项', function () {
        _this.spinning = false
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.rightData = []
        this.unLoadData = []
        this.importDisabled = undefined
        this.showDisable = false
      } else {
        this.getData()
      }
    }
  }
}
</script>

<style lang="less">
  .chooseImport-modal{
    .chooseImport-con{
      .red{
        color: #f30;
      }
      .btn-con{
        text-align: center;
        margin: 30px 0 20px;
        .button-cancel,.button-error{
          font-size: 12px;
        }
      }
    }
   .redBg-row{
      background-color: #f5cdc8;
    }
  }
</style>
