$(window).scroll(function (event) {

})

// function browserRedirect() {
//     var sUserAgent = navigator.userAgent.toLowerCase();
//     if (/ipad|iphone|midp|rv:1.2.3.4|ucweb|android|windows ce|windows mobile/.test(sUserAgent)) {
//     //跳转移动端页面
//     window.location.href="http://f.jcngame.com/fanfan20171208/mobile/index.html" rel="external nofollow" rel="external nofollow" ;
//     } else {
//     //跳转pc端页面
//     window.location.href="http://f.jcngame.com/fanfan20171208//fanmai/index.html" rel="external nofollow" rel="external nofollow" ;
//     }
//    }
//     browserRedirect(); 

$(function () {

    $('.nav_btn').click(function () {
        if ($('.nav_btn').attr('class') == 'nav_btn cur') {
            $('.nav_btn').removeClass('cur');
            $('.nav').removeClass('active');
            $('body').css('overflow', 'inherit');
        } else {
            $('.nav_btn').addClass('cur');
            $('.nav').addClass('active');
            $('body').css('overflow', 'hidden');
        }
    });


    $('.ewm_tc').click(function () {
        $(this).fadeOut();
    })

    if ($(window).width() <= 768) {


    } else {

    }


    $(".gotop").click(function () {
        var anh = $("body").offset().top;
        $("html,body").stop().animate({
            scrollTop: anh
        }, 1000);
        scrollAnh = 0;
        scrollNum = 0;
    });


});

$(function () {
    //下载弹出层
    $(".click-download").click(function () {
        $(".download").show();
    });
    $(".download .close").click(function () {
        $(".download").hide();
    });
    //历史记录弹出层//文件创建
    // $(".click-history,.establish,.more-edit").click(function() {
    //     $(".history").show();
    // });
    $(".history .close,.field-btn").click(function () {
        $(".history").hide();
    });


});

// 鼠标右键事件
$(document).contextmenu(function (e) {
    e.preventDefault() // 阻止右键菜单默认行为
    //判断是否能下载
    if ($(e.target).closest('li').attr('class') == 'list click-download') {
        console.log($(e.target).closest('li').attr('data-id'))
    }
})

