<template>
    <t-dialog class="pc-profile-dialog" :visible="visible" :header="t('个人信息')" :footer="false"
        :close-on-overlay-click="false" width="580px" placement="center" destroy-on-close
        @update:visible="handleVisibleChange">
        <template #body>
            <div class="pc-profile">
                <div class="pc-profile__form">
                    <div class="pc-profile__row">
                        <div class="pc-profile__label">{{ t('昵称') }}</div>
                        <input class="pc-profile__input pc-profile__input--readonly" :value="form.nickname" readonly />
                    </div>

                    <div class="pc-profile__row">
                        <div class="pc-profile__label">{{ t('账号') }}</div>
                        <input class="pc-profile__input pc-profile__input--readonly" :value="form.account" readonly />
                    </div>

                    <div class="pc-profile__row">
                        <div class="pc-profile__label">{{ t('手机号') }}</div>
                        <div class="pc-profile__input-wrap">
                            <input class="pc-profile__input pc-profile__input--readonly" :value="form.phone" readonly
                                :placeholder="t('未绑定')" />
                            <!-- <button type="button" class="pc-profile__input-action" @click="openPhoneDialog">
                                {{ t('修改') }}
                            </button> -->
                        </div>
                    </div>
                </div>
            </div>
        </template>
    </t-dialog>

    <t-dialog class="pc-phone-dialog" v-model:visible="phoneDialogVisible" :header="t('修改手机号')" :footer="false"
        :close-on-overlay-click="false" width="480px" placement="center" destroy-on-close>
        <template #body>
            <div class="pc-phone">
                <div class="pc-phone__form">
                    <div class="pc-phone__row">
                        <div class="pc-phone__label">{{ t('原手机号码') }}</div>
                        <input class="pc-phone__input pc-phone__input--readonly" :value="form.phone" readonly
                            :placeholder="t('未绑定')" />
                    </div>
                    <div class="pc-phone__row">
                        <div class="pc-phone__label">{{ t('新手机号码') }}</div>
                        <input v-model="phoneForm.newPhone" class="pc-phone__input" type="tel"
                            :placeholder="t('请输入您的新手机号')" />
                    </div>
                    <div class="pc-phone__row">
                        <div class="pc-phone__label">{{ t('验证码') }}</div>
                        <div class="pc-phone__input-wrap">
                            <input v-model="phoneForm.code" class="pc-phone__input pc-phone__input--flat"
                                :placeholder="t('请输入验证码')" />
                            <button type="button" class="pc-phone__code-btn" :disabled="getCodeDisabled"
                                @click="handleSendCode">
                                {{ verifyCodeButtonText }}
                            </button>
                        </div>
                    </div>
                </div>

                <div class="pc-phone__footer">
                    <button type="button" class="pc-profile__btn pc-profile__btn--primary" :disabled="!canSubmitPhone"
                        @click="handlePhoneConfirm">
                        {{ t('确定') }}
                    </button>
                    <button type="button" class="pc-profile__btn pc-profile__btn--ghost" @click="closePhoneDialog">
                        {{ t('取消') }}
                    </button>
                </div>
            </div>
        </template>
    </t-dialog>
</template>

