<template>
<el-dialog v-model="dialogVisible" width="880" 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="addrMini" class="header-icon" />
            <div class="header-title">{{ t('personal.verification-addr') }}</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" v-if="authStatus && authStatus.address != 1" >
        <el-row v-if="step == 1" >
            <el-form v-if="authStatus" ref="postRef" :model="form" :rules="rules" autocapitalize="off" size="large" label-position="top" style="user-select: none;width: 100%;" >
                <el-col :span="24">
                    <el-form-item :label="t('dzzm.setType')" prop="voucher_type" >
                        <div style="display: flex;justify-content: space-between;width: 100%;">
                            <div class="identity-type" :class="[form.voucher_standby_1=='1'?'identity-type-active': '', validateType?'vl-error':'' ]" @click="selectType('1')" >
                                <svg-icon icon-class="dzzm1" style="width: 41px;margin-right: 16px;" />
                                <span>{{ t('dzzm.type1') }}</span>
                            </div>
                            <div class="identity-type" :class="[ form.voucher_standby_1=='2' ? 'identity-type-active': '', validateType?'vl-error':'']" @click="selectType('2')" >
                                <svg-icon icon-class="dzzm2" style="width: 41px;margin-right: 16px;" />
                                <span>{{ t('dzzm.type2') }}</span>
                            </div>
                            <div class="identity-type" :class="[ form.voucher_standby_1=='3' ? 'identity-type-active': '', validateType?'vl-error':'']" @click="selectType('3')" >
                                <svg-icon icon-class="dzzm3" style="width: 41px;margin-right: 16px;" />
                                <span>{{ t('dzzm.type3') }}</span>
                            </div>
                        </div>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-collapse-transition>
                        <el-form-item :label="t('identity.images')" v-show="form.voucher_type" >
                            <div>{{ t('identity.tips') }}</div>
                            <div style="display: flex;justify-content: space-between;width: 100%;">
                                <identity-upload v-model="form.voucher_img_0" :baseUrl="baseUrl" />
                            </div>
                            <div style="width: 100%;font-size: 16px;font-weight: 700;color: rgba(153, 153, 153, 1);">{{ t('identity.rule') }}</div>
                            <div style="font-size: 16px;font-weight: 400;color: rgba(153, 153, 153, 1);display: flex;justify-content: space-between;width: 100%;">
                                <span>{{ t('identity.rule_1') }}</span>
                                <span>{{ t('identity.rule_2') }}</span>
                                <span>{{ t('identity.rule_3') }}</span>
                                <span>{{ t('identity.rule_4') }}</span>
                            </div>
                        </el-form-item>
                    </el-collapse-transition>
                </el-col>
                <el-col :span="24">
                    <el-form-item>
                        <el-button :disabled="authStatus == null || authStatus.address == 1" :loading="loading" class="send-btn" @click="save" style="width: 100%;" type="primary">{{ t('dzzm.upload') }}
                            <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-form-item>
                </el-col>
            </el-form>
        </el-row>
        <el-form v-else-if="step == 2" 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.dzzmSuccess') }}</span>
            </div>
            <div style="width: 100%;text-align: center;margin-top: 13px;margin-bottom: 40px;color: rgba(102, 102, 102, 1);font-size: 18px;font-weight: 400;" >{{ t('personal.audit') }}</div>
            <el-form-item>
                <el-button class="send-btn" @click="close" style="width: 590px;" type="primary">{{ t('personal.ok') }}</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>
    <div class="msg-body" v-else>
        <el-descriptions :column="1" size="large" :title="form.voucher_standby_1=='1'? t('dzzm.type1') : 
        (form.voucher_standby_1=='2'?t('dzzm.type1'):(form.voucher_standby_1=='3'?t('dzzm.type3'):''))">
            <el-descriptions-item>
                <el-image style="width: 235px; height: 156px" :src="baseUrl + form.voucher_img_0" :preview-src-list="[baseUrl + form.voucher_img_0]" :fit="fit" />
            </el-descriptions-item>
        </el-descriptions>
    </div>
</el-dialog>
</template>

<script setup >
import { useLocale } from 'element-plus'
import { ref, defineModel, watch, computed } from 'vue';
import { getAuthAddress, saveVoucher } from '@/api/personal'
import { getPersonalAuthStatus, getImgPath } from '@/api/common'
import { serverLang } from '@/utils/i18n'
import useUserStore from '@/store/modules/user'
import IdentityUpload from '@/components/IdentityUpload'
import { isEmpty } from '@/utils/htfx'


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

const loading = ref(false)
const baseUrl = ref(null)
const authStatus = ref(null)
const step = ref(1)

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

let oldData = null

