{% load i18n l10n %}

{% get_current_language as LANGUAGE_CODE %}

	<div id="{{view_name}}_div_grid_container" class="div_grid_container">
		<table id="{{view_name}}_table_grid" ></table>
		<div id="{{view_name}}_grid_pager"> </div>
	</div><!-- /Grid Container -->

<script type="text/javascript" lang="text/javascript">
	{{view_name}}_stopRequest = false;



	{{view_name}}createGrid();


	function {{view_name}}createGrid() {

		// Vars definition
		col_model= {% autoescape off %} {{col_model}} {% endautoescape %};
		col_names = {% autoescape off %} {{col_name}} {% endautoescape %};
		// var jsonReader_id = {% autoescape off %} '{{jsonReader_id}}' {% endautoescape %};
		// var url_grid =  '{{ URL_GRID }}';

		sortname =  '{{ sortname }}';
	    multiselect = false;
		sortorder = 'asc';
		footerrow = '{{footerrow}}';
		// Order data desc by default
		if (sortname.indexOf('-') != -1) {
			sortorder = 'desc';
			// sortname = sortname.replace('-', '');
			sortname = sortname.substring(1, sortname.length);
		}
		if ('{{multiselect}}' != 'false') {
			multiselect = true;
		}
		
		if (footerrow == 'true')
			footerrow = true
		else 
			footerrow = false

		// Grid configuration
		selColumn = undefined
		$("#{{view_name}}_table_grid").jqGrid({
		  	url: '{{ URL_GRID }}', 
		  	datatype:'json',
		  	colNames: col_names,
		  	colModel: col_model,
		  	//rowNum: '-1',
		  	rowNum: '{{row_num|unlocalize}}',
		  	pager: '#{{view_name}}_grid_pager',
		  	sortname: sortname,
		  	sortorder: sortorder,
		  	loadui:'block',
		  	loadtext:'Please Wait ...',
		  	jsonReader : {
			  	root: 'rows',
			  	page: 'page',
			  	total: "total",
			  	records: "records",
			  	userdata: "footerData",

			  	repeatitems: false,
			  	id:'{{jsonReader_id}}',
		  	},
  			altRows: true,
		  	autowidth: true,
		  	caption: "{% trans 'VIEW NAME' %}",
		  	height: 300,
		  	scroll:1,
		  	gridview: true,
		 	viewrecords: true,
		  	pgbuttons : false,
		  	pginput: false,
		  	pgtext: false,
		  	multiboxonly: multiselect,
		  	multiselect: multiselect,
		  	footerrow : footerrow,
			userDataOnFooter : footerrow,
		    subGrid: {{subGrid}},
		    subGridWidth:20,
    		subGridOptions: { "plusicon" : "ui-icon-triangle-1-e",
                      "minusicon" :"ui-icon-triangle-1-s",
                      "openicon" : "ui-icon-arrowreturn-1-e",
                      "reloadOnExpand" : false,
                      "selectOnExpand" : true },		
			

			loadComplete: function(data) {
				// $('#{{view_name}}_control_panel').unblock();
				controlBlock('{{view_name}}_table_grid','unblock')
				controlBlock('{{view_name}}_filters_form','unblock')


				if(data){
					if (data['records'] == '0'){	
						{{view_name}}_stopRequest = true;
					}
					{{view_name}}_load_complete_callback(data);
				}
			},
			gridComplete: function(){

				if ({{view_name}}_stopRequest == true){
					{{view_name}}_stopRequest = false;
					return false;
				}
				{{view_name}}_grid_complete_callback();
			},
			jqGridAfterGridComplete:function(){


			},
			jqGridAfterLoadComplete:function(){

			},

			// Click row
			onSelectRow: function(id){ 
				if (multiselect == false)
					row_id = jQuery("#{{view_name}}_table_grid").jqGrid('getGridParam','selrow');
				else
				{	
					row_id = jQuery("#{{view_name}}_table_grid").jqGrid('getGridParam','selrow');
					row_ids = jQuery("#{{view_name}}_table_grid").jqGrid('getGridParam','selarrrow');
				}
				//this function allows overwriting OnSelectRow
				{{view_name}}_select_row_callback(row_id, id);
			},
			// Click all rows
			onSelectAll: function(aRowids, status){
				{{view_name}}_select_all_row_callback(aRowids, status);
			},
			onHeaderClick: function(){
				{{view_name}}_hear_click();
			},
		    onCellSelect: function (rowid,iCol) {
		    	{{view_name}}_cell_click(rowid,iCol,this);
		    },
			subGridRowExpanded: function(subgrid_id, row_id){
				{{view_name}}_subgrid_row_expanded_callback(subgrid_id, row_id);
			},

			onSortCol: function(index,iCol,sortorder){
				if (sortorder == 'asc')
					sortorder = 'desc';
				else
					sortorder = 'asc';
				{{view_name}}_onsortcol_callback(index, iCol, sortorder);	
			},
			beforeRequest: function(){
				// $('#{{view_name}}_control_panel').block();
				controlBlock('{{view_name}}_table_grid','block')
				controlBlock('{{view_name}}_filters_form','block')

				{{view_name}}_before_request_callback();
				if ({{view_name}}_stopRequest == true){
					{{view_name}}_stopRequest = false;
					return false;
				}
			}
		});
	}


	$("#"+"{{view_name}}"+"_table_grid").jqGrid('hideCol','id');

	// definition by default
	function {{view_name}}_grid_complete_callback(){}
	function {{view_name}}_select_row_callback(row_id){}
	function {{view_name}}_select_all_row_callback(aRowids, status){}
	function {{view_name}}_hear_click(){}
	function {{view_name}}_subgrid_row_expanded_callback(subgrid_id, row_id){}
	function {{view_name}}_cell_click(rowid,iCol,cell){}
	function {{view_name}}_onsortcol_callback(index, iCol, sortorder){}
	function {{view_name}}_load_complete_callback(data){}
	function {{view_name}}_before_request_callback(){}


//EXAMPLE  (NON DELETE) 
// function view_name_cell_click(rowid,iCol,cell){
// 	var i; 
// 	var rows = cell.rows
// 	var rl = rows.length
// 	var  row;
// 	var rowCells;
	
// 	$('#view_name_table_grid .ui-state-highlight').removeClass('ui-state-highlight');

// 	for(i=0; i<rl; i++) {
// 		console.log(i)
// 	    row = rows[i];
// 	    if($(row).hasClass('jqgrow')) {
// 	        rowCells = row.cells;
// 	        	$(rowCells[iCol]).addClass("ui-state-highlight");
// 	    }
// 	}
// }
</script>

