<template>
  <a-modal
    centered
    class="storeTransferOut-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    title="新增店内调出单"
    v-model="isShow"
    @cancle="isShow = false"
    :width="800">
    <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'">
        <a-select
          v-if="isCustomer"
          v-model="form.putPersonSn"
          placeholder="请选择"
          allowClear
          :showSearch="true"
          option-filter-prop="children"
          :filter-option="filterOption"
          @change="putPersonChange">
          <a-select-option v-for="item in custData" :key="item.customerSn" :value="item.customerSn">{{ item.customerName }}</a-select-option>
        </a-select>
        <a-input v-if="!isCustomer" v-model="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-modal>
</template>

<script>
import { VSelect } from '@/components'
import { storeCallOutSave } from '@/api/storeCallOut'
import { custAllList } from '@/api/customer'
import { getLookUpData } from '@/api/data'
import { storeCallOutTypeAllList } from '@/api/storeCallOutType'
export default {
  name: 'StoreTransferOutBasicInfoModal',
  components: { VSelect },
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      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' }]
      },
      custData: [], //  调往对象  下拉数据
      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))
          storeCallOutSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.isShow = false
                _this.$emit('ok', res.data)
              }, 1000)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 调往对象  change
    putPersonTypeChange (e) {
      this.$refs.ruleForm.resetFields()
      this.form.putPersonSn = undefined
      this.form.putPersonName = ''
      this.form.putPersonType = e.target.value
    },
    // 调往对象名称  change
    putPersonChange (val) {
      const ind = this.custData.findIndex(item => item.customerSn == val)
      this.form.putPersonName = this.custData[ind].customerName
    },
    // 调往对象列表
    getCustAllList () {
      custAllList().then(res => {
        if (res.status == 200) {
          this.custData = res.data
        } else {
          this.custData = []
        }
      })
    },
    // 调往对象类型
    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 = []
        }
      })
    },
    filterOption (input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
      } else {
        this.getCustAllList()
        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>
