<style lang="less">
    
</style>

<template>
    <Modal v-model="show">
        <p slot="header">
            <Icon type="information-circled"></Icon>
            <span>请登录</span>
        </p>
        <div>
            <Form ref="form" :label-width="80">
                <FormItem label="用户名">
                    <Input v-model="username" />
                </FormItem>
                <FormItem label="密码">
                    <Input v-model="password" />
                </FormItem>
            </Form>
        </div>
        <div slot="footer">
            <Button type="primary" @click="login">登录</Button>
        </div>
    </Modal>
</template>

<script>
import Cookies from 'js-cookie';
export default {
    name: 'login',
    data() {
        return {
            show: false,
            username: '',
            password: ''
        }
    },
    methods: {
        login() {
            this.axios.post("login", {
                name: this.username,
                password: this.password
            }).then((res) => {
                if (res.data.status === 0) {
                    let role = 1;
                    switch (res.data.role) {
                        case 'admin':
                            role = 1;
                            break;
                        case 'tongyong':
                            role = 2;
                            break;
                        case 'yuesao':
                            role = 3;
                            break;
                        case 'muying':
                            role = 4;
                            break;
                    }
                    Cookies.set("access", role);
                    this.axios.defaults.headers.common['Authorization'] = "Bearer " + res.data.token;
                    this.show = false;
                }
            });
        }
    }
}
</script>