<template>
  <a-modal
    centered
    class="chooseImport-modal"
    :footer="null"
    :maskClosable="false"
    title="导入"
    v-model="isShow"
    @cancel="isShow=false"
    width="75%">
    <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: 500 }"
        :pagination="false"
        bordered>
      </a-table>
      <!-- 按钮 -->
      <div class="btn-con">
        <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="handleExport"
          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 { productForeignQueryList, productForeignExport } from '@/api/productForeign'
export default {
  name: 'ChooseImportModal',
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    paramsData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    const _this = this
    return {
      isShow: this.openModal, //  是否打开弹框
      nowColumns: [
        { title: '序号', dataIndex: 'no', width: '6%', align: 'center' },
        { title: '查询码', dataIndex: 'queryCode', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '外贸产品编码', dataIndex: 'foreignCode', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '箭冠产品编码', dataIndex: 'productCode', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: 'OE码', dataIndex: 'oeCode', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '大厂码',
          children: [
            {
              title: 'MANN No.', dataIndex: 'mannCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' }
            },
            {
              title: 'MAHLE No.', dataIndex: 'mahleCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' }
            },
            {
              title: 'VIC No.', dataIndex: 'vicCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' }
            },
            {
              title: 'FRAM NO.', dataIndex: 'framCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' }
            },
            {
              title: 'WIX No.', dataIndex: 'wixCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' }
            }
          ]
        },
        { title: '产品尺寸', dataIndex: 'size', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '主材料', dataIndex: 'material', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '含盒报价', dataIndex: 'priceFreeBox', width: '8%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '无盒报价', dataIndex: 'priceWithBox', width: '8%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } }
      ],
      loadData: [],
      loading: false
    }
  },
  methods: {
    getData () {
      const _this = this
      _this.loading = true
      const params = {
        path: _this.paramsData.path
      }
      productForeignQueryList(params).then(res => {
        _this.loading = false
        if (res.status == 200) {
          if (res.data && res.data.length > 0) {
            res.data.map((item, index) => {
              item.no = index + 1
            })
          }
          _this.loadData = res.data || []
        }
      })
    },
    // 导出
    handleExport () {
      const _this = this
      if (_this.loadData.length < 1) {
        _this.$message.info('暂无可导出的产品~')
        return
      }
      _this.spinning = true
      const newLoadData = _this.loadData.map(item => {
        if (item.productEntity) {
          item.productName = item.productEntity.name
          item.productCode = item.productEntity.code
          item.productOrigCode = item.productEntity.origCode
          item.productTypeName1 = item.productEntity.productTypeName1
          item.productTypeName2 = item.productEntity.productTypeName2
          item.productTypeName3 = item.productEntity.productTypeName3
        }
        return item
      })
      hdExportExcel(productForeignExport, newLoadData, '外贸产品', function () {
        _this.spinning = false
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.loadData = []
      } else {
        this.getData()
      }
    }
  }
}
</script>

<style lang="less" scoped>

  .chooseImport-modal{
    .chooseImport-con{
      /deep/.sTable td{
        white-space: pre-line;
      }
      .btn-con{
        text-align: center;
        margin: 30px 0 20px;
        .button-cancel,.button-error{
          font-size: 12px;
        }
      }
    }
  }
</style>
