<template>
    <div class="login-body">
        <a class="left-logo" href="http://hczc.hcxxy.com/" target="_blank" v-if="appLogoIcon">
            <img alt="" id="logo-icon" :src="appLogoIcon">
            <img alt="" id="logo-name" :src="appLogoName">
        </a>
        <div class="left-pic-container">
            <Suspense>
                <HcPicVue />
                <template #fallback>
                    Loading...
                </template>
            </Suspense>
        </div>
        <div class="right-container">
            <div class="right-app-title">登录档案管理平台</div>
            <div class="sign-list">
                <h1 class="font-lg">登录</h1>
                <div class="title-tab">
                    <div :class="account?'active':''" @click="accountClick">密码登录</div>
                    <div :class="account?'':'active'" @click="accountClick">游客登录</div>
                </div>
                <div class="form-box mt-4" v-if="account">
                    <el-form ref="formRef" :model="formValue" :rules="formRules" label-width="0px" label-position="left" size="large">
                        <el-form-item prop="username">
                            <el-input v-model="formValue.username" placeholder="账号" clearable/>
                        </el-form-item>
                        <el-form-item prop="password" class="mt-8">
                            <el-input type="password" v-model="formValue.password" placeholder="密码" clearable show-password @keyup="passwordKeyUp">
                                <template #suffix>
                                    <span class="clickable-text" @click="clickableClick">忘记密码</span>
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="tenantId" v-if="tenantMode">
                            <el-input v-model="formValue.tenantId" clearable placeholder="租户ID"/>
                        </el-form-item>
                        <el-form-item>
                            <el-checkbox v-model="checkbox" label="记住密码"/>
                        </el-form-item>
                        <el-form-item class="mt-8">
                            <el-button type="primary" block :loading="loading" @click="formValidateClick">登 录</el-button>
                        </el-form-item>
                    </el-form>
                </div>

                <div class="form-box mt-4" v-else>
                    <el-form ref="touristsRef" :model="touristsForm" :rules="touristsRules" label-width="auto" label-position="left" size="large">
                        <el-form-item prop="phone">
                            <el-input v-model="touristsForm.phone" placeholder="请输入手机号" clearable/>
                        </el-form-item>
                        <el-form-item class="mt-8">
                            <el-button type="primary" block :loading="loading" @click="touristsValidateClick">登 录</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
        </div>
        <!--预先加载首页背景图-->
        <Suspense>
            <HcHomeBg/>
        </Suspense>
    </div>
</template>

<script setup>
import {ref, onMounted, nextTick} from "vue";
import website from '~src/config/index'
import router from '~src/router/index';
import {useAppStore} from "~src/store";
import {useAppLogin} from "~sto/user";
import HcPicVue from './components/pic.vue'
import HcHomeBg from './components/home-bg.vue'
import {getTenantID} from "~api/user";
import {getTopUrl, setAppName} from "~uti/tools";
import logoIcon from "~src/assets/logo/icon.png";
import logoName from "~src/assets/logo/name.png";
import {getStoreValue} from "~uti/storage";
import {formValidate, getArrValue, getObjVal, setImageColorStyle} from "js-fast-way"

const userStore = useAppStore()
userStore.clearStoreData() //先清理下缓存

//系统信息
const appLogoIcon = ref('')
const appLogoName = ref('')

//切换登录类型
const account = ref(true)
const tenantMode = ref(true)

const accountClick = () => {
    account.value = !account.value;
}

onMounted(() => {
    getTenantIdApi()
    setAppImageColor()
})

const setAppImageColor = () => {
    setImageColorStyle('logo-icon', '#1ECC95')
}

//表单
const formRef = ref(null);
const checkbox = ref(false)
const formValue = ref({tenantId: "000000", username: '', password: '', type: "account"})
const formRules = {
    tenantId: {
        required: true,
        message: "请输入租户ID",
        trigger: "blur"
    },
    username: {
        required: true,
        message: "请输入账号",
        trigger: "blur"
    },
    password: {
        required: true,
        message: "请输入密码",
        trigger: "blur"
    }
}
const passwordKeyUp = (e) => {
    if (e.key === "Enter") {
        formValidateClick()
    }
}

//获取租户id
const getTenantIdApi = async () => {
    const {error, code, data} = await getTenantID(getTopUrl())
    const res = getObjVal(data)
    if (!error && code === 200 && res) {
        const {tenantAvatar, tenantAvatarText, tenantTitle, tenantId} = res
        tenantMode.value = false
        //设置标题
        userStore.setTitle(tenantTitle)
        setAppName(tenantTitle)

        //设置logo图标
        const logoIconValue = tenantAvatar ? tenantAvatar : logoIcon
        appLogoIcon.value = logoIconValue
        userStore.setLogoIcon(logoIconValue)

        //设置logo文字
        const logoNameValue = tenantAvatarText ? tenantAvatarText : logoName
        appLogoName.value = logoNameValue
        userStore.setLogoName(logoNameValue)

        //设置租户ID
        formValue.value.tenantId = tenantId
        userStore.setTenantId(tenantId)
    } else {
        tenantMode.value = true
        //设置标题
        userStore.setTitle(website.title)
        setAppName(website.title)

        //设置logo图标
        appLogoIcon.value = logoIcon
        userStore.setLogoIcon(logoIcon)

        //设置logo文字
        appLogoName.value = logoName
        userStore.setLogoName(logoName)

        //设置租户ID
        formValue.value.tenantId = "000000"
        userStore.setTenantId("000000")
    }
    nextTick(() => {
        setAppImageColor()
    })
}

//登录
const loading = ref(false)
const formValidateClick = async () => {
    const formRes = await formValidate(formRef.value)
    if (formRes) {
        loading.value = true;
        useAppLogin(formValue.value).then((res) => {
            loading.value = false;
            window?.$message?.success('登录成功');
            setHomeRouter();
            //   setTimeout(() => {
            //       window?.location?.reload()  //刷新页面
            //   }, 1000);
        }).catch(() => {
            loading.value = false;
        })
    }
}

//处理作为首页的路由
const setHomeRouter = () => {
    const routes = getArrValue(getStoreValue('routes'))
    let appHomeUrl = 'using-stats'
    if (routes.indexOf(appHomeUrl) === -1) {
        appHomeUrl = routes[routes.length - 1]
    }
    userStore.setHomeUrl(appHomeUrl)
    router.push({
        name: appHomeUrl
    });
}

//游客登录
const touristsRef = ref(null);
const touristsForm = ref({phone: ''});
const touristsRules = {
    phone: {
        required: true,
        message: "请输入手机号",
        trigger: "blur"
    }
}
const touristsValidateClick = async () => {
    const formRes = await formValidate(touristsRef.value)
    if (formRes) {
        console.log(touristsForm.value)
    }
}

//忘记密码
const clickableClick = () => {
    const val = '<div style="font-size: 16px;">忘记密码请不要紧张，联系您项目上的专属客服人员电话 <span style="color:#1ECC95;">18423665354</span> ，提供身份证明信息即可初始化密码，建议初始化之后由您单独去更改密码</div>'
    window?.$messageBox?.alert(val, '联系项目客服', {
        confirmButtonText: '确定',
        dangerouslyUseHTMLString: true
    })
}
</script>

<style lang="scss" scoped>
@import "../../styles/view/login.scss";
</style>

<style lang="scss">
.form-box {
    .el-input__wrapper.is-focus{
        box-shadow: 0 0 0 2px var(--el-input-focus-border-color) inset;
    }
    .el-form-item.is-error .el-input__wrapper {
        box-shadow: 0 0 0 2px var(--el-color-danger) inset;
    }
}
</style>
