<template>
  <!--对话框-->
  <el-dialog :title="dialogTitle" :visible="showDialog" width="800px" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="!matchUpdatePassWord" :before-close="close">
    <el-form :model="user_form" :rules="rules" ref="user_form" label-width="120px" label-position="left">
      <el-form-item label="账号" prop="username">
        <el-input maxlength="40" show-word-limit v-model.trim="user_form.username" disabled/>
      </el-form-item>
      <el-form-item label="管理员姓名" prop="name">
        <el-input maxlength="40" show-word-limit v-model.trim="user_form.name"/>
      </el-form-item>
      <el-form-item label="管理员手机号" prop="phone">
        <el-input maxlength="11" show-word-limit v-model.trim="user_form.phone"/>
      </el-form-item>
      <el-form-item label="登录密码" prop="password">
        <el-input maxlength="40" show-word-limit show-password v-model.trim="user_form.password"/>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close" :disabled="matchUpdatePassWord">
        取消
      </el-button>
      <el-button type="primary"  @click="doUpdate">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
    // import {doEditSelf} from "@/api/user_manage";
    import store from "@/store";

    export default {
        name: "UserInfoEdit",
        props: {
          // 是否显示对话框
          dialogVisible: {
            type: Boolean,
            default: false
          },
          //是否是首次登陆验证改密码时
          matchUpdatePassWord: {
            type: Boolean,
            default: false
          }
        },
        data() {
            return {
              username: this.$store.getters.username,
              name: this.$store.getters.name,
              avatar: '',
              // 是否显示对话框
              showDialog: false,
              // 对话框标题
              dialogTitle: '编辑资料',
              // 编辑资料
              user_form: {
                username: this.$store.getters.username,
                name: this.$store.getters.name,
                phone: this.$store.getters.phone,
                password: null,
              },
              // 验证规则
              rules: {
                username: [
                  { required: true, message: '请输入账号名称', trigger: 'blur' },
                ],
                name: [
                  { required: true, message: '请输入管理员姓名', trigger: 'blur' },
                ],
                phone: [
                  { required: true, message: '请输入管理员手机号', trigger: 'blur' }
                ],
                password: [
                  { required: true, message: '请输入登录密码', trigger: 'blur' },
                  {
                    pattern:
                      /^(((?=.*\d)(?=.*[a-z])(?=.*[A-Z]))|((?=.*\d)(?=.*[a-z])(?=.*[~!@#$%^&*]))|((?=.*\d)(?=.*[A-Z])(?=.*[~!@#$%^&*]))|((?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$%^&*]))|((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$%^&*]))).{10,32}$/,
                    message:
                      "必须大小写字母、数字、特殊字符,4种组合中至少满足3种，且密码长度在10-32位之间",
                  },
                ],
              }
            }
        },
      methods: {
        // 确定编辑
        doUpdate() {
          this.$refs.user_form.validate((valid) => {
            if(valid) {
              // doEditSelf(this.user_form).then(async res => {
              //   this.showDialog = false
              //   await this.$store.dispatch('user/getInfo')
              //   this.$message({
              //     type: 'success',
              //     message: '编辑成功'
              //   });
              // })
            }
          })
        },
        //关闭弹窗回调
        close() {
          this.showDialog = false
        },
      },
      mounted() {
        this.showDialog = this.dialogVisible;
      },
      watch: {
          dialogVisible: {
            handler:function (val) {
              this.showDialog = val;
              this.$nextTick(() => {
                this.$refs['user_form'].clearValidate();
              })
            },
            immediate: false,
            deep: true
          },
      }
    }
</script>

<style scoped>

</style>
