<template>
    <view>
        <!-- pages/login/login.wxml -->
        <view class="container">
            <view class="logo-container">
                <image src="/static/resource/images/BAN.png" class="logo-img" mode="widthFix"></image>
            </view>

            <view class="slog-container">
                <view class="login-type">
                    <van-tabs :active="loginType" color="#09C48C" @change="onChange">
                        <van-tab title="个人用户"></van-tab>
                        <van-tab title="集团用户"></van-tab>
                    </van-tabs>
                </view>
                <form @submit="bindLoginSubmit">
                    <view class="form-container">
                        <view class="form-item">
                            <image src="/static/resource/images/shouji.png" class="form-img"></image>
                            <input type="text" name="username" placeholder="请输入手机号/账号" class="form-input" :value="username" @input="bindUsernameInput" />
                            <!-- image wx:if="{{ username }}" src="/resource/images/x.png" class="form-img-right" bindtap="bindUsernameClear"></image -->
                        </view>
                        <view class="form-item">
                            <image src="/static/resource/images/mima.png" class="form-img"></image>
                            <input type="text" password name="password" placeholder="请输入密码" class="form-input" :value="password" @input="bindPasswordInput" />
                            <!-- image wx:if="{{ password }}" src="/resource/images/x.png" class="form-img-right" bindtap="bindPasswordClear"></image -->
                        </view>
                        <view class="form-btn">
                            <button class="login-btn" form-type="submit">登录</button>
                        </view>
                    </view>
                </form>
                <view v-if="loginType == 0" class="other-item flex-row flex-between">
                    <view class="reg-text" @tap="bindRegister">注册</view>
                    <view class="forget-text" @tap="bindSetPassword">忘记密码?</view>
                </view>
            </view>
        </view>

        <view style="margin-top: 60rpx">
            <view>
                <!-- #ifdef MP-WEIXIN -->
                <!-- [miniprogram-to-uniapp] 公众号关注组件 仅微信小程序支持 -->
                <official-account></official-account>
                <!-- #endif -->

                <!-- #ifndef MP-WEIXIN -->
   
                <!-- #endif -->
            </view>
        </view>
        <view class="version">{{ app_version }}</view>
    </view>
</template>

<script>
// pages/login/login.js
var config = require('../../common/config.js');
var http = require('../../common/http.js');
var common = require('../../common/common.js');
var storage = require('../../common/storage.js');
export default {
    data() {
        return {
            app_version: '',
            loginType: 1,
            username: '',
            password: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        const userInfo = storage.getUserInfo();
        if (userInfo) {
            uni.reLaunch({
                url: '/pages/home/home',
                success: function (res) {},
                fail: function (res) {},
                complete: function (res) {}
            });
        }
        this.setData({
            app_version: config.APP_VERSION
        });
        this.setBMSParams('91109a');
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        const that = this;
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        setBMSParams(value) {
            var header = '4E57';
            var midType = '00000000020100';
            var end = '0000000068';
            //内容部分
            const cmdLength = (midType + value + end).length / 2 + 6;
            var cmdLenHex = common.dec2hex(cmdLength, 4); //长度
            var cmdList = common.hexToList(header + cmdLenHex + midType + value + end);
            //校检
            var CRCHex = this.arrayToIntHex(cmdList); //CRC校检位
            //下发整体内容
            var cmdHex = header + cmdLenHex + midType + value + end + CRCHex;
            // console.log(cmdHex)
            return cmdHex;
        },

        arrayToIntHex(array) {
            var total = 0;
            for (var i = 0; array.length > i; i++) {
                total += array[i];
            }
            return common.dec2hex(total, 8);
        },

        bindUsernameInput: function (e) {
            this.setData({
                username: e.detail.value
            });
        },

        bindPasswordInput: function (e) {
            this.setData({
                password: e.detail.value
            });
        },

        bindUsernameClear: function (e) {
            this.setData({
                username: ''
            });
        },

        bindPasswordClear: function (e) {
            this.setData({
                password: ''
            });
        },

        bindLoginSubmit: function (e) {
            console.log('1111');
            if (!e.detail.value) {
                common.simpleToast('请输入登录账号密码');
                return;
            }
            const username = e.detail.value.username;
            const password = e.detail.value.password;
            if (common.isEmpty(username)) {
                common.simpleToast('请输入登录账号');
                return;
            }
            if (common.isEmpty(password)) {
                common.simpleToast('请输入密码');
                return;
            }
            var pData;
            if (this.loginType == 0) {
                pData = {
                    type: 'user',
                    phone: username,
                    password: password
                };
            } else {
                pData = {
                    type: 'group',
                    username: username,
                    password: password
                };
            }
            const that = this;
            // uni.login({
            //     success: function (res) {
            //         const accountInfo = uni.getAccountInfoSync();
                    pData.appid = "wxddbcc3709026525e"
                    http.postApi(config.API_USER_LOGIN, pData, function (resp) {
                        if (resp.data.code === 200) {
                            storage.setApiToken(resp.data.data.token);
                            that.loadUserInfo();
                        } else {
                            common.simpleToast(resp.data.msg);
                        }
                    });
            //     }
            // });
        },

        loadUserInfo: function () {
            http.postApi(config.API_USER_INFO, {}, (resp) => {
                if (resp.data.code === 200) {
                    storage.setUserInfo(resp.data.data);
                    common.simpleToast('登录成功');
                    uni.reLaunch({
                        url: '/pages/home/home',
                        success: function (res) {},
                        fail: function (res) {},
                        complete: function (res) {}
                    });
                }
            });
        },

        onChange(event) {
            this.setData({
                loginType: event.detail.name
            });
        },

        bindRegister() {
            uni.navigateTo({
                url: '/pages/register/register'
            });
        },

        bindSetPassword() {
            uni.navigateTo({
                url: '/pages/setPassword/setPassword'
            });
        }
    }
};
</script>
<style>
@import './login.css';
</style>
