<template>
  <div v-loading="dialogLoading" :element-loading-text="loadingText">

    <el-row :gutter="15" >
      <el-form ref="form" :model="dataSource" :rules="rules" label-width="100px">
        <el-col :span="24">
          <el-form-item label="昵称" prop="nickName">
            <el-input v-model="dataSource.nickName" placeholder="请输入昵称" maxlength="50" show-word-limit clearable :disabled="isDisabled"/>
          </el-form-item>
          <el-form-item label="真实姓名" prop="realName">
            <el-input v-model="dataSource.realName" placeholder="请输入真实姓名" maxlength="5" show-word-limit clearable :disabled="isDisabled"/>
          </el-form-item>
          <el-form-item label="手机号" prop="phoneNumber">
            <el-input v-model="dataSource.phoneNumber" placeholder="请输入手机号" maxlength="11" show-word-limit clearable :disabled="isDisabled"/>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-select v-model="dataSource.sex" placeholder="请选择性别" clearable :disabled="isDisabled">
              <el-option
                v-for="dict in dict.type.sys_user_sex"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="生日" prop="birthday" clearable >
            <el-date-picker clearable
                            v-model="dataSource.birthday"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择生日"
                            :disabled="isDisabled">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="头像地址" prop="avatarUrl" >
            <image-upload v-model="dataSource.avatarUrl" :limit="1" :disabled="isDisabled"/>
          </el-form-item>
          <el-form-item label="客户等级" prop="level">
            <el-input-number v-model="dataSource.level"  :min="1" :max="50" label="请输入客户等级" :disabled="isDisabled"></el-input-number>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataSource.remark" placeholder="请输入备注" maxlength="50" show-word-limit />
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" style="margin-left: 5%">提交</el-button>
      <el-button @click="closeDialog">取 消</el-button>
    </div>
  </div>

</template>

<script>
import {addUser,updateUser} from "../../../../api/app/user";

export default {
  name: "editUser",
  dicts: ['sys_user_sex'],
  props: {
    dataSource:{
      type: Object
    },
    isDisabled:{
      type: Boolean
    }

  },
  data(){
    return {
      dialogLoading:false,
      loadingText:"",
      // 表单校验
      rules: {
        nickName:[{
          required: true,
          message: "昵称不能为空",
          trigger: "blur"
        }],
        realName:[{
          required: true,
          message: "真实姓名不能为空",
          trigger: "blur"
        }],
        phoneNumber:[{
          required: true,
          message: "手机号不能为空",
          trigger: "blur"
        }],
        sex:[{
          required: true,
          message: "性别不能为空",
          trigger: "blur"
        }],
        birthday:[{
          required: true,
          message: "生日不能为空",
          trigger: "blur"
        }],
      },
    }
  },
  methods: {
    closeDialog() {
      var ctx = this
      this.$confirm('是否关闭表单，关闭后数据将丢失？').then(function() {
        ctx.$emit('closeDialog')
      }).then(() => {}).catch(() => {});
    },
    submitForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.dataSource.id != null) {
            updateUser(this.dataSource).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$emit('onSubmit',this.dataSource)
            });
          } else {
            addUser(this.dataSource).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.$emit('onSubmit',response.data)
            });
          }
        }
      });
    }
  }
}
</script>

<style scoped>

</style>
