// 用户切换时间间隔，时间会发生变化
$("#interval").change(function () {
    var interval = $(this).val();
    var endTime = $("#endTime").val();
    var startTime = getStartTime(interval, endTime);
    $("#startTime").val(startTime);
    $("#endTime").val(endTime);
    judgeInterval(interval)
});

//隐藏前进后退按钮
function judgeInterval(interval) {
    if (interval === "自定义") {
        $("#present").css("display", "none");
        $("#before").css("display", "none");
        $("#next").css("display", "none");
        $("#startTime").removeProp("disabled");
        $("#endTime").removeProp("disabled");
    } else {
        $("#present").css("display", "inline-block");
        $("#before").css("display", "inline-block");
        $("#next").css("display", "inline-block");
        $("#startTime").prop("disabled", "true");
        $("#endTime").prop("disabled", "true");
    }
}

$('.form_datetime').datetimepicker({
    minView: "month", //选择日期后，不会再跳转去选择时分秒
    language: 'zh-CN',
    format: 'yyyy-mm-dd',
    todayBtn: false,//今日按钮
    autoclose: 1,
});


//向前
$("#before").click(function () {
    var interval = $("#interval").val();
    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    var year = parseInt(startTime.substring(0, 4));
    var month = parseInt(startTime.substring(5, 7));
    var date = parseInt(startTime.substring(8, 10));

    if (interval == "月份") {
        // 换年份
        if (month == 1) {
            year = year - 1;
            startTime = year + "-12-01";
            endTime = year + "-12-31";
        } else if (month <= 10) {
            month = month - 1;
            startTime = year + "-0" + month + "-01";
            var day = getDays(year, month);
            endTime = year + "-0" + month + "-" + day;
        } else {
            month = month - 1;
            startTime = year + "-" + month + "-01";
            var day = getDays(year, month);
            endTime = year + "-" + month + "-" + day;
        }
    } else if (interval == "周") {
        //根据开始时间推
        var start = new Date(year, month - 1, date);
        start.setDate(start.getDate() - 7);
        startTime = formatTime(start);
        var end = new Date(start);
        end.setDate(start.getDate() + 6);
        endTime = formatTime(end);
    } else if (interval == "季度") {
        // 换年份
        if (month == 1) {
            year = year - 1;
            startTime = year + "-10-01";
            endTime = year + "-12-31";
        } else {
            // 还是本年
            month = month - 3;
            startTime = year + "-0" + month + "-01";
            month = month + 2;
            var day = getDays(year, month);
            endTime = year + "-0" + month + "-" + day;
        }
    } else if (interval == "自定义") {

    } else if (interval == "年份") {
        year = year - 1;
        startTime = year + "-01-01";
        endTime = year + "-12-31";
    } else if (interval == "半年") {
    	if (month <= 6) {
	        year = year - 1;
	        startTime = year + "-07-01";
	        endTime = year + "-12-31";
    	}else{
	        startTime = year + "-01-01";
	        endTime = year + "-06-30";
    	}
    }else if(interval == "天"){
        var start = new Date(year, month - 1, date);
        start.setDate(start.getDate() -1);
        startTime = formatTime(start);
        var end = new Date(start);
        endTime = formatTime(end);
    }

    $("#startTime").val(startTime);
    $("#endTime").val(endTime);
    // 判断向后键的状态
    var temp = getEndTime();
    judgeNext(temp, endTime);
});
// 当前
$("#present").click(function () {
    var interval = $("#interval").val();

    var endTime = getEndTime();
    var startTime = getStartTime(interval, endTime);

    $("#startTime").val(startTime);
    $("#endTime").val(endTime);
    // 判断向后键的状态
    var temp = getEndTime();
    judgeNext(temp, endTime);
});

