<template>
  <a-modal
    centered
    :footer="null"
    :maskClosable="false"
    title="销售单缺货明细"
    v-model="isShow"
    @cancel="isShow=false"
    width="50%">
    <a-descriptions size="small" :column="2">
      <a-descriptions-item label="销售单号">
        {{ detailData&&detailData.salesBillNo || '--' }}
        <span v-if="detailData&&detailData.salesBillNoSource">（原：{{ detailData&&detailData.salesBillNoSource || '--' }}）</span>
      </a-descriptions-item>
      <a-descriptions-item label="客户名称">{{ detailData&&detailData.buyerName || '--' }}</a-descriptions-item>
    </a-descriptions>
    <a-spin :spinning="spinning" tip="Loading...">
      <s-table
        class="sTable fixPagination"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: 500 }"
        :showPagination="false"
        :defaultLoadData="false"
        bordered>
      </s-table>
      <div class="btn-cont" style="padding:20px 20px 0;text-align: center;">
        <!-- <a-button @click="isShow=false">关闭</a-button> -->
        <a-button
          class="button-warning"
          type="primary"
          @click="handleExport"
          :disabled="disabled"
          :loading="exportLoading"
          id="allocateBillList-export">导出Excel</a-button>
        <a-button
          style="margin-left: 5px"
          @click="handleClassifyExport"
          :disabled="disabled"
          :loading="exportClassifyLoading"
          id="allocateBillList-export">分类导出Excel</a-button>
       </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { STable } from '@/components'
import { commonMixin } from '@/utils/mixin'
import { salesStockoutDetail, exportStockout, exportGroupStockout } from '@/api/salesDetail'
import { hdExportExcel } from '@/libs/exportExcel'
export default {
  name: 'StockOutDetail',
  mixins: [commonMixin],
  components: { STable },
  props: {
    openModal: {
      type: Boolean,
      default: false
    },
    salesBillSn: {
      type: [Number, String],
      default: ''
    },
    detailData: {
      type: Object,
      default: function () {
        return null
      }
    }
  },
  data () {
    const _this = this
    return {
      spinning: false,
      isShow: this.openModal,
      disabled: false,
      exportLoading: false,
      exportClassifyLoading: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        this.disabled = true
        delete parameter.tableId
        delete parameter.index
        return salesStockoutDetail({ isOos: 1, salesBillSn: this.salesBillSn }).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = 0
            for (var i = 0; i < data.length; i++) {
              data[i].no = no + i + 1
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  computed:{
    columns () {
      const _this = this
      const arr = [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'productEntity.code', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productEntity.name', width: '25%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'productEntity.origCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单位', dataIndex: 'productEntity.unit', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售数量', dataIndex: 'qty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '销售价', dataIndex: 'price', width: '8%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        // { title: '销售金额', dataIndex: 'totalAmount', width: '8%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '缺货数量', dataIndex: 'lackQty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '缺货金额', dataIndex: 'lackAmount', width: '8%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } }
      ]
      
      if (this.$hasPermissions('B_salesDetail_salesPrice')) { // 售价权限
        arr.splice(6, 0, { title: '销售价', dataIndex: 'price', width: '8%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.splice(7, 0, { title: '销售金额', dataIndex: 'totalAmount', width: '8%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      return arr
    }
  },
  methods: {
    handleExport () {
      const _this = this
      _this.exportLoading = true
      _this.disabled = true
      _this.spinning = true
      hdExportExcel(exportStockout, { isOos: 1, salesBillSn: _this.salesBillSn }, '销售单缺货产品导出', function () {
        _this.exportLoading = false
        _this.spinning = false
        _this.disabled = false
      })
    },
    handleClassifyExport () {
      const _this = this
      _this.exportClassifyLoading = true
      _this.disabled = true
      _this.spinning = true
      hdExportExcel(exportGroupStockout, { isOos: 1, salesBillSn: _this.salesBillSn }, '销售单缺货产品分类导出', function () {
        _this.exportClassifyLoading = false
        _this.spinning = false
        _this.disabled = false
      })
    }
  },
  watch: {
    openModal (nVal, oVal) {
      this.isShow = nVal
    },
    isShow (nVal, oVal) {
      if (!nVal) {
        this.$emit('close')
        this.$refs.table.clearTable()
      } else {
        this.$nextTick(() => {
          this.$refs.table.refresh()
        })
      }
    }
  }
}
</script>

<style>
</style>
