<template>
  <a-modal
    v-model="opened"
    :title="title"
    centered
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    :width="960"
    :footer="null"
    @cancel="cancel"
  >
    <a-spin :spinning="spinning" tip="Loading...">
      <a-descriptions v-if="currentData">
        <a-descriptions-item label="调回单号">{{ currentData.recallBillNo||'--' }}</a-descriptions-item>
        <a-descriptions-item label="创建时间">{{ currentData.createDate||'--' }}</a-descriptions-item>
      </a-descriptions>
      <a-table
        class="sTable fixPagination"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data-source="list"
        :scroll="{ y:500 }"
        :pagination="false"
        bordered>
        <template slot="thNum" slot-scope="text, record">
          <a-input-number
            id="inputNumber"
            v-model="record.confirmQty"
            :precision="0"
            :min="0"
            :max="record.dhNum"
            placeholder="请输入"
          />
        </template>
      </a-table>
      <div class="btn-cont">
        <a-button type="primary" id="recallManagement-confirm-modal-save" @click="handleSave">确认退库</a-button>
        <a-button id="recallManagement-confirm-modal-back" @click="cancel" style="margin-left: 15px;">取消</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { warehouseCascadeList, shelfRecallDetailList } from '@/api/shelfRecall.js'
export default {
  components: { STable },
  mixins: [commonMixin],
  props: {
    isOpenModal: {
      type: Boolean,
      default: false
    },
    currentData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 15 }
      },
      opened: this.isOpenModal,
      confirmLoading: false,
      spinning: false,
      rules: [],
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '15%', align: 'center' },
        { title: '产品名称', dataIndex: 'productName', width: '30%', align: 'left' },
        { title: '调回数量', dataIndex: 'qty', width: '10%', align: 'center' },
        { title: '实际退库数量', width: '10%', align: 'center', scopedSlots: { customRender: 'thNum' } },
        { title: '单位', dataIndex: 'danwei', width: '10%', align: 'center' }
      ],
      list: []
    }
  },
  computed: {
    title () {
      return '调回单退库' + ' —— ' + (this.currentData && this.currentData.createDate ? this.currentData.createDate : '')
    }
  },

  methods: {
    // 保存
    handleSave () {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '调回产品将退回至经销商仓库，确认退库吗？',
        centered: true,
        onOk () {
          _this.$emit('close')
          _this.spinning = true
          warehouseCascadeList({ recallBillSn: _this.currentData.recallBillSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.spinning = false
              _this.$emit('refresh')
              _this.cancel()
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    getData () {
      this.spinning = true
      shelfRecallDetailList({ recallBillSn: this.currentData.recallBillSn }).then(res => {
        if (res.status == 200) {
          this.list = res.data.data
          const no = 0
          for (var i = 0; i < this.list.length; i++) {
            this.list[i].no = no + i + 1
          }
        } else {
          this.list = []
        }
        this.spinning = false
      })
    },
    // 取消
    cancel () {
      this.$emit('close')
    }
  },
  watch: {
    isOpenModal (nVal, oVal) {
      this.opened = nVal
      if (nVal) {
        this.getData()
      }
    }
  }
}
</script>

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