<template>
  <!--pages/login/login.wxml-->
  <view class="container">
    <view class="login-icon">
      <image class="login-img" src="../../image/icon/login-background.svg"></image>
    </view>
    <view class="login-from">
      <view class="inputView">
        <!--账号-->
        <input v-model="username" class="inputText" placeholder="账号" />
        <!--密码-->
        <input v-model="password" class="inputText" placeholder="密码" password="true" />
      </view>
      <view class="loginBtnView">
        <!--按钮-->
        <button class="loginBtn" type="primary" @tap="login">登录</button>
        <button class="loginBtn" type="secondary" bindtap="visitor">访客申请</button>
      </view>
    </view>
  </view>
</template>

<script>
import Taro from '@tarojs/taro'

import './login.css'

export default {
  data() {
    return {
      ...Taro.getApp().globalData,
      username: '',
      password: ''
    }
  },
  methods: {
    login() {
      if (this.username == '' || this.password == '') {
        Taro.showToast({
          title: "请完善登录信息",
          icon: 'error',
          duration: 2000
        })
      } else {
        Taro.request({
          url: `${this.baseUrl}/user/login`,
          data: {
            username: this.username,
            password: this.password,
          },
          success: function (d) {
            let data = d.data;
            if (data.code == 200) {
              if (data.status == "success") {
                Taro.setStorageSync("userInfo", data.userInfo);
                console.log("userInfo", Taro.getStorageSync("userInfo"))
                Taro.showToast({
                  title: "登录成功",
                  icon: 'success',
                  success: function () {
                    Taro.redirectTo({
                      url: '/pages/index/index'
                    })
                  }
                })
              } else {
                Taro.showToast({
                  title: data.msg,
                  icon: 'error'
                })
              }
            } else {
              Taro.showToast({
                title: data.msg,
                icon: 'error',
                duration: 2000
              })
            }
          }
        })
      }
    }
  }
}
</script>
