<template>
<el-dialog v-model="dialogVisible" width="1300" 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="profile" class="header-icon" />
            <div class="header-title">{{ t('personal.confirmProfile') }}</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" >
        <el-row>
            <el-form v-if="authStatus" ref="postRef" :model="form" :rules="rules" inline autocapitalize="off" size="large" label-position="top" style="user-select: none;" >
                <el-col :span="12">
                    <el-form-item :label="t('profile.first_name')" prop="first_name" >
                        <el-input v-model="form.first_name" :maxlength="50" :readonly="authStatus.identity == 1" @input="form.first_name=form.first_name.replace(/[^a-zA-Z' @\-/]/g,'')" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item :label="t('profile.last_name')" prop="last_name" >
                        <el-input v-model="form.last_name" :maxlength="50" :readonly="authStatus.identity == 1" @input="form.last_name=form.last_name.replace(/[^a-zA-Z' @\-/]/g,'')" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item :label="t('profile.birthday')" prop="birthday" >
                        <el-date-picker
                            v-model="form.birthday"
                            value-format="YYYY-MM-DD"
                            type="date"
                            style="width: 100%;"
                            :disabled="authStatus.identity == 1"
                      ></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item :label="t('profile.sex')" prop="sex" >
                        <el-radio-group v-model="form.sex" :disabled="authStatus.identity == 1" >
                            <el-radio value="0">
                                <div style="font-size: 18px;font-weight: 400;display: flex;align-items: center;" >
                                    <svg-icon v-if="form.sex == 0" icon-class="success" style="width: 32px;height: 32px;margin-right: 20px;" />
                                    <svg-icon v-else icon-class="unCheck" style="width: 32px;height: 32px;margin-right: 20px;" />
                                    <span style="color: #606266 !important;" >{{ t('profile.male') }}</span>
                                </div>
                            </el-radio>
                            <el-radio value="1">
                                <div style="font-size: 18px;font-weight: 400;display: flex;align-items: center;" >
                                    <svg-icon v-if="form.sex == 1" icon-class="success" style="width: 32px;height: 32px;margin-right: 20px;" />
                                    <svg-icon v-else icon-class="unCheck" style="width: 32px;height: 32px;margin-right: 20px;" />
                                    <span style="color: #606266 !important;" >{{ t('profile.female') }}</span>
                                </div>
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item :label="t('profile.email')" prop="email" >
                        <el-input readonly :value="userInfo.email" :readonly="authStatus.identity == 1" />
                    </el-form-item>
                </el-col>
                <el-col :span="12" style="display: flex;" >
                    <el-form-item :label="t('profile.phone')" prop="phone_prefix" style="width: 120px;margin-right: 10px;" >
                        <el-input v-model="form.phone_prefix" :maxlength="10" :readonly="authStatus.identity == 1" />
                    </el-form-item>
                    <el-form-item label=" " prop="phone" style="width: calc(100% - 130px);" >
                        <el-input v-model="form.phone" :maxlength="20" :readonly="authStatus.identity == 1" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item :label="t('profile.residence')" prop="residence" >
                        <el-input v-model="form.residence" :maxlength="255" :readonly="authStatus.address == 1" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item :label="t('profile.postcode')" prop="postcode" >
                        <el-input v-model="form.postcode" :maxlength="20" :readonly="authStatus.address == 1" />
                    </el-form-item>
                </el-col>

                <el-col :span="8">
                    <el-form-item :label="t('profile.country')" prop="country" >
                        <el-select v-model="form.country" @change="changeCountry" :disabled="authStatus.address == 1" >
                            <el-option v-for="item in countries" :key="item.id" :label="item.name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item :label="t('profile.address_state')" prop="address_state" >
                        <el-select v-model="form.address_state" @change="changeState" :disabled="authStatus.address == 1" >
                            <el-option v-for="item in stateList" :key="item.id" :label="item.name" :value="item.id+''" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item :label="t('profile.address')" prop="address" >
                        <el-select v-model="form.address" :disabled="authStatus.address == 1" >
                            <el-option v-for="item in cities" :key="item.id" :label="item.name" :value="item.id+''" />
                        </el-select>
                    </el-form-item>
                </el-col>

                <el-col :span="8">
                    <el-form-item :label="t('profile.address_area')" prop="address_area" >
                        <el-input v-model="form.address_area" :maxlength="200" :readonly="authStatus.address == 1" />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item :label="t('profile.address_town')" prop="address_town" >
                        <el-input v-model="form.address_town" :maxlength="200" :readonly="authStatus.address == 1" />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item :label="t('profile.address_street')" prop="address_street" >
                        <el-input v-model="form.address_street" :maxlength="200" :readonly="authStatus.address == 1" />
                    </el-form-item>
                </el-col>

                <el-col :span="24">
                    <el-form-item :label="t('profile.address_detailed')" prop="address_detailed" >
                        <el-input v-model="form.address_detailed" :maxlength="300" :readonly="authStatus.address == 1" />
                    </el-form-item>
                </el-col>

                <el-col :offset="6" :span="12" v-if="!(authStatus.email && authStatus.identity && authStatus.address)">
                    <el-button :disabled="authStatus == null || (authStatus.email && authStatus.identity && authStatus.address)" :loading="loading" class="send-btn" @click="save" style="width: 590px;" type="primary">{{ t('form.submit') }}
                        <template #loading>
                            <l-ring-2 size="20" stroke="2" stroke-length="0.25" bg-opacity="0.1" speed="0.8" color="white" style="margin-right: 17px;" ></l-ring-2>
                        </template>
                    </el-button>
                </el-col>
                
            </el-form>
        </el-row>
        <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 { ref, defineModel, watch, computed } from 'vue';
