<template>
  <a-modal
    centered
    class="chooseImport-modal"
    :footer="null"
    :maskClosable="false"
    title="确认导入"
    v-model="isShow"
    @cancel="isShow=false"
    width="80%">
    <div class="chooseImport-con" v-if="paramsData">
      <!-- 可导入数据 -->
      <p class="">可导入数据{{ paramsData.rightList?paramsData.rightList.length:0 }}条</p>
      <a-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="nowColumns"
        :dataSource="loadData"
        :loading="loading"
        :scroll="{ y: 200 }"
        :pagination="false"
        bordered>
      </a-table>

      <a-divider />
      <!-- 不可导入数据 -->
      <p class="red">不可导入数据{{ paramsData.errorList?paramsData.errorList.length :0 }}条</p>
      <a-table
        class="unTable"
        ref="unTable"
        size="small"
        :rowKey="(record) => record.id"
        :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"
          :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'
import { toThousands, toFixedDecimal } from '@/libs/tools.js'
export default {
  name: 'ChooseImportModal',
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    paramsData: {
      type: Object,
      default: () => {
        return null
      }
    }
  },
  data () {
    const renderContent = (value, row, index) => {
      const obj = {
        children: value || '--',
        attrs: { rowSpan: row.sendBillDetailList ? row.sendBillDetailList.length : 0 }
      }
      return obj
    }
    const renderContentError = (value, row, index) => {
      const obj = {
        children: <a-popover placement="topRight">
          <template slot="content">
            { row.importErrorMsgList.map(item => { return <div>{item}</div> }) }
          </template>
          <a-button type="link">
            查看
          </a-button>
        </a-popover>,
        attrs: { rowSpan: row.sendBillDetailList ? row.sendBillDetailList.length : 0 }
      }
      return obj
    }
    return {
      bloading: false,
      isShow: this.openModal, //  是否打开弹框
      nowColumns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center', customRender: renderContent },
        { title: '客户名称', dataIndex: 'customeName', width: '8%', align: 'left', customRender: renderContent },
        { title: '物流单号', dataIndex: 'transportNo', width: '8%', align: 'center', customRender: renderContent },
        { title: '物流电话', dataIndex: 'transportTele', width: '8%', align: 'center', customRender: renderContent },
        { title: '托运日期', dataIndex: 'sendDate', width: '8%', align: 'center', customRender: renderContent },
        { title: '收货人', dataIndex: 'customerCacateName', width: '6%', align: 'left', customRender: renderContent },
        { title: '收货电话', dataIndex: 'customerCacatePhone', width: '8%', align: 'center', customRender: renderContent },
        { title: '收货地址', dataIndex: 'customerAddressDetail', width: '8%', align: 'center', customRender: renderContent },
        { title: '目的地', dataIndex: 'customerAddress', width: '6%', align: 'center', customRender: renderContent },
        { title: '付款方式', dataIndex: 'payTypeDictValue', width: '6%', align: 'center', customRender: renderContent },
        { title: '交货方式', dataIndex: 'handoverTypeDictValue', width: '6%', align: 'center', customRender: renderContent },
        { title: '货物名称', dataIndex: 'goodsName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '件数', dataIndex: 'goodsQtyStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '体积(m3)', dataIndex: 'goodsVolumeStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? toFixedDecimal(text, 2) : '--') } },
        { title: '立方/元', dataIndex: 'cubeAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '重量(kg)', dataIndex: 'goodsWeightStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? toFixedDecimal(text, 2) : '--') } },
        { title: '每吨/元', dataIndex: 'tonAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '运费(元)', dataIndex: 'transportAmountStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? toFixedDecimal(text, 2) : '--') } },
        { title: '送货费(元)', dataIndex: 'sendAmountStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? toFixedDecimal(text, 2) : '--') } },
        { title: '其他(元)', dataIndex: 'otherAmountStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? toFixedDecimal(text, 2) : '--') } },
        { title: '保价(万)', dataIndex: 'supportValueStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? toFixedDecimal(text, 2) : '--') } }
      ],
      loadData: [],
      nowUnColumns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center', customRender: renderContent },
        { title: '客户名称', dataIndex: 'customeName', width: '8%', align: 'left', customRender: renderContent },
        { title: '物流单号', dataIndex: 'transportNo', width: '8%', align: 'center', customRender: renderContent },
        { title: '物流电话', dataIndex: 'transportTele', width: '8%', align: 'center', customRender: renderContent },
        { title: '托运日期', dataIndex: 'sendDateStr', width: '8%', align: 'center', customRender: renderContent },
        { title: '收货人', dataIndex: 'customerCacateName', width: '6%', align: 'left', customRender: renderContent },
        { title: '收货电话', dataIndex: 'customerCacatePhone', width: '8%', align: 'center', customRender: renderContent },
        { title: '收货地址', dataIndex: 'customerAddressDetail', width: '8%', align: 'center', customRender: renderContent },
        { title: '目的地', dataIndex: 'customerAddress', width: '6%', align: 'center', customRender: renderContent },
        { title: '付款方式', dataIndex: 'payTypeStr', width: '6%', align: 'center', customRender: renderContent },
        { title: '交货方式', dataIndex: 'handoverTypeStr', width: '6%', align: 'center', customRender: renderContent },
        { title: '货物名称', dataIndex: 'goodsName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '件数', dataIndex: 'goodsQtyStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '体积(m3)', dataIndex: 'goodsVolumeStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? toFixedDecimal(text, 2) : '--') } },
        { title: '立方/元', dataIndex: 'cubeAmountStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '重量(kg)', dataIndex: 'goodsWeightStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? toFixedDecimal(text, 2) : '--') } },
        { title: '每吨/元', dataIndex: 'tonAmountStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '运费(元)', dataIndex: 'transportAmountStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? toFixedDecimal(text, 2) : '--') } },
        { title: '送货费(元)', dataIndex: 'sendAmountStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? toFixedDecimal(text, 2) : '--') } },
        { title: '其他(元)', dataIndex: 'otherAmountStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? toFixedDecimal(text, 2) : '--') } },
        { title: '保价(万)', dataIndex: 'supportValueStr', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? toFixedDecimal(text, 2) : '--') } },
        { title: '错误说明', dataIndex: 'importErrorMsgList', width: '10%', align: 'center', customRender: renderContentError }
      ],
      unLoadData: [],
      loading: false
    }
  },
  methods: {
    formatData (data) {
      const ret = []
      data.map((item, index) => {
        item.no = index + 1
        item.id = new Date().getTime() + index
        const clist = item.sendBillDetailList
        clist.map((k, i) => {
          if (i > 0) {
            ret.push(k)
          } else {
            ret.push(Object.assign(item, k))
          }
        })
      })
      return ret
    },
    getData () {
      const paramsData = JSON.parse(JSON.stringify(this.paramsData))
      this.loadData = this.formatData(paramsData.rightList || [])
      this.unLoadData = this.formatData(paramsData.errorList || [])
    },
    // 确认导入
    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>
