<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('personal.confirmMail') }}</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 v-if="authStatus.email == 0" style="display: flex;width: 100%;justify-content: center;font-size: 18px;font-weight: 700;color: rgba(51, 51, 51, 1);margin-bottom: 30px;">
            {{ t('personal.sendSuccess', {email: userInfo.email}) }}
        </div>
        <el-form v-if="authStatus.email == 0" ref="postRef" :model="form" :rules="rules" autocapitalize="off" size="large" label-position="top" style="user-select: none;" >
            <el-form-item>
                <MailCode style="margin: auto;" @complete="onComplete" />
            </el-form-item>
            <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 :disabled="!code || code.length < 6" class="send-btn" @click="save" style="width: 590px;" type="primary">{{ t('personal.verification') }}</el-button>
            </el-form-item>
        </el-form>
        <el-form v-else-if="authStatus.email == 1" class="login-form" >
            <div style="display: flex;justify-content: center;align-items: center;padding-top: 90px;">
                <svg-icon icon-class="success" style="width: 55px;height: 55px;margin-right: 24px;" />
                <span style="font-size: 36px;font-weight: 700;color: #333333" >{{ t('personal.mailSuccess') }}</span>
            </div>
            <div v-if="progress < 3" style="margin-top: 25px;margin-bottom: 60px;text-align: center;font-size: 18px;font-weight: 700;color: rgba(51, 51, 51, 1);">
                {{ t('personal.successText', {n: 3 - progress}) }}
            </div>
            <el-form-item >
                <el-button v-if="authStatus.identity == 0" class="send-btn" @click="close" style="width: 590px;" type="primary">{{ t('personal.start2') }}</el-button>
                <el-button v-else-if="authStatus.address == 0" class="send-btn" @click="close" style="width: 590px;" type="primary">{{ t('personal.start3') }}</el-button>
            </el-form-item>
        </el-form>
        <div class="step-bar">
            <div class="step-item" :class="{ 'step-item-done': n <= progress }" v-for="n in 3" :key="n" ></div>
        </div>
        <div class="step-info">{{ t('personal.progress', {n1: progress, n2: 3}) }}</div>
    </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 MailCode from '@/components/MailCode'
import { sendCodeP, saveVerifyEmail } from '@/api/personal'
import { getPersonalAuthStatus } from '@/api/common'

const { proxy } = getCurrentInstance();
const appStore = useAppStore()
const userStore = useUserStore()
const userInfo = ref(userStore.userInfo)
const { t } = useLocale()
const dialogVisible = defineModel("show")
const props = defineProps({
    data: {
        type: Object,
        default: null
    }
})

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

const progress = computed(() => {
    return (authStatus.value.address?1:0) +
    (authStatus.value.identity?1:0) +
    (authStatus.value.email?1:0) 
})

const code = ref(null)

const onComplete = (val) => {
    code.value = val.reduce((a, b) => a + '' + b)
}

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() {
    console.log("send")
    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)
    sendCodeP({
      email: userInfo.value.email,
      qcc_language: serverLang()
    }).then(resp => {
        sendStatus.value = true
    }).catch(() => {
      clearInterval(timeInterval)
      appStore.setCountdown(0)
      countdown.value = 0
    })
}

const checkPwd = (rule, value, callback) => {
    if (!form.value.mt4_password && !form.value.mt4_password_investor) {
      return callback(new Error(''))
    }
    callback()
}

const d = reactive({
  form: {
    mt4_password: undefined,
    mt4_password_investor: undefined
  },
  rules: {
    mt4_password: [{ trigger: "blur", validator: checkPwd }],
    mt4_password_investor: [{ trigger: "blur", validator: checkPwd }],
  }
});

const { form, rules } = toRefs(d);

function save() {
    saveVerifyEmail({
        code: code.value, qcc_language: serverLang()
    }).then(resp => {
        authStatus.value.email = 1
        appStore.setCountdown(0)
        countdown.value = 0
    })
}

watch(dialogVisible, (n, o) => {
    if(!n) {
        curStep.value = 1
        proxy.resetForm("postRef");
        sendStatus.value = false
    } else {
        getPersonalAuthStatus().then(resp => {
            authStatus.value = resp.data
            if(authStatus.value.email == 0 && countdown.value <= 0) {
                sendCode()
            }
        })
    }
})

function close() {
    dialogVisible.value = false
}
</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;
}

.step-bar {
    margin: 36px auto 0;
    width: 530px;
    height: 16px;
    border-radius: 100px;
    display: flex;
    padding: 4px 6px;
    justify-content: space-between;
    border: 1px solid rgba(229, 229, 229, 1);

    .step-item {
        height: 100%;
        width: 166.6666px;
        border-radius: 100px;
        background: rgba(229, 229, 229, 1);
    }

    .step-item-done {
        background: linear-gradient(90deg, rgba(16, 55, 118, 1) 0%, rgba(100, 116, 175, 1) 100%);
    }
}

.step-info {
    text-align: center;
    margin-top: 15px;
    margin-bottom: 10px;
    font-size: 16px;
    font-weight: 400;
    letter-spacing: 0px;
    color: rgba(153, 153, 153, 1);
}
</style>