<template>
    <div class="wrap">
        <vHead></vHead>
        <div class="login">
            <div class="names">
                <img class="logo" src="../../../assets/img/three.png" alt="">
                <p class="text">或然数据平台</p>
            </div>
            <div class="form">
                <ul class="tab">
                    <li v-for="(item,index) in tabList" :key="index" :class="{active: form.distinguish == item.id}" @click="typeClick(item)">{{item.label}}</li>
                </ul>
                <el-form :model="form" :rules="rules" ref="login" label-width="0px">
                    <el-form-item prop="account">
                        <label class="account"></label>
                        <el-input v-model="form.account" :placeholder="form.distinguish == 1 ? '请输入账号' : '请输入手机号/邮箱'" @keyup.enter.native="submitForm"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <label class="password"></label>
                        <el-input type="password" placeholder="请输入密码" v-model="form.password" @keyup.enter.native="submitForm"></el-input>
                    </el-form-item>
                    <el-form-item prop="code">
                        <label class="code"></label>
                        <el-input placeholder="请输入验证码" v-model="form.code" @keyup.enter.native="submitForm"></el-input>
                        <img @click="getVerImg" :src="verificationIMG" class="ver-img" alt="">
                    </el-form-item>
                    <el-button class="submit" type="primary" @click="submitForm">登录</el-button>
                </el-form>
            </div>
        </div>

        <el-dialog title="绑定手机号" :visible.sync="phoneVisible" :close-on-click-modal="false" @close="closePhone" width="30%">
            <el-form ref="form" label-width="60px">
                <el-form-item label="手机号">
                    <el-input placeholder="请输入手机号" v-model="phone" maxlength="11"></el-input>
                </el-form-item>
                <el-form-item label="验证码">
                    <div class="flex">
                        <el-input v-model="phoneCode" placeholder="请输入验证码" maxlength="6"></el-input>
                        <el-button style="margin-left: 10px" type="text" @click="sendPhoneCode" :disabled="phoneDisabled">{{phoneBtnText}}</el-button>
                    </div>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="phoneVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="phoneSubmit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { mapActions } from 'vuex'
import vHead from '@/layouts/header'
import util from '@/libs/util'
import Setting from '@/setting'
export default {
    data: function() {
        return {
            tabList: [
                {
                    id: '1',
                    label: '账号登录'
                },{
                    id: '2',
                    label: '手机号/邮箱登录'
                }
            ],
            verificationIMG: '',
            form: {
                distinguish: '1',
                account: 'admin',
                password: '111aaa',
                random: '',
                code: ''
            },
            rules: {
                account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
            },
            phoneVisible: false,
            phone: '',
            phoneCode: '',
            phoneDisabled: false,
            phoneTimer: null,
            phoneBtnText: '发送验证码'
        };
    },
    components: {vHead},
    mounted(){
        this.getVerImg()
    },
    methods: {
        ...mapActions('user', [
            'login','setToken'
        ]),
        typeClick(item){
            this.form.account = ''
            this.form.distinguish = item.id
            this.$refs.login.clearValidate()
            this.rules.account[0].message = item.id == 1 ? '请输入账号' : '请输入手机号/邮箱'
        },
        submitForm() {
            this.$refs.login.validate(valid => {
                if (valid) {
                    this.login(this.form).then(() => {
                        let redirect = this.$route.query.redirect ? decodeURIComponent(this.$route.query.redirect) : '/index'
                        this.$router.replace(redirect)
                    }).catch(res => {
                        if(res && res.status == 30001){
                            this.phoneVisible = true
                        }
                        this.getVerImg()
                        this.form.code = ''
                    })
                }
            })
        },
        getVerImg(){
            if(!this.form.account){
                return this.verificationIMG = ''
            }
            this.form.random = Math.floor(Math.random()*999999999)
            this.verificationIMG = this.api.verification+'?random='+`${this.form.random}`
        },
        phoneCountdown(){
            let count = 60
            if(!this.phoneTimer){
                this.phoneDisabled = true
                this.phoneTimer = setInterval(() => {
                    console.log('倒计时中')
                    if(count > 0){
                        count--
                        this.phoneBtnText = `${count}秒后重试`
                    }else{
                        this.phoneDisabled = false
                        clearInterval(this.phoneTimer)
                        this.phoneTimer = null
                        this.phoneBtnText = `发送验证码`
                    }
                },1000)
            }
        },
        closePhone(){
            if(!this.emailDisabled){
                this.emailCode = ''
            }
        },
        sendPhoneCode(){
            if(!this.phone) return util.warningMsg('请输入手机号')
            if(!/^1[3456789]\d{9}$/.test(this.phone)) return util.warningMsg('请输入正确的手机号')
            let data = {
                phone: this.phone,
                types: 2
            }
            this.$post(this.api.sendPhoneOrEmailCode,data).then(res => {
                if(res.message.opener){
                    this.phoneCountdown()
                    this.phoneOpener = res.message.opener
                }else{
                    util.errorMsg(res.message)
                }
                
            }).catch(res => {})
        },
        phoneSubmit(){
            if(!this.phone) return util.warningMsg('请输入手机号')
            if(!/^1[3456789]\d{9}$/.test(this.phone)) return util.warningMsg('请输入正确的手机号')
            if(!this.phoneCode) return util.warningMsg('请输入验证码')
            let data = {
                phone: this.phone,
                types: 2,
                code: this.phoneCode,
                opener: this.phoneOpener,
                platform: Setting.platformId,
                account: this.form.account
            }
            this.$post(this.api.bindPhoneOrEmail,data).then(res => {
                util.successMsg('绑定成功')
                this.form.phone = this.phone
                this.phoneVisible = false

                this.setToken(res.token)
                util.local.set(Setting.tokenKey,res.token,43200000)
                util.successMsg('登录成功')
                setTimeout(() => {
                    this.$post(this.api.saveRecord,{
                        type: 3,
                    }).then(res => {}).catch(res => {})
                },3000)
                this.$router.push('/index/list')
            }).catch(res => {})
        },
    },
};
</script>