// 向后
function back() {
    var interval = $("#interval").val();
    var startTime = $("#startTime").val();
    // 获取年份
    var year = parseInt(startTime.substring(0, 4));
    // 获取月份
    var month = parseInt(startTime.substring(5, 7));
    var date = parseInt(startTime.substring(8, 10));
    var endTime = $("#endTime").val();
    var now = new Date();
    // 获取当前年份
    var presentY = now.getFullYear();
    // 获取当前月份
    var presentM = now.getMonth() + 1;
    // 获取当前日期
    var presentD = now.getDate();
    if (interval == "月份") {
        if (month == 12) {
            year = year + 1;
            // 年份进位后，大于当前的年份，是不科学的
            if (presentY < year) {
                startTime = presentY + "-12-01";
                if (presentD < 10) {
                    endTime = presentY + "-12" + "-0" + presentD;
                } else {
                    endTime = presentY + "-12" + "-" + presentD;
                }
                // 年份进位后，等于当前的年份
            } else if (presentY == year) {
                startTime = year + "-01-01";
                if (presentM > 1) {
                    endTime = year + "-01-31";
                } else {
                    if (presentD < 10) {
                        endTime = year + "-01" + "-0" + presentD;
                    } else {
                        endTime = year + "-01" + "-" + presentD;
                    }
                }
                // 年份进位后，依旧小于当前的年份
            } else {
                startTime = year + "-01-01";
                endTime = year + "-01-31";
            }
        } else {
            month = month + 1;
            // 年份等于当前年份
            if (presentY == year) {
                // 月份超过当前月份，是不科学的
                if (month >= presentM) {
                    if (presentM < 10) {
                        startTime = year + "-0" + presentM + "-01";
                        if (presentD < 10) {
                            endTime = year + "-0" + presentM + "-0" + presentD;
                        } else {
                            endTime = year + "-0" + presentM + "-" + presentD;
                        }
                    } else {
                        startTime = year + "-" + presentM + "-01";
                        if (presentD < 10) {
                            endTime = year + "-" + presentM + "-0" + presentD;
                        } else {
                            endTime = year + "-" + presentM + "-" + presentD;
                        }
                    }
                } else {
                    if (month < 10) {
                        startTime = year + "-0" + month + "-01";
                        var day = getDays(year, month);
                        endTime = year + "-0" + month + "-" + day;
                    } else {
                        startTime = year + "-" + month + "-01";
                        var day = getDays(year, month);
                        endTime = year + "-" + month + "-" + day;
                    }
                }
                // 年份小于当前的年份
            } else {
                if (month < 10) {
                    startTime = year + "-0" + month + "-01";
                    var day = getDays(year, month);
                    endTime = year + "-0" + month + "-" + day;
                } else {
                    startTime = year + "-" + month + "-01";
                    var day = getDays(year, month);
                    endTime = year + "-" + month + "-" + day;
                }
            }
        }
    } else if (interval == "季度") {
        // 前进需要年份进位
        if (month == 10) {
            year = year + 1;
            // 年份进位后大于当前年份是不科学的
            if (year > presentY) {
                startTime = presentY + "-10-01";
                if (presentD < 10) {
                    endTime = year + "-" + presentM + "-0" + presentD;
                } else {
                    endTime = year + "-" + presentM + "-" + presentD;
                }
            } else if (year == presentY) {
                startTime = year + "-01-01";
                // 当前月份大约3月份
                if (presentM > 3) {
                    endTime = year + "-03-31";
                } else {
                    // 当前月份也在第一季度里
                    if (presentD < 10) {
                        endTime = year + "-0" + presentM + "-0" + presentD;
                    } else {
                        endTime = year + "-0" + presentM + "-" + presentD;
                    }
                }
            } else {
                startTime = year + "-01-01";
                endTime = year + "-03-31";
            }
        } else {
            month = month + 3;
            // 季度进位后，超过当前月份是不科学的
            if (year == presentY) {
                if (month >= presentM) {
                    // 当季度进位后大于当前月，以当前月的时间显示季度
                    if (presentM > 0 && presentM < 4) {// 第一季度
                        startTime = year + "-01-01";
                        if (presentD < 10) {
                            endTime = year + "-0" + presentM + "-0" + presentD;
                        } else {
                            endTime = year + "-0" + presentM + "-" + presentD;
                        }
                    } else if (presentM > 3 && presentM < 7) {// 第二季度
                        startTime = year + "-04-01";
                        if (presentD < 10) {
                            endTime = year + "-0" + presentM + "-0" + presentD;
                        } else {
                            endTime = year + "-0" + presentM + "-" + presentD;
                        }
                    } else if (presentM > 6 && presentM < 10) {// 第三季度
                        startTime = year + "-07-01";
                        if (presentD < 10) {
                            endTime = year + "-0" + presentM + "-0" + presentD;
                        } else {
                            endTime = year + "-0" + presentM + "-" + presentD;
                        }
                    } else {// 第四季度
                        startTime = year + "-10-01";
                        if (presentD < 10) {
                            endTime = year + "-" + presentM + "-0" + presentD;
                        } else {
                            endTime = year + "-" + presentM + "-" + presentD;
                        }
                    }
                } else {
                    if (month == 10) {
                        startTime = year + "-" + month + "-01";
                    } else {
                        startTime = year + "-0" + month + "-01";
                    }
                    month = month + 2;
                    if (month >= presentM) {
                        endTime = getEndTime();
                        ;
                    } else {
                        var day = getDays(year, month);
                        endTime = year + "-" + month + "-" + day;
                    }
                }
            } else {
                if (month == 10) {
                    startTime = year + "-" + month + "-01";
                    month = month + 2;
                    var day = getDays(year, month);
                    endTime = year + "-" + month + "-" + day;
                } else {
                    startTime = year + "-0" + month + "-01";
                    month = month + 2;
                    var day = getDays(year, month);
                    endTime = year + "-0" + month + "-" + day;
                }
            }
        }
    } else if (interval == "自定义") {

    } else if (interval == "周") {
        //根据开始时间推
        var start = new Date(year, month - 1, date);
        start.setDate(start.getDate() + 7);
        startTime = formatTime(start);
        var end = new Date(start);
        end.setDate(start.getDate() + 6);
        endTime = formatTime(end);

        if (parseInt((end - new Date()) / 1000 / 60) > 24) { //相差小时大于24小时，即超过当天，置为当天日期
            endTime = formatTime(new Date());
        }
    } else if (interval == "年份") {
        year = year + 1;
        // 年份进位后大于当前年份，是不科学的
        if (year >= presentY) {
            startTime = presentY + "-01-01";
            if (presentM < 10) {
                if (presentD < 10) {
                    endTime = presentY + "-0" + presentM + "-0" + presentD;
                } else {
                    endTime = presentY + "-0" + presentM + "-" + presentD;
                }
            } else {
                endTime = presentY + "-" + presentM + "-" + presentD;
            }
        } else {
            startTime = year + "-01-01";
            endTime = year + "-12-31";
        }
    } else if (interval == "半年") {
        // 前进需要年份进位
        if (month > 6) {
            year = year + 1;
            // 年份进位后大于当前年份是不科学的
            if (year >= presentY) {
                startTime = presentY + "-01-01";
                if(presentM > 6){
                    endTime = year + "-06-30";
                }else{
                    if (presentD < 10) {
                        endTime = year + "-" + presentM + "-0" + presentD;
                    } else {
                        endTime = year + "-" + presentM + "-" + presentD;
                    }
                }
            } else {
                startTime = year + "-01-01";
                endTime = year + "-06-30";
            }
        } else {
            if (year == presentY) {
                startTime = presentY + "-07-01";
                // 进位后大于当前月，以当前月的时间显示
                if (presentM < 10) {
	                if (presentD < 10) {
	                    endTime = year + "-0" + presentM + "-0" + presentD;
	                } else {
	                    endTime = year + "-0" + presentM + "-" + presentD;
	                }
                }else{
                    if (presentD < 10) {
                        endTime = year + "-" + presentM + "-0" + presentD;
                    } else {
                        endTime = year + "-" + presentM + "-" + presentD;
                    }
                }
            } else {
                startTime = year + "-07-01";
                endTime = year + "-12-31";
            }
        }
    }else if(interval == "天"){
        var start = new Date(year, month - 1, date);
        start.setDate(start.getDate() + 1);
        startTime = formatTime(start);
        var end = new Date(start);
        endTime = formatTime(end);
    }
    // 判断向后键的状态
    var temp = getEndTime();
    judgeNext(temp, endTime);
    $("#startTime").val(startTime);
    $("#endTime").val(endTime);
}

