var trRow;
var visibleIndex;
var codeSanad = 0;
var typeSanad = 0;
var gridBuilder;
var isAdminUser = window.top.$("#hfIsAdmin").val().toLowerCase();
$(document).ready(function () {
    if (isAdminUser === "1")
        $("#hfUserCode").val("");

    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    gridBuilder.onExpandChanged(function ($expanRow, rowIndex, isExpand) {
        if (isExpand && $expanRow.html().trim() === "") {
            var detailHtml = getRizData(rowIndex);
            $expanRow.html(detailHtml);
        }
    });
    loadGrid();
});


function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    var userCode = $("#hfUserCode").val() || "-1";
    ajax({
        gridBuilder: gridBuilder,
        url: "SaleApi/GetCashDeskDetails",
        data: {
            userCode: userCode,
            fromDate: $("#txtAzDate").val(),
            toDate: $("#txtTaDate").val(),
            filter: gridBuilder.getFilter(),
            sort: gridBuilder.getSortFields(),
            pageNo: isExportExcel ? -1 : gridBuilder.getCurrentPageNumber()
        },
        success: function (data) {
            if (isExportExcel) {
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            }
            else
                gridBuilder.loadData(data);
        }
    });
}

function getRizData(rowIndex) {
    var code = gridBuilder.getCellValue(rowIndex, "Code");
    var isClose = gridBuilder.getCellValue(rowIndex, "IsClosed");
    var canReturn = gridBuilder.getCellValue(rowIndex, "CanReturn");
    var startBalance = gridBuilder.getCellValue(rowIndex, "StartBalance");
    var naghdFrosh = Number(gridBuilder.getCellValue(rowIndex, "NaghdFrosh"))+ Number(gridBuilder.getCellValue(rowIndex, "SumDebit")) - Number(gridBuilder.getCellValue(rowIndex, "SumRetFrosh"));
    var varizFrosh = gridBuilder.getCellValue(rowIndex, "VarizFrosh");
    var chequFrosh = gridBuilder.getCellValue(rowIndex, "ChequFrosh");
    


    var body = '<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="background-color: #ECEFF1;padding: 16px;border-bottom: 1px solid #ccc;">';
    body += '<div class="col-lg-7 col-md-7 col-sm-7 col-xs-7" style="padding: 0px 46px;" >';
    body += '<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="margin: 16px 0 10px; border-bottom: 1px dashed #ccc;padding: 0px 16px;">';
    body += '<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">';
    body += '<label>' + window.top.initialBalance + '</label>';
    body += '</div>';
    body += '<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">';
    body += '<label>' + window.top.cash + '</label>';
    body += '</div>';
    body += '<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">';
    body += '<label>' + window.top.pos + '</label>';
    body += '</div>';
    body += '<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">';
    body += '<label>' + window.top.check + '</label>';
    body += '</div>';
    body += '</div>';
    body += '<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">';
    body += '<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">';
    body += '<label class="lbl-riz-sandogh" style="font-weight:700;width:auto; margin-left:5px; direction:ltr;font-size:14px;">' + numberWithCommas(startBalance) + '</label>';//....
    body += '</div>';
    body += '<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">';
    body += '<label class="lbl-riz-sandogh" style="font-weight:700;width:auto; margin-left:5px; direction:ltr;font-size:14px;">' + numberWithCommas(naghdFrosh) + '</label>';//....
    body += '</div>';
    body += '<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">';
    body += '<label class="lbl-riz-sandogh" style="font-weight:700;width:auto; margin-left:5px; direction:ltr;font-size:14px;">' + numberWithCommas(varizFrosh) + '</label>';//.....
    body += '<img style="cursor:pointer;margin-top: -2px;width: 20px;" onclick="ShowRizInfo(' + code + ',1)" src="../img/Info.svg">';
    body += '</div>';
    body += '<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">';
    body += '<label class="lbl-riz-sandogh" style="font-weight:700;width:auto; margin-left:5px; direction:ltr;font-size:14px;">' + numberWithCommas(chequFrosh) + '</label>';//...
    body += '<img style="cursor:pointer;margin-top: -2px;width: 20px;" onclick="ShowRizInfo(' + code + ',2)" src="../img/Info.svg">';
    body += '</div>';
    body += '</div>';
    body += '</div>';
    body += '<div class="col-lg-5 col-md-5 col-sm-5 col-xs-5" style="padding: 0px 46px;" >';
    if (isClose === "false") {
        body += '<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" onclick="openModalCloseFund(' + code + ')" style="cursor: pointer;margin-bottom: 8px; border-bottom: 1px dashed #ccc; padding: 8px 16px;">';
        body += '<div class="col-lg-2 col-md-2 col-sm-2 col-xs-2"><img src="../img/cashregisterclose.svg" style="width: 24px;" title=" ' + window.top.edit + ' "/></div>';
        body += '<div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 no-padding">' + window.top.closeCash + '</div>';
        body += '</div>';
    }
    else if (canReturn === "true") {
        body += '<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" onclick="OpenLastPosSystem(' + code + ')" style="cursor: pointer;margin-bottom: 8px; border-bottom: 1px dashed #ccc; padding: 8px 16px;">';
        body += '<div class="col-lg-2 col-md-2 col-sm-2 col-xs-2"><img src="../img/cashregisterclose.svg" style="width: 24px;" title=" ' + window.top.returnClose + ' "/></div>';
        body += '<div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 no-padding">' + window.top.returnClose + '</div>';
        body += '</div>';
    }

    body += ' <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="cursor: pointer;" onclick="printDetailFund(' + code + ')">';
    body += '<div class="col-lg-2 col-md-2 col-sm-2 col-xs-2"><img src="../img/pages/Product/print.svg" style="width: 24px;" title=" ' + window.top.print + ' "/></div>';
    body += ' <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 no-padding">' + window.top.printProfile + '</div>';
    body += '</div>';
    body += '</div>';
    body += '</div>';

    return body;
}

