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

	});
});

let NK_EVENT_EXC_STORE_GRID_LAST_ROW_ID;

let NK_EVENT_EXC_GRID_LIST = '/api/nk/event/exc-grid-list';
let NK_LIST_CLASS = '/api/nk/class/list-class'; // 카테고리리스트
let NK_SAVE_EVENT_EXC_STORE = '/api/nk/event/save-event-exc'; // 이벤트 제외매장 저장

function itp_fn_BIZPOP_NK_EVENT_EXC_STORE(parentPopFn, schParam, returnType) {
	
	$('#ITP_FORM_BIZPOP_NK_EVENT_EXC_STORE_SAVE_BRAND_CD').val(schParam.brandCd);
	$('#ITP_FORM_BIZPOP_NK_EVENT_EXC_STORE_SAVE_EVENT_NO').val(schParam.eventNo);
	
	$('#ITP_FORM_BIZPOP_NK_EVENT_EXC_STORE_BRAND_CD').val(schParam.brandCd);
	$('#ITP_FORM_BIZPOP_NK_EVENT_EXC_STORE_EVENT_NO').val(schParam.eventNo);
	
	// 공통코드 표시
	fn_make_select(CODE_LIST, 'YN', 'ITP_FORM_BIZPOP_NK_EVENT_EXC_STORE_EXC_STORE_YN', true, '제외매장여부 선택');
	
	let itp_BIZPOP_NK_EVENT_EXC_STORE_param = {};
	let itp_BIZPOP_NK_EVENT_EXC_STORE_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_NK_EVENT_EXC_STORE';
	const THIS_GIRD_ID = '#ITP_BIZPOP_NK_EVENT_EXC_STORE_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_NK_EVENT_EXC_STORE_jqGridPager';
	
	$('#ITP_BIZPOP_NK_EVENT_EXC_STORE').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_BIZPOP_NK_EVENT_EXC_STORE_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_NK_EVENT_EXC_STORE').remove();
	}).modal('show');
	
	$('#ITP_FORM_BIZPOP_NK_EVENT_EXC_STORE_EXC_STORE_YN').on('change', function() {
		itp_fn_BIZPOP_NK_EVENT_EXC_STORE_search(THIS_FORM_ID, THIS_GIRD_ID);
	});

	$('#ITP_BIZPOP_NK_EVENT_EXC_STORE_BTN_SEARCH').on('click', function() {
		itp_fn_BIZPOP_NK_EVENT_EXC_STORE_search(THIS_FORM_ID, THIS_GIRD_ID);
	});
	
	$('#ITP_BIZPOP_NK_EVENT_EXC_STORE_BTN_SAVE').on('click', function() {		
		
		// 수정 데이터 가져오기
		let gridUpdateData = [];
		let rowData = $(THIS_GIRD_ID).getRowData();
		$.each(rowData, function(key, value) {
			if (value.viewCd == 'U') {
				gridUpdateData.push(value);
			}
		});
		let param = $('#ITP_FORM_BIZPOP_NK_EVENT_EXC_STORE_SAVE').serializeObject();
		param.gridUpdateData = gridUpdateData;
		
		fn_ajax_call(NK_SAVE_EVENT_EXC_STORE, JSON.stringify(param), function() {			
			parentPopFn();
			$('#ITP_BIZPOP_NK_EVENT_EXC_STORE').modal('hide'); // 팝업닫기			
		}, 'POST');
	});
	
	$('#ITP_BIZPOP_NK_EVENT_EXC_STORE').on('click', function(e) {
		var gridId = THIS_GIRD_ID.replace('#', '');
		itp_fn_grid_reset_selection(e, NK_EVENT_EXC_STORE_GRID_LAST_ROW_ID, gridId, 'NK_EVENT_EXC_STORE');
	});

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

	var itp_fn_BIZPOP_NK_EVENT_EXC_STORE_empty = {
		push: function() {
			let param = $(THIS_FORM_ID).serializeObject();
			itp_BIZPOP_NK_EVENT_EXC_STORE_param = param;
		},
		back: function() {
			$(THIS_FORM_ID + '_BRAND_CD').val(itp_BIZPOP_NK_EVENT_EXC_STORE_param.brandCd);
			$(THIS_FORM_ID + '_EVENT_NO').val(itp_BIZPOP_NK_EVENT_EXC_STORE_param.eventNo);
			$(THIS_FORM_ID + '_STORE_NM').val(itp_BIZPOP_NK_EVENT_EXC_STORE_param.storeNm);
			$(THIS_FORM_ID + '_EXC_STORE_YN').val(itp_BIZPOP_NK_EVENT_EXC_STORE_param.excStoreYn);
		}
	};

	itp_fn_BIZPOP_NK_EVENT_EXC_STORE_empty.push();
	let param = $('#ITP_FORM_BIZPOP_NK_EVENT_EXC_STORE').serializeObject();
	param.gridSize = 10;

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'VIEW_CD', name: 'viewCd',
				label: ITP_MSG_LOCALE.label.viewCd,
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'EXC_STORE_YN', name: 'excStoreYn',
				label: '제외매장선택', 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false,
				editable: true, formatter: 'checkbox', edittype: 'checkbox',
				editoptions: {value: 'Y:N', defaultValue: 'Y'},
				editrules: {required: true}
			},
			{
				index: 'EXC_STORE_NO', name: 'excStoreNo',
				label: '매장번호', // 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'EXC_STORE_NM', name: 'excStoreNm',
				label: '매장명', // 
				width: '15', fixed: false, align: 'center',
				sortable: false, hidden: false
			}
		],
		loadBeforeSend: function(jqXHR) {
		    jqXHR.setRequestHeader('X-AUTH-TOKEN', CONN_KEY);
		},
		postData: JSON.stringify(param),
		url: DOMAIN + NK_EVENT_EXC_GRID_LIST,
		pager: THIS_GIRD_PAGER,
		multiselect: false,
		onCellSelect: function(rowId, cellIdx, cellValue) {
			NK_EVENT_EXC_STORE_GRID_LAST_ROW_ID = itp_fn_set_data_editable(rowId, cellIdx, NK_EVENT_EXC_STORE_GRID_LAST_ROW_ID, THIS_GIRD_ID, ['excStoreNo']);
	   	},
		loadComplete: function(data) {
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'BIZPOP_NK_EVENT_EXC_STORE', itp_BIZPOP_NK_EVENT_EXC_STORE_search, itp_fn_BIZPOP_NK_EVENT_EXC_STORE_empty, true, data.gridRecords, false);
	    },
	    loadError: function(jqXHR, textStatus, errorThrown) {
	    	itp_fn_grid_load_error(jqXHR, textStatus, errorThrown);
	    },
	    onPaging: function(action) {
			param = $('#ITP_FORM_BIZPOP_NK_EVENT_EXC_STORE').serializeObject();
			param.gridSize = 10;
	    	itp_fn_grid_paging(THIS_GIRD_ID, action, param);
	    }
	}).navGrid(THIS_GIRD_PAGER, ITP_GRID_NAV_DEFAULTS.navGrid);
};

var itp_fn_BIZPOP_NK_EVENT_EXC_STORE_search = function(FORM_ID, GRID_ID) {	
	itp_BIZPOP_NK_EVENT_EXC_STORE_search = true;
	let param = $(FORM_ID).serializeObject();
	param.gridSize = 10;
	param.gridPage = $.jgrid.defaults.page;
	$(GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
};
