<template>
  <a-modal
    v-model="opened"
    :title="title"
    centered
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    width="36%"
    :footer="null"
    @cancel="cancel"
  >
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="chooseCustom-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol">
        <a-form-model-item label="出库仓库" prop="warehouseSn">
          <chooseWarehouse ref="warehouse" v-model="form.warehouseSn"></chooseWarehouse>
        </a-form-model-item>
        <a-form-model-item :wrapper-col="{ span: 12, offset: 6 }" style="text-align: center;margin-top:60px;">
          <a-button @click="cancel" style="margin-right: 15px" id="chooseCustom-btn-back">取消</a-button>
          <a-button type="primary" :loading="confirmLoading" @click="handleSubmit" id="chooseCustom-btn-submit">确定</a-button>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { VSelect } from '@/components'
import chooseWarehouse from '@/views/common/chooseWarehouse'
export default {
  name: 'SetWarehouse',
  mixins: [commonMixin],
  components: { VSelect, chooseWarehouse },
  props: {
    show: [Boolean],
    title: {
      type: String,
      default: '批量设置出库仓库'
    }
  },
  data () {
    return {
      opened: this.show,
      spinning: false,
      confirmLoading: false,
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        warehouseSn: undefined
      },
      rules: {
        warehouseSn: [ { required: true, message: '请选择出库仓库', trigger: ['change', 'blur'] } ]
      }
    }
  },
  methods: {
    //  保存
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.$emit('ok', this.form.warehouseSn)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cancel () {
      this.opened = false
      this.$emit('cancel')
      this.$refs.ruleForm.resetFields()
    }
  },
  watch: {
    show (newValue, oldValue) {
      this.opened = newValue
      if (!newValue) {
        this.form.warehouseSn = undefined
        this.$refs.ruleForm.resetFields()
      }
    }
  }
}
</script>
