<template>
  <a-modal
    centered
    class="replenishmentManagement-printSticker-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancle="isShow = false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-descriptions>
        <a-descriptions-item label="补货单号">{{ basicInfoData && basicInfoData.replenishBillNo || '--' }}</a-descriptions-item>
        <a-descriptions-item label="创建时间">{{ basicInfoData && basicInfoData.createDate || '--' }}</a-descriptions-item>
      </a-descriptions>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        rowKeyName="id"
        :row-selection="{ columnWidth: 40, getCheckboxProps: record => ({ props: { disabled: record.confirmQty == 0 } }) }"
        @rowSelection="rowSelectionFun"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: 400 }"
        :showPagination="false"
        :defaultLoadData="false"
        bordered>
        <!-- 打印数量 -->
        <template slot="printQty" slot-scope="text, record">
          <a-input-number
            v-if="record.confirmQty!=0"
            size="small"
            id="replenishmentManagement-printSticker-printQty"
            v-model="record.printQty"
            :precision="0"
            :min="1"
            :max="record.confirmQty"
            placeholder="请输入"
            style="width: 100%" />
          <span v-else>--</span>
        </template>
      </s-table>
      <div class="btn-cont">
        <a-button type="primary" id="replenishmentManagement-printSticker-modal-save" @click="handleSave">开始打印</a-button>
        <a-button id="replenishmentManagement-printSticker-modal-back" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { shelfReplenishDetailList, shelfReplenishDetail, shelfReplenishPrintSign } from '@/api/shelfReplenish'
export default {
  name: 'PrintStickerModal',
  components: { STable, VSelect },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    nowData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      columns: [
        { title: '产品编码', dataIndex: 'product.code', width: '22%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', width: '30%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '补货应发数量', dataIndex: 'qty', width: '13%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '实发数量', dataIndex: 'confirmQty', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '打印数量', scopedSlots: { customRender: 'printQty' }, width: '15%', align: 'center' },
        { title: '单位', dataIndex: 'product.unit', width: '8%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return shelfReplenishDetailList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            for (var i = 0; i < data.length; i++) {
              data[i].no = i + 1
              data[i].printQty = data[i].confirmQty
            }
            this.disabled = false
            this.listData = data || []
          }
          this.spinning = false
          return data
        })
      },
      rowSelectionInfo: null,
      listData: [],
      basicInfoData: null
    }
  },
  computed: {
    modalTit () {
      const hjName = this.nowData && this.nowData.shelfName ? this.nowData.shelfName : ''
      return '补货单打印贴签——' + hjName
    }
  },
  methods: {
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    //  开始打印
    handleSave () {
      if (!this.rowSelectionInfo || (this.rowSelectionInfo && this.rowSelectionInfo.selectedRows.length < 1)) {
        this.$message.warning('请至少选择一个产品')
        return
      }
      this.spinning = true
      shelfReplenishPrintSign({ replenishBillSn: this.nowData && this.nowData.replenishBillSn }).then(res => {
        if (res.status == 200) {
          this.$emit('ok')
          this.isShow = false
        }
        this.spinning = false
      })
    },
    // 基本信息
    getDetail () {
      shelfReplenishDetail({ sn: this.nowData && this.nowData.replenishBillSn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data
        } else {
          this.basicInfoData = null
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.table.clearTable()
      } else {
        const _this = this
        this.getDetail()
        this.$nextTick(() => { // 页面渲染完成后的回调
          _this.$refs.table.refresh(true)
        })
      }
    }
  }
}
</script>

<style lang="less">
.replenishmentManagement-printSticker-modal {
	.btn-cont {
		text-align: center;
		margin: 35px 0 10px;
	}
}
</style>
