<template>
    <div class="login-body">
        <a v-if="appLogoIcon" class="left-logo" href="http://hczc.hcxxy.com/" target="_blank">
            <img id="logo-icon" alt="" :src="appLogoIcon">
            <img id="logo-name" alt="" :src="appLogoName">
        </a>
        <div class="left-pic-container">
            <Suspense>
                <HcPicVue />
                <template #fallback>
                    Loading...
                </template>
            </Suspense>
        </div>
        <div class="right-container">
            <div v-if="isShowHe" class="right-app-title">河南熙诚工程数字档案管理平台</div>
            <div v-else class="right-app-title">工程数字档案管理平台</div>
            
            <div class="sign-list">
                <div class="title-tab">
                    <div :class="account ? 'active' : ''" @click="accountClick">密码登录</div>
                    <div :class="account ? '' : 'active'" @click="accountClick">游客登录</div>
                </div>
                <div v-if="account" class="form-box mt-4">
                    <el-form ref="formRef" :model="formValue" :rules="formRules" label-position="left" label-width="0px" size="large">
                        <el-form-item prop="username">
                            <el-input v-model="formValue.username" clearable placeholder="账号" />
                        </el-form-item>
                        <el-form-item class="mt-8" prop="password">
                            <el-input v-model="formValue.password" clearable placeholder="密码" show-password type="password" @keyup="passwordKeyUp">
                                <template #suffix>
                                    <span class="clickable-text" @click="clickableClick">忘记密码</span>
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-form-item v-if="tenantMode" prop="tenantId">
                            <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 :loading="loading" block type="primary" @click="formValidateClick">登 录</el-button>
                        </el-form-item>
                        <div class="form-protocol" @click="protocolClick">《软件产品用户使用服务协议》</div>
                    </el-form>
                </div>

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

<script setup>
import { nextTick, onMounted, ref } from 'vue'
import router from '~src/router/index'
import { useAppStore } from '~src/store'
import { setUserTenantInfo, useAppLogin } from '~sto/user'
import { getTenantID } from '~api/user'
import { getTopUrl } from '~uti/tools'
import HcPicVue from './components/pic.vue'
import HcHomeBg from './components/home-bg.vue'
import { formValidate, getObjVal, setImageColorStyle } from 'js-fast-way'
import config from '~src/config/index'

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

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

const isShowHe = ref(false)
//首页模式
const IndexModel = ref(userStore.getIndexModel)
//切换登录类型
const account = ref(true)
const tenantMode = ref(true)

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

onMounted(() => {
    //根据端口显示登录信息
    const domains = config.domains, port = config.ports
    const currentDomain = getTopUrl()
    //把http和https加进去
    let newDomain = []
        for (let i = 0; i < domains.length; i++) {
            newDomain.push('http://' + domains[i])
            newDomain.push('https://' + domains[i])
        }
     //判断端口和域名
     try {
        const urls = currentDomain.split(':')
        if (urls.length >= 3 && urls[2] === '6789') {
           isShowHe.value = true

        } else {
            isShowHe.value = false
        }
    } catch (e) {
        isShowHe.value = false
    }
    
    getTenantIdApi()
    setAppImageColor()

    
})

const setAppImageColor = () => {
    setImageColorStyle('logo-icon', '#1ECC95')
    //document.documentElement.setAttribute('class','light color-green')
}

//表单
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)
    tenantMode.value = !(!error && code === 200 && res)
    const { icon, name, id } = await setUserTenantInfo(res)
    appLogoIcon.value = icon //设置logo图标
    appLogoName.value = name //设置logo文字
    formValue.value.tenantId = id //设置租户ID
    await nextTick(() => {
        setAppImageColor()
    })
}

//登录
const loading = ref(false)
const formValidateClick = async () => {
    const formRes = await formValidate(formRef.value)
    if (!formRes) return false
    //登录请求
    loading.value = true
    const { error, msg, data } = await useAppLogin(formValue.value)
    if (error) {
        loading.value = false
      
        return false
    }
    //登录成功
    window?.$message?.success('登录成功')
    setTimeout(() => {
        loading.value = false
        if (IndexModel.value === '1') {
            router.push({ path: '/home/index' })
        } else {
            router.push({ path: '/home/index/static' })
        }
    }, 1500)
}

//游客登录
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,
    })
}

//协议
const protocolClick = () => {
    window.open('https://blade-oss-chongqing.oss-cn-shenzhen.aliyuncs.com/upload/20231213/73025c8aba72e1165e0731942f6e66c9.pdf', '_blank')
}
</script>

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

<style lang="scss">
@import "../../styles/view/login.dark.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;
    }
}

.form-protocol {
    color: #8C8889;
    font-size: 13px;
    cursor: pointer;
    transition: color 0.3s;
    &:hover {
        color: var(--el-color-primary);
    }
}
</style>
