<template>
  <a-modal
    centered
    class="replenishmentManagement-stock-modal"
    :footer="null"
    :maskClosable="false"
    title="提示"
    v-model="isShow"
    @cancle="isShow = false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <p v-if="type=='confirm'" style="text-align: center;margin: 0 0 20px;font-size: 14px;font-weight: bold;">以下产品库存不足，确认继续补货吗？</p>
      <div v-if="type=='outWarehousing'" style="text-align: center;margin: 0 0 20px;font-size: 14px;font-weight: bold;">
        <p style="margin: 0;">以下产品库存不足</p>
        <p style="margin: 0;">如果仍然补货，系统将自动生成【<span style="color: red;">急件</span>】</p>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: 400 }"
        :showPagination="false"
        :defaultLoadData="false"
        bordered>
      </s-table>
      <div class="btn-cont">
        <a-button type="primary" id="replenishmentManagement-stock-modal-save" @click="handleSave">仍然补货</a-button>
        <a-button id="replenishmentManagement-stock-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'
export default {
  name: 'StockModal',
  components: { STable, VSelect },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    params: {
      type: Object,
      default: () => {
        return {}
      }
    },
    listData: { // 列表数据
      type: Array,
      default: () => {
        return []
      }
    },
    type: { // 校验类型。confirm：确认；
      type: String,
      default: 'confirm'
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      columns: [
        { title: '序号', dataIndex: 'no', width: '8%', align: 'center' },
        { title: '产品编码', dataIndex: 'product.code', width: '22%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', width: '32%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '可用库存数量', dataIndex: 'stockQty', width: '15%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '补货实发数量', dataIndex: 'confirmQty', width: '15%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'product.unit', width: '8%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const _this = this
        _this.disabled = true
        _this.spinning = true
        return new Promise(function (resolve, reject) {
          const data = JSON.parse(JSON.stringify(_this.listData))
          data.map((item, index) => {
            item.no = index + 1
          })
          _this.disabled = false
          _this.spinning = false
          resolve(data)
        })
      }
    }
  },
  methods: {
    //  仍然补货
    handleSave () {
      this.$emit('ok', this.type, this.params)
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.table.clearTable()
      } else {
        const _this = this
        this.$nextTick(() => { // 页面渲染完成后的回调
          _this.$refs.table.refresh(true)
        })
      }
    }
  }
}
</script>

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