<template>
  <a-modal
    centered
    :footer="null"
    :maskClosable="false"
    title="销售单缺货明细"
    v-model="isShow"
    @cancel="isShow=false"
    width="70%">
    <!-- 基本信息 -->
    <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.no"
        rowKeyName="no"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: 500 }"
        :showPagination="false"
        :defaultLoadData="false"
        bordered>
        <!-- 单号 -->
        <template slot="productCode" slot-scope="text, record">
          <span style="padding-right: 15px;">{{ text }}</span>
          <a-badge count="促" v-if="record.promotionFlag&&record.promotionFlag.indexOf('GIFT')>=0" :number-style="{ backgroundColor: '#52c41a', zoom:'80%' }"></a-badge>
          <a-badge count="特" v-if="record.promotionFlag&&record.promotionFlag.indexOf('DISCOUNT')>=0" :number-style="{ backgroundColor: '#faad14', zoom:'80%' }"></a-badge>
        </template>
        <!-- 销售价 -->
        <template slot="price" slot-scope="text, record">
          {{ toThousands(text) }}
          <span v-if="record.promotionFlag&&(record.promotionFlag.indexOf('GIFT')>=0||record.promotionFlag.indexOf('DISCOUNT')>=0)" title="原价">({{ toThousands(record.origPrice) }})</span>
        </template>
      </s-table>
      <!-- 操作按钮 -->
      <div class="btn-cont" style="padding:20px 20px 0;text-align: center;">
        <a-button
          class="button-warning"
          type="primary"
          @click="handleExport"
          :disabled="disabled"
          :loading="exportLoading"
          id="allocateBillList-export">导出Excel</a-button>
        <a-button
          style="margin-left: 10px"
          class="button-warning"
          type="primary"
          @click="handleClassifyExport"
          :disabled="disabled"
          :loading="exportClassifyLoading"
          id="allocateBillList-export">分类导出Excel</a-button>
        <a-button style="margin-left: 10px" @click="isShow=false">关闭</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { STable } from '@/components'
import { commonMixin } from '@/utils/mixin'
import { salesStockoutDetail, exportStockout, exportGroupStockout } from '@/api/salesDetailNew'
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 () {
    return {
      spinning: false,
      isShow: this.openModal,
      disabled: false,
      // 导出loading
      exportLoading: false,
      exportClassifyLoading: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        this.disabled = true
        delete parameter.tableId
        delete parameter.index
        return salesStockoutDetail({ 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: 'productCode', width: '15%', align: 'center', scopedSlots: { customRender: 'productCode' } },
        { title: '产品名称', dataIndex: 'product.name', width: '25%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'product.origCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单位', dataIndex: 'product.unit', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售数量', dataIndex: 'salesQty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '缺货数量', dataIndex: 'qty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ]
      // 售价权限
      if (this.$hasPermissions('B_salesDetail_salesPrice')) {
        arr.splice(6, 0, { title: '销售价', dataIndex: 'price', width: '8%', align: 'right', scopedSlots: { customRender: 'price' } })
        arr.splice(7, 0, { title: '销售金额', dataIndex: 'salesAmount', width: '8%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.splice(8, 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, { 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, { 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>