//判断是否在钉钉内
if (dd.env.platform !== "notInDingTalk") {
    console.log('进入钉钉了');
    // var host = 'http://192.168.0.184:12306'
    // var host = 'http://59.110.159.206:12306';
    var host = 'http://101.200.157.76:12306';

    var corpId = "ding2bc0c10b680300a3f2c783f7214b6d69"; //企业ID测试环境
    // var corpId = 'dingdae7689518623a2c35c2f4657eb6378f';
    var agentId = "1130713327"; //微应用ID测试环境
    // var agentId = "1129209616";

    //var host = 'http://59.110.159.206:12306'
    //var host = 'http://39.102.69.8:12306'
    //var corpId = "ding1aaa81bc5ac203a4f2c783f7214b6d69"; //企业ID生产环境
    // var agentId = "1129479435"; //微应用ID生产环境

    var preUrl = ""; //当前页面地址
    var expiresIn = localStorage['expiresIn'] || 0; //过期时间
    var userId = localStorage['userId'] || ''; //用户ID
    var userName = localStorage['userName'] || ''; //用户姓名
    var roleId = localStorage['roleId'] || ''; //用户权限
    var userAvatar = localStorage['userAvatar'] || ''; //用户头像
    var accessToken = ''; //localStorage['accessToken'] || ''; //token
    var currentTime = Math.round(new Date() / 1000); //当前时间

    var dueDate = localStorage['dueDate'];
    var haveLogin = false; //= localStorage['haveLogin'] || false;  //是否登录

    $(window).on("load", function () {
        login();
        var loginIntervalScript = setInterval(function () {
            var tmpHaveLogin = localStorage['haveLogin'] || false;
            if (tmpHaveLogin) {
                haveLogin = tmpHaveLogin;
                accessToken = localStorage['accessToken'] || '';
                clearInterval(loginIntervalScript);
                getNoticeList(1, '', '')
                getNoticeList(2, '', '')
            }
        }, 500);
    })

    //文件预览
    function previewBtn(obj) {
        dd.ready(function () {
            var dingFileId = $(obj).attr('dingfileid');
            var dingFileName = $(obj).attr('dingfilename');
            var dingFileSize = $(obj).attr('dingfilesize');
            var dingSpaceId = $(obj).attr('dingspaceid');
            var fileType = $(obj).attr('filetype');
            dd.biz.cspace.preview({
                "corpId": corpId,
                "spaceId": dingSpaceId,
                "fileId": dingFileId,
                "fileName": dingFileName,
                "fileSize": dingFileSize,
                "fileType": fileType,
                "onSuccess": function () {
                    console.log('ok');
                },
                "onFail": function (err) {
                    console.log(err);
                }
            });
        });
    }

    //获取授权码登录
    function login() {
        console.log('进入');
        dd.ready(function () {
            //获取免登授权码
            dd.runtime.permission.requestAuthCode({
                corpId: corpId,
                onSuccess: function (result) {
                    //获取用户getUserid 需要code
                    $.ajax({
                        url: host + '/auth/token',
                        headers: {
                            'Authorization': 'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
                        },
                        data: {
                            code: result.code
                            // pre_url:preUrl
                        },
                        async: false,
                        type: "post",
                        dataType: "json",
                        success: function (data) {
                            if (data.code == 200) {
                                var userId = localStorage['userId'];
                                if ((userId != '' || userId != undefined) && userId != data.data.userId) {
                                    location.reload();
                                }
                                accessToken = data.data.accessToken;
                                expiresIn = data.data.expiresIn;
                                userId = data.data.userId;
                                userName = data.data.name;
                                userAvatar = data.data.avatar;
                                localStorage['accessToken'] = data.data.accessToken
                                localStorage['expiresIn'] = data.data.expiresIn
                                localStorage['userId'] = data.data.userId
                                localStorage['userName'] = data.data.name
                                localStorage['userAvatar'] = data.data.avatar
                                localStorage['dueDate'] = parseInt(currentTime) + parseInt(data.data.expiresIn);
                                userInfo()
                            } else {
                                layer.msg(data.msg + 'token 失败')
                            }
                        },
                        error: function (data) {
                            console.log(data, "");
                        }
                    });
                },
                onFail: function (err) {
                    console.log(err)
                }
            });
        })
    }

    getJspiOauth()
    //jsapi鉴权
    function getJspiOauth() {
        $.ajax({
            url: host + '/auth/getJspiOauth',
            // headers: {
            //     'Blade-Auth': 'bearer ' + accessToken,
            // },
            data: {
                url: window.location.href
            },
            type: "get",
            dataType: "json",
            success: function (data) {
                if (data.code == 200) {
                    dd.config({
                        agentId: data.data.agentId,
                        corpId: data.data.corpId,
                        timeStamp: data.data.timeStamp.toString(),
                        nonceStr: data.data.nonceStr,
                        signature: data.data.signature,
                        type: 0,
                        jsApiList: [
                            'runtime.info',
                            'biz.contact.complexPicker',
                            'biz.contact.departmentsPicker',
                            'biz.cspace.preview',
                            // 'biz.contact.choose'
                        ]
                    });
                } else {
                    layer.msg(data.msg)
                }
            },
            error: function (data) {
                console.log(data);
            }
        });
    }

    //用户信息
    function userInfo() {
        if (roleId == '') {
            $.ajax({
                url: host + '/ding/user/getUserInfo',
                headers: {
                    'Blade-Auth': 'bearer ' + accessToken,
                },
                data: {},
                type: "get",
                dataType: "json",
                success: function (data) {
                    if (data.code == 200) {
                        localStorage['roleId'] = data.data.roleId
                        localStorage['haveLogin'] = true;
                        $('.head-name .name').text(data.data.username)
                        var tmpAvatar = localStorage['userAvatar'];
                        if (tmpAvatar != '') {
                            $('.head-name .img img').attr('src', localStorage['userAvatar'])
                        }
                    } else {
                        layer.msg(data.msg + 'get user info')
                    }
                },
                error: function (data) {
                    console.log(data);
                }
            });
        } else {
            var tmpAvatar = localStorage['userAvatar'];
            var tmpUserName = localStorage['userName'];
            if (tmpAvatar != '') {
                $('.head-name .img img').attr('src', userAvatar)
            }
            if (tmpUserName != '') {
                $('.head-name .name').text(userName)
            }
        }
    }

    $(function () {
        //getNoticeList(1, '', '')
        //getNoticeList(2, '', '')
        $(".head-news-icon").click(function () {
            $(".head-drop-down").show();
            getNoticeList(1, '', '')
            getNoticeList(2, '', '')
            return false; //关键是这里，阻止冒泡
        });
        $(".head-drop-down").click(function () {
            return false;
        });
        $(".down-ul li").eq(0).addClass("active");
        $(".down-ul li").click(function () {
            $(this).addClass("active").siblings().removeClass("active");
            $(".down-con>div").eq($(this).index()).stop(true).show().siblings().stop(true).hide();
        })
    })
    $(document).click(function () {
        $(".head-drop-down").hide();
    });

    var noticeNum = 0
    //获取通知
    function getNoticeList(type, time1, time2) {
        $.ajax({
            url: host + '/ding/notice/getNoticeList',
            headers: {
                'Blade-Auth': 'bearer ' + accessToken,
            },
            data: {
                current: 1,
                size: 3,
                type: type,
                startTime: time1,
                endTime: time2
            },
            type: "get",
            dataType: "json",
            success: function (data) {
                if (data.code == 200) {
                    $('.head-drop-down .list1').empty()
                    $('.head-drop-down .list2').empty()
                    if (data.data.records.length > 0) {
                        if (type == 1) {
                            $(".down-ul li").eq(0).find('i').text(data.data.records.length)
                        } else {
                            $(".down-ul li").eq(1).find('i').text(data.data.records.length)
                        }
                        data.data.records.forEach(function (item) {
                            noticeNum++
                            var html = ''
                            if (type == 1) {
                                html = '<li>' +
                                    '<a href="#">' +
                                    '<img src="image/icon_03.png">' +
                                    '<p class="font-14 text-one-hide color-b">' + item.content +
                                    '</p>' +
                                    '<span class="color-g6 text-one-hide">' + item.createTime +
                                    '</span>' +
                                    '</a>' +
                                    '</li>'
                                $('.head-drop-down .list1').append(html)
                            } else {
                                html = '<li>' +
                                    '<a href="#">' +
                                    '<img src="image/icon_04.png">' +
                                    '<p class="font-14 text-one-hide color-b">' + item.content +
                                    '</p>' +
                                    '<span class="color-g6 text-one-hide">' + item.createTime +
                                    '</span>' +
                                    '</a>' +
                                    '</li>'
                                $('.head-drop-down .list2').append(html)
                            }
                        })
                        $('.head-news .head-news-icon span').addClass('active')
                        $('.head-news .head-news-icon span').text(noticeNum)
                    }
                } else {
                    layer.msg(data.msg)
                }
            },
            error: function (data) {
                console.log(data);
            }
        });
    }
} else {
    var host = '/api'
}

