<template>
  <a-modal
    centered
    class="replenishmentManagement-confirm-modal"
    :footer="null"
    :maskClosable="false"
    title="待补货产品"
    v-model="isShow"
    @cancel="isShow = false"
    width="80%">
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
            <a-row :gutter="15">
              <a-col :md="8" :sm="24">
                <a-form-item label="货架名称">
                  <shelfSList v-model="queryParam.shelfSn"></shelfSList>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <span class="table-page-search-submitButtons">
                  <a-button type="primary" style="margin-left: 5px" @click="$refs.table.refresh(true)" :disabled="disabled">查询</a-button>
                  <a-button style="margin-left: 5px" @click="resetSearchForm()" :disabled="disabled">重置</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <div class="pages-wrap" style="margin-top:10px;">
          <!-- 列表 -->
          <s-table
            class="sTable"
            ref="table"
            size="small"
            :rowKey="(record) => record.id"
            :columns="columns"
            :data="loadData"
            :scroll="{ y: 400 }"
            bordered>
            <template slot="action" slot-scope="text, record">
              <span class="table-td-link" @click.stop="handleDetail(record)">产品明细</span>
            </template>
          </s-table>
        </div>
      </a-spin>
      <!-- 生成补货单 -->
      <creatReplenishmentOrder :openModal="openCreatRoModal" :nowData="nowData" @ok="resetSearchForm" @close="openCreatRoModal=false" />
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import shelfSList from '@/views/common/shelfList'
import { shelfReplenishList } from '@/api/shelfReplenish'
import creatReplenishmentOrder from './creatReplenishmentOrder.vue'
export default {
  name: 'SlodOutModal',
  components: { STable, VSelect, shelfSList, creatReplenishmentOrder },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      openCreatRoModal: false,
      disabled: false,
      queryParam: {
        shelfSn: undefined
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '货架名称', dataIndex: 'shelfInfo.shelfName', width: '35%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '待补产品款数', dataIndex: 'totalConfirmQty', width: '15%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '待补产品数量', dataIndex: 'totalPutQty', width: '15%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '最近一次补货时间', dataIndex: 'createDate', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '15%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return shelfReplenishList(Object.assign(parameter, this.queryParam)).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.listData = data.list || []
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      localDataSource: [],
      detailData: null, //  详情数据
      nowData: null
    }
  },
  methods: {
    // 重置
    resetSearchForm () {
      this.queryParam.shelfSn = undefined
      this.$refs.table.refresh(true)
    },
    handleDetail (row) {
      this.nowData = row
      this.openCreatRoModal = true
    },
    pageInit () {
      const vm = this
      vm.$nextTick(() => {
        vm.spinning = false
        vm.disabled = false
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        this.pageInit()
      }
    }
  }
}
</script>

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