require(['config'], function() {
	require([
		'jquery'
	], function($) {

	});
});

var itp_fn_MENU_SCH_POPUP_menu_search = function() {
	fn_ajax_call('/api/menu/menu-search', null, itp_fn_MENU_SCH_POPUP_menu_search_callback, 'GET');
};

var itp_fn_MENU_SCH_POPUP_menu_search_callback = function(result) {
	fn_make_select_upper_menu(result.searchList, 'ITP_FORM_MENU_SCH_POPUP_TOP_MENU_NO', true, '상위메뉴 선택');
};

function itp_fn_MENU_SCH_POPUP(parentPopFn, args, returnType) {
	$('#ITP_MENU_SCH_POPUP_ERR_MSG').text('');
	
	itp_fn_MENU_SCH_POPUP_menu_search();
	
	let itp_MENU_SCH_POPUP_param = {};
	let itp_MENU_SCH_POPUP_search = false;

	const THIS_FORM_ID = '#ITP_FORM_MENU_SCH_POPUP';
	const THIS_GIRD_ID = '#ITP_MENU_SCH_POPUP_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_MENU_SCH_POPUP_jqGridPager';
	
	$('#ITP_MENU_SCH_POPUP').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_MENU_SCH_POPUP_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_MENU_SCH_POPUP').remove();
	}).modal('show');

	$('#ITP_MENU_SCH_POPUP_BTN_SEARCH').on('click', function() {
		itp_MENU_SCH_POPUP_search = true;
		let param = $(THIS_FORM_ID).serializeObject();
		param.gridSize = 10;
		param.gridPage = $.jgrid.defaults.page;
		$(THIS_GIRD_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
	});
	
	$('#ITP_MENU_SCH_POPUP_BTN_SELECT').on('click', function() {		
		const rowKey = $(THIS_GIRD_ID).getGridParam('selrow');
	    if (!rowKey) {
	    	return;
	    }

		var list = [];
	    var selectedIds = $(THIS_GIRD_ID).getGridParam('selarrrow');

		if(selectedIds.length > 1) {
			$('#ITP_MENU_SCH_POPUP_ERR_MSG').text('※ 메뉴는 1개만 선택해주세요.');
			return;	
		}

	    for (var i=selectedIds.length-1; i>=0; i--) {
			list.push($(THIS_GIRD_ID).jqGrid('getRowData', selectedIds[i]));	
		}
		parentPopFn(list);
		$('#ITP_MENU_SCH_POPUP').modal('hide'); // 팝업닫기
	});

	$('#ITP_MENU_SCH_POPUP_jqGridEmpty').on('click', function() {
		itp_fn_MENU_SCH_POPUP_empty.back();
		itp_MENU_SCH_POPUP_param.gridSize = 10;
		$(THIS_GIRD_ID).setGridParam({'postData': JSON.stringify(itp_MENU_SCH_POPUP_param)}).trigger('reloadGrid');
	});

	var itp_fn_MENU_SCH_POPUP_empty = {
		push: function() {
			let param = $(THIS_FORM_ID).serializeObject();
			itp_MENU_SCH_POPUP_param = param;
		},
		back: function() {
			$(THIS_FORM_ID + '_USER_ID').val(itp_MENU_SCH_POPUP_param.suserId);
			$(THIS_FORM_ID + '_USER_NM').val(itp_MENU_SCH_POPUP_param.suserNm);
			$(THIS_FORM_ID + '_USER_STAT_CD').val(itp_MENU_SCH_POPUP_param.suserStatCd);
		}
	};

	itp_fn_MENU_SCH_POPUP_empty.push();
	let param = $('#ITP_FORM_MENU_SCH_POPUP').serializeObject();
	param.gridSize = 10;

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'MENU_NO', name: 'menuNo',
				label: ITP_MSG_LOCALE.label.menuNumber, // 
				width: '12', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'MENU_NM', name: 'menuNm',
				label: ITP_MSG_LOCALE.label.menuNameKr, // 
				width: '15', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'ID_PATH_NM', name: 'idPathNm',
				label: ITP_MSG_LOCALE.label.menuPath, //
				width: '30', fixed: false, align: 'left',
				sortable: false, hidden: false
			}
		],
		loadBeforeSend: function(jqXHR) {
		    jqXHR.setRequestHeader('X-AUTH-TOKEN', CONN_KEY);
		},
		postData: JSON.stringify(param),
		url: DOMAIN + '/api/menu/pop-grid-list',
		pager: THIS_GIRD_PAGER,
		multiselect: true,
		loadComplete: function(data) {
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'MENU_SCH_POPUP', itp_MENU_SCH_POPUP_search, itp_fn_MENU_SCH_POPUP_empty, true, data.gridRecords, false);
	    },
	    loadError: function(jqXHR, textStatus, errorThrown) {
	    	itp_fn_grid_load_error(jqXHR, textStatus, errorThrown);
	    },
	    onPaging: function(action) {
	    	itp_fn_grid_paging(THIS_GIRD_ID, action, param);
	    }
	}).navGrid(THIS_GIRD_PAGER, ITP_GRID_NAV_DEFAULTS.navGrid);
};

