<script setup lang="ts">
import { computed, reactive, ref } from "vue";
import * as $userApi from "@/api/sys/user";
import message from "@/utils/message";
import { useNav } from "@/layout/hooks/useNav";

const { logout, username, sysUsername } = useNav();

const form = ref();
const pageData: any = reactive({
  mode: "self",
  modalParam: {
    visible: false,
    closeOnClickModal: true
  },
  currentData: {},
  formParam: {
    infoForm: {},
    loading: false
  }
});
const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入新密码"));
  } else if (
    value === pageData.formParam.infoForm.oldPassword &&
    pageData.mode === "self"
  ) {
    callback(new Error("输入新密码与旧密码不能相同"));
  } else {
    callback();
  }
};
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请再次输入新密码"));
  } else if (value !== pageData.formParam.infoForm.newPassword) {
    callback(new Error("两次输入密码不正确"));
  } else {
    callback();
  }
};
const formRules = computed(() => {
  const rules: any = {
    newPassword: [
      {
        required: true,
        message: "请输入新密码",
        trigger: "blur"
      },
      { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
      { validator: validatePass, trigger: "blur" }
    ],
    confirmPassword: [
      {
        required: true,
        message: "请输入确认密码",
        trigger: "blur"
      },
      { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
      { validator: validatePass2, trigger: "blur" }
    ]
  };
  rules.oldPassword = [
    {
      required: true,
      message: "请输入原密码",
      trigger: "blur"
    }
  ];
  return rules;
});
const formFields = computed(() => {
  const fields: any[] = [
    {
      type: "input",
      label: "原密码",
      prop: "oldPassword",
      placeholder: "请输入原密码"
    },
    {
      type: "input",
      label: "新密码",
      prop: "newPassword",
      placeholder: "请输入新密码"
    },
    {
      type: "input",
      label: "确认密码",
      prop: "confirmPassword",
      placeholder: "请输入确认密码"
    }
  ];
  return fields;
});
const open = (data?: any) => {
  pageData.currentData = data;
  if (data) {
    pageData.mode = "other";
  }
  pageData.modalParam.visible = true;
};
const initData = () => {
  pageData.formParam.infoForm = {};
  pageData.currentData = undefined;
  pageData.modalParam.visible = false;
};
const handleClose = () => {
  initData();
  emits("close");
};
const handleConfirm = () => {
  form.value!.validate((isValid: boolean) => {
    if (isValid) {
      changePasswd();
    }
  });
};
const changePasswd = () => {
  const data = pageData.formParam.infoForm;
  pageData.formParam.loading = true;
  $userApi
    .changePasswd(sysUsername.value, data)
    .then((res: any) => {
      if (res.success) {
        message.success("修改密码成功");
        initData();
        emits("confirm");
        logout();
      } else {
        message.error(res.message);
      }
    })
    .finally(() => {
      pageData.formParam.loading = false;
    });
};
const emits = defineEmits(["close", "confirm"]);
defineExpose({
  open
});
defineOptions({
  name: "UpdatePassword"
});
</script>

<template>
  <div>
    <el-dialog
      title="修改密码"
      v-model="pageData.modalParam.visible"
      :close-on-click-modal="pageData.modalParam.closeOnClickModal"
      destroy-on-close
      width="30%"
      @close="handleClose"
    >
      <!---->
      <el-form
        :model="pageData.formParam.infoForm"
        :rules="formRules"
        ref="form"
        label-width="100px"
        class="mx-auto"
        style="margin-top: 20px; width: 70%"
        v-loading="pageData.formParam.loading"
      >
        <el-form-item label="系统用户">
          <el-input v-model="username" disabled />
        </el-form-item>
        <el-form-item
          v-for="(item, index) in formFields"
          :key="index"
          :prop="item.prop"
          :label="item.label"
        >
          <template v-if="item.type === 'input'">
            <el-input
              v-model="pageData.formParam.infoForm[item.prop]"
              :placeholder="item.placeholder"
              type="password"
              show-password
              clearable
            />
          </template>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button plain @click="handleClose">取消</el-button>
          <el-popconfirm
            title="确认提交?"
            @confirm="handleConfirm"
            confirm-button-text="确定"
            cancel-button-text="取消"
          >
            <template #reference>
              <el-button type="primary" v-loading="pageData.formParam.loading"
                >提 交</el-button
              >
            </template>
          </el-popconfirm>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
