<template>
  <a-modal
    centered
    class="chooseImport-modal"
    :footer="null"
    :maskClosable="false"
    title="确认导入"
    v-model="isShow"
    @cancle="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"
          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 { hdExportExcel } from '@/libs/exportExcel'
import { expenseFailExcel } from '@/api/expenseManagement'
export default {
  name: 'ChooseImportModal',
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    paramsData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      nowColumns: [
        { title: '序号', dataIndex: 'no', width: '8%', align: 'center' },
        { title: '记账类型', dataIndex: 'importAccountType', width: '25%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '记账名称', dataIndex: 'importAccountName', width: '25%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所属区域', dataIndex: 'subareaNames', width: '14%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '所属省份', dataIndex: 'provinceName', width: '14%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '所属城市', dataIndex: 'cityName', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备注', dataIndex: 'remarks', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '费用承担方', scopedSlots: { customRender: 'fycdf' }, align: 'center', width: '15%' },
        { title: '产品信息', scopedSlots: { customRender: 'cpxx' }, align: 'center', width: '15%' }
      ],
      loadData: [],
      nowUnColumns: [
        { title: '序号', dataIndex: 'no', width: '9%', align: 'center' },
        { title: '记账类型', dataIndex: 'importAccountType', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '记账名称', dataIndex: 'importAccountName', width: '21%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所属区域', dataIndex: 'subareaNames', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '所属省份', dataIndex: 'provinceName', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '所属城市', dataIndex: 'cityName', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备注', dataIndex: 'remarks', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '费用承担方', scopedSlots: { customRender: 'fycdf' }, align: 'center', width: '15%' },
        { title: '产品信息', scopedSlots: { customRender: 'cpxx' }, align: 'center', width: '15%' },
        { title: '错误说明', scopedSlots: { customRender: 'errorMsg' }, width: '10%', align: 'center' }
      ],
      unLoadData: [],
      loading: false
    }
  },
  methods: {
    // 格式化数据
    formatProduct (list) {
      const ret = []
      list.map(item => {
        ret.push({
          productCode: item.productCode,
          expense: item.expense,
          productBrandName: item.productBrandName,
          productTypeShowName: (item.productTypeName3 ? (item.productTypeName2 + '>' + item.productTypeName3) : (item.productTypeName1 || item.productTypeName2 || ''))
        })
      })
      return ret
    },
    formatSplit (list) {
      const ret = []
      list.map(item => {
        // 有承担人员
        if (item.childDetailSplitList) {
          item.childDetailSplitList.map(k => {
            ret.push({
              splitObjName: item.importSplitObjName,
              splitObjType: item.importSplitObjType,
              splitAmount: k.splitAmount,
              childDetailSplit: {
                splitObjName: k.importSplitObjName
              }
            })
          })
        } else {
          ret.push({
            splitObjName: item.importSplitObjName,
            splitObjType: item.importSplitObjType,
            splitAmount: item.splitAmount
          })
        }
      })
      return ret
    },
    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.detailProductsList = this.formatProduct(item.detailProductsList)
        item.detailSplitList = this.formatSplit(item.detailSplitList)
        item.expenseAccountNo = paramsData.expenseAccountNo
        item.expenseAccountSn = paramsData.expenseAccountSn
      })
      this.unLoadData.map((item, index) => {
        item.no = index + 1
        item.detailProductsList = this.formatProduct(item.detailProductsList)
        item.detailSplitList = this.formatSplit(item.detailSplitList)
        item.expenseAccountNo = paramsData.expenseAccountNo
        item.expenseAccountSn = paramsData.expenseAccountSn
      })
    },
    // 确认导入
    handleSubmit () {
      if (this.paramsData.rightList.length == 0) {
        this.$message.warning('无可导入费用明细！')
      } else {
        const data = this.paramsData.rightList
        data.map(item => {
          item.expenseAccountNo = this.paramsData.expenseAccountNo
          item.expenseAccountSn = this.paramsData.expenseAccountSn
        })
        this.$emit('ok', data)
        this.isShow = false
      }
    },
    // 导出
    handleError () {
      const _this = this
      if (_this.paramsData.errorList.length < 1) {
        _this.$message.info('暂无可导出错误项~')
        return
      }
      _this.spinning = true
      hdExportExcel(expenseFailExcel, _this.paramsData.errorList, '费用明细导入错误项', 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>
