<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="fycdf" slot-scope="text,record">
          <div v-for="(item,index) in record.detailSplitList" :key="item.id+'-'+index">
            {{ item.splitObjName }}<span v-if="item.childDetailSplit&&item.childDetailSplit.splitObjName">/</span>{{ item.childDetailSplit?item.childDetailSplit.splitObjName:'' }}: ￥{{ item.splitAmount }}
          </div>
        </template>
        <!-- 产品信息 -->
        <template slot="cpxx" slot-scope="text,record">
          <div v-for="item in record.detailProductsList" :key="item.id">
            <div v-if="item.productCode">{{ item.productCode }}: ￥{{ item.expense }}</div>
            <div v-else-if="item.productBrandName">{{ item.productBrandName }}<span v-if="item.productTypeShowName">/</span>{{ item.productTypeShowName }}: ￥{{ item.expense }}</div>
            <div v-else>{{ item.productTypeShowName }}: ￥{{ item.expense }}</div>
          </div>
          <span v-if="!record.detailProductsList||record.detailProductsList.length==0">--</span>
        </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="fycdf" slot-scope="text,record">
          <div v-for="(item,index) in record.detailSplitList" :key="item.no+'-'+index">
            {{ item.splitObjName }}<span v-if="item.childDetailSplit&&item.childDetailSplit.splitObjName">/</span>{{ item.childDetailSplit?item.childDetailSplit.splitObjName:'' }}: ￥{{ item.splitAmount }}
          </div>
        </template>
        <!-- 产品信息 -->
        <template slot="cpxx" slot-scope="text,record">
          <div v-for="(item,index) in record.detailProductsList" :key="item.no+'-'+index">
            <div v-if="item.productCode">{{ item.productCode }}: ￥{{ item.expense }}</div>
            <div v-else-if="item.productBrandName">{{ item.productBrandName }}<span v-if="item.productTypeShowName">/</span>{{ item.productTypeShowName }}: ￥{{ item.expense }}</div>
            <div v-else>{{ item.productTypeShowName }}: ￥{{ item.expense }}</div>
          </div>
          <span v-if="!record.detailProductsList||record.detailProductsList.length==0">--</span>
        </template>
        <template slot="errorMsg" slot-scope="text,record">
          <a-popover placement="topRight">
            <template slot="content">
              <p v-for="d in record.importErrorMsgList">{{ 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"
          :loading="bloading"
          style="padding: 0 40px;">确认导入</a-button>
        <a-button
          id="chooseImport-cancel"
          size="large"
          class="button-cancel"
          :loading="bloading"
          @click="isShow=false"
          style="padding: 0 40px;margin-left: 15px;">取消</a-button>
        <a-button
          type="primary"
          id="chooseImport-error"
          size="large"
          class="button-error"
          :loading="bloading"
          @click="handleError"
          style="padding: 0 40px;margin-left: 15px;">导出错误项</a-button>
      </div>
    </div>
  </a-modal>
</template>

<script>
import { hdExportExcel } from '@/libs/exportExcel'
import { sendBillInsertBatch, exportImportError } from '@/api/sendBill'
export default {
  name: 'ChooseImportModal',
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    paramsData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      bloading: false,
      isShow: this.openModal, //  是否打开弹框
      nowColumns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '客户名称', dataIndex: 'customeName', width: '8%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '物流单号', dataIndex: 'transportNo', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '物流电话', dataIndex: 'transportTele', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '托运日期', dataIndex: 'sendDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收货人', dataIndex: 'customerCacateName', width: '6%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '收货电话', dataIndex: 'customerCacatePhone', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收货地址', dataIndex: 'customerAddressDetail', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '目的地', dataIndex: 'customerAddress', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '付款方式', dataIndex: 'payTypeDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '交货方式', dataIndex: 'handoverTypeDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '货物名称', dataIndex: 'sendBillDetailList.goodsName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '件数', dataIndex: 'sendBillDetailList.goodsQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '体积(m3)', dataIndex: 'sendBillDetailList.goodsVolume', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '立方/元', dataIndex: 'sendBillDetailList.cubeAmountStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '重量(kg)', dataIndex: 'sendBillDetailList.goodsWeight', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '每吨/元', dataIndex: 'sendBillDetailList.tonAmountStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '运费(元)', dataIndex: 'sendBillDetailList.transportAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '送货费(元)', dataIndex: 'sendBillDetailList.sendAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '其他(元)', dataIndex: 'sendBillDetailList.otherAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '保价(万)', dataIndex: 'sendBillDetailList.supportValue', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      loadData: [],
      nowUnColumns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '客户名称', dataIndex: 'customeName', width: '8%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '物流单号', dataIndex: 'transportNo', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '物流电话', dataIndex: 'transportTele', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '托运日期', dataIndex: 'sendDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收货人', dataIndex: 'customerCacateName', width: '6%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '收货电话', dataIndex: 'customerCacatePhone', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收货地址', dataIndex: 'customerAddressDetail', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '目的地', dataIndex: 'customerAddress', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '付款方式', dataIndex: 'payTypeDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '交货方式', dataIndex: 'handoverTypeDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '货物名称', dataIndex: 'sendBillDetailList.goodsName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '件数', dataIndex: 'sendBillDetailList.goodsQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '体积(m3)', dataIndex: 'sendBillDetailList.goodsVolume', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '立方/元', dataIndex: 'sendBillDetailList.cubeAmountStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '重量(kg)', dataIndex: 'sendBillDetailList.goodsWeight', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '每吨/元', dataIndex: 'sendBillDetailList.tonAmountStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '运费(元)', dataIndex: 'sendBillDetailList.transportAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '送货费(元)', dataIndex: 'sendBillDetailList.sendAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '其他(元)', dataIndex: 'sendBillDetailList.otherAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '保价(万)', dataIndex: 'sendBillDetailList.supportValue', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '错误说明', scopedSlots: { customRender: 'errorMsg' }, width: '10%', align: 'center' }
      ],
      unLoadData: [],
      loading: false
    }
  },
  methods: {
    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
        item.sendBillDetailList = item.sendBillDetailList[0]
      })
      this.unLoadData.map((item, index) => {
        item.no = index + 1
        item.sendBillDetailList = item.sendBillDetailList[0]
      })
    },
    // 确认导入
    handleSubmit () {
      if (this.paramsData.rightList.length == 0) {
        this.$message.warning('没有可导入的发货单！')
      } else {
        const data = this.paramsData.rightList
        this.bloading = true
        sendBillInsertBatch(data).then(res => {
          if (res.status == 200) {
            this.isShow = false
            this.$emit('ok')
          }
          this.bloading = false
        })
      }
    },
    // 错误项
    handleError () {
      const _this = this
      if (_this.paramsData.errorList.length < 1) {
        _this.$message.info('暂无错误项！')
        return
      }
      _this.bloading = true
      hdExportExcel(exportImportError, _this.paramsData.errorList, '发货单导出错误项', function () {
        _this.bloading = 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>
