
let MENU_MENU_SEARCH = '/api/menu/menu-search';
let JOB_DETAIL_GRID_LIST = '/api/job/detail-grid-list';

require(['config'], function() {
	require([
		'jquery'
	], function($) {
		// 버튼 권한설정
		fn_proc_btn_auth('SYSTEM06010');
		// 코드
		fn_make_select(CODE_LIST, 'ACT_TP_CD', 'ITP_FORM_SYSTEM06010_ACT_TP_CD', true, '전체');
		// 메뉴
		itp_fn_SYSTEM06010_menu_search();
		
		$('#ITP_TAB_SYSTEM06010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);		
		$('#ITP_FORM_SYSTEM06010_FROM_DT').datepicker('setDate', new Date());
		$('#ITP_FORM_SYSTEM06010_TO_DT').datepicker('setDate', new Date());
		
		itp_fn_jqgrid_resize('#ITP_SYSTEM06010_jqGrid', '#ITP_SYSTEM06010_jqGrid_list', 'lg');
    	itp_fn_fire_window_resize();

    	itp_fn_SYSTEM06010_empty.push();
		itp_fn_SYSTEM06010_search();
		$('#ITP_BTN_SYSTEM06010_SRH').on('click', function() {
			itp_SYSTEM06010_search = true;
			let param = $('#ITP_FORM_SYSTEM06010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			$('#ITP_SYSTEM06010_jqGrid').setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
		});

		$('#ITP_SYSTEM06010_jqGridEmpty').on('click', function() {
			itp_fn_SYSTEM06010_empty.back();
			itp_SYSTEM06010_param.gridSize = $.jgrid.defaults.rowNum;
			$('#ITP_SYSTEM06010_jqGrid').setGridParam({'postData': JSON.stringify(itp_SYSTEM06010_param)}).trigger('reloadGrid');
		});

		$('#ITP_SYSTEM06010_UPLOAD_EXCEL_FORM').on('change', function() {
			itp_fn_excel_upload('#ITP_SYSTEM06010_UPLOAD_EXCEL_FORM', '/SYSTEM06010_excelUpload');
		});
	});
});

let itp_SYSTEM06010_param = {};
let itp_SYSTEM06010_search = false;
var itp_fn_SYSTEM06010_empty = {
	push: function() {
		let param = $('#ITP_FORM_SYSTEM06010_SEARCH').serializeObject();
		itp_SYSTEM06010_param = param;
	},
	back: function() {
		$('#ITP_FORM_SYSTEM06010_FROM_DT').val(itp_SYSTEM06010_param.fromDt);
		$('#ITP_FORM_SYSTEM06010_TO_DT').val(itp_SYSTEM06010_param.toDt);
		$('#ITP_FORM_SYSTEM06010_USER_ID').val(itp_SYSTEM06010_param.userId);
		$('#ITP_FORM_SYSTEM06010_MENU_NO').val(itp_SYSTEM06010_param.menuNo);
		$('#ITP_FORM_SYSTEM06010_ACT_TP_CD').val(itp_SYSTEM06010_param.actTpCd);
	}
};

var itp_fn_SYSTEM06010_menu_search = function() {
	fn_ajax_call(MENU_MENU_SEARCH, null, itp_fn_SYSTEM06010_menu_search_callback, 'GET');
};

var itp_fn_SYSTEM06010_menu_search_callback = function(result) {
	fn_make_select_upper_menu(result.searchList, 'ITP_FORM_SYSTEM06010_MENU_NO', true, '전체');
};

var itp_fn_SYSTEM06010_search = function() {
	let param = $('#ITP_FORM_SYSTEM06010_SEARCH').serializeObject();
	param.gridSize = $.jgrid.defaults.rowNum;
	const THIS_GIRD_ID = '#ITP_SYSTEM06010_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_SYSTEM06010_jqGridPager';

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'VIEW_CD', name: 'viewCd',
				label: ITP_MSG_LOCALE.label.viewCd,
				width: '11', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'JOB_NO', name: 'jobNo',
				label: ITP_MSG_LOCALE.label.historyNo,
				width: '11', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'USER_ID', name: 'userId',
				label: ITP_MSG_LOCALE.label.userId,
				width: '11', fixed: false, align: 'center',
				sortable: true, editable: false
			},
			{
				index: 'USER_NM', name: 'userNm',
				label: ITP_MSG_LOCALE.label.userName,
				width: '11', fixed: false, align: 'center',
				sortable: true, editable: false
			},
			{
				index: 'MENU_NM', name: 'menuNm',
				label: ITP_MSG_LOCALE.label.menuTxt,
				width: '11', fixed: false, align: 'center',
				sortable: true, editable: false
			},
			{
				index: 'ACT_TP_CD_NM', name: 'actTpNm',
				label: ITP_MSG_LOCALE.label.actionType,
				width: '11', fixed: false, align: 'center',
				sortable: true, editable: false
			},
			{
				index: 'ACT_NM', name: 'actNm',
				label: ITP_MSG_LOCALE.label.actionName,
				width: '11', fixed: false, align: 'left',
				sortable: true, editable: false
			},
			{
				index: 'ADD_DT', name: 'addDt',
				label: ITP_MSG_LOCALE.label.connTime,
				width: '11', fixed: false, align: 'center',
				sortable: true, editable: false
			},
			{
				index: 'IP', name: 'ip',
				label: ITP_MSG_LOCALE.label.connIp,
				width: '11', fixed: false, align: 'center',
				sortable: true, editable: false
			}
		],
		loadBeforeSend: function(jqXHR) {
		    jqXHR.setRequestHeader('X-AUTH-TOKEN', CONN_KEY);
		},
		postData: JSON.stringify(param),
		url: DOMAIN + JOB_DETAIL_GRID_LIST,		
		pager: THIS_GIRD_PAGER,
		multiselect: false,
		rownumbers: true,
		loadComplete: function(data) {
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, 'number', 'SYSTEM06010', itp_SYSTEM06010_search, itp_fn_SYSTEM06010_empty);
	    },
	    loadError: function(jqXHR, textStatus, errorThrown) {
	    	itp_fn_grid_load_error(jqXHR, textStatus, errorThrown);
	    },
	    onPaging: function(action) {
	    	var pagingFn = {
    			callBack: function(args) {
    				$(args).trigger('reloadGrid');
    			}
    		};
	    	if (itp_fn_check_grid_is_writing(THIS_GIRD_ID)) {
	    		itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, pagingFn, THIS_GIRD_ID);
	    		return 'stop';
	    	} else {
	    		itp_fn_grid_paging(THIS_GIRD_ID, action, param);
	    	}
	    },
	    onSortCol: function(index, columnIndex, sortOrder) {
	    	var sortingFn = {
    			callBack: function(args) {
    				$(args).trigger('reloadGrid');
    			}
    		};
	    	if (itp_fn_check_grid_is_writing(THIS_GIRD_ID)) {
	    		itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, sortingFn, THIS_GIRD_ID);
	    		return 'stop';
	    	} else {
	    		itp_fn_grid_sorting(THIS_GIRD_ID, index, sortOrder);
	    	}
	    }
	}).navGrid(THIS_GIRD_PAGER, ITP_GRID_NAV_DEFAULTS.navGrid);
};