<template>
  <view class="page-account-login">
    <view class="logo">
      <image
        :src="stationLoginLogoImg"
        mode="heightFix"
        @error="onError"
      />
    </view>
    <!-- 登录 -->
    <view
      v-if="!isForgetPassword"
      class="login-form"
    >
      <view :class="['item',errorTips===1 && !isForgetPassword? 'error':'']">
        <view class="account">
          <text class="input-item">
            账号
          </text>
          <input
            v-model="principal"
            type="text"
            data-type="account"
            placeholder-class="inp-palcehoder"
            placeholder="请输入账号"
            @blur="principal = principal.replace(/\s/g,'')"
          >
        </view>
        <view
          v-if="errorTips===1 && !isForgetPassword"
          class="error-text"
        >
          <text class="warning-icon">
            !
          </text>账号不能为空
        </view>
      </view>
      <view :class="['item',errorTips===2? 'error':'']">
        <view class="account">
          <text class="input-item">
            密码
          </text>
          <input
            v-model="credentials"
            type="password"
            data-type="password"
            placeholder-class="inp-palcehoder"
            placeholder="请输入密码"
            @blur="credentials = credentials.replace(/\s/g,'')"
          >
        </view>
        <view
          v-if="errorTips===2"
          class="error-text"
        >
          <text class="warning-icon">
            !
          </text>请输入密码
        </view>
      </view>
    </view>

    <view v-if="!isForgetPassword">
      <button
        class="authorized-btn"
        @tap="onLogin"
      >
        登录
      </button>
    </view>
  </view>
</template>

<script setup>
import { encrypt } from '@/utils/crypto'
import logoImg from '@/static/images/icon/logo.png'

const errorTips = ref(0) // 错误提示
const stationLoginLogoImg = ref('')
/**
* 生命周期函数--监听页面加载
*/
const isForgetPassword = ref(false) // 是否修改密码
onLoad((options) => {
  if (options.isForgetPassword) {
    isForgetPassword.value = options.isForgetPassword
  }
  onGetUniWebConfig()
})

const onGetUniWebConfig = () => {
  http.request({
    url: '/mall4cloud_admin/ua/web_config/get_activity',
    method: 'GET',
    data: {
      webConfigType: 'STATION_WEBSITE_CONFIG'
    }
  }).then(({ data }) => {
    const logo = data.stationLoginLogoImg
    stationLoginLogoImg.value = logo ? import.meta.env.VITE_APP_RESOURCES_URL + logo : logoImg
    uni.setStorageSync('cloudStationLoginLogoImg', stationLoginLogoImg.value)
  })
}

/**
 * 登录
 */
const principal = ref('')
const credentials = ref('')
const onLogin = () => {
  if (!principal.value) {
    errorTips.value = 1
  } else if (credentials.value.length === 0) {
    errorTips.value = 2
  } else {
    errorTips.value = 0
    http.request({
      url: '/mall4cloud_auth/ua/station_login',
      method: 'POST',
      data: {
        userName: principal.value,
        passWord: encrypt(credentials.value),
        sysType: 4,
        tempUid: uni.getStorageSync('cloudSTempUid')
      }
    }).then(({ data }) => {
      uni.removeStorageSync('cloudStationId')
      uni.setStorageSync('cloudStationToken', data.accessToken)

      uni.showToast({
        title: '登录成功!',
        icon: 'none',
        complete () {
          uni.reLaunch({
            url: '/pages/index/index'
          })
        }
      })
    })
  }
}
const onError = () => {
  stationLoginLogoImg.value = logoImg
}
</script>
<style lang="scss" scoped>
@use "./account-login.scss";
</style>
