<template>
  <a-modal
    centered
    class="storeTransferOut-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="storeTransferOut-basicInfo-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item label="调往对象" prop="putPersonType">
          <a-radio-group v-model="form.putPersonType" @change="putPersonTypeChange">
            <a-radio v-for="(item,index) in putPersonTypeList" :key="index" :value="item.code">{{ item.dispName }}</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="调往对象名称" :prop="isCustomer ? 'putPersonSn' : 'putPersonName'">
          <custList ref="custList" :isValidateEnabled="true" v-if="isCustomer" @change="custChange" v-model="form.putPersonSn"></custList>
          <a-input v-if="!isCustomer" v-model.trim="form.putPersonName" placeholder="请输入调往对象名称(最多30个字符)" allowClear :maxLength="30" />
        </a-form-model-item>
        <a-form-model-item label="调拨类型" prop="callOutType">
          <a-select v-model="form.callOutType" placeholder="请选择调拨类型" allowClear >
            <a-select-option v-for="item in storeCallOutTypeList" :key="item.storeCallOutTypeSn" :value="item.storeCallOutTypeSn">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="备注" prop="remark">
          <a-textarea id="storeTransferOut-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="storeTransferOut-basicInfo-modal-save" @click="handleSave">保存</a-button>
        <a-button id="storeTransferOut-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 { storeCallOutSave } from '@/api/storeCallOut'
import { getLookUpData } from '@/api/data'
import custList from '@/views/common/custList.vue'
import { storeCallOutTypeAllList } from '@/api/storeCallOutType'
export default {
  name: 'StoreTransferOutBasicInfoModal',
  components: { VSelect, custList },
  mixins: [commonMixin],
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        putPersonType: 'CUSTOMER',
        putPersonSn: undefined,
        putPersonName: '',
        callOutType: undefined,
        remark: ''
      },
      rules: {
        putPersonType: [{ required: true, message: '请选择调往对象', trigger: 'change' }],
        putPersonSn: [{ required: true, message: '请选择调往对象名称', trigger: 'change' }],
        putPersonName: [{ required: true, message: '请输入调往对象名称', trigger: 'blur' }],
        callOutType: [{ required: true, message: '请选择调拨类型', trigger: 'change' }]
      },
      putPersonTypeList: [], //  调往对象类型
      storeCallOutTypeList: [] //  调拨类型
    }
  },
  computed: {
    // 当前所选调往对象是否为客户
    isCustomer () {
      return this.form.putPersonType == 'CUSTOMER'
    }
  },
  methods: {
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          _this.spinning = true
          storeCallOutSave(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
        }
      })
    },
    // 调往对象名称 change
    custChange (obj) {
      this.form.putPersonSn = obj.key || undefined
      this.form.putPersonName = obj.row.customerName
    },
    // 调往对象  change
    putPersonTypeChange (e) {
      this.$refs.ruleForm.resetFields()
      this.form.putPersonSn = undefined
      this.form.putPersonName = ''
      this.form.putPersonType = e.target.value
    },
    // 调往对象类型
    getPutPersonTypeList () {
      const _this = this
      getLookUpData({
        pageNo: 1,
        pageSize: 1000,
        lookupCode: 'PUT_PERSON_TYPE'
      }).then(res => {
        if (res.status == 200) {
          _this.putPersonTypeList = res.data.list
        } else {
          _this.putPersonTypeList = []
        }
      })
    },
    //  调拨类型
    getStoreCallOutTypeAllList () {
      storeCallOutTypeAllList().then(res => {
        if (res.status == 200) {
          this.storeCallOutTypeList = res.data
        } else {
          this.storeCallOutTypeList = []
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
        this.$refs.custList.resetForm()
      } else {
        this.getPutPersonTypeList()
        this.getStoreCallOutTypeAllList()
      }
    }
  }
}
</script>

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