<template>
    <div class="base-container">
        <div class="base-left">
            <div class="logo">
                <img class="logo-icon" src="@/assets/images/home/logo.png" alt="" />
                <el-divider class="divider" direction="vertical" />
                <div class="text">PROCESSED PINE DRONE AI</div>
            </div>
        </div>
        <div class="base-right">
            <div class="login-icon">
                <img class="login" src="@/assets/images/common/login-icon.png" alt="" />
                <div class="name">Drone AI</div>
            </div>
            <el-form
                :model="form"
                :rules="rules"
                v-show="!success"
                class="ve_form"
                ref="ref_form"
                @keyup.enter="onSubmit"
            >
                <el-form-item prop="userName">
                    <el-input size="large" v-model.trim="userName" placeholder="Email Address" />
                </el-form-item>
                <el-form-item class="pwd" prop="pwd">
                    <el-input size="large" type="password" v-model.trim="pwd" placeholder="Password" />
                </el-form-item>
                <el-form-item class="operation">
                    <el-checkbox class="checkbox">Keep me logged in </el-checkbox>
                    <span class="txt">Forget password?</span>
                </el-form-item>
                <div class="login" @click="onSubmit">Log in</div>
            </el-form>
            <el-divider class="divider">Dedicated for you with love</el-divider>
            <div class="version">Version V1.0.0</div>
        </div>
    </div>
</template>

<script setup>
import { SET_TOKEN, SET_UNAME, SET_USER_INFO } from "@/store/modules/app/type";
import Common from "@/components/Common";
import { ref, reactive, toRefs } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";

// const rules = {
//   phone: [{ required: true, message: "请输入用户名", trigger: "blur" }]
// };

const rules = {
    userName: [{ required: true, message: "请输入用户名", trigger: "blur" }],
    pwd: [{ required: true, message: "请输入验证码", trigger: "blur" }],
};

const store = useStore();
const router = useRouter();
// 定义两个账号
const guestAccount = { pwd: "游客", userName: "游客" };
const regularAccount = { pwd: "", userName: "13797066447" };
// const form = reactive({
//   "pwd": "",
//   "userName": "13797066447"
// });
const form = reactive({
    pwd: "",
    userName: "",
});
const { userName, pwd } = toRefs(form);
const ref_form = ref(null);
const success = ref(false);
sessionStorage.clear();
store.commit(`app/${SET_TOKEN}`, "");
router.options.isAddDynamicMenuRoutes = false;

// 根据时间设置账号
const setAccountByTime = () => {
    const now = new Date();
    const currentHour = now.getHours();

    if (currentHour >= 18 && currentHour < 24) {
        form.pwd = guestAccount.pwd;
        form.userName = guestAccount.userName;
    } else {
        form.pwd = regularAccount.pwd;
        form.userName = regularAccount.userName;
    }
};

// 初始化账号
setAccountByTime();

const onSubmit = () => {
    ref_form.value.validate(async (valid) => {
        if (valid) {
            const res = await VE_API.system.login(form);
            if (res.code == 0) {
                const { token, userName, djiCloudToken } = res.data;
                store.commit(`app/${SET_TOKEN}`, token);
                store.commit(`app/djiCloudToken`, djiCloudToken);
                store.commit(`app/${SET_UNAME}`, userName);
                res.data["token"] = undefined;
                res.data["x-auth-token"] = undefined;
                res.data["pwd"] = undefined;
                store.commit(`app/${SET_USER_INFO}`, JSON.stringify(res.data));
                success.value = true;
                router.push({ name: "Main" });
            }
        } else {
            return;
        }
    });
};
</script>

<style lang="scss" scoped>
.base-container {
    width: 100%;
    height: 100vh;
    display: flex;
    .base-left {
        width: calc(100% - 500px);
        height: 100%;
        background: url("@/assets/images/home/main-bg.png") no-repeat center center / 100% 100%;
        padding: 22px 30px;
        box-sizing: border-box;
        .logo {
            display: flex;
            align-items: center;
            .logo-icon {
                width: 107px;
                height: 56px;
            }
            .divider {
                height: 16px;
                border-color: rgba(255, 255, 255, 0.3);
                border-width: 2px;
                margin: 15px 16px 0 18px;
            }
            .text {
                font-size: 24px;
                color: #fff;
                margin-top: 15px;
            }
        }
    }
    .base-right {
        width: 500px;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        padding: 40px;
        box-sizing: border-box;
        position: relative;
        .login-icon {
            .login {
                width: 202px;
                height: 202px;
            }
            .name {
                font-size: 50px;
                font-weight: 500;
                margin-top: -30px;
            }
        }
        .ve_form {
            width: 100%;
            margin-top: 26px;
            .pwd {
                margin-top: 37px;
            }
            ::v-deep {
                .el-input--large .el-input__wrapper {
                    padding: 11px 15px;
                    font-size: 16px;
                }
                .el-checkbox__inner {
                    width: 25px;
                    height: 25px;
                    margin-right: 6px;
                }
                .el-checkbox__input.is-checked .el-checkbox__inner {
                    background: #00a342;
                    border-color: #00a342;
                    &:after {
                        border-width: 2px;
                        height: 15px;
                        width: 8px;
                        left: 7px;
                    }
                }
                .el-checkbox__label {
                    color: #818181;
                    font-size: 16px;
                }
                .el-form-item__content {
                    display: flex;
                    justify-content: space-between;
                }
            }
            .operation {
                margin-top: 42px;
                .txt {
                    font-size: 16px;
                    color: #00a342;
                }
            }
            .login {
                border-radius: 10px;
                background: #00a342;
                font-size: 16px;
                padding: 20px;
                text-align: center;
                color: #fff;
                box-shadow: 0 15px 30px -10px rgba(119, 84, 246, 0.25);
            }
        }
        .divider {
            margin-top: 52px;
            ::v-deep {
                .el-divider__text {
                    width: 210px;
                    font-size: 16px;
                    color: rgba(0, 0, 0, 0.4);
                }
            }
        }
        .version {
            position: absolute;
            bottom: 12px;
            right: 12px;
            font-size: 16px;
        }
    }
}
</style>
