<template>
  <a-modal
    centered
    class="chainTransferOut-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    title="新增连锁调出单"
    v-model="isShow"
    @cancle="isShow = false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="chainTransferOut-basicInfo-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item label="调出产品类型" prop="allocationType">
          <v-select
            v-model="form.allocationType"
            ref="allocationType"
            id="chainTransferOut-basicInfo-allocationType"
            code="ALLOCATION_LINKAGE_PRODUCT_TYPE"
            placeholder="请选择调出产品类型"
            allowClear
          ></v-select>
        </a-form-model-item>
        <a-form-model-item label="调往对象" prop="putTenantSn">
          <a-select
            placeholder="请选择调往对象"
            allowClear
            v-model="form.putTenantSn"
            :showSearch="true"
            option-filter-prop="children"
            :filter-option="filterOption">
            <a-select-option v-for="item in putTenantSnData" :key="item.sn" :value="item.sn">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="备注" prop="remark">
          <a-textarea id="chainTransferOut-basicInfo-remark" :maxLength="500" v-model="form.remark" placeholder="请输入备注(最多500个字符)" allowClear />
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button type="primary" id="chainTransferOut-basicInfo-modal-save" @click="handleSave">保存</a-button>
        <a-button id="chainTransferOut-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 { getTenantList, allocLinkageOutSave } from '@/api/allocLinkageOut'
export default {
  name: 'ChainTransferOutBasicInfoModal',
  components: { VSelect },
  mixins: [commonMixin],
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        allocationType: '', // 调出产品类型
        putTenantSn: undefined, //  调往对象
        remark: ''
      },
      rules: {
        allocationType: [{ required: true, message: '请选择调出产品类型', trigger: 'change' }],
        putTenantSn: [{ required: true, message: '请选择调往对象', trigger: 'change' }]
      },
      putTenantSnData: [] //  调往对象  下拉数据
    }
  },
  methods: {
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          _this.spinning = true
          allocLinkageOutSave(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
        }
      })
    },
    filterOption (input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    // 调往对象
    getPutTenantList () {
      getTenantList({}).then(res => {
        if (res.status == 200) {
          this.putTenantSnData = res.data
        } else {
          this.putTenantSnData = []
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
      } else {
        this.getPutTenantList()
      }
    }
  }
}
</script>

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