var user = config.user,
    pwd = config.password,
    videoqualityxmlDoc;
$(document).ready(function() {
    initRecordPage();
    getrecordtime();
    getvideoquality();
    $("#endtimehour").change(function() {
        if ($(this).select2("val") == "24") {
            $("#endtimeinute").select2("val", "00")
            $("#endtimeinute").attr("disabled", true)
        } else {
            $("#endtimeinute").attr("disabled", false)
        }
    });
    $("#enablerecord").change(function() {
        if ($(this).is(":checked")) {
            $(".islock").show();
            if ($("#recordplan").select2("val") == "day") {
                $(".plan-time").hide();
            } else if ($("#recordplan").select2("val") == "all") {
                $(".plan-time").hide();
            } else if ($("#recordplan").select2("val") == "night") {
                $(".plan-time").hide();
            } else {
                $(".plan-time").show();
            }
        } else {
            $(".islock").hide();
        }
    });
    $("#recordplan").change(function() {
        if ($(this).select2("val") == "day") {
            $(".plan-time").hide();
        } else if ($(this).val() == "all") {
            $(".plan-time").hide();
        } else if ($(this).val() == "night") {
            $(".plan-time").hide();
        } else {
            $(".plan-time").show();
        }
    });
    $("#recordSave").click(function() {
        if ("admin" == user || user == "super_admin") {
            multipleSaveState(setecordTime(), setvideoquality());
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }

    });
})

function initRecordPage() {
    if (!$('.select2').hasClass('select2-offscreen')) {
        $(".select2").select2({
            minimumResultsForSearch: -1,
            width: 220
        });
    }
    $("#starttimehour,#starttimeminute,#endtimehour,#endtimeinute").select2({
        minimumResultsForSearch: -1,
        width: 100
    });
    for (var i = 0; i <= 24; i++) {
        var hour = i < 10 ? ("0" + i) : i;
        if (i == 24) {
            $("<option value='" + hour + "'>" + hour + "</option>").appendTo("#endtimehour");
            break
        }
        $("<option value='" + hour + "'>" + hour + "</option>").appendTo("#starttimehour");
        $("<option value='" + hour + "'>" + hour + "</option>").appendTo("#endtimehour");
    }
}

function getrecordtime() {
    var cyURL = config.baseUrl + "/Record/1/RecordScheduleV2";
    $.ajax({
        url: cyURL,
        type: "GET",
        async: false,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            $("#enablerecord").prop('checked', JSON.parse($(xmlDoc).find("Enable").eq(0).text()));
            if (JSON.parse($(xmlDoc).find("Enable").eq(0).text())) {
                $(".islock").show();
            } else {
                $(".islock").hide();
            }
            $("#recordtype").select2("val", $(xmlDoc).find("RecordType").eq(0).text());
            var time = $(xmlDoc).find("TimeBlock_0").eq(0).text();
            if (time == "08:00-20:00") {
                $("#recordplan").select2("val", "day");
                $(".plan-time").hide();
            } else if (time == "00:00-24:00") {
                $("#recordplan").select2("val", "all");
                $(".plan-time").hide();
            } else if (time == "20:00-08:00") {
                $("#recordplan").select2("val", "night");
                $(".plan-time").hide();
            } else {
                $("#recordplan").select2("val", "custom");
                $(".plan-time").show();

            }
            $("#enablerecord").change();
            time = time.split("-");
            var sarry = time[0].split(":");
            var earry = time[1].split(":");
            $("#starttimehour").select2("val", sarry[0]);
            $("#starttimeminute").select2("val", sarry[1]);
            $("#endtimehour").select2("val", earry[0]);
            $("#endtimeinute").select2("val", earry[1]);
            if ($("#endtimehour").select2("val") == "24") {
                $("#endtimeinute").select2("val", "00")
                $("#endtimeinute").attr("disabled", true)
            }

        },
        error: function(xhr) {

        }
    })
}

function getvideoquality() {
    var cyURL = config.baseUrl + "/Record/1/RecordSchedule";
    $.ajax({
        url: cyURL,
        type: "GET",
        async: false,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            videoqualityxmlDoc = xmlDoc;
            $("#videoquality").select2("val", $(xmlDoc).find("RecordSchedule").eq(0).find("StreamType").eq(0).text());
        },
        error: function(xhr) {

        }
    })
}

function setecordTime() {
    var s = $("#starttimehour").select2("val") + $("#starttimeminute").select2("val")
    var e = $("#endtimehour").select2("val") + $("#endtimeinute").select2("val");
    var time = $("#starttimehour").select2("val") + ":" + $("#starttimeminute").select2("val") + "-" + $("#endtimehour").select2("val") + ":" + $("#endtimeinute").select2("val");
    if ($("#recordplan").select2("val") == "day") {
        time = "08:00-20:00";
    } else if ($("#recordplan").select2("val") == "all") {
        time = "00:00-24:00";
    } else if ($("#recordplan").select2("val") == "night") {
        time = "20:00-08:00";
    }
    if (s == e) {
        $.optionBootoast($.i18n.prop('i18n-storage_time_msg'), "danger");
        return;
    }
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><RecordScheduleV2  Version='1.0'>" +
        "<ChannelID>1</ChannelID><Enable>" + $("#enablerecord").is(":checked") + "</Enable><RecordType>" + $("#recordtype").select2("val") + "</RecordType>" +
        "<TimeBlock_0>" + time + "</TimeBlock_0></RecordScheduleV2>"
    var cyURL = config.baseUrl + "/Record/1/RecordScheduleV2";
    var xhrXML;
    $.ajax({
        type: "PUT",
        url: cyURL,
        async: false,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            // SaveState(xhr);
            xhrXML = xhr
        }
    });
    return xhrXML;
}


function setvideoquality() {
    var xhrXML;
    $(videoqualityxmlDoc).find("RecordSchedule").eq(0).find("StreamType").eq(0).text($("#videoquality").select2("val"));
    var cyURL = config.baseUrl + "/Record/1/RecordSchedule";
    $.ajax({
        type: "PUT",
        url: cyURL,
        async: false,
        processData: false,
        data: videoqualityxmlDoc,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            // SaveState(xhr);
            xhrXML = xhr
        }
    });
    return xhrXML;
}