
//$(document).ready(function () {
//$(function () {
   debugger;
//var usermenu = window.LoggedInUserArray[5];
//var usermenu1 = usermenu.split(",");
//var ERPUserId = parseInt(window.LoggedInUserArray[0]);
//var ERPDeptId = parseInt(window.LoggedInUserArray[1]);
var ERPUserId = 1;
var ERPDeptId = 1;




function GetListNew(var2) {



  //  var   var2 = "##";
    //if (usermenu1.indexOf("M225") != -1) {
    var Flag = 0;               //ALL WORK ITEM VIEW RIGHT
    //}
    //else {
    //    var Flag = 4;               //NOT ALL WORK ITEM VIEW RIGHT
    //}

 

    var1 = "1=1";
    var2 = "##";
    debugger;
    var Type = Flag;
    var UserId = ERPUserId;
    var DepartmentId = ERPDeptId;
    var FromDate = $('#DateFrom').val();
    var ToDate = $('#DateTo').val();
    var Variable1 = var1;
    var Variable2 = '';


    $("#grid").jqGrid
    ({
        url: "/Home/GetValues?Type=" + Type + "&UserId=" + UserId + "&DepartmentId=" + DepartmentId + "&FromDate='" + FromDate + "'&ToDate='" + ToDate + "'&Variable1='" + Variable1 + "'&Variable2='" + Variable2 + "'",
        datatype: 'json',
        mtype: 'Get',
        //table header name   
        //WorkItemId ,AssignedToUser,AssignedByUser,DepartmentName, DivisionName,Status,Title,Remarks,AssignedOnDate, ExceededTime,
        //MinuteDiff, TurnAroundTimes, NewWorkRemarks, BillSlNo, BillNo, BillDeptId, Reference, Driver,
        // DriverName,UpdatedOn,Description,StoreKeeper,StoreKeeperId,

        colNames: ['Sl#', 'Dept', 'Salesman', 'OrderType', 'Locn', 'BillNo', 'Date/Time', 'Customer', 'DeliveryArea', 'Status', 'PendingStatus', 'Remarks', 'LPO', 'Time'],
        //  colNames: ['Sl#', 'Dept', 'Salesman', 'OrderType', 'Locn', 'BillNo', 'Date/Time', 'Customer', 'DeliveryArea', 'Status', 'PendingStatus', 'Remarks', 'LPO', 'Time'],
        //colModel takes the data from controller and binds to grid   
        colModel: [
          {
              key: true,
              name: 'slno',
              // index: 'slno',//sl
              editable: true,
              search: true,
              searchoptions: { sopt: ['cn', 'eq', 'ne', 'lt', 'le', 'gt', 'ge'] }
          },
          {
              key: false,
              name: 'Dept',
              //  index: 'Dept',//dep
              editable: true,
              search: true,
              searchoptions: { sopt: ['cn', 'eq', 'ne', 'lt', 'le', 'gt', 'ge'] }
          },
          {
              key: false,
              name: 'AssignedByUser',
              index: 'AssignedByUser',//salesman
              editable: true,
              search: true,
              sortable: true,
              align: 'left',
              stype: "text",
              searchoptions: { "sopt": ["bw", "cn", "eq"] }
          }         
       , {
           key: false,
           name: 'DriverName',
           //  index: 'DriverName',//ordertype
           editable: true,
           formatter: getordertype,
           search: true,
           searchoptions: { sopt: ['cn', 'eq', 'ne', 'lt', 'le', 'gt', 'ge'] }
       },
{
    key: false,
    name: 'DriverName',
    // index: 'DriverName',//loc
    editable: true,
    formatter: getlocation,
    search: true,
    searchoptions: { sopt: ['cn', 'eq', 'ne', 'lt', 'le', 'gt', 'ge'] }
},
{
    key: false,
    name: 'BillNumber',
    //  index: 'BillNumber',//billno
    editable: true,
    search: true,
    searchoptions: { sopt: ['cn', 'eq', 'ne', 'lt', 'le', 'gt', 'ge'] }
},
  {
      key: false,
      name: 'AssignedOnDate',
      // index: 'AssignedOnDate',//date/time
      editable: true,
      formatter: "date",
      formatoptions: { srcformat: "ISO8601Long", newformat: "m/d/Y h:i A" },
  },
   {
       key: false,
       name: 'Description',
       //   index: 'Description',//customer
       editable: true,
       search: true,
       searchoptions: { sopt: ['cn', 'eq', 'ne', 'lt', 'le', 'gt', 'ge'] }

   },
   {
       key: false,
       name: 'StoreKeeper',
       //  index: 'StoreKeeper',//deliveryarea
       editable: true,
       search: true,
       searchoptions: { sopt: ['cn', 'eq', 'ne', 'lt', 'le', 'gt', 'ge'] }
   }, {
       key: false,
       name: 'Status',//status
       //   index: 'Status',//result[i].Status.split('##')[0]
       editable: true,
       search: true,
       searchoptions: { sopt: ['cn', 'eq', 'ne', 'lt', 'le', 'gt', 'ge'] }
   }, {
       key: false,
       name: 'Status',
       //  index: 'Status',//pendingstatus
       editable: true,
       search: true,
       searchoptions: { sopt: ['cn', 'eq', 'ne', 'lt', 'le', 'gt', 'ge'] }
   }, {
       key: false,
       name: 'WorkRemarks',
       //  index: 'WorkRemarks',//remarks
       editable: true,
       search: true,
       searchoptions: { sopt: ['cn', 'eq', 'ne', 'lt', 'le', 'gt', 'ge'] }
   }, {
       key: false,
       name: 'AssignedOnDate',
       //  index: 'AssignedOnDate',//lpo
       editable: true,
       search: true,
       searchoptions: { sopt: ['cn', 'eq', 'ne', 'lt', 'le', 'gt', 'ge'] }
   }, {
       key: false,
       name: 'AssignedOnDate',
       //  index: 'AssignedOnDate',//time
       editable: true,
       formatter: "date",
       formatoptions: { srcformat: "ISO8601Long", newformat: "h:i A" },
       search: true,
       searchoptions: { sopt: ['cn', 'eq', 'ne', 'lt', 'le', 'gt', 'ge'] }
   },


        ],

        pager: jQuery('#pager'),
        // pager: '#grid',
        rowNum: 50,
        rowList: [50, 60, 70, 80,90,100],
        height: '100%',
        viewrecords: true,
        caption: 'Data',
        loadonce: true,
        sortable: true,
        ignoreCase:true,
        emptyrecords: 'No records to display',
        jsonReader:
        {
            root: "rows",
            page: "page",
            total: "total",
            records: "records",
            repeatitems: false,
            Id: "0"
        },
        autowidth: true,
      //  multiselect: false
        //pager-you have to choose here what icons should appear at the bottom  
        //like edit,create,delete icons  
    }).navGrid('#pager',
    {
        search: true,
        edit: false,
        add: false,
        del: false,
        refresh: true,
        searchtext: "Search",

    }

  );


    //$('#grid').on('reloadGrid', function () {
    //    var userData =  $('#grid').jqgrid('getGridParam', 'userData');
    //})


    $('#grid').jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false, defaultSearch: "cn", searchOperators: true, autosearch: true });


  //  $('#grid').trigger('reloadGrid');
    //jQuery("#grid").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: true, defaultSearch: "cn" });

    //jQuery("#grid").filterToolbar(options);

  //  jQuery("#grid").jqGrid('filterToolbar', { autosearch:true });
  //  $('#grid').jqGrid('filterToolbar', { searchOnEnter: true, enableClear: true });


   // $(filteritem).focus().trigger({ type: 'keypress', charCode: 13 });
  

    //loadComplete: function(data){
    //    if(!loadedonce){
    //        //some manupulations
    //        loadedonce = true;
    //    }
    //    else{
    //        var userData = $('#table').jqgrid('getGridParam', 'userData');
    //        //this userData has the latest data i needed
    //    }
    //}

}


