<template>
  <a-modal
    centered
    class="chooseImport-modal"
    :footer="null"
    :maskClosable="false"
    title="确认导入货位"
    v-model="isShow"
    @cancle="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"
          :disabled="disabled"
          :class="loadData.length==0?'button-grey':'button-primary'"
          @click="handleSubmit"
          style="padding: 0 40px;">确认导入</a-button>
        <a-button
          id="chooseImport-cancel"
          size="large"
          :disabled="disabled"
          class="button-cancel"
          @click="isShow=false"
          style="padding: 0 40px;margin-left: 15px;">取消</a-button>
        <a-button
          type="primary"
          id="chooseImport-error"
          size="large"
          :disabled="disabled"
          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 { shelfPlaceReadExcel, shelfSaveBatchExcel, shelfPlaceFailExcel } from '@/api/shelf'
import { toThousands } from '@/libs/tools.js'
export default {
  name: 'ChooseImportModal',
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    paramsData: { // 参数
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    const _this = this
    return {
      toThousands,
      isShow: this.openModal, //  是否打开弹框
      nowColumns: [
        { title: '序号', dataIndex: 'no', width: '8%', align: 'center' },
        { title: '货位号', dataIndex: 'shelfPlaceCode', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productCode', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '45%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '车主价', dataIndex: 'price', width: '8%', align: 'right', customRender: function (text) { return ((text || text == 0) && text.length ? _this.toThousands(text, 2) : '--') } },
        { title: '结算价', dataIndex: 'cost', width: '8%', align: 'right', customRender: function (text) { return ((text || text == 0) && text.length ? _this.toThousands(text, 2) : '--') } },
        { title: '最大库容', dataIndex: 'maxQty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      loadData: [], // 正确列表数据
      nowUnColumns: [
        { title: '序号', dataIndex: 'no', width: '8%', align: 'center' },
        { title: '货位号', dataIndex: 'shelfPlaceCode', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productCode', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '30%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '车主价', dataIndex: 'price', width: '8%', align: 'right', customRender: function (text) { return ((text || text == 0) && text.length ? _this.toThousands(text, 2) : '--') } },
        { title: '结算价', dataIndex: 'cost', width: '8%', align: 'right', customRender: function (text) { return ((text || text == 0) && text.length ? _this.toThousands(text, 2) : '--') } },
        { title: '最大库容', dataIndex: 'maxQty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '错误原因', dataIndex: 'remarks', width: '15%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      unLoadData: [], // 错误列表数据
      loading: false,
      disabled: false
    }
  },
  methods: {
    // 获取解析后的数据
    getData () {
      const _this = this
      this.loading = true
      const params = {
        shelfSn: this.paramsData.shelfSn,
        path: this.paramsData.path
      }
      this.disabled = true
      shelfPlaceReadExcel(params).then(res => {
        this.loading = false
        this.disabled = false
        if (res.status == 200) {
          // 正确列表
          if (res.data.successList && res.data.successList.length > 0) {
            res.data.successList.map((item, index) => {
              item.no = index + 1
            })
          }
          // 错误列表
          if (res.data.failList && res.data.failList.length > 0) {
            res.data.failList.map((item, index) => {
              item.no = index + 1
            })
          }
          _this.loadData = res.data.successList || []
          _this.unLoadData = res.data.failList || []
        }
      })
    },
    // 确认导入
    handleSubmit () {
      if (this.loadData.length == 0) {
        this.$message.warning('无可导入产品！')
      } else {
        this.disabled = true
        shelfSaveBatchExcel(this.loadData).then(res => {
          if (res.status == 200) {
            this.$emit('ok', this.loadData)
            this.isShow = false
          }
          this.disabled = false
        })
      }
    },
    // 导出错误项
    handleError () {
      const _this = this
      if (_this.unLoadData.length < 1) {
        _this.$message.info('暂无可导出错误项~')
        return
      }
      _this.disabled = true
      hdExportExcel(shelfPlaceFailExcel, _this.unLoadData, '货位绑定产品错误项导出', function () {
        _this.disabled = false
      }, true)
    }
  },
  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>
