<template>
  <div>
    <el-form ref="form" :rules="rules"  :model="form" label-width="120px">
      <el-form-item
          label="用户名称："
          prop="role_id"
      >
      <el-select
          v-model="form.user_id"
          :multiple = "false"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="remoteMethod"
          :loading="loading">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.user_name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    
      <el-form-item>
        <div style="text-align: center">
          <el-button type="primary" @click="onSubmit" :disabled="Isdisabled" :loading="isloading">保存</el-button>
          <el-button style="color: #5075FC " @click="closeDlg">取消</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  props: {
    roleDataForm: {
      type: Object,
      default: () => {}
    },
    isloading:{
      type:Boolean,
      default: true
    },
    closeDlg:{
      type:Function
    }
  },
  data() {
    return {
      options: [],
      loading: false,
      form: {
        user_id: ''
      },
      Isdisabled: false,
      rules: {
        user_id: [
          { required: true, message: '用户名称必填' },
        ]
      }
    }
  },
  mounted() {
    if(this.roleDataForm) {
      this.form = Object.assign({} , this.roleDataForm)
    }
  },
  methods: {
    closeDlg() {
      this.$emit('closeDlg',1)
    },
    remoteMethod(query) {  
      let _t = this;
        if (query !== '') {
          _t.loading = true;
        let parames = {
            'keyword':query
        }
        this.$api.user.getUserList(parames).then(res=>{
                _t.loading = false;
                if(res.code==200){
                  if(res.data.rows.length==0){
                    this.$message.error("没找到该用户！！！")
                    return
                  }
                  this.options = res.data.rows
                }else{
                    this.$message.error("查询失败")
                }
            })
        } else {
          this.options = [];
        }
    },
    onSubmit() {
      let that = this
      that.$refs['form'].validate((valid) => {
        if (valid) {
          console.log("提交数据：",this.form)
          this.$emit('roleSubmitClick', this.form)
        }
      })
    },
  }
}
</script>
<style>
.el-cascader-node>.el-radio{
  margin-top:25px;
}
</style>
