<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    <el-form-item :label="initName('nickName')" prop="nickName">
      <el-input v-model="form.nickName" maxlength="30" />
    </el-form-item>
    <el-form-item :label="initName('telphone')" prop="phonenumber">
      <el-input v-model="form.phonenumber" maxlength="11" />
    </el-form-item>
    <el-form-item :label="initName('email')" prop="email">
      <el-input v-model="form.email" maxlength="50" />
    </el-form-item>
    <el-form-item :label="initName('sex')">
      <el-radio-group v-model="form.sex">
        <el-radio label="0">{{ initName('male') }}</el-radio>
        <el-radio label="1">{{ initName('female') }}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">{{ initName('save') }}</el-button>
      <el-button type="danger" size="mini" @click="close">{{ $t('common.close') }}</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserProfile } from "@/api/system/user";
import {translateCommon} from "../../../../utils/common";

export default {
  props: {
    user: {
      type: Object
    }
  },
  data() {
    return {
      form: {},
      prefixLabel:"personal.",
      // 表单校验
      rules: {
        nickName: [
          { required: true, message:this.$t('personal.nickNameValidate'), trigger: "blur" }
        ],
        email: [
          { required: true, message:this.$t('personal.emailRequireValidate'), trigger: "blur" },
          {
            type: "email",
            message: this.$t('personal.emailCorretValidate'),
            trigger: ["blur", "change"]
          }
        ],
        phonenumber: [
          { required: true, message: this.$t('personal.telphoneRequireValidate'), trigger: "blur" },
          // {
          //   pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
          //   message: this.$t('personal.telphoneCorretValidate'),
          //   trigger: "blur"
          // }
        ]
      }
    };
  },
  watch: {
    user: {
      handler(user) {
        if (user) {
          this.form = { nickName: user.nickName, phonenumber: user.phonenumber, email: user.email, sex: user.sex };
        }
      },
      immediate: true
    }
  },
  methods: {
    initName(key){
      return  translateCommon(this.prefixLabel+key)
    },
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUserProfile(this.form).then(response => {
            this.$modal.msgSuccess(this.initName('modifySuccess'));
            this.user.phonenumber = this.form.phonenumber;
            this.user.email = this.form.email;
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>
