<template>
  <a-modal
    centered
    class="confirmationDetail-modal"
    :footer="null"
    :maskClosable="false"
    title="详情"
    v-model="isShow"
    @cancel="isShow=false"
    width="80%">
    <div>
      <!-- <div style="padding: 0 12px;text-align: right;">
        <a-button id="outboundOrderDetail-preview-btn" style="margin-right: 15px;">打印预览</a-button>
        <a-button type="primary" id="outboundOrderDetail-print-btn">快速打印</a-button>
      </div> -->
      <!-- 基础信息 -->
      <a-card size="small" :bordered="false" class="outboundOrderDetail-cont">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="基础信息">
            <a-descriptions size="small" :column="3" style="margin-bottom: 10px;">
              <a-descriptions-item label="客户名称">{{ detailData&&detailData.buyerName || '--' }}</a-descriptions-item>
              <a-descriptions-item label="销退单号">{{ detailData&&detailData.salesReturnBillNo || '--' }}</a-descriptions-item>
              <a-descriptions-item label="创建时间">{{ detailData&&detailData.createDate || '--' }}</a-descriptions-item>
              <a-descriptions-item label="申请退货数量">{{ detailData&&(detailData.totalInitialQty || detailData.totalInitialQty==0) ? detailData.totalInitialQty : '--' }}</a-descriptions-item>
              <a-descriptions-item label="仓库实收数量">{{ detailData&&(detailData.totalReceiveQty || detailData.totalReceiveQty==0) ? detailData.totalReceiveQty : '--' }}</a-descriptions-item>
              <a-descriptions-item label="坏件数量">{{ detailData&&(detailData.totalBadQty || detailData.totalBadQty==0) ? detailData.totalBadQty : '--' }}</a-descriptions-item>
              <a-descriptions-item label="良品数量">{{ detailData&&(detailData.totalGoodQty || detailData.totalGoodQty==0) ? detailData.totalGoodQty : '--' }}</a-descriptions-item>
              <a-descriptions-item label="返库数量">{{ detailData&&(detailData.totalBackStockQty || detailData.totalBackStockQty==0) ? detailData.totalBackStockQty : '--' }}</a-descriptions-item>
              <a-descriptions-item label="实际退货金额" v-if="$hasPermissions('B_returnConfirmation_detail_salesPrice')">{{ detailData&&(detailData.totalAmount || detailData.totalAmount==0) ? toThousands(detailData.totalAmount) : '--' }}</a-descriptions-item>
            </a-descriptions>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <a-card size="small" :bordered="false" class="outboundOrderDetail-cont">
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :scroll="{ y: 400 }"
          :columns="columns"
          :data="loadData"
          :defaultLoadData="false"
          bordered>
        </s-table>
      </a-card>
      <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 { salesReturnFinancialDetailList, salesReturnDetail } from '@/api/salesReturn'
export default {
  name: 'ConfirmationDetailModal',
  mixins: [commonMixin],
  components: { STable },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      detailsData: null, //  详情数据
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return salesReturnFinancialDetailList(Object.assign(parameter, { salesReturnBillSn: 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
        })
      },
      detailData: null //  详情数据
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'productEntity.code', align: 'center', width: '6%', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productEntity.name', width: '14%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '单位', dataIndex: 'productEntity.unit', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '申请退货数量', dataIndex: 'initialQty', align: 'center', width: '6%', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '仓库实收数量', dataIndex: 'receiveQty', align: 'center', width: '6%', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '坏件数量', dataIndex: 'badQty', align: 'center', width: '5%', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '良品数量', dataIndex: 'goodQty', align: 'center', width: '5%', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '返库数量', dataIndex: 'backStockQty', align: 'center', width: '5%', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '退货原因', dataIndex: 'returnReason', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备注', dataIndex: 'returnReason', width: '8%', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      if (this.$hasPermissions('B_returnConfirmation_detail_salesPrice')) { //  售价权限
        arr.splice(11, 0, { title: '实际退货单价', dataIndex: 'price', align: 'right', width: '6%', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
        arr.splice(12, 0, { title: '实际退货金额', dataIndex: 'totalAmount', align: 'right', width: '6%', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
        arr.splice(13, 0, { title: '实际退货单价说明', dataIndex: 'priceRemark', width: '12%', align: 'center', customRender: function (text) { return text || '--' } })
      }
      return arr
    }
  },
  methods: {
    getDetail () {
      salesReturnDetail({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          this.detailData = res.data
        } else {
          this.detailData = null
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.detailData = null
        this.$refs.table.clearTable()
        this.$store.state.app.curActionPermission = ''
      } else {
        this.$store.state.app.curActionPermission = 'B_returnConfirmation_detail'
        this.getDetail()
        this.$nextTick(() => {
          this.$refs.table.refresh(true)
        })
      }
    }
  }
}
</script>

<style lang="less">
  .confirmationDetail-modal{
    .ant-modal-body {
      padding: 10px 10px 20px;
    }
    .btn-cont {
      text-align: center;
      margin: 5px 0 10px;
    }
  }
</style>