<style scoped lang="scss">
    .wrap {
        min-height: 100%;
        background:  url(../../../assets/img/login-bg.png) 0 0/100% 100% no-repeat;
        overflow: hidden;
        
        .login{
            width: 500px;
            margin: 0 auto;
            text-align: center;
            .names{
                margin: 70px 0;
                .text{
                    margin-top: 10px;
                    font-size: 20px;
                    color: #fff;
                }
            }

            .form{
                padding: 30px;
                margin-top: 30px;
                border-radius: 10px;
                background-color: #fff;
                .tab{
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    margin-bottom: 30px;
                    border-bottom: 2px solid #e0e0e0;
                    li{
                        padding: 9px 20px;
                        margin-bottom: -1px;
                        color: #555;
                        cursor: pointer;
                        border-bottom: 2px solid transparent;
                        &.active{
                            border-bottom-color: $main-color;
                        }
                    }
                }
                .el-form{
                    padding: 0 70px;
                }
                .label{
                    margin-bottom: 10px;
                    color: #105CB2;
                }
                /deep/.el-input__inner{
                    position: relative;
                    height: 46px;
                    padding: 0 20px 0 40px;
                    line-height: 46px;
                    border: 0;
                    border-bottom: 1px solid #e4e4e4;
                    border-radius: 0 !important;
                }
                .account,.password,.code{
                    z-index: 10;
                    position: absolute;
                    top: 11px;
                    left: 8px;
                    width: 24px;
                    height: 28px;
                    background: url(../../../assets/img/account.png) 0 0/100% 100% no-repeat;
                }
                .password{
                    width: 22px;
                    height: 24px;
                    background-image: url(../../../assets/img/password.png);
                }
                .code{
                    width: 22px;
                    height: 24px;
                    background-image: url(../../../assets/img/code.png);
                }
                .ver-img{
                    position: absolute;
                    top: -10px;
                    right: 0;
                }
                /deep/.el-form-item__error{
                    top: 105%;
                    left: auto;
                    right: 0;
                    color: #FFA94E;
                }
                .submit{
                    width: 100%;
                    height: 48px;
                    margin-top: 30px;
                    line-height: 48px;
                    padding: 0;
                    font-size: 20px;
                    background-color: $main-color;
                    border-radius: 8px;
                    border: 0;
                }
            }
        }
    }
</style>