<template>
    <div class='home-wrap'>
        <div class="background-wrap">
            <img class="background-image" src="@/assets/image/track.jpg" />
        </div>
        <div class="top-brand-wrap">
            <h1 class="title">5G 赛道</h1>
        </div>
        <div class="card flex-container">
            <van-form>
                <van-cell-group inset>
                    <van-field required v-model="formData.Name" name="姓名" label="姓名" clearable placeholder="请输入姓名" />
                    <van-field required type='number' v-model="formData.Phone" name="手机号" label="手机号" clearable placeholder="请输入手机号" />
                </van-cell-group>
                <van-row gutter="20">
                    <van-col span="12"> <van-button type="primary" block round @click="onLogin">登录</van-button> </van-col>
                    <van-col span="12"> <van-button type="primary" class="register" block round @click="onRegister">注册</van-button> </van-col>
                </van-row>
            </van-form>
        </div>
    </div>
</template>

<script>
import { Form, Field, Toast, CellGroup, Dialog, Button, Col, Row } from 'vant'
import { Login } from 'api/home'

// import imgs1 from '@/assets/image/imgs-01.jpg'
// import imgs2 from '@/assets/image/imgs-02.jpg'

export default {
    name:'Home',
    components:{
        [Form.name]:Form,
        [Field.name]:Field,
        [Toast.name]:Toast,
        [CellGroup.name]:CellGroup,
        [Dialog.name]:Dialog,
        [Button.name]:Button,
        [Col.name]:Col,
        [Row.name]:Row,
    },
    activated(){

    },
    watch:{
        Code(val){
            sessionStorage.Code = val
        }
    },
    data(){
        return {
            photo:'',
            formData:{
                Name:'',
                Phone:'',
            },
            Code:null,            //存入本地的code
        }
    },
    methods:{
        onLogin(){
            if (!this.formData.Name){
                Toast.fail('请填写姓名！')
                return 
            } else if (!this.formData.Phone || this.formData.Phone.length != 11){
                Toast.fail('请填写正确的手机号！')
                return 
            } else {
                Toast.loading({
                    message: '正在登录……',
                    forbidClick: true,
                });
                console.log(this.formData)
                Login(this.formData).then(res => {
                    if (res.code === 0){
                        Toast.success('登陆成功！')
                        this.$router.push('/photos')
                        localStorage.images = JSON.stringify(res.data)
                        // localStorage.images = JSON.stringify([imgs1,imgs2,imgs1,imgs2])
                    } else {
                        Toast.fail('登陆失败：' + res.msg)
                    }
                    Toast.clear()
                }).catch(err => {
                    Toast.fail('登陆失败：' + err)
                    Toast.clear()
                })
            }
        },
        onRegister(){
            this.$router.push('/home')
        }
    }
}
</script>