var gridBuilder;
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onDblClick(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            return;
        }
        openModal(gridBuilder.getCellValue(rowIndex, "Code"));
    });
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    loadGrid();

    var input = document.createElement("input");
    $("body").append(input);
    $(input).attr("type", "file");
    $(input).hide();
    $(input).on("change", function (event) {
        if (this.files.length > 0) {
            var file = event.target.files[0];
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function () {
                ajax({
                    loading: true,
                    url: "GeneralApi/ImportExcel",
                    data: {
                        key: "Customer",
                        base64: reader.result
                    },
                    success: function () {
                        showMsgModal(2, window.top.doneSuccess);
                        loadGrid();
                    }
                });

            };
        }
    });
    $(".sample-excel").on("click", function () {
        ajax({
            loading: true,
            url: "SaleApi/ExportFileSample",
            data: { key: "Customer" },
            success: function (fileUrl) {
                if (fileUrl) {
                    var link = document.createElement("a");
                    link.download = "sample-customer.xlsx";
                    link.href = fileUrl;
                    link.click();
                    $(link).remove();
                }
            }
        });
    });
    $(".import-excel").on("click", function () {
        $("input[type='file']").click();
    });
    imgToSvg();
});
function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        gridBuilder: gridBuilder,
        url: "GeneralApi/GetCustomerList",
        data: {
            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 edit() {
    gridBuilder.dblClick();
}
function openModal(code) {
    var title = code === -1 ? window.top.people : window.top.editPeople;
    var src = "General/AccountSave" + (code > 0 ? "?code=" + code : "");
    openInModal('', src, title, 900, "", false);
}
function openTurnover() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0) {
        showMsgModal(0, window.top.itemNotSelect);
        return;
    }
    var code = gridBuilder.getCellValue(rowIndex, "Code");
    var src = "General/AccountSubSystem?code=" + code;
    openInTab(src, window.top.personTurnover, "");
}
function deleteRow() {
    if (gridBuilder.getActiveRowIndex == -1) {
        showMsgModal(0, window.top.itemNotSelect);
        return;
    }
    if (confirm(window.top.deleteConfirm)) {
        var rowIndex = gridBuilder.getActiveRowIndex();
        var code = gridBuilder.getCellValue(rowIndex, "Code");
        ajax({
            loading: true,
            url: "GeneralApi/DeleteCustomer",
            data: { code: code },
            success: function (result) {
                showMsgModal(2, window.top.deletedSuccessfully);
                loadGrid();
            }
        });
    }
}
function print() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0)
        return;
    var code = gridBuilder.getCellValue(rowIndex, "Code");
    var currentYear = window.top.$("#hfCurrentYear").val();
    src = "Report/Default?GrpName=FormTarafHesab&AzSalMali=" + currentYear + "&CodeTarafH=" + code;
    openInModal('', src, window.top.report, "", "", false);
}