<template>
  <a-modal
    centered
    class="replenishmentManagement-outWarehousing-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancel="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-item label="配送方式">{{ basicInfoData && basicInfoData.dispatchTypeDictValue || '--' }}</a-descriptions-item> -->
      </a-descriptions>
      <div style="padding-bottom: 16px;display: flex;">
        <p style="margin: 0;display: inline-block;flex-shrink: 0;">出库备注：</p>
        <div style="display: inline-block;vertical-align: bottom;flex-grow: 1;max-width: 90%;cursor: pointer;">
          <a-tooltip placement="bottom">
            <template slot="title">
              <span>{{ basicInfoData && basicInfoData.remarks || '--' }}</span>
            </template>
            <p class="text-ellipsis">{{ basicInfoData && basicInfoData.remarks || '--' }}</p>
          </a-tooltip>
        </div>
      </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>
        <!-- 签收数量 -->
        <template slot="putQty" slot-scope="text, record">
          <a-input-number
            v-if="record.qty&&record.qty!=0"
            size="small"
            id="replenishmentManagement-outWarehousing-putQty"
            v-model="record.putQty"
            :precision="0"
            :min="0"
            :max="999999"
            placeholder="请输入"
            style="width: 100%" />
          <span v-else>--</span>
        </template>
      </s-table>
      <div class="btn-cont">
        <a-button type="primary" id="replenishmentManagement-outWarehousing-modal-save" @click="handleSave">确认签收</a-button>
        <a-button id="replenishmentManagement-outWarehousing-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, shelfReplenishPutStock } from '@/api/shelfReplenish'
export default {
  name: 'PutWarehousingModal',
  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: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'product.code', width: '22%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', width: '28%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '补货数量', dataIndex: 'qty', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '签收数量', scopedSlots: { customRender: 'putQty' }, width: '15%', align: 'center' },
        { title: '单位', dataIndex: 'product.unit', width: '6%', 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].putQty = data[i].qty
            }
            this.disabled = false
            this.listData = data || []
          }
          this.spinning = false
          return data
        })
      },
      listData: [], // 列表数据
      basicInfoData: null // 基本信息
    }
  },
  computed: {
    // 弹框标题
    modalTit () {
      const hjName = this.nowData && this.nowData.shelfInfo.shelfName ? this.nowData.shelfInfo.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.putQty || item.putQty == 0) {
              arr.push({
                productSn: item.productSn,
                putQty: item.putQty,
                replenishBillDetailSn: item.replenishBillDetailSn,
                shelfPlaceSn: item.shelfPlaceSn,
                shelfPlaceCode: item.shelfPlaceCode,
                id: item.id
              })
            } else if ((item.confirmQty || item.confirmQty == 0) && !(item.putQty || item.putQty == 0)) {
              arrInd.push(index + 1)
            }
          })
          if (arrInd.length > 0) {
            _this.$message.warning('操作失败！签收数量不能为空')
            return
          }
          const params = {
            replenishBillSn: _this.nowData && _this.nowData.replenishBillSn,
            detailList: arr
          }
          _this.spinning = true
          shelfReplenishPutStock(params).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              _this.spinning = false
              _this.isShow = false
            } else {
              _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-outWarehousing-modal {
  .text-ellipsis{
    margin: 0;
    overflow:hidden;
    text-overflow:ellipsis;
    display:-webkit-box;
    -webkit-box-orient:vertical;
    -webkit-line-clamp:1;
  }
	.btn-cont {
		text-align: center;
		margin: 35px 0 10px;
	}
}
</style>