<script setup lang="ts">
import { computed, onMounted, onUnmounted, reactive, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import AIDeskCoreInstance, { StoreName } from '@tencentcloud/ai-desk-core';

const COUNTDOWN_SECONDS = 60;

const props = defineProps<{
    visible: boolean;
}>();

const emit = defineEmits<{
    (event: 'update:visible', value: boolean): void;
}>();

const { t } = useI18n();

const form = reactive({
    nickname: '',
    account: '',
    phone: '',
});

const phoneDialogVisible = ref(false);
const phoneForm = reactive({
    newPhone: '',
    code: '',
});
const codeCountdown = ref(0);
const hasSentVerifyCode = ref(false);
let codeTimer: ReturnType<typeof setInterval> | null = null;

const onUserIdUpdated = (val: string) => {
    form.account = val ?? '';
};

const onNicknameUpdated = (val: string) => {
    form.nickname = val ?? '';
};

const onMobileUpdated = (val: string) => {
    form.phone = val ?? '';
};

const clearCountdownTimer = () => {
    if (codeTimer) {
        clearInterval(codeTimer);
        codeTimer = null;
    }
};

onMounted(() => {
    AIDeskCoreInstance.coreStore.watch(StoreName.USERINFO, {
        userId: onUserIdUpdated,
        nickname: onNicknameUpdated,
        mobile: onMobileUpdated,
    });
});

onUnmounted(() => {
    AIDeskCoreInstance.coreStore.unwatch(StoreName.USERINFO, {
        userId: onUserIdUpdated,
        nickname: onNicknameUpdated,
        mobile: onMobileUpdated,
    });
    clearCountdownTimer();
});

const getCodeDisabled = computed(() => {
    return phoneForm.newPhone.trim() === '' || codeCountdown.value > 0;
});

const verifyCodeButtonText = computed(() => {
    if (codeCountdown.value > 0) {
        return t('重新获取 {seconds}s', { seconds: codeCountdown.value });
    }
    return hasSentVerifyCode.value ? t('重新获取') : t('获取验证码');
});

const canSubmitPhone = computed(() => {
    return phoneForm.newPhone.trim() !== '' && phoneForm.code.trim() !== '';
});

watch(
    () => props.visible,
    (val) => {
        if (!val) {
            phoneDialogVisible.value = false;
        }
    },
);

const handleVisibleChange = (val: boolean) => {
    emit('update:visible', val);
};

// const openPhoneDialog = () => {
//     phoneForm.newPhone = '';
//     phoneForm.code = '';
//     hasSentVerifyCode.value = false;
//     codeCountdown.value = 0;
//     clearCountdownTimer();
//     phoneDialogVisible.value = true;
// };

const closePhoneDialog = () => {
    phoneDialogVisible.value = false;
};

const startCountdown = () => {
    clearCountdownTimer();
    hasSentVerifyCode.value = true;
    codeCountdown.value = COUNTDOWN_SECONDS;
    codeTimer = setInterval(() => {
        if (codeCountdown.value <= 1) {
            codeCountdown.value = 0;
            clearCountdownTimer();
            return;
        }
        codeCountdown.value -= 1;
    }, 1000);
};

const handleSendCode = async () => {
    if (getCodeDisabled.value) return;
    const mobile = phoneForm.newPhone.trim();
    await AIDeskCoreInstance.userInfoService.getChangeMobileVerifyCode(mobile);
    startCountdown();
};

const handlePhoneConfirm = async () => {
    if (!canSubmitPhone.value) return;
    await AIDeskCoreInstance.userInfoService.updateMobile(form.phone, phoneForm.newPhone, phoneForm.code);
    phoneDialogVisible.value = false;
};
</script>

<style scoped>
.pc-profile {
    padding: 4px 0 0;
}

.pc-profile__form {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.pc-profile__row {
    display: flex;
    align-items: center;
    gap: 24px;
}

.pc-profile__label {
    width: 72px;
    font-size: 14px;
    color: #86909C;
    line-height: 22px;
    flex-shrink: 0;
}

.pc-profile__input {
    flex: 1;
    height: 40px;
    padding: 0 16px;
    border: none;
    border-radius: 8px;
    background: #E9ECF3;
    color: #1D2129;
    font-size: 14px;
    line-height: 22px;
    outline: none;
    box-sizing: border-box;
}

.pc-profile__input::placeholder {
    color: #A5A9B0;
}

.pc-profile__input--readonly {
    color: #4E5969;
    cursor: default;
}

.pc-profile__input-wrap {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 12px;
}

.pc-profile__input-action {
    background: transparent;
    border: none;
    color: #316EF5;
    font-size: 14px;
    line-height: 22px;
    cursor: pointer;
    padding: 0;
    flex-shrink: 0;
}

.pc-profile__footer {
    margin-top: 32px;
    display: flex;
    justify-content: center;
    gap: 16px;
}

.pc-profile__btn {
    min-width: 88px;
    height: 36px;
    padding: 0 20px;
    border-radius: 999px;
    font-size: 14px;
    line-height: 22px;
    cursor: pointer;
    border: 1px solid transparent;
    box-sizing: border-box;
}

.pc-profile__btn--primary {
    background: #316EF5;
    color: #fff;
    border-color: #316EF5;
}

.pc-profile__btn--primary:hover {
    background: #2758c9;
    border-color: #2758c9;
}

.pc-profile__btn--primary:disabled,
.pc-profile__btn--primary:disabled:hover {
    background: #A5A9B0;
    border-color: #A5A9B0;
    cursor: not-allowed;
}

.pc-profile__btn--ghost {
    background: #fff;
    color: #316EF5;
    border-color: #316EF5;
}

.pc-profile__btn--ghost:hover {
    background: #F2F6FF;
}

/* --- 修改手机号 --- */
.pc-phone {
    padding: 4px 0 0;
}

.pc-phone__form {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.pc-phone__row {
    display: flex;
    align-items: center;
    gap: 24px;
}

.pc-phone__label {
    width: 88px;
    font-size: 14px;
    color: #86909C;
    line-height: 22px;
    flex-shrink: 0;
}

.pc-phone__input {
    flex: 1;
    height: 40px;
    padding: 0 16px;
    border: 1px solid #E5E6EB;
    border-radius: 8px;
    background: #fff;
    color: #1D2129;
    font-size: 14px;
    line-height: 22px;
    outline: none;
    box-sizing: border-box;
}

.pc-phone__input:focus {
    border-color: #316EF5;
}

.pc-phone__input::placeholder {
    color: #A5A9B0;
}

.pc-phone__input-wrap {
    flex: 1;
    position: relative;
    display: flex;
    align-items: center;
    height: 40px;
    border: 1px solid #E5E6EB;
    border-radius: 8px;
    background: #fff;
    box-sizing: border-box;
    padding-right: 16px;
}

.pc-phone__input-wrap:focus-within {
    border-color: #316EF5;
}

.pc-phone__input--flat {
    border: none;
    background: transparent;
    height: 100%;
    flex: 1;
}

.pc-phone__code-btn {
    background: transparent;
    border: none;
    color: #316EF5;
    font-size: 14px;
    line-height: 22px;
    cursor: pointer;
    padding: 0;
    flex-shrink: 0;
}

.pc-phone__code-btn:disabled {
    color: #A5A9B0;
    cursor: not-allowed;
}

.pc-phone__footer {
    margin-top: 32px;
    display: flex;
    justify-content: center;
    gap: 16px;
}
</style>

<style>
.pc-profile-dialog .t-dialog,
.pc-phone-dialog .t-dialog {
    border-radius: 16px;
    overflow: hidden;
}

.pc-profile-dialog .t-dialog__header,
.pc-profile-dialog .t-dialog__header-content,
.pc-phone-dialog .t-dialog__header,
.pc-phone-dialog .t-dialog__header-content {
    text-align: left;
    justify-content: flex-start;
}
</style>