import { saveUserBaseInfo, queryBaseInfo } from '@/api/personal'
import { getPersonalAuthStatus, getCountry, getStates, getCity } from '@/api/common'
import { serverLang } from '@/utils/i18n'
import useUserStore from '@/store/modules/user'

const { proxy } = getCurrentInstance();
const { t } = useLocale()
const userStore = useUserStore()
const userInfo = ref(userStore.userInfo)
const dialogVisible = defineModel("show")

const loading = ref(false)

const authStatus = ref(null)

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

const countries = ref([])
const stateList = ref([])
const cities = ref([])

const form = ref({
    "first_name": undefined,
    "last_name": undefined,
    "sex": 0,
    "birthday": undefined,
    "phone_prefix": undefined,
    "phone": undefined,
    "postcode": undefined,
    "residence": undefined,
    "country": undefined,
    "address_state": undefined,
    "address": undefined,
    "address_area": undefined,
    "address_town": undefined,
    "address_street": undefined,
    "address_detailed": undefined
})

const rules = ref({
    first_name: [{ required: true, message: "", trigger: "blur" }],
    last_name: [{ required: true, message: "", trigger: "blur" }],
    birthday: [{ required: true, message: "", trigger: "blur" }],
    sex: [{ required: true, message: "", trigger: "blur" }],
    country: [{ required: true, message: "", trigger: "change" }],
    address: [{ required: true, message: "", trigger: "change" }],
    address_detailed: [{ required: true, message: "", trigger: "blur" }],
    phone_prefix: [{ required: true, message: "", trigger: "blur" }],
    phone: [{ required: true, message: "", trigger: "blur" }],
})

function changeCountry(value) {
    stateList.value = []
    form.value.address_state = undefined
    cities.value = []
    form.value.address = undefined
    if (value) {
        loadState(value)
    }
}

function changeState(value) {
    cities.value = []
    form.value.address = undefined
    if (value) {
        loadCity(value)
    }
}

function save() {
    proxy.$refs["postRef"].validate(valid => {
        if (valid) {
            loading.value = true
            saveUserBaseInfo(form.value).then(resp => {
                loadData()
                window.showTip({
                    title: t('common.success.action'),
                    status: 'success'
                })
                loading.value = false
                dialogVisible.value = false
            }).catch(() => {
                loading.value = false
            })
        }
    })
}

function loadData() {
    queryBaseInfo().then(resp => {
        form.value = resp.data
        if(form.value.country) {
            loadState(form.value.country)
        }
        if (form.value.address_state) {
            loadCity(form.value.address_state)
        }
    })
}

function loadCountry() {
  getCountry().then(resp => {
    countries.value = resp.data
  })
}

function loadState(pid) {
    getStates({pid}).then(resp => {
        stateList.value = resp.data
    })
}

function loadCity(pid) {
    getCity({pid}).then(resp => {
        cities.value = resp.data
    })
}

function close() {
    dialogVisible.value = false
}

watch(dialogVisible, (n, o) => {
    if(!n) {
        proxy.resetForm("postRef");
    } else {
        getPersonalAuthStatus().then(resp => {
            authStatus.value = resp.data
            loadCountry()
            loadData()
        })
    }
})
</script>

<style lang="scss" scoped>
.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;
        padding: 20px 40px 0;

        .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);
}

:deep(.el-radio-group) {
    width: 100%;

    .el-radio {
        min-width: 35%;
    }
}

:deep(.el-radio__input) {
    display: none;
}
</style>