//});




function getordertype(cellValue, opts, rowObject) {

    if (cellValue == null || cellValue == 1451520000) {
        return '';
    } else {

    
        var data = cellValue.split('#')[1];

        return data;
    }
}
function getlocation(cellValue, opts, rowObject) {

    if (cellValue == null || cellValue == 1451520000) {
        return '';
    } else {

        var data = cellValue.split('#')[2];

        return data;
    }
}



//});








//,
//

// {
//  zIndex: 100,
//  caption:"search",
//closeAfterSearch: true,
//closeAfterReset: true,
//closeOnEscape: true,
//multipleSearch: true,
//  sopt: ['eq', 'ne', 'lt', 'le', 'gt', 'ge', 'bw', 'bn', 'ew', 'en', 'cn', 'nc', 'nu', 'nn', 'in', 'ni']  ,
//onSearch: function () {
//    debugger;
//    $('#grid').jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid');
//},
//onReset: function () {
//    $('#grid').jqGrid().trigger('reloadGrid');
//}

// }


//
//  $("#grid").setGridParam({ data: results.rows, localReader: reader }).trigger('reloadGrid');

//  $("#grid").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false, defaultSearch: 'cn' });
//https://stackoverflow.com/questions/26014252/jqgrid-search-not-functioning   search


////////
//https://www.c-sharpcorner.com/article/using-jqgrid-with-asp-net-mvc/
//https://copyprogramming.com/howto/jqgrid-custom-formatter

//https://copyprogramming.com/howto/javascript-trigger-event-on-load-jq