
const MAIN_NOTICE_GRID_ID = '#ITP_MAIN_NOTICE_jqGrid';
const MAIN_NOTICE_GRID_LIST = '#ITP_MAIN_NOTICE_jqGrid_list';
const MAIN_NOTICE_GRID_PAGER = '#ITP_MAIN_NOTICE_jqGridPager';
const MAIN_NOTICE_GRID_EMPTY = '#ITP_MAIN_NOTICE_jqGridEmpty';

let NTICE_DETAIL_GRID_LIST = '/api/ntice/detail-grid-list';

require(['config'], function() {
	require([
		'jquery', 
		'jquery.bootstrap.fileUpload' //파일 업로드가 있는 페이지에서 로딩
	], function($) {
		
		setTimeout(function() {
			// 시간지정
			$('#ITP_FORM_MAIN_NOTICE_SEARCH_SCH_DT').val(itp_fn_date_format(new Date(), 2));
			$('#ITP_FORM_MAIN_NOTICE_SEARCH_USER_ID').val(fn_make_user_info.get('userId'));
			
			// 그리드
			itp_fn_jqgrid_resize(MAIN_NOTICE_GRID_ID, MAIN_NOTICE_GRID_LIST, 'lg');
			itp_fn_MAIN_NOTICE_grid.init();
	    	itp_fn_fire_window_resize();
	
			// 조회
	    	itp_fn_MAIN_NOTICE_search();

		}, 100);
				
	});
});

var itp_fn_MAIN_NOTICE_grid = {
	init: function() {
		// 데이터 없을때
		itp_fn_MAIN_NOTICE_empty.push();
		$(MAIN_NOTICE_GRID_EMPTY).on('click', function() {
			itp_fn_MAIN_NOTICE_empty.back();
			itp_MAIN_NOTICE_param.gridSize = $.jgrid.defaults.rowNum;
			$(MAIN_NOTICE_GRID_ID).setGridParam({'postData': JSON.stringify(itp_MAIN_NOTICE_param)}).trigger('reloadGrid');
		});
	},
	callBack: function() {
		let param = $('#ITP_FORM_MAIN_NOTICE_SEARCH').serializeObject();
		param.gridSize = $.jgrid.defaults.rowNum;
		$(MAIN_NOTICE_GRID_ID).jqGrid({
			colModel: [
				{
					index: 'NTICE_NO', name: 'nticeNo',
					label: ITP_MSG_LOCALE.label.notiNo, //공지번호
					width: '10', fixed: false, align: 'center',
					sortable: false, hidden: false
				},
				{
					index: 'NTICE_TITL', name: 'nticeTitl',
					label: '공지사항', //공지제목
					width: '15', fixed: false, align: 'center',
					sortable: false, hidden: false
				},
				{
					index: 'ADD_DT', name: 'addDt',
					label: ITP_MSG_LOCALE.label.regDt, //등록일
					width: '10', fixed: false, align: 'center',
					sortable: false, hidden: false
				}
			],
			loadBeforeSend: function(jqXHR) {
			    jqXHR.setRequestHeader('X-AUTH-TOKEN', CONN_KEY);
			},
			postData: JSON.stringify(param),
			url: DOMAIN + NTICE_DETAIL_GRID_LIST,
			pager: MAIN_NOTICE_GRID_PAGER,
			multiselect: false,
			rownumbers: true,
			onCellSelect: function(rowId, cellIdx, cellValue) {
				var cm = $(this).jqGrid('getGridParam', 'colModel');
				var colNm = cm[cellIdx].name;
				if (colNm == 'nticeTitl') {
					const nticeNoVal = $(this).jqGrid('getCell', rowId, 'nticeNo');
					const key = {nticeNo: nticeNoVal, viewCd: 'R'};
					var param = $.param(key);
					// itp_fn_MAIN_NOTICE_view(param);
					
					fn_call_popup('comm', 'NOTICE_INFO_POPUP', '#ITP_ASIDE', function() {
												
					}, param, 'S');
					$('#ITP_NOTICE_INFO_POPUP > div.modal-dialog').width('500px');
				}
		   	},
			loadComplete: function(data) {
				//console.log(JSON.stringify(data));
				itp_fn_grid_load_complete(data, MAIN_NOTICE_GRID_ID, true, 'number', 'MAIN_NOTICE', itp_MAIN_NOTICE_search, itp_fn_MAIN_NOTICE_empty, false, data.gridRecords, false);
				var ids = $(MAIN_NOTICE_GRID_ID).getDataIDs();
				$.each(ids, function(idx, rowId) {
					$(MAIN_NOTICE_GRID_ID).jqGrid('setCell', rowId, 'nticeTitl', '', ITP_GRID_COL_STYLE.link);
				});

				if ($('#ITP_CONTAINER').data('openYn') == 'Y') {
					$('#ITP_CONTAINER').data('openYn', 'N');
					const keyNew = {nticeNo: $('#ITP_CONTAINER').data('nticeNo'), viewCd: 'R'};
					var paramNew = $.param(keyNew);
					itp_fn_MAIN_NOTICE_view(paramNew);
				}
		    },
		    loadError: function(jqXHR, textStatus, errorThrown) {
		    	itp_fn_grid_load_error(jqXHR, textStatus, errorThrown);
		    },
		    onPaging: function(action) {
		    	itp_fn_grid_paging(MAIN_NOTICE_GRID_ID, action, param);
		    }
		}).navGrid(MAIN_NOTICE_GRID_PAGER, ITP_GRID_NAV_DEFAULTS.navGrid);
	},
	clearData : function() {
		$(MAIN_NOTICE_GRID_ID).jqGrid('clearGridData', true);
		$(MAIN_NOTICE_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
		$(MAIN_NOTICE_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
		$(MAIN_NOTICE_GRID_EMPTY).show();
	},
	unload : function() {
		$.jgrid.gridUnload(MAIN_NOTICE_GRID_ID);
	}
};

var itp_fn_MAIN_NOTICE_search = function() {
	itp_fn_MAIN_NOTICE_grid.unload();
	itp_fn_MAIN_NOTICE_grid.callBack();
};

let itp_MAIN_NOTICE_param = {};
let itp_MAIN_NOTICE_search = false;
var itp_fn_MAIN_NOTICE_empty = {
	push: function() {		
		let param = $('#ITP_FORM_MAIN_NOTICE_SEARCH').serializeObject();
		itp_MAIN_NOTICE_param = param;
	},
	back: function() {
		
	}
};
