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

	});
});

let API_POP_GRID_LIST = '/api/store/pop-grid-list';

function itp_fn_BIZPOP_BRAND_STORE(parentPopFn, args, returnType) {
	
	let itp_BIZPOP_BRAND_STORE_param = {};
	let itp_BIZPOP_BRAND_STORE_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_BRAND_STORE';
	const THIS_GIRD_ID = '#ITP_BIZPOP_BRAND_STORE_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_BRAND_STORE_jqGridPager';

	$('#ITP_FORM_BIZPOP_BRAND_ID').val(fn_make_user_info.get('brandId'));
	if(args) {
		$('#ITP_FORM_BIZPOP_BRAND_STORE_NM').val(args.storeNm);
	}
	
	$('#ITP_BIZPOP_BRAND_STORE').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_BIZPOP_BRAND_STORE_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_BRAND_STORE').remove();
	}).modal('show');

	$('#ITP_BIZPOP_BRAND_STORE_BTN_SEARCH').on('click', function() {
		itp_BIZPOP_BRAND_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_BRAND_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');

		if(selectedIds.length > 1) { // 1개만 선택
			return;
		} 
		
	    for (var i=selectedIds.length-1; i>=0; i--) {
			list.push($(THIS_GIRD_ID).jqGrid('getRowData', selectedIds[i]));	
		}
		parentPopFn(list);
		$('#ITP_BIZPOP_BRAND_STORE').modal('hide'); // 팝업닫기
	});

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

	var itp_fn_BIZPOP_BRAND_STORE_empty = {
		push: function() {
			let param = $(THIS_FORM_ID).serializeObject();
			itp_BIZPOP_BRAND_STORE_param = param;
		},
		back: function() {
			$(THIS_FORM_ID + '_NM').val(itp_BIZPOP_BRAND_STORE_param.storeNm);
		}
	};

	itp_fn_BIZPOP_BRAND_STORE_empty.push();
	let param = $('#ITP_FORM_BIZPOP_BRAND_STORE').serializeObject();
	param.gridSize = 10;

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'BRAND_ID', name: 'brandId',
				label: '브랜드ID', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'BRAND_NM', name: 'brandNm',
				label: '브랜드명', //
				width: '20', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'STORE_ID', name: 'storeId',
				label: '매장ID', //
				width: '0', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'STORE_NM', name: 'storeNm',
				label: '매장명', //
				width: '20', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'STORE_ST_NM', name: 'storeStNm',
				label: '상태', //
				width: '20', fixed: false, align: 'center',
				sortable: false, hidden: false
			}, 
			{
				index: 'LOAN_DVSN', name: 'loanDvsn',
				label: "정산타입", 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: true
			}
		],
		loadBeforeSend: function(jqXHR) {
		    jqXHR.setRequestHeader('X-AUTH-TOKEN', CONN_KEY);
		},
		postData: JSON.stringify(param),
		url: DOMAIN + API_POP_GRID_LIST,
		pager: THIS_GIRD_PAGER,
		multiselect: true,
		loadComplete: function(data) {
			// console.log(data);
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'BIZPOP_BRAND_STORE', itp_BIZPOP_BRAND_STORE_search, itp_fn_BIZPOP_BRAND_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);
};
