<template>
  <view class="login-header">
    <image src="/static/logo.png" mode="scaleToFill" @tap="switchEnv" />
    <view class="login-title">计全收银Pro</view>
  </view>
  <LoginInput title="账号" place="请输入登录名/手机号" require ref="refAccount" v-model:value="vdata.username" />
  <LoginInput title="密码" password place="请输入密码" require type="password" ref="refPwd" v-model:value="vdata.pwd" />
  <view class="button-main">
    <JButton @click="submit">登录</JButton>
  </view>
  <!-- #ifdef MP-ALIPAY -->
  <view class="support-content">
    <view class="sup-title">技术支持：</view>
    <view class="phone-number">{{ vdata.phone || '18611727422' }}</view>
  </view>
  <!-- #endif -->
  <EnvChangeTips ref="refEnv" />
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue'
import { $login, $userInfo, $getCompanyInfos } from '@/http/apiManager'
import EnvChangeTips from './components/EnvChangeTips.vue'
import storageManage from '@/commons/utils/storageManage.js'
const refAccount = ref(null)
const refPwd = ref(null)
const refEnv = ref(null)
const vdata = reactive({})
onMounted(() => {
  //  #ifdef MP-WEIXIN
  wxfaceapp.checkWxFacePayOsInfo({
    success(res) {
      console.log('success [checkWxFacePayOsInfo]', res)
      storageManage.deviceNo(res.osSerialNumber) //本地存储写入 设备号
      console.log(storageManage.deviceNo())
    },
    fail(err) {
      console.log('fail [checkWxFacePayOsInfo]', err)
    },
  })
  // #endif
  // #ifdef MP-ALIPAY
  my.ix.getSysProp({
    key: 'ro.serialno',
    success: (r) => {
      console.log('r', r)
      storageManage.deviceNo(r.value) //本地存储写入 设备号
    },
  })
  // #endif
  $getCompanyInfos().then(({ bizData }) => {
    const { siteInfo } = bizData
    vdata.phone = siteInfo.companyTel
  })
})
const submit = () => {
  console.log('submit')
  if (refAccount.value.check() && refPwd.value.check()) {
    $login(vdata).then(({ bizData }) => {
      // 保存 token
      storageManage.token(bizData.iToken)
      // 请求用户信息
      return $userInfo().then(({ bizData }) => {
        // 保存用户数据
        storageManage.userInfo(bizData)
        // 存储设备号
        //  #ifdef MP-WEIXIN
        //跳转首页
        uni.reLaunch({
          url: '/pages/index/index',
        })
        // #endif
        // #ifdef MP-ALIPAY
        console.log('执行')
        uni.reLaunch({
          url: '/pages/aliIndex/index',
        })
        // #endif
      })
    })
  }
}
const switchEnv = () => refEnv.value.tapFunc()
</script>

<style lang="scss" scoped>
.login-header {
  display: flex;
  flex-direction: column;
  align-items: center;

  image {
    margin-top: 80rpx;
    width: 260rpx;
    height: 260rpx;
  }

  .login-title {
    margin-bottom: 50rpx;
    font-weight: 700;
    font-size: 46rpx;
  }
}

.button-main {
  margin-top: 80rpx;
}
.support-content {
  display: flex;
  justify-content: center;
  margin-top: 50rpx;
  font-size: 30rpx;
  font-weight: 500;
}
</style>
