<template>
  <a-modal
    centered
    class="chooseImport-modal"
    :footer="null"
    :maskClosable="false"
    title="导入"
    v-model="isShow"
    @cancel="isShow=false"
    :width="900">
    <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>
      </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>
      </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 { productPriceImport, productPriceFailExcel } from '@/api/product'
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: '10%', align: 'center' },
        { title: '产品名称', dataIndex: 'name', width: '25%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'code', width: '25%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '省级价', dataIndex: 'provincePrice', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '市级价', dataIndex: 'cityPrice', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '特约价', dataIndex: 'specialPrice', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '终端价', dataIndex: 'terminalPrice', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '车主价', dataIndex: 'carOwnersPrice', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '变更原因', dataIndex: 'changeReason', width: '10%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      loadData: [],
      nowUnColumns: [
        { title: '序号', dataIndex: 'no', width: '9%', align: 'center' },
        { title: '产品名称', dataIndex: 'name', width: '21%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'code', width: '18%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '省级价', dataIndex: 'provincePrice', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '市级价', dataIndex: 'cityPrice', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '特约价', dataIndex: 'specialPrice', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '终端价', dataIndex: 'terminalPrice', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '车主价', dataIndex: 'carOwnersPrice', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '变更原因', dataIndex: 'changeReason', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备注', dataIndex: 'errorMsg', width: '14%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      unLoadData: [],
      loading: false
    }
  },
  methods: {
    getData () {
      const _this = this
      this.loading = true
      const params = {
        path: this.paramsData.path
      }
      productPriceImport(params).then(res => {
        this.loading = false
        if (res.status == 200) {
          if (res.data.rightList && res.data.rightList.length > 0) {
            res.data.rightList.map((item, index) => {
              item.no = index + 1
            })
          }
          if (res.data.errorList && res.data.errorList.length > 0) {
            res.data.errorList.map((item, index) => {
              item.no = index + 1
            })
          }
          _this.loadData = res.data.rightList || []
          _this.unLoadData = res.data.errorList || []
        }
      })
    },
    // 确认导入
    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(productPriceFailExcel, _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 = []
      } 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>
