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

	});
});

let ITP_FORM_BIZPOP_CATEGORY_MANAGE_CMPY_CD;

let CATEGORY_LOCATION_POPUP_GRID_LIST = '/api/category/location-popup-grid-list';
let WORKPLACE_LIST_WORKPLACE = '/api/workplace/list-workplace';
let CATEGORY_LIST_CATEGORY = '/api/category/list-category';

function itp_fn_BIZPOP_CATEGORY(parentPopFn, args, returnType) {
	// 회사코드 조회
	//var data = JSON.parse(sessionStorage.getItem('itp_manage_cmpy_cd'));
	//ITP_FORM_BIZPOP_CATEGORY_MANAGE_CMPY_CD = data.cmpyCd;
	
	// 공통코드
	fn_make_select(CODE_LIST, 'APPLY_YN', 'ITP_FORM_BIZPOP_CATEGORY_APPLY_YN', true, '적용여부 선택');
	// 사업장리스트
	itp_fn_BIZPOP_CATEGORY_workplace_search(ITP_FORM_BIZPOP_CATEGORY_MANAGE_CMPY_CD);
	
	let itp_BIZPOP_CATEGORY_param = {};
	let itp_BIZPOP_CATEGORY_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_CATEGORY';
	const THIS_GIRD_ID = '#ITP_BIZPOP_CATEGORY_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_CATEGORY_jqGridPager';
	
	$('#ITP_BIZPOP_CATEGORY').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_BIZPOP_CATEGORY_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_CATEGORY').remove();
	}).modal('show');

	$('#ITP_BIZPOP_CATEGORY_BTN_SEARCH').on('click', function() {
		itp_BIZPOP_CATEGORY_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_CATEGORY_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_CATEGORY').modal('hide'); // 팝업닫기
	});

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

	var itp_fn_BIZPOP_CATEGORY_empty = {
		push: function() {
			let param = $(THIS_FORM_ID).serializeObject();
			itp_BIZPOP_CATEGORY_param = param;
		},
		back: function() {
			$(THIS_FORM_ID + '_USER_ID').val(itp_BIZPOP_CATEGORY_param.suserId);
			$(THIS_FORM_ID + '_USER_NM').val(itp_BIZPOP_CATEGORY_param.suserNm);
			$(THIS_FORM_ID + '_USER_STAT_CD').val(itp_BIZPOP_CATEGORY_param.suserStatCd);
		}
	};

	itp_fn_BIZPOP_CATEGORY_empty.push();
	let param = $('#ITP_FORM_BIZPOP_CATEGORY').serializeObject();
	param.gridSize = 10;

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'WKPE_NO', name: 'wkpeNo',
				label: '사업장번호', // 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'LCTN_NO', name: 'lctnNo',
				label: '위치번호', // 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'LCTN_DTL_CD', name: 'lctnDtlCd',
				label: '위치상세코드', // 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: true
			},
			{
				index: 'WKPE_NM', name: 'wkpeNm',
				label: '사업장', // 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'CATE_NM', name: 'cateNm',
				label: '카테고리', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'FLOOR_CNT', name: 'floorCnt',
				label: '층', //
				width: '8', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'LCTN_NM', name: 'lctnNm',
				label: '위치', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'LCTN_DTL_NM', name: 'lctnDtlNm',
				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 + CATEGORY_LOCATION_POPUP_GRID_LIST,
		pager: THIS_GIRD_PAGER,
		multiselect: true,
		loadComplete: function(data) {
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'BIZPOP_CATEGORY', itp_BIZPOP_CATEGORY_search, itp_fn_BIZPOP_CATEGORY_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_CATEGORY_workplace_search = function(cmpyCdVal) {
	const key = {cmpyCd: cmpyCdVal};
	var param = $.param(key);
	fn_ajax_call(WORKPLACE_LIST_WORKPLACE, param, itp_fn_BIZPOP_CATEGORY_workplace_search_callback, 'GET');
};

var itp_fn_BIZPOP_CATEGORY_workplace_search_callback = function(result) {
	fn_make_select_workplace(result, 'ITP_FORM_BIZPOP_CATEGORY_WKPE_NO');
	
	itp_fn_BIZPOP_CATEGORY_category_search($('#ITP_FORM_BIZPOP_CATEGORY_WKPE_NO').val());
};

var itp_fn_BIZPOP_CATEGORY_category_search = function(wkpeNoVal) {
	if(!wkpeNoVal || wkpeNoVal == '') {
		return;
	}
	const key = {wkpeNo: wkpeNoVal};
	var param = $.param(key);
	fn_ajax_call(CATEGORY_LIST_CATEGORY, param, itp_fn_BIZPOP_CATEGORY_category_search_callback, 'GET');
};

var itp_fn_BIZPOP_CATEGORY_category_search_callback = function(result) {
	fn_make_select_category(result, 'ITP_FORM_BIZPOP_CATEGORY_CATE_NO', true, '카테고리 선택');	
};