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

	});
});

function itp_fn_USER_SCH_POPUP(parentPopFn, args, returnType) {
	fn_make_select(CODE_LIST, 'USER_STAT_CD', 'ITP_FORM_USER_SCH_POPUP_USER_STAT_CD', true, '사용자상태 선택');
	
	let itp_USER_SCH_POPUP_param = {};
	let itp_USER_SCH_POPUP_search = false;

	const THIS_FORM_ID = '#ITP_FORM_USER_SCH_POPUP';
	const THIS_GIRD_ID = '#ITP_USER_SCH_POPUP_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_USER_SCH_POPUP_jqGridPager';
	
	$('#ITP_USER_SCH_POPUP').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_USER_SCH_POPUP_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_USER_SCH_POPUP').remove();
	}).modal('show');

	$('#ITP_USER_SCH_POPUP_BTN_SEARCH').on('click', function() {
		itp_USER_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_USER_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_USER_SCH_POPUP').modal('hide'); // 팝업닫기
	});

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

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

	itp_fn_USER_SCH_POPUP_empty.push();
	let param = $('#ITP_FORM_USER_SCH_POPUP').serializeObject();
	param.gridSize = 10;

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'USER_ID', name: 'userId',
				label: ITP_MSG_LOCALE.label.userId, // 
				width: '20', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'USER_NM', name: 'userNm',
				label: ITP_MSG_LOCALE.label.userName, // 
				width: '20', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'USER_STAT_NM', name: 'userStatNm',
				label: ITP_MSG_LOCALE.label.status, //
				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/user/pop-grid-list',
		pager: THIS_GIRD_PAGER,
		multiselect: true,
		loadComplete: function(data) {
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'USER_SCH_POPUP', itp_USER_SCH_POPUP_search, itp_fn_USER_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);
};

