
function initializeSetWorkHours() {
    var columns = [
        { data: "AcCode", name: "AcCode", render: $.fn.dataTable.render.preview("AcCode"), className: 'dt-left' },
        { data: "ShiftType", name: "ShiftType", filtertype: "text" },
        { data: "EtBefore", name: "EtBefore", filtertype: "text" },
        { data: "EtAfter", name: "EtAfter", filtertype: "text" },
    ];

    initializeDataTables('set-workhours', 'Keywords', columns);

    $dataTable = $('#set-workhours');
}

function openSetWhModal(day, begin, end, shiftType, shiftTypeText, anQid) {
    if (!begin) {
        begin = 0;
    }
    if (!end) {
        end = 0;
    }
    if (!day) {
        day = 0;
    }

    if (!shiftType || shiftType == "-1") {
        shiftType = $("#DefaultEntryType").val();
        shiftTypeText = $("#DefaultEntryTypeText").val();
    }

    $("#ItemAnQid").val(anQid);

    var dayText = day == 0 ? $("#LblCurrentDay").val() : $("#LblDay").val() + " +" + day;

    var refDelayDay = new Option(dayText, day, true, true);
    $('#ModalDelayDay').append(refDelayDay).trigger('change');

    var beginHours = parseInt(begin / 60);
    var beginMinutes = parseInt(begin % 60);
    var beginText = ("0" + beginHours).slice(-2) + ":" + ("0" + beginMinutes).slice(-2);
    $('#ModalBegin').val(beginText);

    var endHours = parseInt(end / 60);
    var endMinutes = parseInt(end % 60);
    var endText = ("0" + endHours).slice(-2) + ":" + ("0" + endMinutes).slice(-2);
    $('#ModalEnd').val(endText);

    var refType = new Option(shiftTypeText, shiftType, false, false);
    $('#ModalShiftType').append(refType).trigger('change');

    $("#setWhModal").modal("show");
}

function deleteSetWhItem() {
    var acCode = $("#AcCode").val();
    var anQid = $("#AnQid").val();
    var delayDay = $("#ModalDelayDay").val();

    $.ajax({
        url: $("#UrlDeleteItem").val() + '?anQid=' + anQid + "&delayDays=" + delayDay,
        dataType: 'json',
        method: "DELETE",
        success: function (data) {
            window.open($("#UrlEdit").val() + '?acCode=' + acCode, "_self");
        },
        fail: function (error) {
            swal(error);
        }
    });
}

function saveSetWhItem() {

    var delayDays = $("#ModalDelayDay").val();
    if (delayDays < 0) {
        delayDays = 0;
    }

    var beginParts = $("#ModalBegin").val().split(':');
    var beginMinutes = (+beginParts[0]) * 60 + (+beginParts[1]);
    if (!beginMinutes) {
        beginMinutes = 0;
    }

    var endParts = $("#ModalEnd").val().split(':');
    var endMinutes = (+endParts[0]) * 60 + (+endParts[1]);
    if (!endMinutes) {
        endMinutes = 0;
    }

    if (endMinutes <= beginMinutes) {
        $("#InputValidation").text($("#ErrTimeNotCorrect").val());

        return;
    }

    $.ajax({
        url: $("#UrlSaveItem").val(),
        dataType: 'json',
        method: "POST",
        data: {
            AcCode: $("#AcCode").val(),
            AnQid: $("#ItemAnQid").val(),
            DelayDays: delayDays,
            Begin: beginMinutes,
            End: endMinutes,
            ShiftType: $("#ModalShiftType").val(),
        },
        success: function (data) {
            if (data.Status) {
                window.open($("#UrlEdit").val() + '?acCode=' + $("#AcCode").val(), "_self");
            }
            else {
                $("#InputValidation").text($("#ErrTimeOverlap").val());
            }
        },
        fail: function (error) {
            swal(error);
        }
    });
}

function saveHeader() {
    $.ajax({
        url: $("#UrlSaveHeader").val(),
        dataType: 'json',
        method: "POST",
        data: {
            AcCode: $("#_AcCode").val(),
            AnQid: $("#AnQid").val(),
            ShiftType: $("#ShiftType").val(),
            EtBefore: $("#EtBefore").val(),
            EtAfter: $("#EtAfter").val(),
        },
        success: function (data) {
            if (data.AnQid == 0) {
                swal("AcCode: Already Exists");

                return;
            }

            window.open($("#UrlEdit").val() + '?acCode=' + data.AcCode, "_self");
        },
        fail: function (error) {
            swal(error);
        }
    });
}