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

	});
});

let FRCOMP_STORE_POP_GRID_LIST = '/api/nk/store/grid-list';
let BRAND_LIST_BRAND = '/api/nk/brand/list-brand'; // 브랜드리스트

function itp_fn_BIZPOP_FRCOMP_STORE(parentPopFn, cmpyCd, returnType) {
	// 브랜드그룹코드
	$('#ITP_FORM_BIZPOP_FRCOMP_STORE_CMPY_CD').val(cmpyCd);
	// 공통코드
	fn_make_select(CODE_LIST, 'STORE_TYPE', 'ITP_FORM_BIZPOP_FRCOMP_STORE_STORE_TYPE', true, '매장형태 선택');
	// 브랜드리스트
	itp_fn_BIZPOP_FRCOMP_STORE_brand_list();
	
	let itp_BIZPOP_FRCOMP_STORE_param = {};
	let itp_BIZPOP_FRCOMP_STORE_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_FRCOMP_STORE';
	const THIS_GIRD_ID = '#ITP_BIZPOP_FRCOMP_STORE_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_FRCOMP_STORE_jqGridPager';
	
	$('#ITP_BIZPOP_FRCOMP_STORE').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_BIZPOP_FRCOMP_STORE_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_FRCOMP_STORE').remove();
	}).modal('show');

	$('#ITP_BIZPOP_FRCOMP_STORE_BTN_SEARCH').on('click', function() {
		itp_BIZPOP_FRCOMP_STORE_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_FRCOMP_STORE_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_FRCOMP_STORE').modal('hide'); // 팝업닫기
	});

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

	var itp_fn_BIZPOP_FRCOMP_STORE_empty = {
		push: function() {
			let param = $(THIS_FORM_ID).serializeObject();
			itp_BIZPOP_FRCOMP_STORE_param = param;
		},
		back: function() {
			$(THIS_FORM_ID + '_BRAND_CD').val(itp_BIZPOP_FRCOMP_STORE_param.sbrandCd);
			$(THIS_FORM_ID + '_STORE_NM').val(itp_BIZPOP_FRCOMP_STORE_param.sstoreNm);
			$(THIS_FORM_ID + '_STORE_TYPE').val(itp_BIZPOP_FRCOMP_STORE_param.sstoreType);
		}
	};

	itp_fn_BIZPOP_FRCOMP_STORE_empty.push();
	let param = $('#ITP_FORM_BIZPOP_FRCOMP_STORE').serializeObject();
	param.gridSize = 10;

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'SALE_STORE_NO', name: 'saleStoreNo',
				label: '판매매장번호', // 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'BRAND_CD', name: 'brandCd',
				label: '브랜드코드', // 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'BRAND_NM', name: 'brandNm',
				label: '브랜드명', //
				width: '12', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'STORE_NO', name: 'storeNo',
				label: '매장번호', //
				width: '6', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'STORE_NM', name: 'storeNm',
				label: '매장명', //
				width: '12', fixed: false, align: 'center',
				sortable: false, hidden: false
			}
		],
		loadBeforeSend: function(jqXHR) {
		    jqXHR.setRequestHeader('X-AUTH-TOKEN', CONN_KEY);
		},
		postData: JSON.stringify(param),
		url: DOMAIN + FRCOMP_STORE_POP_GRID_LIST,
		pager: THIS_GIRD_PAGER,
		multiselect: true,
		loadComplete: function(data) {
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'BIZPOP_FRCOMP_STORE', itp_BIZPOP_FRCOMP_STORE_search, itp_fn_BIZPOP_FRCOMP_STORE_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);
};

var itp_fn_BIZPOP_FRCOMP_STORE_brand_list = function() {
	const cmpyCdVal = $('#ITP_FORM_BIZPOP_FRCOMP_STORE_CMPY_CD').attr('data-cmpy-cd');	
	const key = {cmpyCd: cmpyCdVal, userTypeCd: USER_TYPE_CD, userId: USER_ID};
	var param = $.param(key);
	fn_ajax_call(BRAND_LIST_BRAND, param, itp_fn_BIZPOP_FRCOMP_STORE_brand_list_callback, 'GET');
};

var itp_fn_BIZPOP_FRCOMP_STORE_brand_list_callback = function(result) {
	fn_make_select_brand(result, 'ITP_FORM_BIZPOP_FRCOMP_STORE_BRAND_CD', true, '브랜드');
};
