<template>
  <div class="register">
    <div class="one">登录</div>
    <div class="two">
      <el-col :span="24" class="tab">
        <el-col :span="9" class="title" @click="toTab(0)" :class="[activeName == '0' ? 'tab0' : 'tab1']">用户名密码登录</el-col>
        <el-col :span="9" class="title" @click="toTab(1)" :class="[activeName == '1' ? 'tab0' : 'tab1']">短信快捷登录</el-col>
      </el-col>
      <div v-if="activeName == '0'">
        <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="70px" class="form" label-position="left">
          <el-col :span="24" class="content">
            <el-form-item label="账号" prop="account">
              <el-input size="large" 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 size="large" 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="text">
              <span>没有账号</span>
              <span @click="toRegister">立即注册</span>
            </el-col>
            <div class="checks_box">
              <el-checkbox v-model="isAgree" size="large" class="checks_inpt" />
              <div class="checks_text">
                <span>我已阅读并同意</span>
                <span @click="toAgree"> 服务条款、隐私政策</span>
              </div>
            </div>
          </el-col>
        </el-form>
      </div>
      <div v-else>
        <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="70px" class="form" label-position="left">
          <el-col :span="24" class="content">
            <el-form-item label="手机号" prop="phone">
              <el-input size="large" clearable v-model="form.phone" placeholder="请输入手机号">
                <template #prefix>
                  <el-icon>
                    <Iphone />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item label="验证码" prop="checkCode">
              <el-input size="large" class="check-code-box" v-model.number="form.checkCode" placeholder="请输入验证码">
                <template #prefix>
                  <el-icon>
                    <Message />
                  </el-icon>
                </template>
                <template v-slot:append>
                  <el-button :disabled="codeCd" @click="handleCaptcha('form')"
                    >获取验证码
                    <span v-if="codeCd">({{ long }})</span>
                  </el-button>
                </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="text">
              <span>没有账号</span>
              <span @click="toRegister">立即注册</span>
            </el-col>
            <div class="checks_box">
              <el-checkbox v-model="isAgree" size="large" class="checks_inpt" />
              <div class="checks_text">
                <span>我已阅读并同意</span>
                <span @click="toAgree"> 服务条款、隐私政策</span>
              </div>
            </div>
          </el-col>
        </el-form>
      </div>
    </div>
  </div>
  <el-dialog v-model="dialog" title="使用协议">
    <div v-html="configInfo.agreement"></div>
  </el-dialog>
</template>
<script setup>
// 基础
import { cloneDeep } from 'lodash-es'
// 接口
import { LoginStore } from '@/store/api/login'
const loginStore = LoginStore()
// 路由
const router = useRouter()
const activeName = ref(0)

const configInfo = inject('configInfo')

// 用户协议
const isAgree = 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 toTab = async (active) => {
  activeName.value = active
  form.value = {}
}
// 登录
const submitForm = async (formEl) => {
  if (!isAgree.value) {
    ElMessage({
      message: '请阅读并同意用户协议和隐私政策',
      type: 'warning'
    })
    return
  }
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const data = cloneDeep(form.value)
      data.type = 'User'
      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: '/login', query: { status: '0' } })
}
// 查看隐私协议
const toAgree = () => {
  dialog.value = !dialog.value
}
</script>
<style scoped lang="scss">
.register {
  .one {
    text-align: center;
    font-size: $global-font-size-24;
    font-weight: bold;
    padding: 5px 0 10px 0;
  }
  .two {
    .tab {
      display: flex;
      align-items: center;
      justify-content: center;
      margin: 10px;

      .title {
        text-align: center;
        margin: 0 40px;
        font-family: PingFangSC-Semibold !important;
        font-size: $global-font-size-20;
        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 {
      padding: 20px;
      .remark {
        cursor: default;
        text-align: right;
        margin: 10px 0;
        color: #9a9a9a;
      }
      .button {
        text-align: center;
        padding: 10px 0;

        :deep(.el-button) {
          width: 100px !important;
          height: 44px !important;
          border: 0 !important;
          border-radius: 50px !important;
          color: #f8f8f8 !important;
          font-size: $global-font-size-18 !important;
          text-align: center !important;
          line-height: 40px !important;
          cursor: pointer !important;
          font-family: PingFangSC-Regular !important;
          background-color: $global-color-107 !important;
        }
      }
      .text {
        text-align: center;
        font-size: $global-font-size-18;
        span:last-child {
          color: $global-color-107;
          margin: 0 0 0 2px;
          cursor: default; /* 将鼠标样式更改为箭头 */
        }
      }
      .checks_box {
        display: flex;
        align-items: center;
        justify-content: center;
        .checks_inpt {
          margin: 0 5px 0 0;
        }
        .checks_text {
          cursor: default;
          span:last-child {
            color: #2374ff;
          }
        }
      }
    }
  }
  :deep(table) {
    border-collapse: collapse !important;
    width: 100% !important;
    text-align: center !important;
  }

  :deep(th) {
    border: 1px solid #ddd !important;
    padding: 8px !important;
  }

  :deep(td) {
    border: 1px solid #ddd !important;
    padding: 8px !important;
  }
  :deep(table tr:nth-child(even)) {
    background-color: #f2f2f2 !important;
  }

  :deep(table tr:hover) {
    background-color: #ddd !important;
  }
}
</style>
