<template>
  <a-modal
    centered
    class="replenishmentManagement-confirm-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="补货单号">{{ this.nowData && this.nowData.replenishBillNo || '--' }}</a-descriptions-item>
        <!-- <a-descriptions-item label="单据来源">{{this.nowData && this.nowData.billType || '--'}}</a-descriptions-item> -->
      </a-descriptions>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :defaultLoadData="false"
        :showPagination="false"
        :scroll="{ y: 450 }"
        bordered>
        <!-- 实发数量 -->
        <template slot="confirmQty" slot-scope="text, record">
          <a-input-number
            size="small"
            id="replenishmentManagement-confirm-confirmQty"
            v-model="record.confirmQty"
            :precision="0"
            :min="0"
            :max="record.qty"
            placeholder="请输入"
            style="width: 100%" />
        </template>
      </s-table>
      <div class="btn-cont">
        <a-button type="primary" id="replenishmentManagement-confirm-modal-save" @click="handleSave">确认补货</a-button>
        <a-button id="replenishmentManagement-confirm-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 } from '@/api/shelfReplenish'
export default {
  name: 'ConfirmModal',
  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: 'no', width: '8%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '22%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '32%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '补货应发数量', dataIndex: 'qty', width: '15%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '实发数量', scopedSlots: { customRender: 'confirmQty' }, width: '15%', align: 'center' },
        { title: '单位', dataIndex: 'productUnit', width: '8%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return shelfReplenishDetailList({ replenishBillSn: this.nowData && this.nowData.replenishBillSn }).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].confirmQty = data[i].qty
            }
            this.disabled = false
          }
          this.listData = data || []
          this.spinning = false
          return data
        })
      },
      listData: []
    }
  },
  computed: {
    modalTit () {
      const hjName = this.nowData && this.nowData.shelfName ? this.nowData.shelfName : ''
      return '补货单确认——' + hjName
    }
  },
  methods: {
    //  确认补货
    handleSave () {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认对该数字货架进行补货吗？',
        centered: true,
        onOk () {
          const arr = []
          const arrInd = []
          _this.listData.map((item, index) => {
            if (item.confirmQty || item.confirmQty == 0) {
              arr.push({
                productSn: item.productSn,
                confirmQty: item.confirmQty
              })
            } else {
              arrInd.push(index + 1)
            }
          })
          if (arrInd.length > 0) {
            let str = ''
            arrInd.map(item => {
              str += item + '、'
            })
            str = str.substr(0, str.length - 1)
            _this.$message.warning('请完善第' + str + '行数据后再提交')
            return
          }
          const params = {
            replenishBillSn: _this.nowData && _this.nowData.replenishBillSn,
            detailList: arr
          }
          _this.$emit('ok', 'confirm', 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-confirm-modal {
	.btn-cont {
		text-align: center;
		margin: 35px 0 10px;
	}
}
</style>
