<template>
  <div id="app">
    <router-view />
    <theme-picker />
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="修改密码"
      :visible.sync="passShow"
      width="620px"
      append-to-body
      class="popup"
      :show-close="false"
    >
      <el-form ref="form" :model="user" :rules="rules" label-width="80px">
        <el-form-item label="旧密码" prop="oldPassword">
          <el-input
            v-model="user.oldPassword"
            placeholder="请输入旧密码"
            type="password"
            show-password
          />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="user.newPassword"
            placeholder="请输入新密码"
            type="password"
            show-password
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="user.confirmPassword"
            placeholder="请确认新密码"
            type="password"
            show-password
          />
        </el-form-item>
        <el-form-item label=" " style="margin-bottom: 0px">
          <template slot-scope="scope">
            <el-button type="primary" @click="submit" size="mini"
              >确 定</el-button
            >
          </template>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { updateUserPwd } from "@/api/system/user";
import ThemePicker from "@/components/ThemePicker";
import { mapState, mapMutations } from "vuex";
export default {
  name: "App",
  components: { ThemePicker },
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      passShow: false,
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined,
      },
      rules: {
        oldPassword: [
          { required: true, message: "旧密码不能为空", trigger: "blur" },
        ],
        newPassword: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            pattern: /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[^\da-zA-Z\s]).{8,20}$/,
            message: "必须包含数字、字母、特殊字符, 长度为8到20位",
            trigger: "blur",
          },
        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" },
        ],
      },
      userInfo: {},
    };
  },
  computed: {
    ...mapState({
      forceUpdPwdFlag: (state) => state.user.forceUpdPwdFlag,
    }),
  },
  watch: {
    // 监听store中forceUpdPwdFlag的变化
    forceUpdPwdFlag(new1, old) {
      if (new1) {
        this.passShow = true;
      }
    },
  },

  created() {},
  methods: {
    submit() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          const res = await updateUserPwd(
            this.user.oldPassword,
            this.user.newPassword
          );
          const { code, msg } = res;
          if (code === 200) {
            this.passShow = false;
            this.$message.success("修改成功");
            // const res = await getInfo();
          } else {
            this.$message.warning(msg);
          }
        }
      });
    },
  },
  metaInfo() {
    return {
      title:
        this.$store.state.settings.dynamicTitle &&
        this.$store.state.settings.title,
      titleTemplate: (title) => {
        return title
          ? `${title} - ${process.env.VUE_APP_TITLE}`
          : process.env.VUE_APP_TITLE;
      },
    };
  },
};
</script>
<style scoped>
#app .theme-picker {
  display: none;
}
</style>
<style>
.header-title {
  display: -webkit-box !important;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
}
.wj-uploader .el-icon-upload:before {
  font-size: 100px;
}
.wj-uploader .el-upload {
  display: flex;
  align-content: center;
  justify-content: center;
  flex-wrap: wrap;
}
.wj-uploader .el-upload__text {
  line-height: 20px;
  width: 100% !important;
}
.avatar-uploader {
  height: 180px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9 !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 100% !important;
  height: 180px !important;
  line-height: 150px;
}
.el-upload-dragger,
.el-upload {
  width: 100% !important;
}
.el-upload-list__item {
  transition: none !important;
}
.el-upload-list__item:first-child {
  margin-top: 0;
}
</style>
