$(document).ready(function () {

    $("#jqxLoader").jqxLoader({ width: 100, height: 60, imagePosition: 'top' });
    var gridBankVoucher = $("#grid");
    var rowindexc;
    var rowidnew;
    var gridf5view = $("#gridf5view");
    var EditId = 0;
    //TotalWidthSetting
    var totalwidth = 10;
    var WindowWidthFull = screen.width - percentage(screen.width, 14);
    var WindowHeightFull = screen.height - percentage(screen.height, 15);
    var WindowWidth = screen.width - percentage(screen.width, 15);
    var WindowHeight = $(this).height() - 150;
    var gridHeight = WindowHeight - 20;
    //region gridcolumnname

    var dtpfromdate = $("#dtpfromdate1");
    dtpfromdate.jqxDateTimeInput({ formatString: 'dd/MM/yyyy', height: '20px', width: '60%' });
    var lblfunctionname = $("#lblfunctionname");
    var lblfundate = $("#lblfundate");
    var lblbalanceamt = $("#lblbalanceamt");
    lblbalanceamt.hide();
    lblfunctionname.hide();
    lblfundate.hide();
    var grdSeriolNo = "RefNo";
    var grdCustomerName = "CustomerName";
    var grdCash = "CashAmount";
    var grdRTGSAmt = "RTGSAmount";
    var grdRTGSNo = "RTGSNo";
    var grdRTGSDate = "RTGSdate";
    var grdChequeNo = "ChequeNo";
    var grdChequeDate = "Chequedate";
    var grdChequeAmt = "ChequeAmount";
    var grdAmount = "Amount";
    var grdCardAmt = "CardAmount";
    var grdId = "Id";
    var grdMahalId = "MahalRegisterMasterRefId";
    var grdEditMode = "EditMode";
    //endregion
    var cellclass = function (row, datafield, value, rowdata) {
        if (rowdata.FreeQtyStatus == 1) {
            return "editedRow";
        }

    }
    var gridcolumns = [
        {
            text: 'S.No', sortable: false, filterable: false, editable: false,
            groupable: false, draggable: false, resizable: false,
            datafield: '', columntype: 'number', width: 50, hidden: false, pinned: true,
            cellsrenderer: function (row, column, value) {
                return "<div style='margin:4px;'>" + (value + 1) + "</div>";
            }
        },
        { text: 'SeriolNo', datafield: grdSeriolNo, width: 100, hidden: false, pinned: true, cellsalign: 'right', cellclassname: cellclass },
        { text: 'CustomerName', datafield: grdCustomerName, width: 250, hidden: false, pinned: true },
        { text: 'Cash', datafield: grdCash, width: 100, hidden: false, pinned: true, cellsalign: 'right' },
        { text: 'RTGSNo', datafield: grdRTGSNo, width: 100, hidden: false, pinned: true, cellsalign: 'right'},
        { text: 'RTGSDate', datafield: grdRTGSDate, width: 110, hidden: false, columntype: 'datetimeinput', pinned: false, culture: 'as-IN', formatString: 'dd/MM/yyyy', cellsformat: 'dd/MM/yyyy', max: new Date(2050, 0, 1), editMode: 'full' },
        { text: 'RTGSAmt', datafield: grdRTGSAmt, width: 100, hidden: false, pinned: true, cellsalign: 'right' },
        { text: 'ChequeNo', datafield: grdChequeNo, width: 100, hidden: false, pinned: true, cellsalign: 'right' },
        { text: 'ChequeDate', datafield: grdChequeDate, width: 100, hidden: false, cellclassname: cellclass, columntype: 'datetimeinput', pinned: false, culture: 'as-IN', formatString: 'dd/MM/yyyy', cellsformat: 'dd/MM/yyyy', max: new Date(2050, 0, 1), editMode: 'full' },
        { text: 'ChequeAmt', datafield: grdChequeAmt, width: 100, hidden: false, pinned: true, cellsalign: 'right' },
        { text: 'CardAmt', datafield: grdCardAmt, width: 100, hidden: false, pinned: true, cellsalign: 'right' },
        { text: 'Amount', datafield: grdAmount, width: 100, hidden: false, pinned: true, cellsalign: 'right' },
        { text: 'MahalId', datafield: grdMahalId, width: 80, hidden: true, pinned: false },
        { text: 'Id', datafield: grdId, width: 100, hidden: true, pinned: false },
    ]

    $("#txtEditpassword").jqxPasswordInput({ width: 70, height: 15 });
    $("#txtEditpassword").bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {
            if ($("#txtEditpassword").val() != "" && $("#txtEditpassword").val() != null) {
                var type = "";
                if (PasswordType == 1) {
                    type = "EditPassword";
                }
                else if (PasswordType == 0) {
                    type = "FormConfig";
                }
                else if (PasswordType == 2) {
                    type = "AdminPower";
                }
                $.ajax({
                    async: false,
                    url: "/Master/EditPassword",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: '{"password":"' + $("#txtEditpassword").val() + '","type":"' + type + '"}',
                    //data: datavalue,
                    success: function (data) {
                        if (data.ok == true) {
                            $('#LockEditWindow').jqxWindow('Close');
                        }
                        else {
                            alert("Invaild Password !!!.");
                        }
                    },
                    error: function (error) {
                        MsgBox('Technical Fault Contact Software Vendor  !!!.');
                    }
                });
            }
        }
    });
    var PasswordType = 0;
    function EditPasswordWindow(type) {
        var titlenew = "";
        if (type == 1) {
            titlenew = "Edit Pwd";
        }
        else if (type == 0) {
            titlenew = "Form Pwd";
        }
        else if (type == 2) {
            titlenew = "Admin Pwd";
        }
        let centerX = (screen.width - 450) / 2;
        let centerY = (screen.height - 450) / 2;

        $('#LockEditWindow').jqxWindow({
            position: { x: centerX, y: centerY },
            showCollapseButton: false,
            showCloseButton: false,
            height: 150,
            width: 120,
            title: titlenew,
            isModal: true,
            keyboardCloseKey: 0,
            resizable: false,
            initContent: function () {
            }
        });
        $('#LockEditWindow').jqxWindow('Open');
        setTimeout(function () {
            $('#txtEditpassword').val('');
            $("#txtEditpassword").focus();
        }, 250);

    }
    var Presskey = "";
    $('#LockEditWindow').on('close', function (event) {
        setTimeout(function () {
            if (Presskey == "View") {
                methods.loadgrid(objlistnew);
                gridBankVoucher.jqxGrid('focus');
            }
            if (Presskey == "Delete") {
                var temp = gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdCustomerName);
                var value = gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdId);
                var str = "Wish to Delete the Record " + temp + "?";
                MsgBoxYesNo(str).then(function (reply) {
                    if (reply == "Yes") {
                        loaderopen();
                        $.ajax({

                            url: "/Transaction/DeleteCustomerPayment",
                            type: "POST",
                            dataType: "json",
                            cache: false,
                            contentType: "application/json; charset=utf-8",
                            data: '{"Id":' + value + '}',
                            success: function (data) {
                                if (data.ok) {
                                    loaderclose();
                                    NotificationSuccess(data.message);
                                    var rowcount = gridBankVoucher.jqxGrid('getdatainformation').rowscount;
                                    DeleteRow(gridBankVoucher, rowindexc, 0, grdId, rowcount, 0);
                                    methods.calculation();
                                    setTimeout(function () {
                                        var rowscount = gridBankVoucher.jqxGrid('getdatainformation').rowscount;
                                        gridBankVoucher.jqxGrid('selectcell', rowscount - 1, grdSeriolNo);
                                        gridBankVoucher.jqxGrid('focus');
                                    }, 50);
                                }
                                else {
                                    loaderclose();
                                    MsgBox(data.message);
                                }
                            },
                            error: function (error) {
                                loaderclose();
                                MsgBox('Technical Fault Contact Software Vendor  !!!.');
                            }
                        });
                    }
                });
            }

        }, 250);

    });
    gridBankVoucher.bind('keypress', function (event) {
        var e = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (e == '13') {
            return;
        }
        var cell = gridBankVoucher.jqxGrid('getselectedcell');
        var rowindex = cell.rowindex;
        var columnname = cell.datafield;
        if (columnname == grdSeriolNo || columnname == grdRTGSAmt || columnname == grdChequeAmt || columnname == grdCardAmt || columnname == grdCash) {
            GridKeyPressValidation(gridBankVoucher, event, 18, "float", 2);
        }
        else if (columnname == grdRTGSNo) {
            GridKeyPressValidation(gridBankVoucher, event, 50, "string", 2);
        }
        else if (columnname == grdChequeNo || columnname == grdRTGSNo) {
            GridKeyPressValidation(gridBankVoucher, event, 15, "int", 2);
        }
    });
    gridBankVoucher.on("celldoubleclick", function (event) {
        var cell = gridBankVoucher.jqxGrid('getselectedcell');
        var rowindex = cell.rowindex;
        var columnname = cell.datafield;
        //if (columnname == grdNarration || columnname == grdRTGSNo) {
        //    gridBankVoucher.jqxGrid({ editmode: 'selectedcell' });
        //}

    });
    
    gridBankVoucher.on('cellselect', function (event) {
        var cell = gridBankVoucher.jqxGrid('getselectedcell');
        var rowindex = cell.rowindex;
        var columnname = cell.datafield;
        //if (columnname == grdAmount || columnname == grdRTGSAmt || columnname == grdChequeNo || columnname == grdRTGSNo) {
        //    GridEnterMode = true;
        //}

        if (columnname == grdRTGSDate || columnname == grdChequeDate) {
            gridBankVoucher.jqxGrid({ editmode: 'selectedcell' });
        }
    });

    gridBankVoucher.on('cellendedit', function (event) {
        var cell = gridBankVoucher.jqxGrid('getselectedcell');
        var rowindex = cell.rowindex;
        var columnname = cell.datafield;
        //if (columnname != grdAccountId) {
        //    gridBankVoucher.jqxGrid({ editmode: 'programmatic' });
        //}
    });
   
    
    dtpfromdate.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {
            var rowscount = gridBankVoucher.jqxGrid('getdatainformation').rowscount;
            gridBankVoucher.jqxGrid('selectcell', rowscount - 1, grdSeriolNo);
            gridBankVoucher.jqxGrid('focus');
        }
    });

    gridBankVoucher.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '8') {
            gridBankVoucher.trigger('keypress');
        }
        if (key == '13') {
            var cell = gridBankVoucher.jqxGrid('getselectedcell');
            var rowcount = gridBankVoucher.jqxGrid('getrows').length;
            rowindexc = cell.rowindex;
            var columnname = cell.datafield;
            var rowscount = gridBankVoucher.jqxGrid('getdatainformation').rowscount;
            var columnindex = gridBankVoucher.jqxGrid('getcolumnindex', cell.datafield);
            var gridcount = gridBankVoucher.jqxGrid('getrows');
            var editable = gridBankVoucher.jqxGrid('endcelledit', rowindexc, cell.datafield, false);
            var value = gridBankVoucher.jqxGrid('getcellvalue', rowindexc, cell.datafield);
            if (columnname == grdSeriolNo) {
                var SeriolNo = gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdSeriolNo);
                $.ajax({
                    url: "/Transaction/SelectSeriolNumber",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: '{"No":' + SeriolNo + '}',
                    success: function (data) {
                        getdata = data.master;

                        $('#jqxLoader').jqxLoader('close');
                        if (getdata.length != 0) {
                            lblbalanceamt.show();
                            lblfunctionname.show();
                            lblfundate.show();
                            lblfunctionname.text("Function Name: " + getdata[0].FunctionName);
                            var dateString = getdata[0].FunctionDate;
                            var StartDateServer = dateString;
                            var parsedDate = new Date(parseInt(StartDateServer.substr(6)));
                            var finalDate = parsedDate.toLocaleDateString();
                            var input = finalDate;
                            var output = input.replace(/(\d\d)\/(\d\d)\/(\d{4})/, "$2-$1-$3");
                            lblfundate.text("Function Date: " + output);
                            lblbalanceamt.text("Balacne Amt: " + getdata[0].Balance);
                            var CustomerName = getdata[0].Name;
                            var Id = getdata[0].Id;
                            gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdMahalId, Id);
                            gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdCustomerName, CustomerName);
                            gridBankVoucher.jqxGrid('selectcell', rowindexc, grdCash);
                            gridBankVoucher.jqxGrid('focus');
                        }
                        else {
                            MsgBox("Invalid Bill No!!!..");
                            return;
                        }
                    },
                    error: function (error) {
                        $('#jqxLoader').jqxLoader('close');
                        MsgBox('Technical Fault Contact Software Vendor  !!!.');
                    }
                }).done(function () {
                });

            }
            else if (columnname == grdCash) {

                gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdCash, ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdCash)).toFixed(2));
                gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdAmount, ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdCash)).toFixed(2));
                var Cash = ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdCash));
                if (Cash == 0) {
                    gridBankVoucher.jqxGrid('selectcell', rowindexc, grdRTGSNo);
                    gridBankVoucher.jqxGrid('focus');
                }
                else {
                  
                    gridBankVoucher.jqxGrid('selectcell', rowindexc, grdAmount);
                    gridBankVoucher.jqxGrid('focus');
                }


            }
            else if (columnname == grdRTGSNo) {

                gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdRTGSNo, ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdRTGSNo)).toFixed(2));
                gridBankVoucher.jqxGrid('selectcell', rowindexc, grdRTGSAmt);
                gridBankVoucher.jqxGrid('focus');

            }
            else if (columnname == grdAmount) {

                var Cash = ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdCash));
                if (Cash == 0) {
                    gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdAmount, ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdAmount)).toFixed(2));
                    gridBankVoucher.jqxGrid('selectcell', rowindexc, grdRTGSDate);
                    gridBankVoucher.jqxGrid('focus');
                }
                else {

                    if (rowscount - 1 == rowindexc) {
                        addrow(gridBankVoucher);
                        gridBankVoucher.jqxGrid('selectcell', rowscount, grdSeriolNo);
                        gridBankVoucher.jqxGrid('focus');
                    }
                    else {
                        //  addrow(gridsale);
                        gridBankVoucher.jqxGrid('selectcell', rowscount - 1, grdSeriolNo);
                        gridBankVoucher.jqxGrid('focus');

                    }
                }

            }
            else if (columnname == grdRTGSAmt) {

                gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdRTGSAmt, ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdRTGSAmt)).toFixed(2));
                var RtgAmt = gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdRTGSAmt);
                if (RtgAmt == 0) {
                    gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdAmount, ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdRTGSAmt)).toFixed(2));
                    gridBankVoucher.jqxGrid('selectcell', rowindexc, grdChequeNo);
                    gridBankVoucher.jqxGrid('focus');

                }
                else {
                    gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdAmount, ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdRTGSAmt)).toFixed(2));
                    gridBankVoucher.jqxGrid('selectcell', rowindexc, grdAmount);
                    gridBankVoucher.jqxGrid('focus');

                }

            }
            else if (columnname == grdChequeNo) {
                gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdChequeNo, ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdChequeNo)).toFixed(2));
                gridBankVoucher.jqxGrid('selectcell', rowindexc, grdChequeAmt);
                gridBankVoucher.jqxGrid('focus');
            }
            else if (columnname == grdChequeAmt) {

                gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdChequeAmt, ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdChequeAmt)).toFixed(2));
                var CheAmt = gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdChequeAmt);
                if (CheAmt == 0) {
                    gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdAmount, ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdChequeAmt)).toFixed(2));
                    gridBankVoucher.jqxGrid('selectcell', rowindexc, grdCardAmt);
                    gridBankVoucher.jqxGrid('focus');
                }
                else {
                    gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdAmount, ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdChequeAmt)).toFixed(2));
                    gridBankVoucher.jqxGrid('selectcell', rowindexc, grdAmount);
                    gridBankVoucher.jqxGrid('focus');
                }
            }
            else if (columnname == grdCardAmt) {
                gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdCardAmt, ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdCardAmt)).toFixed(2));
                gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdAmount, ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdCardAmt)).toFixed(2));
                gridBankVoucher.jqxGrid('selectcell', rowindexc, grdRTGSDate);
                gridBankVoucher.jqxGrid('focus');
            }
            else if (columnname == grdRTGSDate) {
                //gridBankVoucher.jqxGrid('setcellvalue', rowindexc, grdRTGSDate, ValNum(gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdRTGSDate)));
                gridBankVoucher.jqxGrid('selectcell', rowindexc, grdChequeDate);
                gridBankVoucher.jqxGrid('focus');
            }
            else if (columnname == grdChequeDate) {

                if (rowscount - 1 == rowindexc) {
                   // var tempamount = gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdChequeAmount);
                    addrow(gridBankVoucher);
                    gridBankVoucher.jqxGrid('selectcell', rowscount, grdSeriolNo);
                    gridBankVoucher.jqxGrid('focus');
                }
                else {
                    //  addrow(gridsale);
                    gridBankVoucher.jqxGrid('selectcell', rowscount - 1, grdSeriolNo);
                    gridBankVoucher.jqxGrid('focus');

                }

            }
            else if (key === 46) {
                var cell = gridBankVoucher.jqxGrid('getselectedcell');
                var rowcount = gridBankVoucher.jqxGrid('getdatainformation').rowscount;
                rowindexc = cell.rowindex;
                var columnname = cell.datafield;
                var columnindex = gridBankVoucher.jqxGrid('getcolumnindex', cell.datafield);
                var value = gridBankVoucher.jqxGrid('getcellvalue', rowindexc, grdId);
                if (value != 0 && value != null) {
                    if (pagedelete == 1) {
                        Presskey = "Delete";
                        PasswordType = 1;
                        EditPasswordWindow(1);


                    }
                    else {
                        MsgBox("Page Delete Permission Denied !!!.");
                        return;
                    }
                }
                else {

                    DeleteRow(gridBankVoucher, rowindexc, columnindex, grdAccountId, rowcount, 1);
                    var rowscount = gridBankVoucher.jqxGrid('getdatainformation').rowscount;
                    gridBankVoucher.jqxGrid('selectcell', rowscount - 1, grdSeriolNo);
                    gridBankVoucher.jqxGrid('focus');
                }
            }
        }
    });

    $(document).on('keydown', function (e) {
        //F1 Press
        var flag = 1;

        // F1
        if (e.keyCode === 112) {
            e.preventDefault();
           methods.CustomerPaymentSave();
        }


        ////F2 Press
        if (e.keyCode === 113) {
            e.preventDefault();
            

        }
        if (e.keyCode === 114) {
            e.preventDefault();
           
        }
        //Esc Press
        if (e.keyCode === 27) {
            e.preventDefault();
            if ($("#LockEditWindow").jqxWindow('isOpen') == false) {
                var str = 'Do You Want To Quit Page?'
                MsgBoxYesNo(str).then(function (reply) {
                    if (reply == "Yes") {
                        window.location.href = "/Login/Home";
                    }
                });
            }
        }
        //F5
        if (e.keyCode === 116) {
            if (testing == false) {
                e.preventDefault();
            }
            $("#dtpfromdate").jqxDateTimeInput({ formatString: 'dd/MM/yyyy', height: '22px', width: '100%' });
            $("#dtptodate").jqxDateTimeInput({ formatString: 'dd/MM/yyyy', height: '22px', width: '100%' });
            $("#btnview").jqxButton({ width: 100, height: 25, value: "View", textPosition: "center" });
            $("#dtpfromdate").jqxDateTimeInput('setDate', new Date());
            $("#dtptodate").jqxDateTimeInput('setDate', new Date());
            var objlist = [{ fromdate: $("#dtpfromdate").jqxDateTimeInput('getDate'), todate: $("#dtptodate").jqxDateTimeInput('getDate'), Id: 0 }];
            methods.F5View(objlist);
        }


        //F10
        if (e.keyCode === 121) {
            e.preventDefault();
            var str = "Do you Want Clear The Form";
            MsgBoxYesNo(str).then(function (replay) {
                if (replay == "Yes") {
                    methods.Clear();
                }
                else {
                    return;
                }
            });

        }
    });

    $(document).on('click', "#btnview", function () {
        var sid = 0;

        if (dateformat(dateformatIN($("#dtpfromdate").jqxDateTimeInput('getDate'))) > dateformat(dateformatIN($("#dtptodate").jqxDateTimeInput('getDate')))) {
            MsgBox("From Date Is Greater Than To Date");
            return;
        }
        var objlist = [{ fromdate: $("#dtpfromdate").jqxDateTimeInput('getDate'), todate: $("#dtptodate").jqxDateTimeInput('getDate'), Id: 0 }];
        methods.F5View(objlist);
    });

    methods = (function () {
        return {
            init: function () {
            
               // methods.createElements();
              //  $("#jqxWidget").css('visibility', 'visible');
                methods.Clear();
                var data = [];
                methods.loadgrid(data);
            },
            Addrowfunc: function () {
                addrow(gridBankVoucher);
                var rowscount = gridBankVoucher.jqxGrid('getdatainformation').rowscount;
                gridBankVoucher.jqxGrid('selectcell', rowscount - 1, grdAccountId);
                gridBankVoucher.jqxGrid('focus');
            },
            createElements: function () {

                var jqxWidget = $('#jqxWidget');
                var offset = jqxWidget.offset();
                $('#windowmain').jqxWindow({
                    resizable: false,
                    minWidth: 400,
                    minHeight: 200,
                    width: 1500,
                    keyboardCloseKey: 0,
                    maxWidth: WindowWidthFull,
                    maxHeight: WindowHeightFull,
                    height: WindowHeight,
                    zIndex: 50

                });
                $('#windowmain').jqxWindow('focus');
                $("#jqxWidget").css('visibility', 'visible');
            },
            Escape: function () {
                window.open("/Login/Home", "_self");
            },
            F5View: function (objlist) {
                var masterdata = {};
                var detailsdata = {};
                $.ajax({
                    async: false,
                    url: "/Transaction/CustomerPaymentF5View",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(objlist),
                    success: function (data) {
                        masterdata = data[0].CustomerPaymentMaster;
                        detailsdata = data[0].CustomerPaymentDetails;
                    },
                    error: function (error) {
                        MsgBox('Technical Fault Contact Software Vendor  !!!.');
                    }
                });

                var source =
                {
                    localdata: masterdata,
                    datatype: "local",
                    datafields:
                        [
                            { name: 'RefNo', type: 'number' },
                            { name: 'CashAmount', type: 'number' },
                            { name: 'ChequeAmount', type: 'number' },
                            { name: 'CardAmount', type: 'number' },
                            { name: 'TotalAmount', type: 'number' },

                        ],
                    id: 'RefNo' //MasterData Id
                };
                var masterdataAdapter = new $.jqx.dataAdapter(source);
                //Details Source
                //Details are creating two times **I it Orders **II Creates Columns

                var detailssource =
                {
                    localdata: detailsdata,
                    datatype: "local",
                    datafields:
                        [
                            { name: 'RefNo', type: 'number' },
                            { name: 'Cashamount', type: 'number' },
                            { name: 'chequeAmount', type: 'number' },
                            { name: 'CardAmount', type: 'number' },
                            { name: 'ChequeNo', type: 'string' },
                            { name: 'ChequeDate', type: 'date' },
                            
                        ],
                    id: 'RefNo' //Details Id
                };
                var detailsDataAdapter = new $.jqx.dataAdapter(detailssource, { autoBind: true });
                var orders = detailsDataAdapter.records;
                var nestedGrids = new Array();
                var initrowdetails = function (index, parentElement, gridElement, record) {
                    var id = record.uid.toString();
                    var grid = $($(parentElement).children()[0]);
                    nestedGrids[index] = grid;
                    var filtergroup = new $.jqx.filter();
                    var filter_or_operator = 1;
                    var filtervalue = id;
                    var filtercondition = 'equal';
                    var filter = filtergroup.createfilter('stringfilter', filtervalue, filtercondition);
                    var ordersbyid = [];
                    for (var m = 0; m < orders.length; m++) {
                        var result = filter.evaluate(orders[m]["RefNo"]);
                        if (result)
                            ordersbyid.push(orders[m]); //ordering
                    }
                    var orderssource = {
                        datafields:
                            [                                                 //Details list
                                { name: 'RefNo', type: 'number' },
                                { name: 'Cashamount', type: 'number' },
                                { name: 'chequeAmount', type: 'number' },
                                { name: 'CardAmount', type: 'number' },
                                { name: 'ChequeNo', type: 'string' },
                                { name: 'ChequeDate', type: 'date' },
                            ],
                        id: 'RefNo',
                        localdata: ordersbyid
                    }


                    var nestedGridAdapter = new $.jqx.dataAdapter(orderssource);
                    if (grid != null) {                                 //Details Grid Columns Creating
                        grid.jqxGrid({
                            source: nestedGridAdapter, width: '100%',
                            height: '95%',
                            columnsautoresize: true,
                            columnsresize: true,
                            enablekeyboarddelete: false,
                            columns: [
                                { text: 'RefNo', datafield: 'RefNo', width: 150 },
                                { text: 'CashAmount', datafield: 'Cashamount', width: 150 },
                                { text: 'ChequeAmount', datafield: 'chequeAmount', width: 150 },
                                { text: 'CardAmount', datafield: 'CardAmount', width: 150 },
                                { text: 'ChequeNo', datafield: 'ChequeNo', width: 150 },
                                { text: 'ChequeDate', datafield: 'ChequeDate', width: 150, cellsformat: 'dd/MM/yyyy', width: 150 },
                        

                            ]
                        });
                        localizationobj.thousandsseparator = "";
                        grid.jqxGrid('localizestrings', localizationobj);
                    }
                }

                //Master Grid Creating
                gridf5view.jqxGrid(
                    {
                        width: '100%',
                        height: '85%',
                        rowsheight: 25,
                        columnsheight: 25,
                        columnsautoresize: true,
                        columnsresize: true,
                        scrollmode: 'deferred',
                        editable: false,
                        theme: 'classic',
                        showaggregates: true,
                        editmode: 'selectedcell',
                        keyboardnavigation: true,
                        selectionmode: 'singlerow',
                        rowdetails: true,
                        initrowdetails: initrowdetails,
                        enablekeyboarddelete: false,
                        rowdetailstemplate: { rowdetails: "<div id='grid' style='margin: 10px;'></div>", rowdetailsheight: 220, rowdetailshidden: true },
                        source: source,
                        columns: [
                            { text: 'RefNo', datafield: 'RefNo', hidden: false, width: 80 },
                            { text: 'CashAmount', datafield: 'CashAmount', width: 250 },
                            { text: 'ChequeAmount', datafield: 'ChequeAmount', width: 100, cellsalign: 'right', cellsformat: 'd2' },
                            { text: 'CardAmount', datafield: 'CardAmount', width: 100, cellsalign: 'right', cellsformat: 'd2' },
                            { text: 'TotalAmount', datafield: 'TotalAmount', width: 100, cellsalign: 'right', cellsformat: 'd2' },
                        ]
                    });
                var localizationobj = {};
                localizationobj.thousandsseparator = "";
                gridf5view.jqxGrid('localizestrings', localizationobj);
                F5ViewWindow("#F5Viewwindow");
            },
            //Grid Empty Check
            gridemptycheck: function () {
                var griddata = gridBankVoucher.jqxGrid('getrows');
                var rowcount = griddata.length;
                if (gridBankVoucher.jqxGrid('getcellvalue', rowcount - 1, grdId) == "" || gridBankVoucher.jqxGrid('getcellvalue', rowcount - 1, grdId) == null) {
                    if (rowcount > 1) {
                        var id = gridBankVoucher.jqxGrid('getrowid', rowcount - 1);
                        gridBankVoucher.jqxGrid('deleterow', id);
                    }
                }
                griddata = gridBankVoucher.jqxGrid('getrows');
                rowcount = griddata.length;
                for (var i = 0; i <= griddata.length - 1; i++) {
                    if (griddata[i].EditMode == 1) {

                        if (griddata[i].CustomerName == "" || griddata[i].CustomerName == null) {
                            MsgBox("Enter All Account Name in the Grid !!!.");
                            gridBankVoucher.jqxGrid('selectcell', i, grdId);
                            gridBankVoucher.jqxGrid('focus');
                            return false;
                        }
                        if ((griddata[i].Amount == 0 || griddata[i].Amount == null) && (griddata[i].RTGSAmt == 0 || griddata[i].RTGSAmt == null)) {
                            MsgBox("Enter Any one Amount in the Grid !!!.");
                            gridBankVoucher.jqxGrid('selectcell', i, grdId);
                            gridBankVoucher.jqxGrid('focus');
                            return false;
                        }

                    }
                }
                return true;
            },

            Clear: function () {
                gridBankVoucher.jqxGrid('clear');
                addrow(gridBankVoucher);
                lblbalanceamt.text("");
                lblfunctionname.text("");
                lblfundate.text("");
                lblbalanceamt.hide();
                lblfunctionname.hide();
                lblfundate.hide();
                dtpfromdate.jqxDateTimeInput('focus');
            },
            loadgrid: function (data) {
                
                var source =
                {
                    localdata: data,
                    datatype: "local",
                    datafields:
                        [
                            { name: grdId, type: 'number' },
                            { name: grdMahalId, type: 'number' },
                            { name: grdSeriolNo, type: 'string' },
                            { name: grdRTGSDate, type: 'date' },
                            { name: grdCustomerName, type: 'string' },  
                            { name: grdAmount, type: 'number' },
                            { name: grdCash, type: 'string' },
                            { name: grdRTGSNo, type: 'string' },
                            { name: grdRTGSAmt, type: 'number' },
                            { name: grdChequeDate, type: 'date' },
                            { name: grdChequeNo, type: 'string' },
                            { name: grdChequeAmt, type: 'number' },
                            { name: grdCardAmt, type: 'number' },
                            { name: grdEditMode, type: 'number' },
                        ],
                    addrow: function (rowid, rowdata, position, commit) {
                        commit(true);
                        gridBankVoucher.jqxGrid('setcellvalue', rowid, grdCustomerName);
                        rowidnew = rowid;
                        AutoScroll(gridBankVoucher, rowid);
                    },
                    deleterow: function (rowid, commit) {
                        commit(true);
                    },
                    updaterow: function (rowid, newdata, commit) {
                        newdata.EditMode = 1;
                        commit(true);
                    }
                };
                var dataAdapter = new $.jqx.dataAdapter(source);

                gridBankVoucher.jqxGrid(
                    {
                        width: '98%',
                        height: gridHeight,
                        editable: true,
                        theme: 'classic',
                        rowsheight: 25,
                        columnsheight: 25,
                        showaggregates: true,
                        editmode: 'programmatic',
                        keyboardnavigation: true,
                        selectionmode: 'singlecell',
                        source: dataAdapter,
                        columnsresize: true,
                        enablekeyboarddelete: false,
                        columns: [
                            {
                                text: 'S.No', sortable: false, filterable: false, editable: false,
                                groupable: false, draggable: false, resizable: false,
                                datafield: '', columntype: 'number', width: 50,
                                cellsrenderer: function (row, column, value) {
                                    return "<div style='margin:4px;'>" + (value + 1) + "</div>";
                                }
                            }

                        ]
                    });
                var localizationobj = {};
                localizationobj.thousandsseparator = "";
                gridBankVoucher.jqxGrid('localizestrings', localizationobj);
                gridBankVoucher.jqxGrid("render");
                gridBankVoucher.jqxGrid({ columns: gridcolumns });
                addrow(gridBankVoucher);
                var objlistnew = gridBankVoucher.jqxGrid('getrows');
                gridBankVoucher.jqxGrid('selectcell', objlistnew.length - 1, grdSeriolNo);


            },
            CustomerPaymentSave: function () {
                if (methods.gridemptycheck() == false)
                    return;
                var CreditDate1 = dateformat(dtpfromdate.val());
                var BalanceAmt1 = ValNum(lblbalanceamt.text());
                var datec = dtpfromdate.val();
                var griddata = gridBankVoucher.jqxGrid('getrows');
                if (datec != "") {
                    for (var i = 0; i < griddata.length; i++) {
                        griddata[i].RefDate = CreditDate1;
                        griddata[0].status = 1;
                        griddata[0].Active = 0;
                       
                    }
                }

                var str = 'Do you Want to Save The Customer Payment Details?'
                MsgBoxYesNo(str).then(function (reply) {
                    if (reply == "Yes") {
                        $('#jqxLoader').jqxLoader('open');
                        $.ajax({
                            url: "/Transaction/CustomerPaymentSave",
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                            data: JSON.stringify(griddata),
                            success: function (data) {
                                if (data.ok) {
                                    $('#jqxLoader').jqxLoader('close');
                                    NotificationSuccess(data.message);
                                    methods.Clear();

                                }
                                else {
                                    $('#jqxLoader').jqxLoader('close');
                                    // notification("topright", "error", "fa fa-exclamation-circle vd_red", "Error Notification", data.message);
                                    MsgBox(data.message);
                                }

                            },
                            error: function (error) {
                                $('#jqxLoader').jqxLoader('close');
                                MsgBox('Technical Fault Contact Software Vendor  !!!.');
                            }
                        }).done(function () {
                        });
                    }
                });
            },
        };
    })();
    window.methods = methods;
    methods.init();
});