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

	});
});

let NK_RECOMMEND_PRODUCT_POP_GRID_LIST = '/api/nk/product/grid-list';
let NK_LIST_CLASS = '/api/nk/class/list-class'; // 카테고리리스트
let NK_SAVE_RECOMMEND_PRODUCT = '/api/nk/recommend/product/save-recommend-product'; // 추천상품정보 저장

function itp_fn_BIZPOP_NK_RECOMMEND_PRODUCT(parentPopFn, schParam, returnType) {
	
	$('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_SAVE_BRAND_CD').val(schParam.brandCd);
	$('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_SAVE_STORE_NO').val(schParam.storeNo);
	
	$('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_CMPY_CD').val(schParam.cmpyCd);
	$('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_BRAND_CD').val(schParam.brandCd);
	$('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_STORE_NO').val(schParam.storeNo);
	
	// 공통코드 표시
	if(schParam.manageType == 'B') {
		$('<option/>', {'value': 'B', 'text': '본사상품'}).appendTo($('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_PRDT_TYPE'));	
	} else if(schParam.manageType == 'S') {
		$('<option/>', {'value': 'S', 'text': '매장상품'}).appendTo($('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_PRDT_TYPE'));
		$('<option/>', {'value': 'B', 'text': '본사상품'}).appendTo($('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_PRDT_TYPE'));
	}
	
	// 카테고리리스트
	itp_fn_BIZPOP_NK_RECOMMEND_PRODUCT_class_list();
	
	let itp_BIZPOP_NK_RECOMMEND_PRODUCT_param = {};
	let itp_BIZPOP_NK_RECOMMEND_PRODUCT_search = false;

	const THIS_FORM_ID = '#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT';
	const THIS_GIRD_ID = '#ITP_BIZPOP_NK_RECOMMEND_PRODUCT_jqGrid';
	const THIS_GIRD_PAGER = '#ITP_BIZPOP_NK_RECOMMEND_PRODUCT_jqGridPager';
	
	$('#ITP_BIZPOP_NK_RECOMMEND_PRODUCT').one('shown.bs.modal', function(e) {
		itp_fn_jqgrid_resize(THIS_GIRD_ID, '#ITP_BIZPOP_NK_RECOMMEND_PRODUCT_jqGrid_list', '');
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_NK_RECOMMEND_PRODUCT').remove();
	}).modal('show');
	
	$('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_PRDT_TYPE').on('change', function() {
		// 카테고리리스트
		itp_fn_BIZPOP_NK_RECOMMEND_PRODUCT_class_list(function() {
			itp_fn_BIZPOP_NK_RECOMMEND_PRODUCT_search(THIS_FORM_ID, THIS_GIRD_ID);	
		});	
	});
	
	$('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_CLASS_CD').on('change', function() {
		itp_fn_BIZPOP_NK_RECOMMEND_PRODUCT_search(THIS_FORM_ID, THIS_GIRD_ID);	
	});

	$('#ITP_BIZPOP_NK_RECOMMEND_PRODUCT_BTN_SEARCH').on('click', function() {
		itp_fn_BIZPOP_NK_RECOMMEND_PRODUCT_search(THIS_FORM_ID, THIS_GIRD_ID);
	});
	
	$('#ITP_BIZPOP_NK_RECOMMEND_PRODUCT_BTN_SAVE').on('click', function() {		
		const rowKey = $(THIS_GIRD_ID).getGridParam('selrow');
	    if (!rowKey) {
	    	return;
	    }

		var gridInsertData = [];
	    var selectedIds = $(THIS_GIRD_ID).getGridParam('selarrrow');
		
	    for (var i=selectedIds.length-1; i>=0; i--) {
			gridInsertData.push($(THIS_GIRD_ID).jqGrid('getRowData', selectedIds[i]));	
		}
		
		let param = $('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_SAVE').serializeObject();
		param.gridInsertData = gridInsertData;
		
		fn_ajax_call(NK_SAVE_RECOMMEND_PRODUCT, JSON.stringify(param), function() {			
			parentPopFn();
			$('#ITP_BIZPOP_NK_RECOMMEND_PRODUCT').modal('hide'); // 팝업닫기			
		}, 'POST');
	});

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

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

	itp_fn_BIZPOP_NK_RECOMMEND_PRODUCT_empty.push();
	let param = $('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT').serializeObject();
	param.gridSize = 10;

	$(THIS_GIRD_ID).jqGrid({
		colModel: [
			{
				index: 'CLASS_NM', name: 'classNm',
				label: '카테고리', 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'PRDT_NO', name: 'prdtNo',
				label: '상품번호', // 
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'PRDT_NM', name: 'prdtNm',
				label: '상품명', // 
				width: '15', fixed: false, align: 'center',
				sortable: false, hidden: false
			},
			{
				index: 'SALE_PRICE', name: 'salePrice',
				label: '상품가격', //
				width: '10', fixed: false, align: 'center',
				sortable: false, hidden: false, 
				formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
			},
			{
				index: 'BARCODE_NO', name: 'barcodeNo',
				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 + NK_RECOMMEND_PRODUCT_POP_GRID_LIST,
		pager: THIS_GIRD_PAGER,
		multiselect: true,
		loadComplete: function(data) {
			itp_fn_grid_load_complete(data, THIS_GIRD_ID, true, undefined, 'BIZPOP_NK_RECOMMEND_PRODUCT', itp_BIZPOP_NK_RECOMMEND_PRODUCT_search, itp_fn_BIZPOP_NK_RECOMMEND_PRODUCT_empty, true, data.gridRecords, false);
	    },
	    loadError: function(jqXHR, textStatus, errorThrown) {
	    	itp_fn_grid_load_error(jqXHR, textStatus, errorThrown);
	    },
	    onPaging: function(action) {
			param = $('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT').serializeObject();
			param.gridSize = 10;
	    	itp_fn_grid_paging(THIS_GIRD_ID, action, param);
	    }
	}).navGrid(THIS_GIRD_PAGER, ITP_GRID_NAV_DEFAULTS.navGrid);
};

var itp_fn_BIZPOP_NK_RECOMMEND_PRODUCT_search = function(FORM_ID, GRID_ID) {	
	itp_BIZPOP_NK_RECOMMEND_PRODUCT_search = true;
	let param = $(FORM_ID).serializeObject();
	param.gridSize = 10;
	param.gridPage = $.jgrid.defaults.page;
	$(GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
};

var itp_fn_BIZPOP_NK_RECOMMEND_PRODUCT_class_list = function(fnCall) {
	
	const prdtType = $('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_PRDT_TYPE').val();
	const brandCdVal = $('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_BRAND_CD').val();
	let storeNoVal = $('#ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_STORE_NO').val();
	// 본사상품	
	if(prdtType == 'B') storeNoVal = '0000';
	
	const key = {brandCd: brandCdVal, storeNo: storeNoVal};
	var param = $.param(key);
	fn_ajax_call(NK_LIST_CLASS, param, function(result) {
		
		fn_make_select_class(result, 'ITP_FORM_BIZPOP_NK_RECOMMEND_PRODUCT_CLASS_CD', true, '카테고리');
		
		if (fnCall !== undefined || typeof fnCall !== 'undefined') fnCall();
		 	
	}, 'GET');
};
