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

	});
});

function itp_fn_AUTH_SCH_POPUP(parentPopFn, args, returnType) {
	fn_make_select(CODE_LIST, 'AUTH_TP_CD', 'ITP_FORM_AUTH_SCH_POPUP_AUTH_TYPE_CD', true, '권한타입 선택');
	itp_fn_AUTHSCHPOPUP_menu_search(); // 상위메뉴조회
	
	let itp_AUTH_SCH_POPUP_param = {};
	let itp_AUTH_SCH_POPUP_search = false;

	const THIS_FORM_ID = '#ITP_FORM_AUTH_SCH_POPUP';
	const THIS_GIRD_ID = '#ITP_AUTH_SCH_POPUP_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_AUTH_SCH_POPUP_jqGridPager';

	$('#ITP_AUTH_SCH_POPUP').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_AUTH_SCH_POPUP_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_AUTH_SCH_POPUP').remove();
	}).modal('show');

	$('#ITP_AUTH_SCH_POPUP_BTN_SEARCH').on('click', function() {
		itp_AUTH_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_AUTH_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');
	    for (var i=selectedIds.length-1; i>=0; i--) {
			list.push($(THIS_GIRD_ID).jqGrid('getRowData', selectedIds[i]));	
		}
		parentPopFn(list);		
		$('#ITP_AUTH_SCH_POPUP').modal('hide'); // 팝업닫기
	});

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

	var itp_fn_AUTH_SCH_POPUP_empty = {
		push: function() {
			let param = $(THIS_FORM_ID).serializeObject();
			itp_AUTH_SCH_POPUP_param = param;
		},
		back: function() {
			$(THIS_FORM_ID + '_KEYWORD').val(itp_AUTH_SCH_POPUP_param.keyword);
		}
	};

	itp_fn_AUTH_SCH_POPUP_empty.push();
	let param = $('#ITP_FORM_AUTH_SCH_POPUP').serializeObject();
	param.gridSize = 10;

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'AUTH_NO', name: 'authNo',
				label: ITP_MSG_LOCALE.label.permitCd, // 권한번호
				width: '20', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'AUTH_NM', name: 'authNm',
				label: ITP_MSG_LOCALE.label.permitNameKr, //권한명
				width: '20', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'TOP_MENU_NM', name: 'topMenuNm',
				label: ITP_MSG_LOCALE.label.topMenuName, //
				width: '25', fixed: false, align: 'center',
				sortable: false, hidden: false
			}
		],
		loadBeforeSend: function(jqXHR) {
		    jqXHR.setRequestHeader('X-AUTH-TOKEN', CONN_KEY);
		},
		postData: JSON.stringify(param),
		url: DOMAIN + '/api/auth/pop-grid-list',
		pager: THIS_GIRD_PAGER,
		multiselect: true,
		loadComplete: function(data) {
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'AUTH_SCH_POPUP', itp_AUTH_SCH_POPUP_search, itp_fn_AUTH_SCH_POPUP_empty, true, data.gridRecords, false);
			//itp_fn_grid_make_cell_link(THIS_GIRD_ID, ['selected'], ITP_MSG_LOCALE.label.selected, ITP_GRID_COL_STYLE.selected);
	    },
	    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);
};

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

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

