<style lang="less">
    @import './login.less';
    .ivu-form-item-content {
        margin-left: 0;
    }
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                            <p style="color:#f00;text-align:center">{{message}}</p>
                        </FormItem>
                    </Form>
                    <!-- <p class="login-tip">输入任意用户名和密码即可</p> -->
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
import util from '@/libs/util';
export default {
    data () {
        return {
            message: '',
            form: {
                userName: '',
                password: ''
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        handleSubmit () {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    this.axios.post("login", {
                        name: this.form.userName,
                        password: this.form.password
                    }).then((res) => {
                        if (res.data.status === 0) {
                            Cookies.set('user', this.form.userName);
                            Cookies.set('token', res.data.token);
                            let role = 1;
                            let home = 'home_index';
                            switch (res.data.role) {
                                case 'admin':
                                    role = 1;
                                    home = "order_housekeeper";
                                    break;
                                case 'tongyong':
                                    role = 2;
                                    home = "order_housekeeper";
                                    break;
                                case 'yuesao':
                                    home = "order_matron";
                                    role = 3;
                                    break;
                                case 'muying':
                                    role = 4;
                                    home = "order_product";
                                    break;
                            }
                            Cookies.set("access", role);
                            this.axios.defaults.headers.common['Authorization'] = "Bearer " + res.data.token;
                            this.$router.push({
                                name: home
                            });
                        } else {
                            this.message = res.data.message;
                        }
                    });
                    /*Cookies.set('user', this.form.userName);
                    Cookies.set('password', this.form.password);
                    if (this.form.userName === 'iview_admin') {
                        Cookies.set('access', 0);
                    } else {
                        Cookies.set('access', 1);
                    }
                    */
                }
            });
        }
    }
};
</script>

<style>

</style>
