<template>
  <a-modal
    centered
    class="shelfMonitoring-add-modal"
    :footer="null"
    :maskClosable="false"
    title="新增调回单"
    v-model="isShow"
    @cancle="isShow = false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-descriptions>
        <a-descriptions-item label="货架名称">{{ nowData && nowData.shelfInfo && nowData.shelfInfo.shelfName || '--' }}</a-descriptions-item>
      </a-descriptions>
      <!-- 提示 -->
      <a-alert type="info" style="margin-bottom:10px">
        <div slot="message">调回数量不能大于当前库存。如果调回数量为0，请移除此产品。</div>
      </a-alert>
      <!-- 列表 -->
      <a-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :dataSource="listData"
        :scroll="{ y: 400 }"
        :pagination="false"
        bordered>
        <!-- 调回数量 -->
        <template slot="outQty" slot-scope="text, record">
          <a-input-number
            size="small"
            id="shelfMonitoring-add-outQty"
            v-model="record.outQty"
            :precision="0"
            :min="0"
            :max="record.qty"
            placeholder="请输入"
            :style="{width: '100%', color: record.outQty==0 ? 'red': ''}" />
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record, index">
          <a-button
            size="small"
            type="link"
            class="button-primary"
            @click="handleDel(record, index)"
            id="shelfMonitoring-add-btn">移除</a-button>
        </template>
      </a-table>
      <div class="btn-cont">
        <a-button type="primary" :disabled="listData.length==0" id="shelfMonitoring-add-modal-save" @click="handleSave">确认调回</a-button>
        <a-button id="shelfMonitoring-add-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 { shelfRecallSave } from '@/api/shelfRecall'
export default {
  name: 'ShelfMonitoringAddModal',
  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: 'shelfPlaceCode', width: '16%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productCode', width: '13%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '17%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '当前库存', dataIndex: 'qty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '最大库容', dataIndex: 'maxQty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '滞销天数', dataIndex: 'unsalableDays', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '调货数量', scopedSlots: { customRender: 'outQty' }, width: '10%', align: 'center' },
        { title: '单位', dataIndex: 'productUnit', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '8%', align: 'center' }
      ],
      listData: []
    }
  },
  methods: {
    //  确认调回
    handleSave () {
      const _this = this
      const arr = []
      const arrInd = []
      this.listData.map((item, index) => {
        if (item.outQty) {
          arr.push({
            shelfPlaceSn: item.shelfPlaceSn,
            productSn: item.productSn,
            qty: item.outQty
          })
        } else {
          arrInd.push(index + 1)
        }
      })
      if (arrInd.length > 0) {
        this.$message.warning('调回数量不能为空或0，请移除后提交')
        return
      }
      const params = {
        shelfSn: this.nowData && this.nowData.shelfInfo && this.nowData.shelfInfo.shelfSn,
        detailList: arr
      }
      shelfRecallSave(params).then(res => {
        if (res.status == 200) {
          _this.$emit('ok')
          _this.isShow = false
          _this.$message.success('保存并确认成功')
        }
      })
    },
    // 移除
    handleDel (row, ind) {
      this.listData.splice(ind, 1)
      this.listData.map((item, index) => {
        item.no = index + 1
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.listData = []
      } else {
        if (this.nowData && this.nowData.list) {
          this.nowData.list.map((item, index) => {
            item.no = index + 1
            item.outQty = item.qty
          })
        }
        this.listData = JSON.parse(JSON.stringify(this.nowData && this.nowData.list || []))
      }
    }
  }
}
</script>

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