<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) {
                    Cookies.set('user', this.username);
                    Cookies.set('token', res.data.token);
                    if (res.data.role == "admin") {
                        Cookies.set("access", 1);
                    } else {
                        Cookies.set("access", 0);
                    }
                    this.axios.defaults.headers.common['Authorization'] = "Bearer " + res.data.token;
                    this.show = false;
                }
            });
        }
    }
}
</script>