<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="confirmQty" slot-scope="text,record,index">
          <a-input-number
            size="small"
            id="recallModal-inputNumber"
            @focus="currentInd=index"
            @change="changeNum"
            v-model="record.confirmQty"
            :precision="0"
            :min="0"
            :max="record.qty"
            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 {
      currentInd: 0,
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 15 }
      },
      opened: this.isOpenModal,
      confirmLoading: false,
      spinning: false,
      rules: [],
      columns: [
        { title: '产品编码', dataIndex: 'product.code', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', width: '30%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '调回数量', dataIndex: 'qty', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '实退数量', width: '15%', align: 'center', scopedSlots: { customRender: 'confirmQty' } },
        { title: '单位', dataIndex: 'product.unit', width: '15%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      list: []
    }
  },
  computed: {
    title () {
      return '调回单退库' + ' —— ' + (this.currentData && this.currentData.shelfInfo ? this.currentData.shelfInfo.shelfName : '')
    }
  },

  methods: {
    changeNum (val) {
      console.log(val)
      this.list[this.currentInd].confirmQty = val
      this.list.splice()
      console.log(this.list[this.currentInd].confirmQty, '调回数量')
    },
    // 保存
    handleSave () {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '调回产品将退回至经销商仓库，确认退库吗？',
        centered: true,
        onOk () {
          // _this.$emit('close')
          _this.spinning = true
          const arr = []
          _this.list.map((item, index) => {
            if (item.confirmQty || item.confirmQty == 0) {
              arr.push({
                productSn: item.productSn,
                confirmQty: item.confirmQty,
                recallBillDetailSn: item.recallBillDetailSn
              })
            }
          })
          const params = {
            shelfSn: _this.list[0].shelfSn,
            recallBillSn: _this.list[0].recallBillSn,
            recallBillNo: _this.list[0].recallBillNo,
            detailList: arr
          }
          warehouseCascadeList(params).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
          const no = 0
          for (var i = 0; i < this.list.length; i++) {
            this.list[i].no = no + i + 1
            this.list[i].confirmQty = this.list[i].qty
          }
        } else {
          this.list = []
        }
        this.spinning = false
      })
    },
    // 取消
    cancel () {
      this.$emit('close')
    }
  },
  watch: {
    isOpenModal (nVal, oVal) {
      this.opened = nVal
      if (nVal) {
        this.getData()
        // if (this.currentData) {
        //   this.currentData.map((item, index) => {
        //     item.no = index + 1
        //     item.confirmQty = item.qty
        //   })
        // }
        // this.list = JSON.parse(JSON.stringify(this.currentData || []))
      }
    }
  }
}
</script>

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