$("#next").click(function () {
    back();
});

// 判断next按钮的状态
function judgeNext(temp, endTime) {
    if (temp == endTime) {
        $("#next").attr("class", "njcn btn");
        $("#next").unbind();
    } else {
        restoreNext();
    }
}

// 恢复向后键的所有时间（点击事件、button变化事件）
function restoreNext() {
    var $events = $._data($("#next")[0], "events");
    if ($events && $events["click"]) {

    } else {
        $("#next").attr("class", "njcn btn btn-primary");
        $("#next").bind("click", function () {
            back();
        })
    }
    if ($events && $events["mouseover"]) {

    } else {
        $("#next").bind("mouseover", function () {
            $(this).attr("class", "njcn btn");
        })
    }
    if ($events && $events["mouseout"]) {

    } else {
        $("#next").bind("mouseout", function () {
            $(this).attr("class", "njcn btn btn-primary");
        })
    }
}

// 获取月份的天数
function getDays(year, month) {
    var max = new Date(year, month, 0).getDate();
    return max;
}

// 时间格式化
function formatTime(time) {
    return time.getFullYear() + '-' + ((time.getMonth() + 1) < 10 ? '0' : '') + (time.getMonth() + 1) + '-' + (time.getDate() < 10 ? '0' : '') + time.getDate();
}

