<template>
    <div class="body">
        <van-swipe class="my-swipe" :autoplay="3000" :height="200" indicator-color="white">
            <van-swipe-item v-for="(item, index) in images" :key="index" class="swipe-img">
                <img :src="item"/>
            </van-swipe-item>
        </van-swipe>
        <van-grid :column-num="3">
            <van-grid-item v-for="(item,index) in routers" :key="index" :icon="item.icon" :text="item.name" 
            :info="item.name=='消息记录'&&unreadNum>0?unreadNum:''"
            :to="item.url">
            </van-grid-item>
        </van-grid>
        <div class="tip"  v-if="isCorpWX">
            工时报表统计等更多功能，请从PC端企业微信进入
        </div>
        <div class="tip"  v-if="!isCorpWX">
            体验报表统计等更多功能，尽在PC端<br>
            网页用户访问http://worktime.ttkuaiban.com，钉钉用户直接从钉钉PC端进入工时管家
        </div>
        <Footer page="index"></Footer>
    </div>
</template>

<script>
    import Footer from "@/components/Footer";

    export default {
        data() {
            return {
                isCorpWX:false,
                isWX:false,
                user: JSON.parse(localStorage.userInfo),
                unreadNum:0,
                images: [
                    // require('../../assets/img/index/banner_1.png'),
                    require('../../assets/img/index/banner4.jpeg'),
                    // require('../../assets/img/index/banner_3.png'),
                ],
                routers: [
                    {
                        name: '查看日报',
                        url: '/calendar',
                        icon: 'description'
                    },
                    
                    {
                        name: '填写日报',
                        url: '/edit',
                        icon: 'edit'
                    },
                    
                    
                ],
            };
        },
        created() {
            let index = this.active + 1;
            this.list = this[`list${index}`]; // this.list1
        },
        mounted() {
            var ua = navigator.userAgent.toLowerCase();
            if (ua.indexOf("wxwork") > 0) {
                this.isCorpWX = true;
            } else if (ua.indexOf("micromessenger") > 0) {
                this.isWX = true;
            }

            if (this.user.company.packageEngineering == 1) {
                this.routers.push(
                {
                    name: '专业审核',
                    url: '/profession_review',
                    icon: 'todo-list-o'
                });
            }
            if (this.user.role == 0) {
                //普通员工
                if (this.user.manageDeptId != 0 && this.user.company.packageEngineering == 1) {
                    this.routers.push(
                    {
                        name: '部门审核',
                        url: '/department_review',
                        icon: 'todo-list-o'
                    });
                }
                if (this.user.leader) {
                    this.routers.push({
                        name: '项目报告审核',
                        url: '/review',
                        icon: 'todo-list-o'
                    });
                } 
            } else if (this.user.role == 1 || this.user.role == 2 || this.user.role == 5){
            // } else if (this.user.role == 1 || this.user.role == 2){
                if (this.user.company.packageEngineering == 1) {
                    this.routers.push(
                    {
                        name: '部门审核',
                        url: '/department_review',
                        icon: 'todo-list-o'
                    });
                }
                
                this.routers.push({
                        name: '项目报告审核',
                        url: '/review',
                        icon: 'todo-list-o'
                    });
                this.routers.push({
                        name: '项目管理',
                        url: '/project',
                        icon: 'label-o'
                    });
                
                    // 自己加的
                // this.routers.push({
                //         name: '导入日报审核',
                //         url: '/audit',
                //         icon: 'label-o'
                //     });
            } else {
                if (this.user.manageDeptId != 0 && this.user.company.packageEngineering == 1) {
                    this.routers.push(
                    {
                        name: '部门审核',
                        url: '/department_review',
                        icon: 'todo-list-o'
                    });
                }
                if (this.user.role == 3||this.user.role == 5||this.user.role == 6) {//项目管理员，公司领导，公司高层
                    this.routers.push({
                        name: '项目管理',
                        url: '/project',
                        icon: 'label-o'
                    });
                }
                if (this.user.leader) {
                    this.routers.push({
                        name: '项目报告审核',
                        url: '/review',
                        icon: 'todo-list-o'
                    });
                }
            }
            if(this.user.role == 1 || this.user.role == 2) {
                this.routers.push({
                    name: '导入日报审核',
                    url: '/audit',
                    icon: 'label-o'
                });
            } else {
                if(this.user.role == 6 && this.user.timeType.needDeptAudit == 1) {
                    this.routers.push({
                        name: '导入日报审核',
                        url: '/audit',
                        icon: 'label-o'
                    });
                }
            }
            this.routers.push({
                                name: '消息记录',
                                url: '/msg',
                                icon: 'todo-list-o',
                                info: this.unreadNum
                            });
            this.getMessage();
            this.bindIfNessary();
            if (localStorage.userInfo != null) {
                this.getAccountInfo();
            }
            // this.routers.push({
            //                     name: '自动计时',
            //                     url: '/timetool',
            //                     icon: 'clock-o',
            //                     info: this.unreadNum
            //                 });
            this.getMessage();
            this.bindIfNessary();
            if (localStorage.userInfo != null) {
                this.getAccountInfo();
            }
        },
        components: {
            Footer
        },
        methods: {
            //获取账户信息
            getAccountInfo() {
                this.$axios.get('/user/loginByUserId', {params:{userId: this.user.id}})
                            .then(res => {
                                console.log(res);
                                if (res.code == 'error') {
                                    localStorage.errorMsg = res.msg;
                                    //账号信息有问题，不能使用。
                                    this.$router.push("/error");
                                } else {
                                    
                                    localStorage.userInfo = JSON.stringify(res.data);
                                    this.user = res.data;
                                } 
                            }).catch(err=> {
                                alert('err=' + err);
                            });
            },
            bindIfNessary() {
                let href = window.location.href;
                var requestUrl = "";
                if (this.isCorpWX && (this.user.corpwxUserid == null|| this.user.corpwxUserid == undefined || this.user.corpwxUserid == 'undefined')) {//优先检查企业微信环境
                    requestUrl = "/wxcorp/bindCorpWeiXin";
                } else if (this.isWX && (this.user.wxOpenid == null || this.user.wxOpenid == undefined || this.user.wxOpenid == 'undefined')) {
                    requestUrl = "/wechat/bindWeiXin";
                } 
                
                if (requestUrl.length > 0) {
                    // localStorage.openId = 'o1L3L5lOrOl3_UEJjONaoT2Rne1I';
                    //会自动跳转到首页
                    // let href = 'http://hq.tangusoft.com/?code=011Ptjgc2rx1eI09Irgc2Rvsgc2PtjgF&state=1#/index';
                    
                    if (href.includes("com/?code")) {  //url包括 com/?code 证明为从微信跳转回来的
                        var url = href; //vue自动在末尾加了 #/ 符号，截取去掉
                        var jingPosit = url.indexOf("com/") + 4; //获取域名结束的位置

                        // var urlLeft = url.substring(0, jingPosit);//url左侧部分
                        var urlRight = url.substring(jingPosit, url.length); //url右侧部分
                        console.log('urlRight=' + urlRight);
                        // window.location = urlLeft + "#/home" + urlRight;//拼接跳转
                        //获取code
                        var code = urlRight.substring('?code='.length,urlRight.indexOf('#/index'));
                        if (code.indexOf('&state=1') > 0) {
                            code = code.substring(0, code.indexOf('&state=1'));
                        }

                        //调用后台接口，注册用户
                        console.log('获取到code=='+code);

                        this.$axios.get(requestUrl, {params:{code:code, userId: this.user.id}})
                            .then(res => {
                                console.log(res);
                                if (res == null) {
                                    this.$toast.fail('绑定失败');
                                } else if(res.errcode != null) {
                                    //报错了
                                    console.log(res.errmsg);
                                } else {
                                    //获取openId
                                    if (res.data != null && ((this.isWX && res.data.wxOpenid != undefined)
                                                || (this.isCorpWX && res.data.corpwxUserid != undefined))) {
                                        localStorage.userInfo = JSON.stringify(res.data);
                                        console.log('绑定成功');
                                        this.user = res.data;
                                        window.location.href = '/#/my/center';
                                    }
                                }
                            }).catch(err=> {
                                alert('err=' + err);
                            });
                    }
                }
            },
            getGoods() {
                let url = "/goods"; // /api/goods
                this.$axios
                    .get(url)
                    .then(res => {
                    console.log("res", res);
                    })
                    .catch(err => {
                    console.log("err", err);
                    });
            },
            //获取消息
            getMessage() {
                this.$axios.post("/information/list", {
                })
                .then(res => {
                    if(res.code == "ok") {
                        var list = res.data;
                        this.unreadNum = list.filter(l=>l.checked==0).length;
                        console.log(this.unreadNum);
                        
                    } 
                }).catch(err=> {toast.clear();});
            },
        },
        activated(){
            this.getMessage()
        }
    };
</script>

<style lang="less" scoped>
    .swipe-img {
        img {
            width: 100%;
            height: 100%;
        }
    }

    .body {
        height: calc(100vh - 50px);
        position: relative;
    }

    .tip {
        position: absolute;
        width: 100%;
        bottom: 5px;
        font-size: 14px;
        color: #8f8f8f;
        margin-top: 20px;
        text-align: center;
        line-height: 20px;
    }
</style>
