<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-form-model
        id="replenishmentManagement-outWarehousing-form"
        ref="ruleForm"
        layout="inline"
        :model="form"
        :rules="rules"
        style="margin-bottom: 10px;" >
        <a-row :gutter="15">
          <a-col :md="12" :sm="24">
            <a-form-model-item label="补货单号">{{ basicInfoData && basicInfoData.replenishBillNo || '--' }}</a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-model-item label="创建时间">{{ basicInfoData && basicInfoData.createDate || '--' }}</a-form-model-item>
          </a-col>
          <!-- <a-col :md="12" :sm="24">
            <a-form-model-item label="配送方式" prop="dispatchType" style="margin: 0;">
              <a-radio-group v-model="form.dispatchType">
                <a-radio v-for="(item,index) in dispatchTypeList" :key="index" :value="item.code">{{ item.dispName }}</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col> -->
          <a-col :md="24" :sm="24">
            <a-form-model-item label="出库备注">
              <a-input id="replenishmentManagement-outWarehousing-remarks" v-model.trim="form.remarks" allowClear placeholder="请输入出库备注"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: 400 }"
        :showPagination="false"
        :defaultLoadData="false"
        bordered>
      </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 { getLookUpItem } from '@/api/lookup'
import { shelfReplenishDetailList, shelfReplenishDetail, shelfReplenishOutStock } from '@/api/shelfReplenish'
export default {
  name: 'OutWarehousingModal',
  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: 'product.code', width: '22%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', 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: '实发数量', dataIndex: 'confirmQty', width: '15%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'product.unit', 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
            }
            this.disabled = false
            this.listData = data || []
          }
          this.spinning = false
          return data
        })
      },
      form: {
        dispatchType: undefined,
        remarks: ''
      },
      rules: {
        'dispatchType': [{ required: true, message: '请选择配送方式', trigger: ['blur', 'change'] }]
      },
      dispatchTypeList: [
        { name: '送货到店', val: 1 },
        { name: '快递到店', val: 2 }
      ],
      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.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.$confirm({
            title: '提示',
            content: '确认信息无误并进行出库吗？',
            centered: true,
            onOk () {
              const arr = []
              _this.listData.map((item, index) => {
                arr.push({
                  productSn: item.productSn,
                  confirmQty: item.confirmQty,
                  replenishBillDetailSn: item.replenishBillDetailSn,
                  id: item.id
                })
              })
              const params = {
                replenishBillSn: _this.nowData && _this.nowData.replenishBillSn,
                detailList: arr,
                dispatchType: _this.form.dispatchType,
                remarks: _this.form.remarks,
                shelfSn: _this.nowData && _this.nowData.shelfSn
              }
              _this.spinning = true
              shelfReplenishOutStock(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
                }
              })
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 基本信息
    getDetail () {
      shelfReplenishDetail({ sn: this.nowData && this.nowData.replenishBillSn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data
        } else {
          this.basicInfoData = null
        }
      })
    },
    // 配送方式
    getLookUpItem () {
      getLookUpItem({ lookupCode: 'DISTRIBUTION_MODE', pageNo: 1, pageSize: 1000 }).then(res => {
        if (res.status == 200 && res.data && res.data.list) {
          this.dispatchTypeList = res.data.list
        } else {
          this.basicInfoData = []
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.table.clearTable()
        this.form.remarks = ''
        this.$refs.ruleForm.resetFields()
      } else {
        const _this = this
        // this.getLookUpItem()
        this.getDetail()
        this.$nextTick(() => { // 页面渲染完成后的回调
          _this.$refs.table.refresh(true)
        })
      }
    }
  }
}
</script>

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