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

	});
});

let NK_DID_CONTENTS_GRID_LIST = '/api/nk/did/contents-grid-list';

function itp_fn_BIZPOP_NK_DISPLAY_CONTENTS(parentPopFn, schParam, returnType) {
	
	$('#ITP_FORM_BIZPOP_NK_DISPLAY_CONTENTS_CMPY_CD').val(schParam.cmpyCd);
	$('#ITP_FORM_BIZPOP_NK_DISPLAY_CONTENTS_BRAND_CD').val(schParam.brandCd);
	$('#ITP_FORM_BIZPOP_NK_DISPLAY_CONTENTS_STORE_NO').val(schParam.storeNo);
	
	// 공통코드
	fn_make_select(CODE_LIST, 'USE_YN', 'ITP_FORM_BIZPOP_NK_DISPLAY_CONTENTS_USE_YN');
	fn_make_select(CODE_LIST, 'CONTENTS_TYPE_CD', 'ITP_FORM_BIZPOP_NK_DISPLAY_CONTENTS_CONTENTS_TYPE_CD');
	
	let itp_BIZPOP_NK_DISPLAY_CONTENTS_param = {};
	let itp_BIZPOP_NK_DISPLAY_CONTENTS_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_NK_DISPLAY_CONTENTS';
	const THIS_GIRD_ID = '#ITP_BIZPOP_NK_DISPLAY_CONTENTS_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_NK_DISPLAY_CONTENTS_jqGridPager';
	
	$('#ITP_BIZPOP_NK_DISPLAY_CONTENTS').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_BIZPOP_NK_DISPLAY_CONTENTS_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_NK_DISPLAY_CONTENTS').remove();
	}).modal('show');
	
	$('#ITP_FORM_BIZPOP_NK_DISPLAY_CONTENTS_USE_YN, #ITP_FORM_BIZPOP_NK_DISPLAY_CONTENTS_CONTENTS_TYPE_CD').on('change', function() {
		itp_fn_BIZPOP_NK_DISPLAY_CONTENTS_search(THIS_FORM_ID, THIS_GIRD_ID);	
	});

	$('#ITP_BIZPOP_NK_DISPLAY_CONTENTS_BTN_SEARCH').on('click', function() {
		itp_fn_BIZPOP_NK_DISPLAY_CONTENTS_search(THIS_FORM_ID, THIS_GIRD_ID);
	});
	
	$('#ITP_BIZPOP_NK_DISPLAY_CONTENTS_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_BIZPOP_NK_DISPLAY_CONTENTS').modal('hide'); // 팝업닫기
		
	});

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

	var itp_fn_BIZPOP_NK_DISPLAY_CONTENTS_empty = {
		push: function() {
			let param = $(THIS_FORM_ID).serializeObject();
			itp_BIZPOP_NK_DISPLAY_CONTENTS_param = param;
		},
		back: function() {
			$(THIS_FORM_ID + '_PRDT_TYPE').val(itp_BIZPOP_NK_DISPLAY_CONTENTS_param.sprdtType);
			$(THIS_FORM_ID + '_CLASS_CD').val(itp_BIZPOP_NK_DISPLAY_CONTENTS_param.sclassCd);
			$(THIS_FORM_ID + '_PRDT_NM').val(itp_BIZPOP_NK_DISPLAY_CONTENTS_param.sprdtNm);
			$(THIS_FORM_ID + '_BARCODE_NO').val(itp_BIZPOP_NK_DISPLAY_CONTENTS_param.sbarcodeNo);
		}
	};

	itp_fn_BIZPOP_NK_DISPLAY_CONTENTS_empty.push();
	let param = $('#ITP_FORM_BIZPOP_NK_DISPLAY_CONTENTS').serializeObject();
	param.gridSize = 10;

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'CONTENTS_NO', name: 'contentsNo',
				label: '컨텐츠번호', 
				width: '12', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'CONTENTS_NM', name: 'contentsNm',
				label: '컨텐츠명', 
				width: '20', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'CONTENTS_TYPE_NM', name: 'contentsTypeNm',
				label: '컨텐츠타입', 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},				
			{
				index: 'USE_YN_NM', name: 'useYnNm',
				label: '사용여부', 
				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 + NK_DID_CONTENTS_GRID_LIST,
		pager: THIS_GIRD_PAGER,
		multiselect: true,
		loadComplete: function(data) {
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'BIZPOP_NK_DISPLAY_CONTENTS', itp_BIZPOP_NK_DISPLAY_CONTENTS_search, itp_fn_BIZPOP_NK_DISPLAY_CONTENTS_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_DISPLAY_CONTENTS').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_DISPLAY_CONTENTS_search = function(FORM_ID, GRID_ID) {	
	itp_BIZPOP_NK_DISPLAY_CONTENTS_search = true;
	let param = $(FORM_ID).serializeObject();
	param.gridSize = 10;
	param.gridPage = $.jgrid.defaults.page;
	$(GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
};
