<template>
  <a-modal
    centered
    class="confirmationDetail-modal"
    :footer="null"
    :maskClosable="false"
    title="采购退货详情"
    v-model="isShow"
    @cancel="isShow=false"
    width="80%">
    <div>
      <!-- 基础信息 -->
      <a-collapse :activeKey="['1']" style="margin-bottom:10px">
        <a-collapse-panel key="1">
          <template slot="header">
            基础信息
          </template>
          <a-descriptions size="small" :column="4" 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.warehouseName || '--' }}</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="是否抓单">{{ detailsData&&detailsData.grabFlag =='0'?'否':'是' }}</a-descriptions-item>
            <a-descriptions-item label="补充说明" :span="4">{{ detailsData&&detailsData.explainInfo || '--' }}</a-descriptions-item>
            <a-descriptions-item label="附件" :span="4">
              <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-collapse-panel>
      </a-collapse>
      <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_purchaseReturnDetail_costPrice')">退货金额：<strong>{{ statisticsObj&&statisticsObj.totalCost ? toThousands(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 { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { sparePartsReturnQueryPage, queryPageCount, sparePartsReturnInfo } from '@/api/sparePartsReturn'
export default {
  name: 'ConfirmationDetailModal',
  mixins: [commonMixin],
  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_purchaseReturnDetail_costPrice')) { //  售价权限
        arr.splice(7, 0, { title: '退货单价', dataIndex: 'cost', width: '10%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
        arr.splice(9, 0, { title: '退货金额', dataIndex: 'totalCost', width: '10%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
      }
      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) {
            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.detailsData = null
        this.statisticsObj = null
        this.$refs.tableDetail.clearTable()
        this.$store.state.app.curActionPermission = ''
      } else {
        this.$store.state.app.curActionPermission = 'B_purchaseReturnDetail'
        this.getStatisticsData()
        this.getBasicsInfo()
        this.$nextTick(() => {
          this.$refs.tableDetail.refresh(true)
        })
      }
    }
  }
}
</script>

<style lang="less">
  .confirmationDetail-modal{
    .ant-modal-body {
      padding: 20px 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>
