<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main animate__animated animate__backInRight" v-loading="loading">
        <el-col :span="24" class="one">
          <div class="loginform">
            <el-col :span="24" class="tab">
              <el-col
                :span="6"
                class="title"
                @click="toTab(0)"
                :class="[activeName == '0' ? 'tab0' : 'tab1']"
                >个人登录</el-col
              >
              <el-col
                :span="6"
                class="title"
                @click="toTab(1)"
                :class="[activeName == '1' ? 'tab0' : 'tab1']"
                >管理登录</el-col
              >
            </el-col>
            <el-col :span="24" class="content" v-show="activeName == '0'">
              <el-form
                ref="ruleFormRef"
                :model="form"
                :rules="rules"
                label-width="60px"
                class="form"
                label-position="left"
              >
                <el-form-item label="账号" prop="account">
                  <el-input clearable v-model="form.account" placeholder="请输入账号">
                    <template #prefix>
                      <el-icon>
                        <User />
                      </el-icon>
                    </template>
                  </el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                  <el-input
                    v-model="form.password"
                    type="password"
                    show-password
                    placeholder="请输入登录密码"
                  >
                    <template #prefix>
                      <el-icon>
                        <Unlock />
                      </el-icon>
                    </template>
                  </el-input>
                </el-form-item>
                <el-col :span="24" class="remark">
                  <span>忘记密码?</span>
                </el-col>
                <el-col :span="24" class="button">
                  <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
                </el-col>
                <el-col :span="24" class="agree">
                  <span>登录即表示您同意</span>
                  <span @click="dialog = true">《{{ siteInfo.zhTitle }}使用协议》</span>
                </el-col>
                <el-col :span="24" class="other">
                  <span>还没有账号?</span>
                  &nbsp;&nbsp;
                  <span @click="toRegister">去注册</span>
                </el-col>
              </el-form>
            </el-col>
            <el-col :span="24" class="content" v-show="activeName == '1'">
              <el-form
                ref="ruleFormRef"
                :model="form"
                :rules="rules"
                label-width="60px"
                class="form"
                label-position="left"
              >
                <el-form-item label="账号" prop="account">
                  <el-input clearable v-model="form.account" placeholder="请输入账号">
                    <template #prefix>
                      <el-icon>
                        <User />
                      </el-icon>
                    </template>
                  </el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                  <el-input
                    v-model="form.password"
                    type="password"
                    show-password
                    placeholder="请输入登录密码"
                  >
                    <template #prefix>
                      <el-icon>
                        <Unlock />
                      </el-icon>
                    </template>
                  </el-input>
                </el-form-item>
                <el-col :span="24" class="remark">
                  <span>忘记密码?</span>
                </el-col>
                <el-col :span="24" class="button">
                  <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
                </el-col>
                <el-col :span="24" class="agree">
                  <span>登录即表示您同意</span>
                  <span @click="dialog = true">《{{ siteInfo.zhTitle }}使用协议》</span>
                </el-col>
                <el-col :span="24" class="other">
                  <span>还没有账号?</span>
                  &nbsp; &nbsp;
                  <span @click="toRegister">去注册</span>
                </el-col>
              </el-form>
            </el-col>
          </div>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog v-model="dialog" title="使用协议">
      <div v-html="configInfo.agreement"></div>
    </el-dialog>
  </div>
</template>
<script setup>
// 基础
import { siteInfo } from '@/layout/site'
import { cloneDeep } from 'lodash-es'
// 接口
import { DesignStore } from '@/store/api/platform/design'
import { LoginStore } from '@/store/api/login'
const loginStore = LoginStore()
const designStore = DesignStore()
const $checkRes = inject('$checkRes')
// 路由
const router = useRouter()
// 加载中
const loading = ref(false)
const dialog = ref(false)
const form = ref({})
const ruleFormRef = ref()
const rules = reactive({
  account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
})
const activeName = ref(0)
const configInfo = ref({})
// 请求
onMounted(async () => {
  loading.value = true
  search()
  loading.value = false
})
const search = async () => {
  // 基础设置
  const result = await designStore.query({})
  if ($checkRes(result)) configInfo.value = result.data[0] || {}
}
// 选择
const toTab = async (active) => {
  activeName.value = active
  form.value = {}
}
// 登录
const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const data = cloneDeep(form.value)
      if (activeName.value == 0) data.type = 'User'
      else data.type = 'Admin'
      const res = await loginStore.login(data)
      if (res.errcode == '0') {
        ElMessage({ message: `登录成功`, type: 'success' })
        localStorage.setItem('token', res.data)
        // 路由
        router.push({ path: '/' })
      } else {
        ElMessage({ message: `${res.errmsg}`, type: 'error' })
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
// 去注册
const toRegister = () => {
  router.push({ path: '/register' })
}
</script>
<style scoped lang="scss">
.main {
  .one {
    background-image: url(/images/loginbg.jpeg);
    background-position: center center;
    background-repeat: no-repeat;
    height: calc(100vh - 22.6vh);
    width: 100%;
    background-size: cover;
    display: flex;
    justify-content: space-evenly;

    .loginform {
      position: absolute;
      top: 50%;
      left: 50%;
      height: 380px;
      padding: 20px 85px;
      background: hsla(0, 0%, 100%, 0.6);
      box-shadow: 0 0 30px 0 rgba(51, 51, 51, 0.2);
      -webkit-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%);
      border-top: 10px solid #1492ff;

      .tab {
        display: flex;
        align-items: center;
        justify-content: center;

        .title {
          text-align: center;
          margin: 0 40px;
          font-family: PingFangSC-Semibold !important;
          font-size: 20px;
          letter-spacing: -0.14px;
          line-height: 32px;
          font-weight: bold;
        }

        .tab0 {
          color: #1492ff;
          border-bottom: 2px solid;
          border-bottom-color: #1492ff;
          padding-bottom: 10px;
        }

        .tab1 {
          color: #333333;
          padding-bottom: 10px;
        }
      }

      .content {
        margin: 35px 50px 0px;

        .remark {
          display: flex;
          justify-content: flex-end;

          span {
            cursor: pointer;
            font-family: PingFangSC-Regular;
            font-size: 14px;
            color: #333333;
            letter-spacing: -0.09px;
            text-align: right;
            line-height: 32px;
          }

          span:hover {
            color: #2374ff;
          }
        }

        .button {
          padding: 10px 0;

          :deep(.el-button) {
            width: 100% !important;
            height: 44px !important;
            border: 0 !important;
            color: #f8f8f8 !important;
            font-size: 16px !important;
            text-align: center !important;
            line-height: 40px !important;
            cursor: pointer !important;
            font-family: PingFangSC-Regular !important;
            background-color: #1492ff !important;
          }
        }

        .agree {
          padding: 0 0 30px 0;
          color: #999;
          font-size: 12px;

          span:last-child {
            color: #2374ff;
          }
        }

        .other {
          text-align: center;
          font-family: PingFangSC-Regular;
          font-size: 14px;
          color: #333333;

          span:last-child {
            color: #2374ff;
          }
        }
      }
    }
  }
}
</style>