function showDetail(s, e) {
    visibleIndex = e.visibleIndex;
    var img = $('#grdSanadList_DXDataRow' + visibleIndex + ' td:first svg');
    var isOpenDetail = $($(img).find('path')[0]).attr('d') == 'M12,10.6l8.3,8.3l1.4-1.4l-9-9L12,7.8l-0.7,0.7l-9,9l1.4,1.4L12,10.6z';
    trRow = $('#grdSanadList_DXDataRow' + visibleIndex);
    var trDetail = $('#grdSanadList_DXDRow' + visibleIndex);
    if (isOpenDetail)
        $($(img).find('path')[0]).attr('d', 'M 3.71875 7.78125 L 2.28125 9.21875 L 11.28125 18.21875 L 12 18.90625 L 12.71875 18.21875 L 21.71875 9.21875 L 20.28125 7.78125 L 12 16.0625 Z');
    else
        $($(img).find('path')[0]).attr('d', 'M12,10.6l8.3,8.3l1.4-1.4l-9-9L12,7.8l-0.7,0.7l-9,9l1.4,1.4L12,10.6z');

    if (isOpenDetail) {
        $(trDetail).hide();
    }
    else {
        if ($(trDetail).length == 1)
            $(trDetail).show();
        else {
            grdSanadList.GetRowValues(visibleIndex, 'Code;IsClosed;CanReturn', getRizData);
        }
    }
    throw true;
}

function getDataRizSanad(code, type) {
    ajax({
        loading: true,
        url: "SaleApi/GetDataRizSanad",
        data: { code: code, typePayment: type },
        async: false,
        success: function (result) {
            madeTagDetailRizSanad(result);
        }
    });
}

