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

	});
});

let AFFL_POP_GRID_LIST = '/api/affl/pop-grid-list';
let BRAND_POP_GRID_LIST = '/api/brand/affl-brand-list';

function itp_fn_BIZPOP_AFFL_SHOP(parentPopFn, args, returnType) {
	
	let itp_BIZPOP_AFFL_SHOP_param = {};
	let itp_BIZPOP_AFFL_SHOP_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_AFFL_SHOP';
	const THIS_GIRD_ID = '#ITP_BIZPOP_AFFL_SHOP_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_AFFL_SHOP_jqGridPager';
	
	$('#ITP_BIZPOP_AFFL_SHOP').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_BIZPOP_AFFL_SHOP_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_AFFL_SHOP').remove();
	}).modal('show');

	$('#ITP_BIZPOP_AFFL_SHOP_BTN_SEARCH').on('click', function() {
		itp_BIZPOP_AFFL_SHOP_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_BIZPOP_AFFL_SHOP_BTN_SELECT').on('click', function() {		
		const rowKey = $(THIS_GIRD_ID).getGridParam('selrow');
	    if (!rowKey) {
	    	return;
	    }
	    const selRow = $(THIS_GIRD_ID).getRowData(rowKey);
	    console.log(selRow);

		var afflData = {};
		var callbackFn = function(result) {
			afflData['AFFL_SHOP'] = selRow;
			afflData['AFFL_BRAND'] = result.searchList;
			parentPopFn(afflData);
			$('#ITP_BIZPOP_AFFL_SHOP').modal('hide'); // 팝업닫기
		};
		var param = {'afflShopId': selRow.afflShopId};
		fn_ajax_call(BRAND_POP_GRID_LIST, param, callbackFn, 'GET');
	});

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

	var itp_fn_BIZPOP_AFFL_SHOP_empty = {
		push: function() {
			let param = $(THIS_FORM_ID).serializeObject();
			itp_BIZPOP_AFFL_SHOP_param = param;
		},
		back: function() {
			$(THIS_FORM_ID + '_CMPY_CD').val(itp_BIZPOP_AFFL_SHOP_param.scmpyCd);
			$(THIS_FORM_ID + '_CMPY_NM').val(itp_BIZPOP_AFFL_SHOP_param.scmpyNm);
			$(THIS_FORM_ID + '_CMPY_TYPE_CD').val(itp_BIZPOP_AFFL_SHOP_param.scmpyTypeCd);
		}
	};

	itp_fn_BIZPOP_AFFL_SHOP_empty.push();
	let param = $('#ITP_FORM_BIZPOP_AFFL_SHOP').serializeObject();
	param.gridSize = 10;

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'AFFL_SHOP_ID', name: 'afflShopId',
				label: '브랜드그룹코드', // 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'AFFL_SHOP_NM', name: 'afflShopNm',
				label: '브랜드그룹명', //
				width: '12', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'ST_NM', name: 'stNm',
				label: '상태', //
				width: '6', fixed: false, align: 'center',
				sortable: false, hidden: false
			}
		],
		loadBeforeSend: function(jqXHR) {
		    jqXHR.setRequestHeader('X-AUTH-TOKEN', CONN_KEY);
		},
		postData: JSON.stringify(param),
		url: DOMAIN + AFFL_POP_GRID_LIST,
		pager: THIS_GIRD_PAGER,
		multiselect: false,
		loadComplete: function(data) {
			console.log(data);
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'BIZPOP_AFFL_SHOP', itp_BIZPOP_AFFL_SHOP_search, itp_fn_BIZPOP_AFFL_SHOP_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);
};
