<!--
 * @Author: ym
 * @Date: 2021-09-28 10:53:53
 * @LastEditTime: 2022-01-09 18:57:16
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \background-front-end\src\views\common\home.vue
-->
<template>
  <div class="mod-home">
    <el-dialog
      title="提示：当前密码安全级别较低，请您修改密码"
      :visible.sync="dialogVisible"
      width="500px"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <el-form ref="ruleForm" :model="form" :rules="rules">
        <el-form-item
          label="新密码"
          :label-width="formLabelWidth"
          prop="password"
        >
          <el-input
            placeholder="请输入新密码"
            style="width: 240px; margin-right: 20px"
            v-model="form.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="isPassword"
          label="确认新密码:"
          :label-width="formLabelWidth"
        >
          <el-input
            style="width: 240px"
            placeholder="请再次输入新密码"
            v-model="form.isPassword"
            show-password
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <!-- <el-button @click="dialogVisible = false">取 消</el-button> -->
        <el-button type="primary" @click="confirmPassword"
          >确定并重新登入</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { clearLoginInfo } from "@/utils";
export default {
  data() {
    return {
      dialogVisible: false,
      formLabelWidth: "120px",
      form: {
        isPassword: "",
        password: "",
      },
      rules: {
        password: [
          {
            required: true,
            message: "请输入新密码",
            trigger: "blur",
          },
          {
            min: 8,
            max: 12,
            message:
              "密码由8-12位字母、数字、特殊符号（~、@、#、$、%、*）的组成，请重新输入",
            trigger: "blur",
          },
          {
            trigger: "blur",
            validator: (rule, value, callback) => {
              var passwordreg =
                /(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,12}/;
              if (!passwordreg.test(value)) {
                callback(
                  new Error(
                    "密码由8-12位字母、数字、特殊符号（~、@、#、$、%、*）的组成，请重新输入"
                  )
                );
              } else {
                callback();
              }
            },
          },
        ],
        isPassword: [
          {
            required: true,
            message: "请输入确认密码",
            trigger: "blur",
          },
          {
            trigger: "blur",
            validator: (rule, value, callback) => {
              if (value != this.form.password) {
                callback(new Error("密码输入不一致"));
              } else {
                callback();
              }
            },
          },
        ],
      },
    };
  },
  created() {
    if (
      JSON.parse(sessionStorage.getItem("password")) == "123456" &&
      JSON.parse(sessionStorage.getItem("role")) === "ROLE_BRAND_MANAGER"
    ) {
      this.dialogVisible = true;
    }
  },
  methods: {
    confirmPassword() {
      this.$refs.ruleForm.validate((valid) => {
        console.log(valid);
        if (valid) {
          this.$api.mer_admin
            .changePassword({
              oldPassword: JSON.parse(sessionStorage.getItem("password")),
              newPassword: this.form.password,
            })
            .then((res) => {
              this.$api.logout().then(({ data }) => {
                clearLoginInfo();
                this.$router.push({ name: "login" });
              });
            });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.mod-home {
  line-height: 1.5;
}
.el-carousel__item h3 {
  color: #475669;
  font-size: 18px;
  opacity: 0.75;
  line-height: 300px;
  margin: 0;
  color: #fff;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