$(function () {
    var interval = $("#interval").val();
    var endTime = getEndTime();
    var startTime = getStartTime(interval, endTime);
    $("#startTime").val(startTime);
    $("#endTime").val(endTime);
    $("#next").attr("class", "njcn btn");
    $("#next").unbind();
    // 除了自定义其他时间间隔用户可以选择日期
    if (interval === "自定义") {
        $("#present").css("display", "none");
        $("#before").css("display", "none");
        $("#next").css("display", "none");
        $("#startTime").removeProp("disabled");
        $("#endTime").removeProp("disabled");
    } else {
        $("#present").css("display", "inline-block");
        $("#before").css("display", "inline-block");
        $("#next").css("display", "inline-block");
        $("#startTime").prop("disabled", "true");
        $("#endTime").prop("disabled", "true");
    }
});

// 获取当前日期，精确到日
function getEndTime() {
    var now = new Date();
    var sep = "-";
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    if (month < 10) {
        month = "0" + month;
    }
    var date = now.getDate();
    if (date < 10) {
        date = "0" + date;
    }
    // 拼接当前的日期
    var endTime = year + sep + month + sep + date;
    return endTime;
}

// 获取起始日期
function getStartTime(interval, endTime) {
    var now = new Date();
    var sep = "-";
    var arr = endTime.split("-");
    var year = arr[0];
    var month = arr[1];
    var date = arr[2];
    // 按月份间隔
    if (interval == "月份") {
        date = "01";
        // 按季度间隔
    } else if (interval == "季度") {
        if (month > 0 & month < 4) {// 第一季度
            month = "01";
            date = "01";
        } else if (month > 3 && month < 7) {// 第二季度
            month = "04";
            date = "01";
        } else if (month > 6 && month < 10) {// 第三季度
            month = "07";
            date = "01";
        } else {// 第四季度
            month = 10;
            date = "01";
        }
    } else if (interval == "年份") {
        month = "01";
        date = "01";
    } else if (interval == "半年") {
    	if (month < 7){
            month = "01";
            date = "01";
    	}else{
            month = "07";
            date = "01";
    	}
    } else if (interval == '周') {
        var start = new Date(year, month - 1, date);
        var dayOfWeek = (start.getDay() == 0) ? 7 : start.getDay(); // 如果为周日，则置为7天
        start.setDate(start.getDate() - dayOfWeek + 1);// 计算开始时间

        if (formatTime(new Date()) != endTime) { // 如果结束时间不是当前时间
            var end = new Date(start);
            end.setDate(end.getDate() + 6);
            $("#endTime").val(formatTime(end));
        }

        return formatTime(start);
    }else if(interval == '天'){
        var start = new Date(year, month - 1, date);
        return formatTime(start);
    }

    var startTime = year + sep + month + sep + date;
    return startTime;
}