const form = ref({
    "id": undefined,
    "user_id": undefined,
    "voucher_type": 'DZZM',
    "voucher_no": undefined,
    "voucher_img_0": undefined,
    "voucher_img_1": undefined,
    "voucher_remark": undefined,
    "voucher_standby_1": '1',
    "voucher_standby_2": undefined,
    "voucher_standby_3": undefined,
    "voucher_standby_4": undefined,
    "voucher_standby_5": undefined,
    "apply_status": undefined,
    "audit_opinion": undefined,
    "apply_type": undefined,
    "apply_id": undefined,
})

const validateType = ref(false)
const validate0 = ref(false)

const validateVoucherType = (rule, value, callback) => {
    if (isEmpty(value)) {
    validateType.value = true
    callback(new Error(''))
    } else {
    validateType.value = false
    callback()
    }
}

const validateImg0 = (rule, value, callback) => {
    if (isEmpty(value)) {
    validate0.value = true
    callback(new Error(''))
    } else {
    validate0.value = false
    callback()
    }
}

const rules = ref({
    voucher_img_0: [{ validator: validateImg0, trigger: "blur" }],
    voucher_standby_1: [{ validator: validateVoucherType, trigger: "blur" }]
})

function reset() {
    oldData = null
    validateType.value = false
    validate0.value = false
    form.value = {
        "id": undefined,
        "user_id": undefined,
        "voucher_type": 'DZZM',
        "voucher_no": undefined,
        "voucher_img_0": undefined,
        "voucher_img_1": undefined,
        "voucher_remark": undefined,
        "voucher_standby_1": '1',
        "voucher_standby_2": undefined,
        "voucher_standby_3": undefined,
        "voucher_standby_4": undefined,
        "voucher_standby_5": undefined,
        "apply_status": undefined,
        "audit_opinion": undefined,
        "apply_type": undefined,
        "apply_id": undefined,
        "voucher_name": undefined
    }
}

function selectType(type) {
    validateType.value = false
    form.value.voucher_standby_1 = type
}

function loadData() {
    getAuthAddress().then(resp => {
        oldData = resp.data
        if (oldData != null) {
            form.value = JSON.parse(JSON.stringify(oldData))
        }
    })
}

function save() {
    proxy.$refs["postRef"].validate(valid => {
        if (valid) {
            let data = {
                qcc_language: serverLang(),
                voucher: []
            }
            if (oldData) {
                // 编辑
                let obj = { id: form.value.id }
                if (form.value.voucher_type != oldData.voucher_type) {
                    obj.voucher_type = {
                        nv: form.value.voucher_type,
                        ov: obj.voucher_type,
                        edit_power: 0,
                        voucher_type: form.value.voucher_type
                    }
                }
                if (form.value.voucher_img_0 != oldData.voucher_img_0) {
                    obj.voucher_img_0 = {
                        nv: form.value.voucher_img_0,
                        ov: oldData.voucher_img_0,
                        edit_power: 1,
                        voucher_type: form.value.voucher_type
                    }
                }
                if (form.value.voucher_standby_1 != oldData.voucher_standby_1) {
                    obj.voucher_standby_1 = {
                        nv: form.value.voucher_standby_1,
                        ov: oldData.voucher_standby_1,
                        edit_power: 1,
                        voucher_type: form.value.voucher_type
                    }
                }
                data.voucher.push(obj)
            } else {
                // 新增
                data.voucher.push({
                    id: "",
                    voucher_type: {
                        nv: form.value.voucher_type,
                        ov: "",
                        edit_power: 0,
                        voucher_type: form.value.voucher_type
                    },
                    voucher_img_0: {
                        nv: form.value.voucher_img_0,
                        ov: "",
                        edit_power: 1,
                        voucher_type: form.value.voucher_type
                    },
                    voucher_standby_1: {
                        nv: form.value.voucher_standby_1,
                        ov: "",
                        edit_power: 1,
                        voucher_type: form.value.voucher_type
                    }
                })
            }
            loading.value = true
            saveVoucher(data).then(resp => {
                step.value = 2
                loading.value = false
            }).catch(() => {
                loading.value = false
            })
        }
    })
}

function close() {
    dialogVisible.value = false
}

onMounted(()=> {
    getImgPath().then(resp => {
        baseUrl.value = resp.data
    })
})

watch(dialogVisible, (n, o) => {
    if(!n) {
        step.value = 1
        proxy.resetForm("postRef");
        reset()
    } else {
        getPersonalAuthStatus().then(resp => {
            authStatus.value = resp.data
            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);
}

.identity-type {
    width: 218px;
    height: 97px;
    border-radius: 1px;
    background: rgba(255, 255, 255, 0.07);
    border: 1px solid rgba(229, 229, 229, 1);
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24px;
    font-weight: 700;
    color: rgba(51, 51, 51, 1);
}

.identity-type-active {
    box-shadow: 0px 2px 9px  rgba(0, 0, 0, 0.25);
}

.vl-error {
    box-shadow: 0 0 0 1px var(--el-color-danger) inset;
}
</style>