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

	});
});

let SAUSER_POP_SALEUSER_GRID_LIST = '/api/srep/pop-grid-list';
// let SAUSER_POP_SALEUSER_GRID_LIST = '/api/sauser/pop-saleuser-grid-list';

function itp_fn_BIZPOP_NK_SALES_USER_SCH(parentPopFn, schParam, returnType) {
	
	let itp_BIZPOP_NK_SALES_USER_SCH_param = {};
	let itp_BIZPOP_NK_SALES_USER_SCH_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_NK_SALES_USER_SCH';
	const THIS_GIRD_ID = '#ITP_BIZPOP_NK_SALES_USER_SCH_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_NK_SALES_USER_SCH_jqGridPager';
	
	$('#ITP_BIZPOP_NK_SALES_USER_SCH').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_BIZPOP_NK_SALES_USER_SCH_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_NK_SALES_USER_SCH').remove();
	}).modal('show');

	$('#ITP_BIZPOP_NK_SALES_USER_SCH_BTN_SEARCH').on('click', function() {
		itp_BIZPOP_NK_SALES_USER_SCH_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_NK_SALES_USER_SCH_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개만 선택
			itp_fn_modal_alert('1명을 선택해주세요.');
			return;
		}
		
	    for (var i=selectedIds.length-1; i>=0; i--) {
			list.push($(THIS_GIRD_ID).jqGrid('getRowData', selectedIds[i]));	
		}
		parentPopFn(list);
		$('#ITP_BIZPOP_NK_SALES_USER_SCH').modal('hide'); // 팝업닫기
	});

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

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

	itp_fn_BIZPOP_NK_SALES_USER_SCH_empty.push();
	let param = $('#ITP_FORM_BIZPOP_NK_SALES_USER_SCH').serializeObject();
	param.gridSize = 10;

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'SALE_MGR__ID', name: 'saleMgrId',
				label: '영업담당자ID', 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'SALE_MGR_NM', name: 'saleMgrNm',
				label: '영업담당자명', // 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'ST_CD', name: 'stCd',
				label: '상태', //
				width: '12', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'ST_NM', name: 'stNm',
				label: '상태', //
				width: '12', fixed: false, align: 'center',
				sortable: false, hidden: true
			}
		],
		loadBeforeSend: function(jqXHR) {
		    jqXHR.setRequestHeader('X-AUTH-TOKEN', CONN_KEY);
		},
		postData: JSON.stringify(param),
		url: DOMAIN + SAUSER_POP_SALEUSER_GRID_LIST,
		pager: THIS_GIRD_PAGER,
		multiselect: true,
		loadComplete: function(data) {
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'BIZPOP_NK_SALES_USER_SCH', itp_BIZPOP_NK_SALES_USER_SCH_search, itp_fn_BIZPOP_NK_SALES_USER_SCH_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);
};

