var selectedCodes = [];
var gridBuilderNoSelect;
var gridBuilderSelect;
$(".go-step").on("click", function () {
    $(".div-step").hide();
    $("#" + $(this).attr("data-target")).show();
});
$(document).ready(function () {
    gridBuilderNoSelect = $("#gridListNoSelect").gridBuilder();
    gridBuilderSelect = $("#gridListSelect").gridBuilder();

    gridBuilderNoSelect.onExportClick(function () {
        loadGrid("gridListNoSelect", { exportExcel: true });
    });
    gridBuilderSelect.onExportClick(function () {
        loadGrid("gridListSelect", { exportExcel: true });
    });
    $("#txtManualDate").prop("disabled", true);
    loadGrid("gridListNoSelect");
});


function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    var selected = gridId === "gridListSelect";
    var gridBuilder = selected ? gridBuilderSelect : gridBuilderNoSelect;
    ajax({
        url: "AccountingApi/GetNoneReferenceDocumentList",
        gridBuilder: gridBuilder,
        data: {
            codeList: selectedCodes,
            isSelected: selected,
            filter: gridBuilder.getFilter(),
            sort: gridBuilder.getSortFields(),
            pageNo: isExportExcel ? -1 : gridBuilder.getCurrentPageNumber(),
            selectionCriteria: gridBuilder.selectionViewMode ? "'" + gridBuilder.selectionList.join("','") + "'" : null
        },
        success: function (data) {
            if (isExportExcel) {
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            }
            else
                gridBuilder.loadData(data);
        }
    });
}

function setDisabled(state, isMonth, isAll) {
    $("#rbManualDate").prop("disabled", isMonth ? true : state);
    $("#txtManualDate").prop("disabled", isMonth || isAll ? true : state);
    $("#rbAutoDate").prop("disabled", state);
    $("#drpAutoDateType").prop("disabled", state);
    if (isMonth) {
        $("#rbAutoDate").prop("checked", true);
    }
    if (state) {
        $("#rbAutoDate").prop("checked", false);
        $("#rbManualDate").prop("checked", false);
    }
    if (isAll) {
        $("#rbAutoDate").prop("checked", true);
    }

}
function sanadTypeChange(value) {
    setDisabled(value == "day" ? true : false, value == "month" ? true : false, value == "all" ? true : false);
}
function dateChange(value) {
    var state = (value == "rbAutoDate" ? true : false);
    $("#txtManualDate").prop("disabled", state);
    $("#drpAutoDateType").prop("disabled", !state);
}
function save() {
    var type;
    var sanadType = $(".sanadType:checked").val();
    var dateType = $(".dateType:checked").val();
    var dateManual = (dateType == "rbManualDate" ? $("#txtManualDate").val() : "");
    var stateDate = (dateType == "rbManualDate" ? 2 : $("#drpAutoDateType").val());
    if (stateDate == 2 && dateManual.length == 0 && sanadType == "all") {
        showMsgModal(0, window.top.completeRequest);
        return;
    }
    switch (sanadType) {
        case "all":
            type = 1;
            break;
        case "day":
            type = 2;
            break;
        case "month":
            type = 3;
            break;
    }
    ajax({
        loading: true,
        url: 'AccountingApi/SaveReferenceDocument',
        data: {
            documentCodeList: selectedCodes,
            saveType: type,
            autoDateType: stateDate,
            documentDate: dateManual,
            summery: $("#txtTozihat").val()
        },
        success: function (retValue) {
            showMsgModal(2, retValue.FinalMessage);
            selectedCodes = [];
            $(".div-step").hide();
            $("#step1").show();
            loadGrid("gridListNoSelect");
            loadGrid("gridListSelect");
        }
    });
}

function AddSingle() {
    var codes = gridBuilderNoSelect.getSelectedRowsCellValue("Code");
    if (codes.length === 0) {
        var activeRowCode = gridBuilderNoSelect.getActiveRowCellValue("Code");
        if (activeRowCode)
            codes.push(activeRowCode);
    }
    addRightToLeft(codes);
}
function addRightToLeft(value) {
    for (var i = 0; i < value.length; i++) {
        if (!selectedCodes.indexOf(value[i]) > -1) {
            selectedCodes.push(value[i]);
        }
    }
    gridBuilderNoSelect.setUnselected(-1);
    gridBuilderSelect.setUnselected(-1);

    loadGrid("gridListNoSelect");
    loadGrid("gridListSelect");
}
function AddAll() {
    gridBuilderNoSelect.setSelected(-1);
    var code = gridBuilderNoSelect.getSelectedRowsCellValue('Code');
    addRightToLeft(code);
}
function removeSingle() {
    var codes = gridBuilderSelect.getSelectedRowsCellValue('Code');
    if (codes.length === 0) {
        var activeRowCode = gridBuilderSelect.getActiveRowCellValue("Code");
        if (activeRowCode)
            codes.push(activeRowCode);
    }
    addLeftToRight(codes);
}
function addLeftToRight(value) {
    for (var i = 0; i < value.length; i++) {
        if (selectedCodes.length > 0) {
            var index = selectedCodes.indexOf(value[i]);
            selectedCodes.splice(index, 1);
        }
    } 
    gridBuilderNoSelect.setUnselected(-1);
    gridBuilderSelect.setUnselected(-1);

    loadGrid("gridListNoSelect");
    loadGrid("gridListSelect");

}
function removeAll() {
    gridBuilderSelect.setSelected(-1);
    var code = gridBuilderSelect.getSelectedRowsCellValue('Code');
    addLeftToRight(code);
}
