var fileBase64 = '';
var state = getQuery("state");
$(document).ready(function () {
    $("#grdDskwor").gridBuilder().onExportClick(function () {
        loadGrid("grdDskwor", { exportExcel: true });
    });
    $('input[type=file]').change(function (event) {
        var fileName = $(this).val().replace(/C:\\fakepath\\/i, '');
        $('#txtPath').val(fileName);
        if (this.files.length > 0) {
            var file = event.target.files[0];
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function () {
                fileBase64 = reader.result;
            };
        }
    });
    if (state === "0")
        loadGrid();
});
function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    var data;
    if (state === "1")
        data = { file: fileBase64 };
    else
        data = { code: getQuery("code") };
    ajax({
        loading: true,
        url: "SalaryApi/" + (state === "1" ? "GetInsuranceFile" : "GetInsuranceDiskInfo"),
        data: data,
        success: function (result) {
            if (isExportExcel) {
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            }
            else {
                $("#grdDskwor").gridBuilder().loadData(result.Dskwor);
                $("#grdDskkar").gridBuilder().loadData(result.Dskkar);
            }
        }
    });
}
/*function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    if (!fileBase64) {
        alert("فایل خود را انتخاب کنید");
        return;
    }
    ajax({
        loading: true,
        url: "SalaryApi/GetFileInsurance",
        data: { file: fileBase64 },
        success: function (result) {
            if (!gridId) {
                $("#grdDskwor").gridBuilder().loadData(result.Dskwor);
            }
            else {
                var gridBuilder = $("#" + gridId).gridBuilder();
                var data = result.Dskwor;
                if (isExportExcel) {
                    exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
                }
                else {
                    gridBuilder.loadData(data);
                }
            }
        }
    });
}*/
function getDownloadFile() {
    ajax({
        url: "SalaryApi/CreateInsuranceFile",
        data: { SummaryFormCode: getQuery("code"), listType: $("#txtDSK_KIND").val(), listNo: $("#txtDSK_LISTNO").val(), listDesc: $("#txtDSK_DISC").val(), monpym: $("#txtMON_PYM").val() },
        success: function (result) {
            var fileName = 'filebime';
            var link = document.createElement('a');
            link.download = fileName;
            link.href = result.ReturnValue;
            link.click();
            $(link).remove();
        }
    });
}
function nextStep1() {
    if (fileBase64 != '') {
        $('#step1').hide();
        $('#step2').show();
        loadGrid();
    }
    else
        alert("فایل خود را انتخاب کنید");
}

function nextStep2() {

    if ($("#txtDSK_KIND").val() != "" && $("#txtDSK_LISTNO").val() != "" && $("#txtDSK_DISC").val() != '' && $("#txtMON_PYM").val() != "") {
        $('#step2').hide();
        $('#step3').show();
        getDownloadFile();
    }
    else {
        alert("جاهای های خالی را پر کنید");
    }
}
function getFileInsuremnet() {
    if (strFile) {
        ajax({
            url: "Salary/SummaryFormInsurance/GetFileInsuremnrt",
            data: JSON.stringify({
                file: strFile,
            }),
            success: function (result) {
                json = eval(result.d);
                nextViewLevel();
            }
        });
    }
    else {
        showMsgModal(0, window.top.selectFile);
    }
}

function setDataToGridDskKar(item, grid) {
    for (var i = 0; i < item.length; i++) {
        grid.AddNewRow();
        grid.batchEditApi.EndEdit();
        var trs = getGridTableRows(grid);
        var lastRow = getGridRowId($(trs[trs.length - 1]));
        for (var j = 0; j < grid.GetColumnsCount(); j++) {
            var column = grid.GetColumn(j);
            grid.batchEditApi.SetCellValue(lastRow, column.fieldName, item[i][column.fieldName]);
        }
    }
}
