define(['lightyear','multitabs', '../libs/Chart','form','bootstrap'], function (lightyear,multitabs, Chart,Form,bootstrap) {
    //Do setup work here
    var Index = {
        index: function () {
            if($('#loginform').length == 0){
                this.main();
            }else{
                this.login();
            }
        },
        main:function (){
            $("body").on('click', '[data-stopPropagation]', function (e) {
                e.stopPropagation();
            });

            // 滚动条
            const ps = new PerfectScrollbar('.lyear-layout-sidebar-scroll', {
                swipeEasing: false,
                suppressScrollX: true
            });

            // 侧边栏
            $(document).on('click', '.lyear-aside-toggler', function () {
                $('.lyear-layout-sidebar').toggleClass('lyear-aside-open');
                $("body").toggleClass('lyear-layout-sidebar-close');

                if ($('.lyear-mask-modal').length == 0) {
                    $('<div class="lyear-mask-modal"></div>').prependTo('body');
                } else {
                    $('.lyear-mask-modal').remove();
                }
            });

            // 遮罩层
            $(document).on('click', '.lyear-mask-modal', function () {
                $(this).remove();
                $('.lyear-layout-sidebar').toggleClass('lyear-aside-open');
                $('body').toggleClass('lyear-layout-sidebar-close');
            });

            // 侧边栏导航
            $(document).on('click', '.nav-item-has-subnav > a', function () {
                $subnavToggle = jQuery(this);
                $navHasSubnav = $subnavToggle.parent();
                $topHasSubNav = $subnavToggle.parents('.nav-item-has-subnav').last();
                $subnav = $navHasSubnav.find('.nav-subnav').first();
                $viSubHeight = $navHasSubnav.siblings().find('.nav-subnav:visible').outerHeight();
                $scrollBox = $('.lyear-layout-sidebar-scroll');
                $navHasSubnav.siblings().find('.nav-subnav:visible').slideUp(500).parent().removeClass('open');
                $subnav.slideToggle(300, function () {
                    $navHasSubnav.toggleClass('open');

                    // 新增滚动条处理
                    var scrollHeight = 0;
                    pervTotal = $topHasSubNav.prevAll().length,
                        boxHeight = $scrollBox.outerHeight(),
                        innerHeight = $('.sidebar-main').outerHeight(),
                        thisScroll = $scrollBox.scrollTop(),
                        thisSubHeight = $(this).outerHeight(),
                        footHeight = 121;

                    if (footHeight + innerHeight - boxHeight >= (pervTotal * 48)) {
                        scrollHeight = pervTotal * 48;
                    }
                    if ($subnavToggle.parents('.nav-item-has-subnav').length == 1) {
                        $scrollBox.animate({ scrollTop: scrollHeight }, 300);
                    } else {
                        // 子菜单操作
                        if (typeof ($viSubHeight) != 'undefined' && $viSubHeight != null) {
                            scrollHeight = thisScroll + thisSubHeight - $viSubHeight;
                            $scrollBox.animate({ scrollTop: scrollHeight }, 300);
                        } else {
                            if ((thisScroll + boxHeight - $scrollBox[0].scrollHeight) == 0) {
                                scrollHeight = thisScroll - thisSubHeight;
                                $scrollBox.animate({ scrollTop: scrollHeight }, 300);
                            }
                        }
                    }
                });
            });

            // 设置主题配色
            setTheme = function (input_name, data_name) {
                $("input[name='" + input_name + "']").click(function () {
                    $('body').attr(data_name, $(this).val());
                });
            }
            setTheme('logo_bg', 'data-logobg');
            setTheme('header_bg', 'data-headerbg');
            setTheme('sidebar_bg', 'data-sidebarbg');
            $(function(){
                // 选项卡
                $('#iframe-content').multitabs({
                    iframe: true,
                    nav: {
                        backgroundColor: '#ffffff',
                    },
                    init: [{
                        type: 'main',
                        title: '首页',
                        url: 'index/'+Config.fixedpage
                    }]
                });
                
            })

            $(document).on('click', '.nav-item .multitabs', function () {
                $('.nav-item').removeClass('active');
                $('.nav-subnav li').removeClass('active');
                $(this).parents('li').addClass('active');
                $(this).parents('.nav-item-has-subnav').addClass('open').first().addClass('active');
            });
        
            $(document).on('click', '.btn-logout', function (e) {
                $.getJSON($(this).data('url'),function(res){
                    if(res.code === 0){
                        lightyear.notify('即将跳转登录页面~', 'success', 2000, 'mdi mdi-emoticon-happy', 'top', 'center')
                        if(res.data.url){
                            setTimeout(function(){
                                location.href = res.data.url;
                            },2000);
                        }
                    }else{
                        return lightyear.notify(res.msg, 'danger', 3000, 'mdi mdi-emoticon-happy', 'top', 'center');
                    }
                });
            });
        },
        login: function () {
            var form = $('form');
            Form.api.bindevent(form,function (res) {
                if (res.code) {
                    $('.codeImage').trigger('click');
                    lightyear.notify(res.msg, 'danger', 3000, 'mdi mdi-emoticon-happy', 'top', 'center');
                    return false;
                }
                lightyear.notify('登录成功，页面即将自动跳转~', 'success', 2000, 'mdi mdi-emoticon-happy', 'top', 'center')
                setTimeout(function () {
                    location.reload();
                }, 2000);
                return false;
            });
        },
        password: function () {
            var form = $('form');
            console.log(Form)
            Form.api.bindevent(form,function (res) {
                if (res.code) {
                    $('.codeImage').trigger('click');
                    return lightyear.notify(res.msg, 'danger', 3000, 'mdi mdi-emoticon-happy', 'top', 'center');
                }
                lightyear.notify('修改成功', 'success', 2000, 'mdi mdi-emoticon-happy', 'top', 'center')
                setTimeout(function () {
                    location.reload();
                }, 2000);
                return true;
            });
        },
        profile: function () {
            var form = $('form');
            Form.api.bindevent(form,function (res) {
                if (res.code) {
                    return lightyear.notify(res.msg, 'danger', 3000, 'mdi mdi-emoticon-happy', 'top', 'center');
                }
                lightyear.notify('修改成功', 'success', 2000, 'mdi mdi-emoticon-happy', 'top', 'center')
                return true;
            });
        },
        dashboard: function () {
            // new Chart(
            //     document.getElementsByClassName('js-chartjs-bars'),
            //     {
            //         type: 'bar',
            //         data: {
            //             labels: recharge_labels,
            //             datasets: [
            //                 {
            //                     label: '充值统计',
            //                     data: recharge_values,
            //                     borderColor: '#358ed7',
            //                     backgroundColor: 'rgba(53, 142, 215, 0.175)',
            //                     borderWidth: 1,
            //                     fill: false,
            //                     lineTension: 0.5
            //                 },
            //             ]
            //         }
            //     }
            // );
            // new Chart(
            //     document.getElementsByClassName('js-chartjs-lines'),
            //     {
            //         type: 'bar',//line
            //         data: {
            //             labels: withdrawl_labels,
            //             datasets: [
            //                 {
            //                     label: '提现统计',
            //                     data: withdrawl_values,
            //                     borderColor: '#358ed7',
            //                     backgroundColor: 'rgba(53, 142, 215, 0.175)',
            //                     borderWidth: 1,
            //                     fill: false,
            //                     lineTension: 0.5
            //                 }
            //             ]
            //         }
            //     }
            // );
            $.getJSON('index/money_lines',function(res){
                res = res.data;
                var labels=[];
                var withdrawl_values=[];
                var recharge_values=[];
                for (let date in res) {
                    labels.push(date);
                    recharge_values.push(res[date].recharge);
                    withdrawl_values.push(res[date].withdrawl);
                }

                new Chart(
                    document.getElementsByClassName('js-money-chartjs'),
                    {
                        type: 'line',
                        data: {
                            labels: labels,
                            datasets: [
                                {
                                    label: '充值统计',
                                    data: recharge_values,
                                    borderColor: 'rgba(33, 111, 5, 0.46)',
                                    backgroundColor: 'rgba(33, 111, 5, 0.46)',
                                    borderWidth: 2,
                                    fill: false,
                                    lineTension: 0.5
                                },
                                {
                                    label: '提现统计',
                                    data: withdrawl_values,
                                    borderColor: 'rgba(161, 72, 16, 0.81)',
                                    backgroundColor: 'rgba(161, 72, 16, 0.81)',
                                    borderWidth: 2,
                                    fill: false,
                                    lineTension: 0.5
                                }
                            ]
                        }
                    }
                );
            });
            $.getJSON('index/role_buy_lines',function(res){
                res = res.data;
                var amount=[];
                var reward=[];
                var residual=[];
                var labels=[];
                for (let date in res) {
                    labels.push(date);
                    amount.push(res[date].amount);
                    reward.push(res[date].reward);
                    residual.push(res[date].residual);
                }
                new Chart(document.getElementsByClassName('js-role_buy_lines'),{
                    type: 'line',
                    data: {
                        labels: labels,
                        datasets: [
                            {
                                label: '总购买金额',
                                data: amount,
                                borderColor: 'rgba(43, 191, 232, 0.7)',
                                backgroundColor: 'rgba(43, 191, 232, 0.7)',
                                borderWidth: 2,
                                fill: false,
                                lineTension: 0.5
                            },
                            {
                                label: '奖励统计',
                                data: reward,
                                borderColor: 'rgba(166, 53, 215, 0.7)',
                                backgroundColor: 'rgba(166, 53, 215, 0.7)',
                                borderWidth: 2,
                                fill: false,
                                lineTension: 0.5
                            },
                            {
                                label: '沉淀统计',
                                data: residual,
                                borderColor: 'rgba(59, 199, 8, 0.7)',
                                backgroundColor: 'rgba(59, 199, 8, 0.7)',
                                borderWidth: 2,
                                fill: false,
                                lineTension: 0.5
                            }
                        ]
                    }
                });
            });
        },
        reward: function () {
            var form = $('form');
            Form.api.bindevent(form)
        }
    }
    return Index
});