$("[data-event='true']").click(function () {
    $(this).parent().find("[data-event='true']").removeClass('isactive').addClass('dontisactive');
    $(this).addClass('isactive');
    $(this).parent().find('input[type="hidden"]').val($(this).attr('data-id'));
    if ($(this).parent().find('input[type="hidden"]').attr('id') == 'hfStateSanad')
        grdSanad.PerformCallback();
    else
        grdBank.PerformCallback();
});

var data = [];
var sanad = '', bank = '';
var pageBefor = 1;
var saveFinal = false;

function conflict() {
    grdSanad.GetSelectedFieldValues('Code', OnGetRowValuesSanad);
}
function DoPrint() {
    var title = window.top.report;
    var src = "Report/Default?GrpName=Hs_Confilictbank&ListBank=" + getQuery("code") + " &SalMali=&AzDate=" + $('#lblAzDate').text() + "&TaDate=" + $('#txtTaDate').val();
    var type = "";
    openInModal('', src, title,"","",false);
}
function OnGetRowValuesSanad(Value) {
    setTimeout(function () {
        var page = getPaging(1) + "sanad";

        if (!saveFinal) {
            for (var i = 0; i < data.filter(function (item) { return (item.page == page); }).length; i++) {
                var checked = data.filter(function (item) { return (item.page == page); })
                grdSanad.SelectRowOnPage(checked[i].RowID, true)
            }
        }

        if (data.length != 0)
            data = data.filter(function (item) { return (item.page != page); });

        for (var i = 0; i < Value.length; i++) {
            data.push({ 'type': 1, 'value': Value[i][0], 'page': page, 'RowID': Value[i][1] });
        }

        if (!saveFinal) {
            sanad = Value;
            if (grdSanad.cpEvent)
                delete grdSanad.cpEvent;
        }

        grdBank.GetSelectedFieldValues('CodeRiz;RowId', OnGetRowValuesBank);
    }, 600);
}
function OnGetRowValuesBank(Value) {
    setTimeout(function () {

        var page = "";
        page = getPaging(2) + "bank";

        if (!saveFinal) {
            for (var i = 0; i < data.filter(function (item) { return (item.page == page); }).length; i++) {
                var checked = data.filter(function (item) { return (item.page == page); })
                grdBank.SelectRowOnPage(checked[i].RowID, true)
            }
        }

        if (data.length != 0)
            data = data.filter(function (item) { return (item.page != page); });

        for (var i = 0; i < Value.length; i++) {
            data.push({ 'type': 2, 'value': Value[i][0], 'page': page, 'RowID': Value[i][1] });
        }

        if (!saveFinal) {
            bank = Value;
            if (grdBank.cpEvent)
                delete grdBank.cpEvent;
        }
        else {
            saveChange();
        }

    }, 800);
}
function saveChange()
{
    var dataSanad = data.filter(function (item) { return (item.type == 1); });
    var dataBnak = data.filter(function (item) { return (item.type == 2); });

    var sanadList = $.map(dataSanad, function (a, index) {
        if (a != null)
            return a.value;
    });
    var bankList = $.map(dataBnak, function (a, index) {
        if (a != null)
            return a.value;
    });

    ajax({
        loading: true,
        url: "Accounting/ConflictSolve/Conflict",
        data: { sanad: sanadList, bank: bankList },
        success: function (result) {
            saveFinal = false;
            grdSanad.PerformCallback();
            grdBank.PerformCallback();
        }
    });
}
function save() {
    saveFinal = true;
    grdSanad.GetSelectedFieldValues('Code;RowId', OnGetRowValuesSanad);
}
function OnBeginCallback(s, e) {

    if (!saveFinal) {
        if (s.globalName == "grdSanad") {
            if (s.cpEvent)
                return;
            s.cpEvent = 'selected';
            grdSanad.GetSelectedFieldValues('Code;RowId', OnGetRowValuesSanad);
        }
        else if (s.globalName == "grdBank") {
            if (s.cpEvent)
                return;
            s.cpEvent = 'selected';
            grdBank.GetSelectedFieldValues('CodeRiz;RowId', OnGetRowValuesBank);
        }
    }
}
function getPaging(type) {
    var page = 1;
    if (type == 1)
        page = $(grdSanad.mainElement).parents('.panel-grid').find('.grid-box-paging').find('.grid-currentpage')[0].innerText;
    else
        page = $(grdBank.mainElement).parents('.panel-grid').find('.grid-box-paging').find('.grid-currentpage')[0].innerText;
    if (pageBefor < page && !saveFinal)
        page = page - 1;
    else
        page = page;
    pageBefor = page;

    return page;
}
function deleteRelation(){
    debugger;
}