<script lang="ts" setup>
import { ref } from 'vue';
import { traduction } from '@/utils/language';
import { SecondAuthorityFail } from '@/model/base-enum';
import ErrorIcon from '@/components/ErrorIcon/ErrorIcon.vue';
import ClearableIcon from '@/components/ClearableIcon/ClearableIcon.vue';
import { getStoreData } from '@/utils/composition';

defineExpose({
  setErrorId(errId?: string) {
    // errId表示错误ID，当前仅支持 ReauthFailed 与 UserLocked ，若不传该参数，则默认使用 ReauthFailed, AuthorizationFailed主要是兼容其他分支未修改为ReauthFailed的情况
    const _errId =
      errId || SecondAuthorityFail.REAUTHFAILED || SecondAuthorityFail.AUTHORIZATIONFAILED;
    const msg = traduction(_errId + '.attrMessage');
    errorMsg.value = msg;
  },
  clearError() {
    errorMsg.value = '';
  },
  clearPassword: () => {
    currentPassword.value = '';
  },
  show() {
    initModal();
    show.value = true;
  },
  hide() {
    show.value = false;
  },
});
const pwdMaxLen = getStoreData('loct', 'pml');
const emit = defineEmits(['close']);
const show = ref(false);
const currentPassword = ref('');
const errorMsg = ref('');
const disabled = ref(true);

// 每次打开时，重置数据
function initModal() {
  currentPassword.value = '';
  clearError();
  disabled.value = true;
}

// 计算保存按钮的高亮状态
function computedState() {
  disabled.value = currentPassword.value.length === 0 || errorMsg.value !== '';
}

// 值变化时，清除报错信息并重新计算保存按钮的高亮状态
function passwordChange() {
  clearError();
  computedState();
}

// 清除报错
function clearError() {
  errorMsg.value = '';
}

// 点击确定或取消按钮时，对外发送close事件
function sendCloseReason(reason: boolean) {
  if (reason) {
    disabled.value = true;
  }
  const param: { reason: boolean; password: string } = { reason, password: currentPassword.value };
  emit('close', param);
}
</script>

<template>
  <div v-if="show" v-reqKeepAlive.dialog class="second-container">
    <el-dialog
      v-model="show"
      :close-on-click-modal="false"
      :title="$t('COMMON_CONFIRM')"
      class="second-pwd"
      @open="initModal"
      @close="sendCloseReason(false)"
    >
      <div class="dialog-contain">
        <el-form class="second-form">
          <el-form-item :error="errorMsg">
            <template #label>
              {{ $t('COMMON_LOGIN_PASSWORD') }}
            </template>
            <el-input
              id="secondPassword"
              v-model="currentPassword"
              v-clearable
              type="password"
              clearable
              autocomplete="new-password"
              :maxlength="pwdMaxLen"
              :placeholder="$t('COMMON_PASSWORD_PLACEHOLDER')"
              @input="passwordChange()"
            >
              <template #suffix>
                <ErrorIcon />
                <ClearableIcon />
              </template>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="center footer">
        <el-button
          id="secondOk"
          type="primary"
          :disabled="disabled"
          size="small"
          @click="sendCloseReason(true)"
        >
          {{ $t('COMMON_OK') }}
        </el-button>
        <el-button id="secondCancel" size="small" @click="sendCloseReason(false)">
          {{ $t('COMMON_CANCEL') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.second-container {
  :deep(.el-dialog) {
    width: 432px;
  }
  :deep(.el-dialog__close) {
    font-size: 20px;
  }
}
.second-form {
  width: 100%;
}
.dialog-contain {
  display: flex;
  .second-password-input {
    flex: 1;
    .el-input {
      width: 100%;
    }
  }
}

.center {
  text-align: center;
}

.footer {
  padding-top: 8px;
  :deep(.el-button) {
    min-width: 64px;
  }
}

:deep(.el-form-item__label::before) {
  content: '*';
  position: absolute;
  font-size: 12px;
  margin-left: -6px;
  color: var(--o-form-require-color);
  margin-right: 0;
}
</style>
