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

	});
});

let API_ITEM_CLASS_LEVEL_POP	= '/api/item/class-level-list';		//품목분류 level 검색
let API_ITEM_CLASS_SAVE_POP		= '/api/item/save-itemClassMng';	// 저장

var popPageObj = {
	init: function () {
		this.form();
		this.button();
		popItemClassLevel.init('ITP_FORM_STINFO07010_POP_ITEM_CLASS', 1);
	},
	parentPopFn: null,
	args: null,
	returnType: null,
	button: function() {
		$('#ITP_BTN_STINFO07010_CLOSE_POP').on('click', function () {
			$('#ITP_BIZPOP_ITEM_CLASS_MANAGE').modal('hide'); // 팝업닫기
		});
		$('#ITP_BTN_STINFO07010_DETAIL_SAVE_POP').on('click', function () {
			const formId = '#ITP_FORM_NOTICE_INFO_POPUP';
			var validFn = function() {
				var isValid = false;
				$(formId).find('input, select, checkbox, textarea').each(function(k) {
					if($(this).data('check-required')) {
						var arry = $(this).data('check-required').split(',');
						isValid = itp_fn_form_validate(formId, '#' + $(this).attr('id'), arry, undefined);
						if(!isValid) return false;
					}
				});
				return isValid;
			};
			itp_fn_form_clear_validate(null, formId);
			if (validFn()) {
				$('#ITP_FORM_STINFO07010_POP_BRAND_ID').val(fn_make_user_info.get('brandId'));
				$('#ITP_FORM_STINFO07010_POP_ITEM_CLASS_MGNT_NO').val(popItemClassLevel.itemClassMgntNo());
				let param = $(formId).serializeObject();
				console.log(JSON.stringify(param));
				var saveFn = function(result) {
					popPageObj.parentPopFn(result);
					$('#ITP_BIZPOP_ITEM_CLASS_MANAGE').modal('hide'); // 팝업닫기
				};
				fn_ajax_call(API_ITEM_CLASS_SAVE_POP, JSON.stringify(param), saveFn, 'POST');
			}
		});
	},
	form: function() {
		$('#ITP_FORM_STINFO07010_POP_ITEM_CLASS1').on('change', function() {
			$('#ITP_FORM_NOTICE_INFO_POPUP').find('.fnItemClassCd').text(popItemClassLevel.itemClassMgntNo());
			($(this).val() === '') ? popItemClassLevel.clear(2): popItemClassLevel.change(2);
		});
		$('#ITP_FORM_STINFO07010_POP_ITEM_CLASS2').on('change', function() {
			$('#ITP_FORM_NOTICE_INFO_POPUP').find('.fnItemClassCd').text(popItemClassLevel.itemClassMgntNo());
			($(this).val() === '') ? popItemClassLevel.clear(3): popItemClassLevel.change(3);
		});
		$('#ITP_FORM_STINFO07010_POP_ITEM_CLASS3').on('change', function() {
			$('#ITP_FORM_NOTICE_INFO_POPUP').find('.fnItemClassCd').text(popItemClassLevel.itemClassMgntNo());
			($(this).val() === '') ? popItemClassLevel.clear(4): popItemClassLevel.change(4);
		});
		$('#ITP_FORM_STINFO07010_POP_ITEM_CLASS4').on('change', function() {
			$('#ITP_FORM_NOTICE_INFO_POPUP').find('.fnItemClassCd').text(popItemClassLevel.itemClassMgntNo());
		});
	}
};

var popItemClassLevel = {
	levelID: '',
	init: function(id, level) {
		this.levelID = id;
		this.clear(level);
		this.make(level);
	},
	make: function(level) {
		this.call(level);
	},
	change: function(level) {
		this.clear(level);
		this.call(level);
	},
	call: function(level) {
		var _this = this;
		var callbackFn = function(result) {
			fn_make_select_item_class(result.searchList, _this.levelID + level, true,'품목분류Level' + level);
		};
		var keys =  function() {
			var keys = {sBrandId:fn_make_user_info.get('brandId'), sItemLevel: level};
			for(var i=1; i<level; i++) {
				var key = 'sItemClass' + i;
				var id = '#' + _this.levelID + i;
				keys[key] = $(id + ' option:selected').val();
			}
			return keys;
		};
		fn_ajax_call(API_ITEM_CLASS_LEVEL_POP, $.param(keys(level)), callbackFn, 'GET');
	},
	clear: function(level) {
		for(var i=level; i<5; i++) {
			$('#' + (this.levelID + i)).find('option:not(:first)').remove();
		}
		$('#ITP_FORM_NOTICE_INFO_POPUP').find('.fnItemClassCd').text(this.itemClassMgntNo());
	},
	itemClassMgntNo: function() {
		var mgntNo = '';
		for(var i=1; i<5; i++) {
			mgntNo +=  $('#' + this.levelID + i + ' option:selected').val();
		}
		return mgntNo;
	}
};

function itp_fn_BIZPOP_ITEM_CLASS_MANAGE(parentPopFn, args, returnType) {
	$('#ITP_BIZPOP_ITEM_CLASS_MANAGE').one('shown.bs.modal', function(e) {
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_BIZPOP_ITEM_CLASS_MANAGE').remove();
	}).modal('show');

	popPageObj.parentPopFn = parentPopFn;
	popPageObj.args = args;
	popPageObj.returnType = returnType;
	popPageObj.init();

}
