<template>
  <a-modal
    centered
    class="confirmationDetail-modal"
    :footer="null"
    :maskClosable="false"
    title="采购退货详情"
    v-model="isShow"
    @cancel="isShow=false"
    :width="960">
    <div>
      <a-descriptions size="small" :column="3" style="margin-bottom: 10px;">
        <a-descriptions-item label="采购退货单号">{{ detailsData&&detailsData.sparePartsReturnNo || '--' }}</a-descriptions-item>
        <a-descriptions-item label="供应商名称">{{ detailsData&&detailsData.supplier&&detailsData.supplier.supplierName || '--' }}</a-descriptions-item>
        <a-descriptions-item label="退货原因">{{ detailsData&&detailsData.returnReasonDictValue || '--' }}</a-descriptions-item>
        <a-descriptions-item label="创建时间">{{ detailsData&&detailsData.createDate || '--' }}</a-descriptions-item>
        <a-descriptions-item label="出库时间" v-if="detailsData&&detailsData.state!='AUDIT_REJECT'">{{ detailsData&&detailsData.auditDate || '--' }}</a-descriptions-item>
        <a-descriptions-item label="出库时间" v-else>--</a-descriptions-item>
        <a-descriptions-item label="业务状态">{{ detailsData&&detailsData.stateDictValue || '--' }}</a-descriptions-item>
        <a-descriptions-item label="补充说明" :span="3">{{ detailsData&&detailsData.explainInfo || '--' }}</a-descriptions-item>
        <a-descriptions-item label="附件" :span="3">
          <span v-if="detailsData&&detailsData.attachmentList&&detailsData.attachmentList.length>0">
            <a target="_blank" style="color: #00aaff;text-decoration: underline;margin-right: 15px;" :href="item.filePath" v-for="item in detailsData.attachmentList" :key="item.id">
              {{ item.fileName }}
            </a>
          </span>
          <span v-else>--</span>
        </a-descriptions-item>
      </a-descriptions>
      <a-alert type="info" style="margin-bottom:10px">
        <div slot="message">
          <span>产品款数：<strong>{{ statisticsObj&&statisticsObj.totalCategory|| '--' }}</strong>；</span>
          <span>申请退货总数量：<strong>{{ statisticsObj&&statisticsObj.qty||'--' }}</strong>；</span>
          <span v-if="$hasPermissions('B_isShowCost')">退货金额：<strong>{{ statisticsObj&&statisticsObj.totalCost || '--' }}</strong></span>
        </div>
      </a-alert>
      <s-table
        class="sTable"
        ref="tableDetail"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :defaultLoadData="true"
        bordered>
      </s-table>
      <div class="btn-cont"><a-button id="confirmationDetail-modal-back" @click="isShow = false">返回列表</a-button></div>
    </div>
  </a-modal>
</template>

<script>
import { STable } from '@/components'
import { sparePartsReturnQueryPage, queryPageCount, sparePartsReturnInfo } from '@/api/sparePartsReturn'
import { toFixedDecimal } from '@/libs/tools.js'
export default {
  name: 'ConfirmationDetailModal',
  components: { STable },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: [Number, String],
      default: ''
    },
    nowData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      detailsData: null, //  详情数据
      statisticsObj: null, // 统计金额数据
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return sparePartsReturnQueryPage(Object.assign(parameter, { sparePartsReturnSn: this.itemSn })).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
            }
            this.disabled = false
          }
          return data
        })
      }
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '7%', align: 'center' },
        { title: '入库单号', dataIndex: 'sparePartsNo', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productCode', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', width: '30%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '单位', dataIndex: 'product.unit', width: '14%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '批次号', dataIndex: 'stockBatchNo', width: '20%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '入库数量', dataIndex: 'putQty', width: '14%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '申请退货数量', dataIndex: 'qty', width: '14%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ]
      if (this.$hasPermissions('B_isShowCost')) { //  售价权限
        arr.splice(7, 0, { title: '退货单价', dataIndex: 'cost', width: '14%', align: 'center', customRender: function (text) { return ((text || text == 0) ? toFixedDecimal(text, 2) : '--') } })
        arr.splice(9, 0, { title: '退货金额', dataIndex: 'totalCost', width: '14%', align: 'center', customRender: function (text) { return ((text || text == 0) ? toFixedDecimal(text, 2) : '--') } })
      }
      return arr
    }
  },
  methods: {
    getBasicsInfo () {
      sparePartsReturnInfo({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          this.detailsData = res.data
        } else {
          this.detailsData = null
        }
      })
    },
    // 获取金额统计
    getStatisticsData () {
      queryPageCount({ sparePartsReturnSn: this.itemSn }).then(res => {
        if (res.status == 200) {
          if (res.data) {
            res.data.totalCost = (res.data && (res.data.totalCost || res.data.totalCost == 0)) ? toFixedDecimal(res.data.totalCost, 2) : '--'
            this.statisticsObj = res.data
          } else {
            this.statisticsObj = null
          }
        } else {
          this.statisticsObj = null
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.tableDetail.clearTable()
      } else {
        this.getStatisticsData()
        this.getBasicsInfo()
      }
    },
    itemSn (newValue, oldValue) {
      if (this.isShow && newValue) {
        const _this = this
        setTimeout(() => {
          _this.$refs.tableDetail.refresh(true)
        }, 200)
      }
    }
  }
}
</script>

<style lang="less">
  .confirmationDetail-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .btn-cont {
      text-align: center;
      margin-top:24px;
    }
  }
  .auditProgress{
    margin-top:24px;
  }
  .ant-divider-horizontal{
    margin:20px 0 4px;
  }
  .tip{
    line-height: 30px;
  }
</style>
