<template>
  <a-modal
    centered
    class="warehouseAllocation-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    title="新增"
    v-model="isShow"
    @cancel="isShow = false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="warehouseAllocation-basicInfo-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol" >
        <a-form-model-item required label="调出仓库" prop="outWarehouseSn">
          <a-select id="warehouseAllocation-basicInfo-outWarehouseSn" allowClear placeholder="请选择调出仓库" v-model="form.outWarehouseSn" >
            <a-select-option v-for="(item, index) in warehouseList" :id="'basicInfo-outWarehouseSn-'+item.warehouseSn" :key="index" :value="item.warehouseSn">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item required label="调入仓库" prop="putWarehouseSn">
          <a-select id="warehouseAllocation-basicInfo-putWarehouseSn" allowClear placeholder="请选择调入仓库" v-model="form.putWarehouseSn" >
            <a-select-option v-for="(item, index) in warehouseList" :id="'basicInfo-putWarehouseSn-'+item.warehouseSn" :key="index" :value="item.warehouseSn">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="关联单号" prop="relevanceNo">
          <a-input id="warehouseAllocation-basicInfo-relevanceNo" :maxLength="50" v-model.trim="form.relevanceNo" placeholder="请输入关联单号(最多50个字符)" allowClear />
        </a-form-model-item>
        <a-form-model-item label="备注" prop="remark">
          <a-textarea id="warehouseAllocation-basicInfo-remark" :maxLength="120" v-model="form.remark" placeholder="请输入备注(最多120个字符)" allowClear />
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button type="primary" id="warehouseAllocation-basicInfo-modal-save" @click="handleSave">保存</a-button>
        <a-button id="warehouseAllocation-basicInfo-modal-back" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { VSelect } from '@/components'
import { warehouseAllList } from '@/api/warehouse.js'
import { allocWarehouseSave } from '@/api/allocWarehouse.js'
export default {
  name: 'WarehouseAllocationBasicInfoModal',
  components: { VSelect },
  mixins: [commonMixin],
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
      } else {
        this.getWarehouseList()
      }
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        outWarehouseSn: undefined, // 调出仓库
        putWarehouseSn: undefined, // 调入仓库
        relevanceNo: '', // 关联单据号
        remark: '' // 备注
      },
      rules: {
        putWarehouseSn: [{ required: true, message: '请选择调入仓库', trigger: 'blur' }],
        outWarehouseSn: [{ required: true, message: '请选择调出仓库', trigger: 'blur' }]
      },
      warehouseList: [], // 仓库列表
      brandData: [] //  供应商  下拉数据
    }
  },
  methods: {
    // 获取仓库列表
    getWarehouseList () {
      warehouseAllList({}).then(res => {
        if (res.status == 200) {
          this.warehouseList = res.data || []
        } else {
          this.warehouseList = []
        }
      })
    },
    //  保存
    handleSave () {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          this.spinning = true
          allocWarehouseSave(this.form).then(res => {
            if (res.status == 200) {
              this.isShow = false
              this.$emit('ok', res.data)
              this.spinning = false
            } else {
              this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    }
  }

}
</script>

<style lang="less">
.warehouseAllocation-basicInfo-modal {
	.ant-modal-body {
		padding: 40px 40px 24px;
	}
	.btn-cont {
		text-align: center;
		margin: 35px 0 10px;
	}
}
</style>
