text: (function () {
  	// JS construct
  })();
  
  function initPage(){
  	LobiAdmin.highlightCode();
  	
  	initValidationDefaults();
  	$("#form_input").validate({
  		rules: {
  			price_grade_code: {
  				required: true,
  				nowhitespace: true,
  			},	
  			price_grade_name: {
  				required: true,
  			},	
  			margin: {
  				required: true,
  				number: true,
  				max: 100,
  				min: 0
  			},
  		},
  	});
  	
  	DataTable = $('#table_list_data').DataTable({ 
  		"pageLength": 10,
  		"numbers_length": 4,
  		"processing": true,
  		"serverSide": true,
  		"responsive": true,
  		"ajax": {
  			"url": base_url + Modules + '/' + Controller + '/getList',
  			"type": "POST",
  			"data": function ( d ) {
  				d.active = $('#filter_status').val();
  			},
  		},
  		"columns": [
  			{"data": "price_grade_code"},
  			{"data": "price_grade_name"},
  			{"data": "margin", "className": "text-right"},
  			{"data": "active", "width": 40, "className": "text-center"},
  			{"data": null, "width": 20, "className": "text-center"}
        ],
  		"columnDefs": [
  			{
  				"targets": 2,
  				"orderable": true,
  				"searchable": false,
  				"render": function ( data, type, row ) {
  					return accounting.formatNumber(row.margin, 2);
  				},
  			},
  			{
  				"targets": 3,
  				"orderable": true,
  				"searchable": false,
  				"render": function ( data, type, row ) {
  					if (row.active == 1){
  						return '<span class="badge badge-success text-center">Aktif</span>';
  					} else {
  						return '<span class="badge badge-danger text-center">Tidak Aktif</span>';
  					}
  				},
  			},
  			{
  				"targets": -1,
  				"orderable": false,
  				"searchable": false,
  				"render": function ( data, type, row ) {
  					let actions = '';
  					if (Priv.edit_flag == 1) {
  						actions = '<button type="button" class="btn btn-outline-info btn-sm waves-effect waves-light" onclick="app_edit(' + row.id + ');"><i class="glyphicon glyphicon-pencil"></i> </button> ';
  					}
  					return actions;
  				},
  			},
  		],
  	});
  }
  
  // START function default
  
  function app_create() { 
  	window.location.href = '#' + Modules + '/' + Controller + '/create';
  }
  
  function Simpan() {
  	if (!$('#form_input').valid()) {
  		MsgBox.Notification('Periksa inputan anda.', 'bottom right', 'warning', 'mini');
  		return;
  	}
  	
  	data2Send = JSON.stringify({
  		id: idData,
  		price_grade_code: $('#price_grade_code').val(),
  		price_grade_name: $('#price_grade_name').val(),
  		margin: accounting.unformat($('#margin').val()),
  		active: $('#active').is(':checked') ? 1 : 0
  	});
  	
  	MsgBox.Confirm('Yakin akan simpan data ini?', 'Simpan data').then(result => {
  		let url = action === 'create' ? base_url + Modules + '/' + Controller + '/save/' : base_url + Modules + '/' + Controller + '/update/';
  		FetchWithTimeout(url, data2Send, 'POST', 5000)
  		.then((data) => {
  			if (data.result) {
  				MsgBox.Notification(data.msg.toString(), 'bottom right', 'success');
  				ResetForm();
  				app_refresh();
  				data2Send = null;
  				idData = null;
  			} else {
  				MsgBox.Notification(data.msg.toString(), 'bottom right', 'warning');
  			}
  		})
  		.catch((err) => {
  			MsgBox.Notification(err.toString(), 'bottom right', 'warning');
  		});
  	}).catch(err => {
  		console.log(err);
  	});
  }
  
  function app_edit(id = null) {
  	FetchWithTimeout(base_url + Modules + '/' + Controller + '/getData2Edit/json/' + id, null, 'GET', 5000)
  	.then((data) => {
  		if (data.result) {
  			ResetForm();
  			action = 'edit';
  			let row = data.data;
  			idData = row.id
  			$('#price_grade_code').val(row.price_grade_code); $('#price_grade_code').prop('readonly', true);
  			$('#price_grade_name').val(row.price_grade_name);
  			$('#margin').val(accounting.formatNumber(row.margin, 2));
  			$('#active').prop('checked', row.active == 1 ? true : false);
  		} else {
  			MsgBox.Notification(data.msg.toString(), 'bottom right', 'warning');
  		}
  	})
  	.catch((err) => {
  		MsgBox.Notification(err.toString(), 'bottom right', 'warning');
  	});
  }
  
  function app_delete(multi = 0, id = null, menu_id = '') {
  	if (multi) {
  		if (dataArr.length <= 0) {
  			MsgBox.Notification('Checklist terlebih dahulu data yang akan dihapus.', 'bottom right', 'info');
  			return;
  		}
  		data2Send = JSON.stringify({
  			id: dataArr
  		});
  		MsgBox.Confirm('Hapus menu checklist ?').then(result => {
  			FetchWithTimeout(base_url + Modules + '/' + Controller + '/delete/', data2Send, 'POST', 5000)
  			.then((data) => {
  				if (data.result) {
  					MsgBox.Notification(data.msg.toString(), 'bottom right', 'success');
  					app_refresh();
  				} else {
  					MsgBox.Notification(data.msg.toString(), 'bottom right', 'warning');
  				}
  			})
  			.catch((err) => {
  				MsgBox.Notification(err.toString(), 'bottom right', 'warning');
  			});
  		}).catch(err => {
  			console.log(err);
  		});
  	} else {
  		data2Send = JSON.stringify({
  			id: id
  		});
  		MsgBox.Confirm('Hapus menu ' + menu_id + ' ?').then(result => {
  			FetchWithTimeout(base_url + Modules + '/' + Controller + '/delete/', data2Send, 'POST', 5000)
  			.then((data) => {
  				if (data.result) {
  					MsgBox.Notification(data.msg.toString(), 'bottom right', 'success');
  					app_refresh();
  				} else {
  					MsgBox.Notification(data.msg.toString(), 'bottom right', 'warning');
  				}
  			})
  			.catch((err) => {
  				MsgBox.Notification(err.toString(), 'bottom right', 'warning');
  			});
  		}).catch(err => {
  			console.log(err);
  		});
  		
  	}
  }
  
  function app_pdf() {
  	window.open(base_url + Modules + '/' + Controller + '/pdf/', '_blank');
  }
  
  function app_xls() {
  	window.open(base_url + Modules + '/' + Controller + '/xls/', '_blank');
  }
  
  function app_refresh() {
  	data2Send = null;
  	DataTable.ajax.reload(null,true);
  }
  
  function Add2Arr(CheckAll = 0, id = null) {
  	if (CheckAll) {
  		dataArr = []; 
  		let chkboxes = $('input[name=\'chk\']');
  		for (let i = 0; i < chkboxes.length; i++) {
  			if ($('#CheckAll').is(':checked')) {
  				$('#chk_' + i).prop('checked', true);
  				dataArr.push(parseInt($('#chk_' + i).val()));
  			} else {
  				$('#chk_' + i).prop('checked', false);
  			}
  		}
  	} else {
  		if (dataArr.indexOf(id) > -1) {
  			dataArr.splice(dataArr.indexOf(id), 1);
  		} else {
  			dataArr.push(id);
  		}
  		$('#CheckAll').prop('checked', false);
  	}
  }
  
  function Filter() {
  	DataTable.ajax.reload(null,true);
  }
  
  // END function default
  
  function ResetForm() {
  	action = 'create';
  	$('#price_grade_code').prop('readonly', false);
  	$('#active').prop('checked', true);
  	$('#form_input')[0].reset(); // Kosongkan input
  	clearValidation('#form_input');
  }