/**
 * Created by admin on 2018/10/8.
 */
/*时间戳转时间段*/
var formatDuring =  function (mss) {
    var now = new Date(mss);
    var year = now.getFullYear();
    var month = now.getMonth();
    var date = now.getDate();
    var hour = now.getHours();
    var minu = now.getMinutes();

    var year = parseInt(mss / (1000 * 60 * 60 * 24*365));
    var mouth = parseInt(mss % (1000 * 60 * 60 * 24*365) / (1000 * 60 * 60 * 24*30));
    var days = parseInt(mss % (1000 * 60 * 60 * 24*365) % (1000 * 60 * 60 * 24*30) / (1000 * 60 * 60 * 24));
    var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = (mss % (1000 * 60)) / 1000;
    if(mss<(1000*60*60)){
        return minutes + "m";
    } else if(mss<(1000 * 60 * 60 * 24)){//时分
        return hours + "h" + minutes + "min";
    } else if(mss>=(1000 * 60 * 60 * 24) && mss<(1000 * 60 * 60 * 24*30)){//日
        return days + "d";
    } else if(mss>=(1000 * 60 * 60 * 24 * 30) && mss<(1000 * 60 * 60 * 24*365)){//月日
        return mouth + "m" + days + "d";
    } else if(mss>=(1000 * 60 * 60 * 24 * 365)){//年月日
        if(mouth==0){
            if (days == 0){
                return year + "y"
            }
            return year + "y" + days + "d";
        } else if (days == 0){
            return year + "y" + mouth + "m"
        }
        else{
            return year + "y" + mouth + "m" + days + "d";
        }
    }
}
/*时间转时间戳*/
var formatTime = function(time){
    time = time.substring(0,19);
    time = time.replace(/-/g,'/');
    var timestamp = new Date(time).getTime();
    return timestamp;
}
var axis_all = $(".slider-outer").width();
var slider = $(".slider-content-center").width()
$(".slider-outer").css("width",axis_all);
$(".slider-content-prev").css("left",axis_all - slider-32-35);
$(".slider-content-center").css("left",axis_all - slider-16-35);
$(".slider-content-next").css("left",axis_all -16-35);
var middleTimeWidth;
$(function(){
    var canMove = false;
    var leftResize = false;
    var rightResize = false;
    var thisX,prevLeft,nextLeft;
    var thisid = "";
    /*中间按下*/
    $(".slider-content-center").mousedown(function (e) {
        thisid = $("#"+$(this).parent().parent().find("div.flowchart").attr("id")).parent().parent();

        $(this).parent().parent().find("input.flowchart").val($(this).parent().parent().find("div.flowchart").attr("id"));

        canMove = true;
        thisX = e.pageX;
        prevLeft = thisid.find(".slider-content-prev").offset().left;
        nextLeft = thisid.find(".slider-content-next").offset().left;
        slider = thisid.find(".slider-content-center").width();
        var url = $(this).parent().parent().find('.links').data('url');
        var switch_num = $(this).parent().parent().find('.links').data('port');
        thisid.find(".flowchartUrl").val(url);
        thisid.find(".flowchartSwitch").val(switch_num);
        getDate(url,switch_num,"","","")
    });
    /*左侧按下*/
    $(".slider-content-prev").mousedown(function (e) {
        thisid = $("#"+$(this).parent().parent().find("div.flowchart").attr("id")).parent().parent();
        thisid.find("input.flowchart").val($(this).parent().parent().find("div.flowchart").attr("id"));
        $("body").css("cursor","e-resize");
        leftResize = true;
        thisX = e.pageX;
        slider = thisid.find(".slider-content-center").width();
        prevLeft = thisid.find(".slider-content-prev").offset().left;
        nextLeft = thisid.find(".slider-content-next").offset().left;

        var url = $(this).parent().parent().find('.links').data('url');
        var switch_num = $(this).parent().parent().find('.links').data('port');
        thisid.find(".flowchartUrl").val(url);
        thisid.find(".flowchartSwitch").val(switch_num);

    });
    /*右侧按下*/
    $(".slider-content-next").mousedown(function (e) {
        thisid = $("#"+$(this).parent().parent().find("div.flowchart").attr("id")).parent().parent();
        thisid.find("input.flowchart").val($(this).parent().parent().find("div.flowchart").attr("id"));
        $("body").css("cursor","e-resize");
        rightResize = true;
        thisX = e.pageX;
        slider = thisid.find(".slider-content-center").width();
        prevLeft = thisid.find(".slider-content-prev").offset().left;
        nextLeft = thisid.find(".slider-content-next").offset().left;

        var url = $(this).parent().parent().find('.links').data('url');
        var switch_num = $(this).parent().parent().find('.links').data('port');
        thisid.find(".flowchartUrl").val(url);
        thisid.find(".flowchartSwitch").val(switch_num);
    });
    /*鼠标滑过中间*/
    $(".slider-content-center").mouseover(function(){
        $(this).parent().parent().find(".slider-content-prev").css("background-color","#e8f5ff");
        $(this).parent().parent().find(".slider-content-center").css("background-color","#e8f5ff");
        $(this).parent().parent().find(".slider-content-next").css("background-color","#e8f5ff");
    });
    $(".slider-content-center").mouseout(function(){
        $(this).parent().parent().find(".slider-content-prev").css("background-color","#fff");
        $(this).parent().parent().find(".slider-content-center").css("background-color","#fff");
        $(this).parent().parent().find(".slider-content-next").css("background-color","#fff")
    });
    $("body").mousemove(function (e) {
        if($(".slider-outer").length>0 && thisid !=""){
            var allX = $(".slider-outer").offset().left;
            var sliderX = thisid.find(".slider-content-center").offset().left;
            var mouseX = e.pageX;
            var leftPosition;
            /*鼠标滑动*/
            if(canMove){
                leftPosition = mouseX - allX -(thisX-prevLeft );
                if(leftPosition < 35){
                    leftPosition = 35
                } if(leftPosition >= axis_all - slider-32-35){
                    leftPosition = axis_all - slider-32-35
                }
                thisid.find(".slider-content-prev").css("left",leftPosition);
                thisid.find(".slider-content-center").css("left",leftPosition+16);
                thisid.find(".slider-content-next").css("left",leftPosition+16+slider);

                middleTimeWidth = thisid.find(".slider-content-center").width();
                getSelectTime();
            }

            /*左侧拉伸*/
            if(leftResize){
                leftPosition = mouseX - allX;//鼠标移动位置距离左边，
                var closePrev = thisX - prevLeft - allX+35;//点击位置-左边边缘位置
                var thisleft = leftPosition-closePrev;

                if($('.rrd_flowchart').length>0){
                    if((leftPosition-closePrev-35)<=0){
                        thisleft = 35
                    }
                    if((leftPosition-closePrev+16+35)<=nextLeft){
                        thisid.find(".slider-content-prev").css("left",thisleft);
                        thisid.find(".slider-content-center").css("left",thisleft+16);
                        thisid.find(".slider-content-center").css("width",nextLeft-(thisleft+14+35));
                    }
                } else {
                    if((leftPosition-closePrev-35-16)<=0){
                        thisleft = 35+16
                    }
                    if((leftPosition-closePrev+16+35)<=nextLeft){
                        thisid.find(".slider-content-prev").css("left",thisleft-16);
                        thisid.find(".slider-content-center").css("left",thisleft);
                        thisid.find(".slider-content-center").css("width",nextLeft-(thisleft+15+35));
                    }
                }

                middleTimeWidth = thisid.find(".slider-content-center").width();
                if(middleTimeWidth == 0){
                    getSelectTime(5*60000);
                } else {
                    getSelectTime();
                }
            }
            /*右侧拉伸*/
            if(rightResize){
                leftPosition = mouseX - allX;
                var closePrev = thisX - nextLeft - allX+35;
                var thisleft = leftPosition-closePrev;

                if($('.rrd_flowchart').length>0){
                    if(thisleft>=axis_all-35-16){
                        thisleft = axis_all-35-16
                    }
                    if(thisleft>=prevLeft-22){
                        thisid.find(".slider-content-next").css("left",thisleft);
                        thisid.find(".slider-content-center").css("width",thisleft-sliderX+38);
                    }
                } else{
                    if(thisleft>=axis_all-35){
                        thisleft = axis_all-35
                    }
                    if(thisleft>=prevLeft-22){
                        thisid.find(".slider-content-next").css("left",thisleft-16);
                        thisid.find(".slider-content-center").css("width",thisleft-sliderX+38);
                    }
                }

                middleTimeWidth = thisid.find(".slider-content-center").width();
                if(middleTimeWidth == 0){
                    getSelectTime(5*60000);
                } else {
                    getSelectTime();
                }
            }
        }
    });
    /*鼠标弹起*/
    $("body").mouseup(function () {
        if(canMove){
            canMove = false;
            getDate(thisid.find(".flowchartUrl").val(),thisid.find(".flowchartSwitch").val(),parseInt(thisid.find(".thisStart").val()),parseInt(thisid.find(".thisEnd").val()),true,thisid.find("input.flowchart").val())
        }

        if(leftResize){
            var mouseupWidth = thisid.find(".slider-content-center").width();
            var mouseupleft = thisid.find(".slider-content-next").offset().left-thisid.find(".slider-outer").offset().left;
            if(mouseupWidth<10){
                thisid.find(".slider-content-center").css("width",10+"px");
                thisid.find(".slider-content-prev").css("left",mouseupleft-10-16);
                thisid.find(".slider-content-center").css("left",mouseupleft-10)
            }
            getDate(thisid.find(".flowchartUrl").val(),thisid.find(".flowchartSwitch").val(),parseInt(thisid.find(".thisStart").val()),parseInt(thisid.find(".thisEnd").val()),true,thisid.find("input.flowchart").val())
            leftResize = false;
        }
        if(rightResize){
            var mouseupWidth = thisid.find(".slider-content-center").width();
            var mouseupleft = thisid.find(".slider-content-prev").offset().left-thisid.find(".slider-outer").offset().left;
            if(mouseupWidth<10){
                thisid.find(".slider-content-center").css("width",10+"px");
                thisid.find(".slider-content-next").css("left",mouseupleft+10+16);
            }
            getDate(thisid.find(".flowchartUrl").val(),thisid.find(".flowchartSwitch").val(),parseInt(thisid.find(".thisStart").val()),parseInt(thisid.find(".thisEnd").val()),true,thisid.find("input.flowchart").val())
            rightResize = false;
        }

        $("body").css("cursor","default");
    });
    document.addEventListener('mouseup', function(e) {
        if(e.clientY > window.innerWidth || e.clientY<0 || e.clientX<0 ||e.clientX>window.innerHeight){
            if(canMove){
                canMove = false;
                getDate(thisid.find(".flowchartUrl").val(),thisid.find(".flowchartSwitch").val(),parseInt(thisid.find(".thisStart").val()),parseInt(thisid.find(".thisEnd").val()),true,thisid.find("input.flowchart").val())
            }
            if(leftResize){
                var mouseupWidth = thisid.find(".slider-content-center").width();
                var mouseupleft = thisid.find(".slider-content-next").offset().left-thisid.find(".slider-outer").offset().left;
                if(mouseupWidth<10){
                    thisid.find(".slider-content-center").css("width",10+"px");
                    thisid.find(".slider-content-prev").css("left",mouseupleft-10-16);
                    thisid.find(".slider-content-center").css("left",mouseupleft-10)
                }
                getDate(thisid.find(".flowchartUrl").val(),thisid.find(".flowchartSwitch").val(),parseInt(thisid.find(".thisStart").val()),parseInt(thisid.find(".thisEnd").val()),true,thisid.find("input.flowchart").val())
                leftResize = false;
            }
            if(rightResize){
                var mouseupWidth = thisid.find(".slider-content-center").width();
                var mouseupleft =thisid.find(".slider-content-prev").offset().left-thisid.find(".slider-outer").offset().left;
                if(mouseupWidth<10){
                    thisid.find(".slider-content-center").css("width",10+"px");
                    thisid.find(".slider-content-next").css("left",mouseupleft+10+16);
                }
                getDate(thisid.find(".flowchartUrl").val(),thisid.find(".flowchartSwitch").val(),parseInt(thisid.find(".thisStart").val()),parseInt(thisid.find(".thisEnd").val()),true,thisid.find("input.flowchart").val())
                rightResize = false;
            }
            $("body").css("cursor","default");
        }
    })
    /*点击日期获取id*/
    var canCheckDate=false;
    $('.jeindiv').click(function () {
        canCheckDate = true;
        var url = $(this).parent().parent().parent().parent().find('.links').data('url');
        var switch_num = $(this).parent().parent().parent().parent().find('.links').data('port');
        $(".flowchartUrl").val(url);
        $(".flowchartSwitch").val(switch_num);
        $("input.flowchart").val($(this).parent().parent().parent().parent().find("div.flowchart").attr("id"));
    });
    $(".jeindiv1").click(function(){
        if(thisid==""){
            thisid = $("#"+$(this).parent().parent().parent().parent().find("div.flowchart").attr("id")).parent().parent();
        }
        var result=$(this).html().split("  ")[1];
        var hour =  result.split(":")[0];
        var minute = result.split(":")[1];
        $(".hour").val(hour);
        $(".minute").val(minute);
        $(this).parent().find($(".thisSeletDate").val($(this).attr("class")))
    })
    $(".jeindiv2").click(function(){
        if(thisid==""){
            thisid = $("#"+$(this).parent().parent().parent().parent().find("div.flowchart").attr("id")).parent().parent();
        }
        var result=$(this).html().split("  ")[1];
        var hour =  result.split(":")[0];
        var minute = result.split(":")[1];
        $(".hour").val(hour);
        $(".minute").val(minute);
        $(this).parent().find($(".thisSeletDate").val($(this).attr("class")))
    })
    /*监听日期插件改变*/
    var canChange=
    $('.jeindiv1').on('DOMNodeInserted',function(){
        if(canCheckDate){
            canCheckDate = false;
            var txtStartTime = formatTime(thisid.find('.jeindiv1').text());
            var txtEndTime = formatTime(thisid.find('.jeindiv2').text());

            if(txtStartTime>=parseInt(thisid.find(".flowchartEnd").val())) {
                txtStartTime = parseInt(thisid.find(".flowchartStart").val());
                $('.jeindiv1').html(formatDateTime(parseInt(thisid.find(".flowchartStart").val()),"yyddmmhms"))
                getDate(thisid.find(".flowchartUrl").val(),thisid.find(".flowchartSwitch").val(),parseInt(thisid.find(".flowchartStart").val()),txtEndTime,true,thisid.find("input.flowchart").val());
                thisid.find(".selectTime").html(formatDuring(txtEndTime-parseInt(thisid.find(".flowchartStart").val())));

            } else {
                if(txtStartTime<parseInt(thisid.find(".flowchartStart").val())) {
                    txtStartTime = parseInt(thisid.find(".flowchartStart").val());
                    $('.jeindiv1').html(formatDateTime(txtStartTime,"yyddmmhms"));
                    getDate(thisid.find(".flowchartUrl").val(),thisid.find(".flowchartSwitch").val(),parseInt(thisid.find(".flowchartStart").val()),txtEndTime,true,thisid.find("input.flowchart").val());
                    thisid.find(".selectTime").html(formatDuring(txtEndTime-parseInt(thisid.find(".flowchartStart").val())));

                } else if(txtStartTime<txtEndTime){
                    getDate(thisid.find(".flowchartUrl").val(),thisid.find(".flowchartSwitch").val(),txtStartTime,txtEndTime,true,thisid.find("input.flowchart").val());
                    thisid.find(".selectTime").html(formatDuring(txtEndTime-txtStartTime));
                }
            }
        }
    });
    $('.jeindiv2').on('DOMNodeInserted',function(){
        if(canCheckDate){
            canCheckDate = false;

            var txtStartTime = formatTime($('.jeindiv1').text());
            var txtEndTime = formatTime($('.jeindiv2').text());

            if(txtEndTime>=parseInt($(".flowchartEnd").val())) {
                txtEndTime = parseInt($(".flowchartEnd").val());
                $('.jeindiv2').html(formatDateTime(parseInt(thisid.find(".flowchartEnd").val()),"yyddmmhms"))
            } else {
                if(txtEndTime<=parseInt($(".flowchartStart").val()) || txtEndTime<=txtStartTime) {
                    txtEndTime = parseInt($(".flowchartEnd").val());
                    $('.jeindiv2').html(formatDateTime(parseInt(thisid.find(".flowchartEnd").val()),"yyddmmhms"))
                }
                else {
                    getDate(thisid.find(".flowchartUrl").val(),thisid.find(".flowchartSwitch").val(),txtStartTime,txtEndTime,true,thisid.find("input.flowchart").val())
                    thisid.find(".selectTime").html(formatDuring(txtEndTime-txtStartTime));
                }
            }
        }

    })
    function prev(obj){
        $("input.flowchart").val($(obj).parent().parent().find("div.flowchart").attr("id"));
        var sliderWidth = thisid.find(".slider-content-prev").width()+thisid.find(".slider-content-center").width()+thisid.find(".slider-content-next").width();
        var offsetPrev = thisid.find(".slider-content-prev").offset().left-$(".slider-outer").offset().left;
        var offsetCenter = thisid.find(".slider-content-center").offset().left-$(".slider-outer").offset().left;
        var offsetNext = thisid.find(".slider-content-next").offset().left-$(".slider-outer").offset().left;
        if(offsetPrev-sliderWidth>35){
            thisid.find(".slider-content-prev").css("left",offsetPrev-sliderWidth);
            thisid.find(".slider-content-center").css("left",offsetCenter-sliderWidth);
            thisid.find(".slider-content-next").css("left",offsetNext-sliderWidth);
        } else {
            thisid.find(".slider-content-prev").css("left",35);
            thisid.find(".slider-content-center").css("left",35+16);
            thisid.find(".slider-content-next").css("left",35+sliderWidth-16);
        }
        getSelectTime();
        $("input.flowchart").val($(obj).parent().parent().find("div.flowchart").attr("id"));
        var url = $(obj).parent().parent().find('.links').data('url');
        var switch_num = $(obj).parent().parent().find('.links').data('port');
        $(".flowchartUrl").val(url);
        $(".flowchartSwitch").val(switch_num);
        getDate(thisid.find(".flowchartUrl").val(),thisid.find(".flowchartSwitch").val(),parseInt(thisid.find(".thisStart").val()),parseInt(thisid.find(".thisEnd").val()),true,thisid.find("input.flowchart").val())

    }
    function next(obj){
        $("input.flowchart").val($(obj).parent().parent().find("div.flowchart").attr("id"));
        var sliderWidth = thisid.find(".slider-content-prev").width()+thisid.find(".slider-content-center").width()+thisid.find(".slider-content-next").width();
        var offsetPrev = thisid.find(".slider-content-prev").offset().left-$(".slider-outer").offset().left;
        var offsetCenter = thisid.find(".slider-content-center").offset().left-$(".slider-outer").offset().left;
        var offsetNext = thisid.find(".slider-content-next").offset().left-$(".slider-outer").offset().left;
        if(offsetNext<(axis_all -16-35-sliderWidth)){
            thisid.find(".slider-content-prev").css("left",offsetPrev+sliderWidth);
            thisid.find(".slider-content-center").css("left",offsetCenter+sliderWidth);
            thisid.find(".slider-content-next").css("left",offsetNext+sliderWidth);
        } else {
            thisid.find(".slider-content-prev").css("left",axis_all-18-sliderWidth+32-16-35);
            thisid.find(".slider-content-center").css("left",axis_all-18-35-sliderWidth+32);
            thisid.find(".slider-content-next").css("left",axis_all-16-35);
        }
        getSelectTime();
        $("input.flowchart").val($(obj).parent().parent().find("div.flowchart").attr("id"));
        var url = $(obj).parent().parent().find('.links').data('url');
        var switch_num = $(obj).parent().parent().find('.links').data('port');
        $(".flowchartUrl").val(url);
        $(".flowchartSwitch").val(switch_num);
        getDate(thisid.find(".flowchartUrl").val(),thisid.find(".flowchartSwitch").val(),parseInt(thisid.find(".thisStart").val()),parseInt(thisid.find(".thisEnd").val()),true,thisid.find("input.flowchart").val())
    }

    function getSelectTime(mintime){
        var allTimeWidth,startTime,endTime,allTime;
        allTimeWidth = thisid.find(".slider-outer").width()-35-35-32;
        allTime = thisid.find(".totalTime").val();
        if(!middleTimeWidth){
            middleTimeWidth = thisid.find(".slider-content-center").width();
        }
        var timeLength = (middleTimeWidth/allTimeWidth)*allTime;
        var left = (thisid.find(".slider-content-center").offset().left-thisid.find(".slider-outer").offset().left-35-16)/allTimeWidth;
        var right= (thisid.find(".slider-content-center").offset().left-thisid.find(".slider-outer").offset().left-35-16+middleTimeWidth)/allTimeWidth;
        thisid.find(".thisStart").val(parseInt(thisid.find(".flowchartStart").val())+parseInt(left*thisid.find(".totalTime").val()));
        thisid.find(".thisEnd").val(parseInt(thisid.find(".flowchartStart").val())+right*parseInt(thisid.find(".totalTime").val()));
        if(mintime != null){
            thisid.find(".selectTime").html(formatDuring(mintime));
        } else {
            thisid.find(".selectTime").html(formatDuring(timeLength));
        }

    }
    function getDate(url,switch_num,startTime,endTime,getTime,id){
        // console.log("url:"+url,"switch_num:"+switch_num,"startTime:"+startTime,"endTime:"+endTime,"getTime:"+getTime,"id:"+id)

        var point = endTime - startTime;
        if(startTime ==""){
            startTime = new Date().getTime();
        }
        if(endTime == ""){
            endTime = new Date().getTime();
        }

        if(getTime==""){
            // $.getJSON(url+'&switch_num='+switch_num+'&start='+startTime+'&end='+endTime+'&t='+Math.random(), {}, function(res){
            //     if(typeof res.status != 'undefined' && res.status == 'success'){
            //     }
            // })
        } else {
            if($('.rrd_flowchart').length>0){
                url = $('.rrd_flowchart').data('url')
                $.getJSON(location.href + url+'&start='+startTime+'&end='+endTime+'&get_traffic=1&t='+Math.random(), {}, function(res){
                    if(res.status == 'success'){
                        mapChart("#rrd_flowchart_img",res,switch_num,point)
                    }else{
                        $("#rrd_flowchart_img").html("<div style='text-align: center;color:#999;'>"+ language.emptyFlowchartData +"</div>");
                        $("#rrd_flowchart_img").css("height","50px !important;")
                        $("#rrd_flowchart_img").next().hide()
                    }
                })
            } else{
                $.getJSON(location.href + url+'&switch_num='+switch_num+'&start='+startTime+'&end='+endTime+'&get_traffic=1&t='+Math.random(), {}, function(res){
                    if(typeof res.status != 'undefined' && res.status == 'success'){
                        mapChart("#"+id, res,switch_num,point)
                    }else{
                        var content = '没有流量数据';
                        // if(/^\d+$/i.test(switch_num)){
                        //     content = language.pleaseRefreshSwitchSNMPInfo
                        // }
                        $("#"+id).html("<div style='text-align: center;color:#999;'>"+ content +"</div>");
                        $("#"+id).css("height","50px !important;")
                        $("#"+id).next().hide()
                    }
                })
            }
        }

    }
});

//一次绑定多个选择DIV类型（日期选择）
var divel = document.querySelectorAll(".divmore");
for(var j=0;j<divel.length;j++){
    var divmat = divel[j].getAttribute("placeholder");
    jeDate(divel[j],{
        format: divmat,
        theme:{bgcolor:"#00A1CB",pnColor:"#00CCFF"},
        format: "YYYY-MM-DD hh:mm:ss"
    });
}



