<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"
            :style="{ height: '450px' }"
            :scroll="{ y: 400 }"
            :showPagination="false"
            bordered>
            <template slot="shelfName" slot-scope="text, record">
              {{ text }}<span style="color:#ff5500;margin-left:5px;" v-if="record.state!=='ENABLE'">（状态为“已暂停”）</span>
            </template>
            <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 { queryListForReplenish } from '@/api/shelf'
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', customRender: function (text) { return text } },
        { title: '货架名称', dataIndex: 'shelfName', scopedSlots: { customRender: 'shelfName' }, width: '35%', align: 'left' },
        { title: '待补产品款数', dataIndex: 'productCategory', width: '15%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') }, sorter: true },
        { title: '待补产品数量', dataIndex: 'productQty', width: '15%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') }, sorter: true },
        { title: '最近一次补货时间', dataIndex: 'lastDate', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, sorter: true },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '15%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        if (parameter.sortOrder) {
          this.queryParam.queryWord = parameter.sortField
          this.queryParam.orderBy = parameter.sortOrder == 'ascend' ? 'ASC' : 'DESC'
        } else {
          this.queryParam.queryWord = undefined
          this.queryParam.orderBy = undefined
        }
        return queryListForReplenish(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
            }
            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) {
      if (row.productCategory && row.productQty) {
        this.nowData = row
        this.openCreatRoModal = true
      } else {
        this.$message.info('此货架没有待补货产品')
      }
    },
    pageInit () {
      const vm = this
      vm.$nextTick(() => {
        vm.resetSearchForm()
        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>
