<template>
  <a-modal
    centered
    class="backorderDetail-modal"
    :footer="null"
    :maskClosable="false"
    title="详情"
    v-model="isShow"
    @cancel="isShow=false"
    width="70%">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 基础信息 -->
      <a-page-header :ghost="false" :backIcon="false" class="backorderDetail-cont">
        <template slot="subTitle">
          <span style="color: #666;font-weight: bold;">销售单号：{{ detailData&&detailData.salesBillNo || '--' }}</span>
          <a-badge count="促" v-if="detailData&&detailData.promoFlag==1" :number-style="{ backgroundColor: '#52c41a', zoom:'80%' }"></a-badge>
        </template>
        <template slot="extra" v-if="$hasPermissions('B_oosPrint')">
          <a-button id="backorderDetail-preview-btn" :disabled="localDataSource.length==0" @click="handlePrint('preview')" style="margin-right: 15px;">打印预览</a-button>
          <a-button type="primary" id="backorderDetail-print-btn" :disabled="localDataSource.length==0" @click="handlePrint('print')">快捷打印</a-button>
        </template>
      </a-page-header>
      <a-card size="small" :bordered="false" class="backorderDetail-cont">
        <!-- alert -->
        <a-alert type="info" style="margin-bottom:10px">
          <div slot="message">
            缺货总款数：<strong>{{ detailData && (detailData.totalCategory || detailData.totalCategory==0) ? detailData.totalCategory : '--' }}</strong>，
            缺货总数量：<strong>{{ detailData && (detailData.totalQty || detailData.totalQty==0) ? detailData.totalQty : '--' }}</strong>，
            <span v-if="$hasPermissions('B_oosDetail_salesPrice')">缺货总售价：<strong>{{ detailData && (detailData.totalAmount || detailData.totalAmount==0) ? toThousands(detailData.totalAmount) : '--' }}</strong></span>
          </div></a-alert>
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :defaultLoadData="false"
          :scroll="{ y: 500 }"
          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=='REGULAR'" :number-style="{ backgroundColor: '#108ee9', 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>
      </a-card>
    </a-spin>
    <!-- 打印 -->
    <div id="print"></div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { printFun } from '@/libs/JGPrint.js'
import { oosDetail, oosDetailList, oosDetailPrint } from '@/api/oos'
export default {
  name: 'BackorderDetailModal',
  mixins: [commonMixin],
  components: { STable },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      detailData: null, //  详情数据
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return oosDetailList(Object.assign(parameter, this.queryParam, { oosBillSn: 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.localDataSource = data.list
            this.disabled = false
          }
          return data
        })
      },
      localDataSource: []
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'productEntity.code', width: '15%', align: 'left', scopedSlots: { customRender: 'productCode' } },
        { title: '产品名称', dataIndex: 'productEntity.name', width: '27%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '仓库', dataIndex: 'warehouseName', width: '12%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'productEntity.origCode', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '缺货数量', dataIndex: 'qty', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'productEntity.unit', width: '8%', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      if (this.$hasPermissions('B_oosDetail_costPrice')) { //  成本
        arr.push({ title: '缺货成本金额', dataIndex: 'totalShowCostAmount', width: '10%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
      }
      if (this.$hasPermissions('B_oosDetail_salesPrice')) { //  售价权限
        arr.push({ title: '售价', dataIndex: 'price', width: '15%', align: 'right', scopedSlots: { customRender: 'price' } })
        arr.push({ title: '缺货金额', dataIndex: 'totalAmount', width: '10%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
      }
      return arr
    }
  },
  methods: {
    //  获取详情
    getDetail () {
      oosDetail({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          this.detailData = res.data
        } else {
          this.detailData = null
        }
      })
    },
    // 打印预览/快捷打印
    handlePrint (type) {
      const _this = this
      this.$store.state.app.curActionPermission = 'B_oosPrint'
      _this.spinning = true
      // 打印或预览
      printFun(oosDetailPrint, { sn: this.itemSn }, type, '缺货单', () => {
        _this.spinning = false
        _this.$store.state.app.curActionPermission = 'B_oosDetail'
      })
    }
  },
  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_oosDetail'
      }
    },
    itemSn (newValue, oldValue) {
      if (this.isShow && newValue) {
        const _this = this
        this.getDetail()
        setTimeout(() => {
          _this.$refs.table.refresh(true)
        }, 200)
      }
    }
  }
}
</script>

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