<template>
  <a-modal
    centered
    class="bulkWarehousingOrder-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="bulkWarehousingOrder-basicInfo-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol" >
        <a-form-model-item label="供应商" prop="supplierSn">
          <supplier v-model="form.supplierSn" isPermission placeholder="请输入供应商名称"></supplier>
        </a-form-model-item>
        <a-form-model-item label="入库类型" prop="sparePartsType">
          <v-select
            v-model="form.sparePartsType"
            ref="sparePartsType"
            id="bulkWarehousingOrder-basicInfo-sparePartsType"
            code="SPARE_PARTS_TYPE"
            placeholder="请选择入库类型"
            allowClear></v-select>
        </a-form-model-item>
        <a-form-model-item label="入库仓库" prop="warehouseSn" v-show="isShowWarehouse">
          <warehouse
            v-model="form.warehouseSn"
            isPermission
            id="bulkWarehousingOrder-basicInfo-warehouseSn"
            placeholder="请选择入库仓库"
          />
        </a-form-model-item>
        <a-form-model-item label="关联单号" prop="relationNo">
          <a-input
            id="bulkWarehousingOrder-relationNo"
            :maxLength="30"
            v-model.trim="form.relationNo"
            placeholder="请输入关联单号(最多30个字符)"
            allowClear />
        </a-form-model-item>
        <a-form-model-item label="入库备注" prop="remark">
          <a-textarea
            id="bulkWarehousingOrder-remark"
            :maxLength="30"
            v-model="form.remark"
            placeholder="请输入备注(最多30个字符)"
            allowClear />
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button id="bulkWarehousingOrder-basicInfo-modal-back" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
        <a-button type="primary" id="bulkWarehousingOrder-basicInfo-modal-save" @click="handleSave">保存</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { VSelect } from '@/components'
import { sparePartsSave } from '@/api/spareParts'
import warehouse from '@/views/common/chooseWarehouse.js'
import supplier from '@/views/common/supplier.js'
export default {
  name: 'BulkWarehousingOrderBasicInfoModal',
  mixins: [commonMixin],
  components: { VSelect, supplier, warehouse },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      formItemLayout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 16 }
      },
      form: {
        supplierSn: undefined, // 供应商
        sparePartsType: undefined, //  散件类型
        warehouseSn: undefined, // 仓库
        relationNo: '', //  关联单号
        remark: '' // 备注
      },
      rules: {
        supplierSn: [
          { required: true, message: '请选择供应商', trigger: 'change' }
        ],
        sparePartsType: [
          { required: true, message: '请选择入库类型', trigger: 'change' }
        ],
        warehouseSn: [{ required: true, message: '请选择入库仓库', trigger: 'change' }],
      },
      supplierList: [] //  供应商  下拉数据
    }
  },
  methods: {
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          _this.spinning = true
          sparePartsSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.isShow = false
                _this.$emit('ok', res.data)
                _this.spinning = false
              }, 1000)
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
      }else{
        this.form.warehouseSn = this.isShowWarehouse ? undefined : this.defWarehouse.warehouseSn
      }
    }
  }
}
</script>

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