window.MobileLib = {
    isMobile: function (block) {
        if (window.innerWidth < 500) {
            block && block()
            return true
        } else {
            return false
        }
    },
    modal: function (opts) {
        return this.isMobile(function () {
            layer.open({
                type: 1,
                title: [opts.title || '', 'font-size:18px;padding:0;text-align:center'],
                content: opts.body || null,
                offset: opts.offset || [0, 0],
                area: opts.area || ['100%', '100%'],
                closeBtn: 1,
                isOutAnim: false,
                anim: opts.animate || -1,
                skin: opts.customClass || null,
                success: function (dom, index) {
                    opts.success && opts.success(dom, index)
                }
            })
        })
    },
    actionSheet: function (opts) {
        opts.animate = 2
        opts.area = ['100%', '40%']
        opts.offset = ['60%', 0]
        opts.title = false
        return this.modal(opts)
    }
}

//自定义请求方法
function dxtcRequest(type, url, data, successFunction, contentType = 'application/x-www-form-urlencoded') {
    console.log(contentType);
    $.ajax({
        url: url,
        headers: {
            'Blade-Auth': 'bearer ' + localStorage.accessToken
        },
        data: data,
        type: type,
        contentType: contentType,
        dataType: "json",
        success: successFunction,
        error: function (data) {
            console.log(data);
            layer.msg(data.responseJSON.msg);
        }
    });
}


// 筛选

function screening(arr, num) {
    console.log(arr);
    for (let index = 0; index < arr.length; index++) {
      
        if (arr[index].id == num) {
            return index
        }

    }
}