function madeTagDetailRizSanad(model) {
    $('#thHeaderRizSanad').empty();
    $('#tbRizSanad').empty();
    $('#divBodyModel').empty();
    var tagHeader = "";
    var tagBody = "";
    if (typeSanad == 1)//bank
    {
        tagHeader += '<tr style="display: block; width: 100%; padding: 4px 0; height: 28px;border-bottom: 2px solid #ccc;">';
        tagHeader += '<td class="stbl-td" style="width: 15%; text-align: right; display: inline-block; padding: 0px 6px;">';
        tagHeader += '<label class="lbl-sandogh-riz">' + window.top.factorNumber + '</label></td>';
        tagHeader += '<td class="stbl-td" style="width: 25%; text-align: right; display: inline-block;padding: 0px 6px;">';
        tagHeader += '<label class="lbl-sandogh-riz">' + window.top.customerName + '</label></td>';
        tagHeader += '<td class="stbl-td" style="width: 20%; text-align: right; display: inline-block; padding: 0px 6px;">';
        tagHeader += '<label class="lbl-sandogh-riz">' + window.top.bankCode + '</label></td>';
        tagHeader += '<td class="stbl-td" style="width: 20%; text-align: right; display: inline-block;padding: 0px 6px;">';
        tagHeader += '<label class="lbl-sandogh-riz">' + window.top.amount + ' </label>';
        tagHeader += '</td>';
        tagHeader += '<td class="stbl-td" style="width: 14%; text-align: right; display: inline-block;padding: 0px 6px;">';
        tagHeader += '<label class="lbl-sandogh-riz">' + window.top.trackingCode + '</label></td>';
        tagHeader += '</tr>';
        $('#exampleModalLabel').text(window.top.listCardReader);
    }
    else if (typeSanad == 2) { // check 
        tagHeader += '<tr style="display: block; width: 100%; padding: 4px 0; height: 28px;border-bottom: 2px solid #ccc;">';
        tagHeader += '<td class="stbl-td" style="width: 15%; text-align: right; display: inline-block;padding: 0px 6px;">';
        tagHeader += '<label class="lbl-sandogh-riz">' + window.top.factorNumber + '</label></td>';
        tagHeader += '<td class="stbl-td" style="width: 25%; text-align: right; display: inline-block; padding: 0px 6px;">';
        tagHeader += '<label class="lbl-sandogh-riz">' + window.top.customerName + '</label></td>';
        //tagHeader += '<td class="stbl-td" style="width: 10%; text-align: right; display: inline-block;padding: 0px 6px;">';
        //tagHeader += '<label class="lbl-sandogh-riz">' + window.top.Datee +'</label></td>';
        tagHeader += '<td class="stbl-td" style="width: 20%; text-align: right; display: inline-block;padding: 0px 6px;">';
        tagHeader += '<label class="lbl-sandogh-riz">' + window.top.bankName + ' </label></td>';
        tagHeader += '<td class="stbl-td" style="width: 20%; text-align: right; display: inline-block;padding: 0px 6px;">';
        tagHeader += '<label class="lbl-sandogh-riz">' + window.top.amount + '</label></td>';
        tagHeader += '<td class="stbl-td" style="width: 15%; text-align: right; display: inline-block;padding: 0px 6px;">';
        tagHeader += '<label class="lbl-sandogh-riz">' + window.top.checkNumber + ' </label></td>';

        tagHeader += '</tr>';
        $('#exampleModalLabel').text(window.top.checkListReceived);
    }

    $('#thHeaderRizSanad').append(tagHeader);

    if (model.length != 0) {

        for (var i = 0; i < model.length; i++) {
            if (typeSanad == 1) {
                tagBody += '<tr class="stbl-tr odd " style="height: 28px;border-bottom: 1px solid #ccc;" >';
                tagBody += '<td class="stbl-td" style="width: 15%; text-align: right; display: inline-block;padding: 0px 6px;margin: 8px 0 2px;">';
                tagBody += ' <label class="lbl-sandogh-riz">' + model[i].CodeSanad + '</label></td>';
                tagBody += '<td class="stbl-td" style="width: 25%; text-align: right; display: inline-block;padding: 0px 6px;margin: 8px 0 2px;">';
                tagBody += '<label class="lbl-sandogh-riz">' + (model[i].NameTarafH !== null ? model[i].NameTarafH : window.top.miscellaneousCustomer) + '</label>';
                tagBody += '</td>';
                tagBody += '<td class="stbl-td" style="width: 20%; text-align: right; display: inline-block;padding: 0px 6px;margin: 8px 0 2px;">';
                tagBody += '<label class="lbl-sandogh-riz">' + (model[i].NameBank !== null ? model[i].NameBank : "") + '</label></td>';
                tagBody += '<td class="stbl-td" style="width: 20%; text-align: right; display: inline-block;padding: 0px 6px;margin: 8px 0 2px;">';
                tagBody += '<label class="lbl-sandogh-riz">' + numberWithCommas(model[i].Mablagh.toString()) + '</label></td>';
                tagBody += '<td class="stbl-td" style="width: 10%; text-align: right; display: inline-block;padding: 0px 6px;margin: 8px 0 2px;">';
                tagBody += '<label class="lbl-sandogh-riz">' + (model[i].CodePeigiri || "") + '</label>';
                tagBody += '</td>';
                tagBody += '</tr>';
            }
            else if (typeSanad == 2) {
                tagBody += '<tr class="stbl-tr odd" style="height: 28px;border-bottom: 1px solid #ccc;">';
                tagBody += '<td class="stbl-td" style="width: 15%; text-align: right; display: inline-block;padding: 0px 6px;margin: 8px 0 2px;">';
                tagBody += ' <label class="lbl-sandogh-riz">' + model[i].CodeSanad + '</label></td>';
                tagBody += '<td class="stbl-td" style="width: 25%; text-align: right; display: inline-block;padding: 0px 6px;margin: 8px 0 2px;">';
                tagBody += '<label class="lbl-sandogh-riz">' + (model[i].NameTarafH !== null ? model[i].NameTarafH : window.top.miscellaneousCustomer) + '</label>';
                tagBody += '</td>';
                tagBody += '<td class="stbl-td" style="width: 20%; text-align: right; display: inline-block;padding: 0px 6px;margin: 8px 0 2px;">';
                tagBody += '<label class="lbl-sandogh-riz">' + (model[i].NameBank !== null ? model[i].NameBank : "") + '</label></td>';
                tagBody += '<td class="stbl-td" style="width: 20%; text-align: right; display: inline-block;padding: 0px 6px;margin: 8px 0 2px;">';
                tagBody += '<label class="lbl-sandogh-riz">' + numberWithCommas(model[i].Mablagh.toString()) + '</label></td>';
                tagBody += '<td class="stbl-td" style="width: 10%; text-align: right; display: inline-block;padding: 0px 6px;margin: 8px 0 2px;">';
                tagBody += '<label class="lbl-sandogh-riz">' + (model[i].CodePeigiri || "") + '</label>';
                tagBody += '</td>';
                tagBody += '</tr>';
            }
        }
        $('#tbRizSanad').append(tagBody);
    }
    else {
        $('#divBodyModel').append("<lable>" + window.top.noRowsToDisplay + "</lable>")

    }
}

function ShowRizInfo(code, type) {
    codeSanad = code;
    typeSanad = type;
    getDataRizSanad(codeSanad, typeSanad);
    $('#rizSanadModal').modal();
}

function openModalCloseFund(Value) {
    var title = window.top.closeCash;
    var src = "Sale/ShopCashOpenClose?code=" + Value + "&type=1";
    openInModal('', src, title);
}

function OpenLastPosSystem(Value) {
    if (confirm(window.top.confirmMsg)) {
        ajax({
            loading: true,
            url: "SaleApi/OpenLastPosSystem",
            data: { code: Value },
            success: function (result) {
                showMsgModal(2, result.FinalMessage);
                loadGrid();
            }
        });
    }
}

function printDetailFund(code) {
    ajax({
        loading: true,
        url: "SaleApi/PrintSandoghList",
        data: { code: code, },
        async: false,
        success: function (result) {
            var link = document.createElement('a');
            link.download = window.top.downloadDocument;
            link.href = result;
            link.click();
            $(link).remove();
        }
    });
}