<template>
<el-dialog v-model="dialogVisible" width="637" class="dialog-box"
    align-center
    destroy-on-close
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false">
    <template #header="{ close, titleId, titleClass }">
        <div class="msg-header" >
            <svg-icon icon-class="miniMail" class="header-icon" />
            <div class="header-title">{{ t('capital.verify_email') }}</div>
            <div class="header-close" >
            <el-icon color="rgba(196, 196, 196, 1)" @click="close" ><Close /></el-icon>
            </div>
        </div>
    </template>
    <div class="msg-body" >
        <div style="display: flex;width: 100%;justify-content: center;align-items: center;font-size: 18px;font-weight: 700;color: rgba(51, 51, 51, 1);margin-bottom: 30px;">
            <svg-icon icon-class="wallte" style="width: 70px;height: 70px;margin: 10px 20px 0 0;" />
            <div style="font-size: 36px;">{{ t('capital.verify_email') }}</div>
        </div>
        <div style="display: flex;width: 100%;justify-content: center;font-size: 18px;font-weight: 700;color: rgba(51, 51, 51, 1);margin-bottom: 30px;">
            {{ t('capital.wallte_verify_email', {email: userInfo.email}) }}
        </div>
        <el-form ref="postRef" autocapitalize="off" size="large" label-position="top" style="user-select: none;" >
            <div class="send-info">
                <div>
                    <div v-if="countdown" style="color: rgba(41, 187, 228, 1);" >{{ countdown + t('personal.countdownText') }}</div>
                    <div v-else style="display: flex;align-items: center;color: rgba(41, 187, 228, 1);cursor: pointer;" @click="sendCode" >
                        <svg-icon icon-class="uploadRetry" style="width: 25px;height: 25px;margin-right: 8px;" />
                        <span>{{ t('personal.reSend') }}</span>
                    </div>
                </div>
            </div>
            <el-form-item >
                <el-button class="send-btn" @click="close" style="width: 590px;" type="primary">{{ t('capital.verification') }}</el-button>
            </el-form-item>
        </el-form>
    </div>
</el-dialog>

</template>

<script setup>
import { useLocale } from 'element-plus'
import useAppStore from '@/store/modules/app'
import useUserStore from '@/store/modules/user'
import { ref, defineModel, watch, computed } from 'vue';
import { serverLang } from '@/utils/i18n'
import { sendBankMail, saveVerifyEmail } from '@/api/personal'

const { proxy } = getCurrentInstance();
const appStore = useAppStore()
const userStore = useUserStore()
const userInfo = ref(userStore.userInfo)
const { t } = useLocale()
const dialogVisible = defineModel("show")
const emit = defineEmits(['done'])

const props = defineProps({
    id: [String, Number]
})

const authStatus = ref({
  address: 0,
  identity: 0,
  email: 0
})
const code = ref(null)

let timeInterval
const curStep = ref(1)
const sendStatus = ref(false)

const countdown = ref(((new Date().getTime() - appStore.countdown) / 1000) > 60 ? 0 : (60 - (new Date().getTime() - appStore.countdown) / 1000).toFixed(0));
  if (countdown.value > 0) {
    timeInterval = setInterval(() => {
      if ((new Date().getTime() - appStore.countdown) / 1000 > 0 && (new Date().getTime() - appStore.countdown) / 1000 < 60) {
        countdown.value--
      } else {
        clearInterval(timeInterval)
        appStore.setCountdown(0)
        countdown.value = 0
      }
    }, 1000)
}

function sendCode() {
    countdown.value = 60
    appStore.setCountdown(new Date().getTime())
    timeInterval = setInterval(() => {
      if ((new Date().getTime() - appStore.countdown) / 1000 > 0 && (new Date().getTime() - appStore.countdown) / 1000 < 60) {
        countdown.value--
      } else {
        clearInterval(timeInterval)
        appStore.setCountdown(0)
        countdown.value = 0
      }
    }, 1000)
    sendBankMail({
      id: props.id
    }).then(resp => {
        sendStatus.value = true
    }).catch(() => {
      clearInterval(timeInterval)
      appStore.setCountdown(0)
      countdown.value = 0
    })
}

watch(dialogVisible, (n, o) => {
    if(!n) {
        curStep.value = 1
        sendStatus.value = false
    } else {
        if(countdown.value <= 0) {
            sendCode()
        }
    }
})

function close() {
    dialogVisible.value = false
    emit('done', props.id)
}
</script>

<style lang="scss">
.dialog-box {
    border-radius: 16px;
    padding: 0;

    .el-dialog__header {
        margin-right: 0;
        padding: 0;
        overflow: hidden;
    }

    .el-dialog__body {
        box-sizing: border-box;
        padding: 16px 22px;
        min-height: 250px;
    }

    .msg-header {
        border-radius: 16px 16px 0 0;
        width: 100%;
        height: 50px;
        position: relative;
        overflow: hidden;
        background-color: #0F3675;
        display: flex;
        align-items: center;
        color: #fff;

        .header-icon {
            width: 25px;
            height: 28px;
            margin-left: 22px;
            margin-right: 14px;
        }

        .header-title {
            font-size: 26px;
            font-weight: 700;
        }

        .header-close {
            cursor: pointer;
            position: absolute;
            bottom: 0;
            right: 20px;
            font-size: 32px;
        }
    }

    .msg-body {
        background-color: #ffffff;

        .account {
            font-size: 18px;
            font-weight: 700;
            color: rgba(51, 51, 51, 1);
            margin-bottom: 14px;
            width: 50%;
        }
    }
}

.send-info {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 52px;
    margin: 20px;
    font-size: 22px;
}

.send-btn {
    margin-left: auto;
    height: 50px;
    font-size: 20px;
    flex-grow: 1;
    flex-shrink: 0;
}
</style>