
let ITP_FORM_SYSTEM02010_IS_WRITING = false;
let ITP_FORM_SYSTEM02010_IS_DUPLICATE = false;

let MENU_SAVE_MENU = '/api/menu/save-menu';
let MENU_DEL_MENU = '/api/menu/del-menu';
let MENU_CHECK_DUPLICATE = '/api/menu/check-duplicate';
let MENU_MENU_SEARCH = '/api/menu/menu-search';
let MENU_INFO_MENU = '/api/menu/info-menu';

require(['config'], function() {
	require([
		'jquery'
	], function($) {
		// 버튼 권한설정
		fn_proc_btn_auth('SYSTEM02010');
		// 공통코드 표시
		fn_make_select(CODE_LIST, 'USE_YN', 'ITP_FORM_SYSTEM02010_USE_YN', true, '선택');
		fn_make_select(CODE_LIST, 'OPEN_CD', 'ITP_FORM_SYSTEM02010_OPEN_CD', true, '선택');
		fn_make_select(CODE_LIST, 'USE_YN', 'ITP_FORM_SYSTEM02010_ICON_YN', true, '선택');
	
		itp_fn_SYSTEM02010_search();
    	itp_fn_fire_window_resize();

		$('#ITP_BTN_SYSTEM02010_SRH').on('click', function() {
			itp_fn_SYSTEM02010_search();
		});

		$('#ITP_AJAX_SYSTEM02010_SRH_CONTAINER').on('click', '.list-group .list-group-item', function() {
			const $this = $(this);
			var itemFn = {
				callBack: function(args) {
					$('#ITP_AJAX_SYSTEM02010_SRH_CONTAINER .list-group .list-group-item').removeClass('active');
					$this.addClass('active');
					const txt = $this.attr('data-itp-param-key');
					const level = $this.attr('data-level');
					itp_fn_SYSTEM02010_detail(txt, level);
				}
			};
			if (ITP_FORM_SYSTEM02010_IS_WRITING) {
				itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, itemFn, $this);
			} else {
				itemFn.callBack($this);
			}
		});

		$('#ITP_BTN_SYSTEM02010_ADD').on('click', function() {
			var addFn = {
    			callBack: function(args) {
    				const item = $('#ITP_SYSTEM02010_TREEVIEW').find('.list-group-item.active');
    				const mKey = item.attr('data-itp-param-key'); // 상위메뉴코드
    				const mTxt = item.find('span').first().text(); // 상위메뉴명

					// 정보초기화
					itp_fn_form_clear_validate(null, '#ITP_FORM_SYSTEM02010_DETAIL');
					$('#ITP_FORM_SYSTEM02010_VIEW_CD').val('C');
					$('#ITP_FORM_SYSTEM02010_TOP_MENU_NO').val(mKey);
					$('#ITP_FORM_SYSTEM02010_TOP_MENU_NM').val(mTxt);
					$('#ITP_FORM_SYSTEM02010_MENU_NO').val('');
					$('#ITP_FORM_SYSTEM02010_MENU_ID').val('');
					$('#ITP_FORM_SYSTEM02010_USE_YN').val('');
					$('#ITP_FORM_SYSTEM02010_MENU_NM_KR').val('');
					$('#ITP_FORM_SYSTEM02010_CONNECT_URL').val('');
					$('#ITP_FORM_SYSTEM02010_OPEN_CD').val('');
					$('#ITP_FORM_SYSTEM02010_ORD_NO').val('');
					$('#ITP_FORM_SYSTEM02010_ICON_YN').val('');
					$('#ITP_FORM_SYSTEM02010_ROLE_DESC').val('');
					$('#ITP_FORM_SYSTEM02010_MENU_ID').removeAttr('readonly');
					$('#ITP_BTN_SYSTEM02010_DUP').removeAttr('disabled');
    			}
    		};
			if (ITP_FORM_SYSTEM02010_IS_WRITING) {				
				itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, addFn, null);
			} else {
				addFn.callBack();
			}
		});

		$('#ITP_BTN_SYSTEM02010_SAVE').on('click', function() {
			const formId = '#ITP_FORM_SYSTEM02010';
			itp_fn_form_clear_validate(null, formId + '_DETAIL');
			var vali_menuId = itp_fn_form_validate(formId + '_DETAIL', formId + '_MENU_ID', ['empty'], undefined);
			var vali_useYn = itp_fn_form_validate(formId + '_DETAIL', formId + '_USE_YN', ['empty'], undefined);
			var vali_menuNmKr = itp_fn_form_validate(formId + '_DETAIL', formId + '_MENU_NM_KR', ['empty'], undefined);
			var vali_connectUrl = itp_fn_form_validate(formId + '_DETAIL', formId + '_CONNECT_URL', ['empty'], undefined);
			var vali_openCd = itp_fn_form_validate(formId + '_DETAIL', formId + '_OPEN_CD', ['empty'], undefined);
			var vali_ordNo = itp_fn_form_validate(formId + '_DETAIL', formId + '_ORD_NO', ['empty', 'number'], undefined);

			if (vali_menuId && vali_useYn && vali_menuNmKr && vali_connectUrl && vali_openCd && vali_ordNo) {
				if (ITP_FORM_SYSTEM02010_IS_DUPLICATE) {
					itp_fn_modal_duplicate(ITP_MSG_LOCALE.label.menuId, formId + '_MENU_ID');
				} else {
					let param = $(formId + '_DETAIL').serializeObject();
					// console.log(JSON.stringify(param)); 
					var searhFn = function() {
						ITP_FORM_SYSTEM02010_IS_WRITING = false; 
						itp_fn_SYSTEM02010_search();
					};
					fn_ajax_call(MENU_SAVE_MENU, JSON.stringify(param), searhFn, 'POST');
				}
			}
		});

		$('#ITP_BTN_SYSTEM02010_DELETE').on('click', function() {
			var deleteFn = {
    			callBack: function(args) {
    				let param = $('#ITP_FORM_SYSTEM02010_DETAIL').serialize();
    				var searhFn = function() {
						itp_fn_SYSTEM02010_search();
    				};
					fn_ajax_call(MENU_DEL_MENU, param, searhFn, 'GET');
    			}
    		};
			itp_fn_modal_confirm(ITP_MSG_LOCALE.message.form.deleteData, deleteFn, null);
		});
		
		$('#ITP_BTN_SYSTEM02010_DUP').on('click', function() {
			const formId = '#ITP_FORM_SYSTEM02010';
			itp_fn_form_clear_validate(null, formId + '_DETAIL');
			var vali_menuId = itp_fn_form_validate(formId + '_DETAIL', formId + '_MENU_ID', ['empty'], undefined);
			if (vali_menuId) {
				var dupFn = function(result) {
					//console.log(JSON.stringify(result)); return;
					if (result.code == 'N') {
						ITP_FORM_SYSTEM02010_IS_DUPLICATE = false;
					} else {
						ITP_FORM_SYSTEM02010_IS_DUPLICATE = true;
					}
				};
				const param = {'menuId':  $(formId + '_MENU_ID').val()};
				fn_ajax_call(MENU_CHECK_DUPLICATE, param, dupFn, 'GET');
			}
		});
	});
});

var itp_fn_SYSTEM02010_search = function() {
	var searhFn = {
		callBack: function(args) {
			var keyword = {keyword: $('#ITP_KEYWORD_SYSTEM02010_INPUT').val()};
			var param = $('#ITP_SYSTEM02010_ADV_SRH_FORM').serialize() + '&' + $.param(keyword);
			fn_ajax_call(MENU_MENU_SEARCH, param, itp_fn_SYSTEM02010_search_callback, 'GET');
		}
	};
	if (ITP_FORM_SYSTEM02010_IS_WRITING) {
		itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, searhFn, null);
	} else {
		searhFn.callBack();
	}
};

var itp_fn_SYSTEM02010_search_callback = function(result) {
	
	$('#ITP_SYSTEM02010_TREEVIEW').empty(); 
	
	$.each(result.searchList, function(index, item) {
		if(item.level == 1) { // 최상위메뉴
			$('#ITP_SYSTEM02010_TREEVIEW').append($('#ITP_SYSTEM02010_MENUROWCOPY').val());
			$('#ITP_SYSTEM02010_TREEVIEW li:last').append('<ul id="ITP_SYSTEM02010_LVL2_LIST"></ul>');
			
			$('#ITP_SYSTEM02010_TREEVIEW .list-group-item:first').attr('data-itp-param-key', item.menuNo);
			$('#ITP_SYSTEM02010_TREEVIEW .list-group-item:first').attr('data-level', item.level);
			$('#ITP_SYSTEM02010_TREEVIEW .list-group-item:first').find('.fnMenuNm').html(item.menuNm);
			$('#ITP_SYSTEM02010_TREEVIEW .list-group-item:first').find('.fnUseYnNm').html(item.useYnNm);
		} else if(item.level == 2) { 
			$('#ITP_SYSTEM02010_LVL2_LIST').append($('#ITP_SYSTEM02010_MENUROWCOPY').val());
			$('#ITP_SYSTEM02010_LVL2_LIST .list-group-item:last').attr('data-itp-param-key', item.menuNo);
			$('#ITP_SYSTEM02010_LVL2_LIST .list-group-item:last').attr('data-level', item.level);
			$('#ITP_SYSTEM02010_LVL2_LIST .list-group-item:last').find('.fnMenuNm').html(item.menuNm);
			$('#ITP_SYSTEM02010_LVL2_LIST .list-group-item:last').find('.fnUseYnNm').html(item.useYnNm);
			if(item.bmenuCnt > 0) {
				$('#ITP_SYSTEM02010_LVL2_LIST li:last').append('<ul class="fnLvl3List"></ul>');	
				$('#ITP_SYSTEM02010_LVL2_LIST .fnLvl3List:last').attr('data-menu-no', item.menuNo);	
			} 
		} else if(item.level == 3) {
			$('#ITP_SYSTEM02010_LVL2_LIST li').each(function() {
				if($(this).find('.fnLvl3List').data('menuNo') == item.topMenuNo) {
					$(this).find('.fnLvl3List').append($('#ITP_SYSTEM02010_MENUROWCOPY').val());
					$(this).find('.fnLvl3List .list-group-item:last').attr('data-itp-param-key', item.menuNo);
					$(this).find('.fnLvl3List .list-group-item:last').attr('data-level', item.level);
					$(this).find('.fnLvl3List .list-group-item:last').find('.fnMenuNm').html(item.menuNm);
					$(this).find('.fnLvl3List .list-group-item:last').find('.fnUseYnNm').html(item.useYnNm);
					
					if(item.bmenuCnt > 0) {
						$(this).find('.fnLvl3List li:last').append('<ul class="fnLvl4List"></ul>');
						$(this).find('.fnLvl3List .fnLvl4List:last').attr('data-menu-no', item.menuNo);	
					}
				}
			});
		} else if(item.level == 4) {
			$('#ITP_SYSTEM02010_LVL2_LIST li').each(function() {
				$(this).find('.fnLvl3List li').each(function() {
					if($(this).find('.fnLvl4List').data('menuNo') == item.topMenuNo) {
						$(this).find('.fnLvl4List').append($('#ITP_SYSTEM02010_MENUROWCOPY').val());
						$(this).find('.fnLvl4List .list-group-item:last').attr('data-itp-param-key', item.menuNo);
						$(this).find('.fnLvl4List .list-group-item:last').attr('data-level', item.level);
						$(this).find('.fnLvl4List .list-group-item:last').find('.fnMenuNm').html(item.menuNm);
						$(this).find('.fnLvl4List .list-group-item:last').find('.fnUseYnNm').html(item.useYnNm);
						
						if(item.bmenuCnt > 0) {
							$(this).find('.fnLvl4List li:last').append('<ul class="fnLvl5List"></ul>');
							$(this).find('.fnLvl4List .fnLvl5List:last').attr('data-menu-no', item.menuNo);	
						}	
					}	
				});
			});
		} else if(item.level == 5) {
			$('#ITP_SYSTEM02010_LVL2_LIST li').each(function() {
				$(this).find('.fnLvl3List li').each(function() {
					$(this).find('.fnLvl4List li').each(function() {						
						if($(this).find('.fnLvl5List').data('menuNo') == item.topMenuNo) {							
							$(this).find('.fnLvl5List').append($('#ITP_SYSTEM02010_MENUROWCOPY').val());
							$(this).find('.fnLvl5List .list-group-item:last').attr('data-itp-param-key', item.menuNo);
							$(this).find('.fnLvl5List .list-group-item:last').attr('data-level', item.level);
							$(this).find('.fnLvl5List .list-group-item:last').find('.fnMenuNm').html(item.menuNm);
							$(this).find('.fnLvl5List .list-group-item:last').find('.fnUseYnNm').html(item.useYnNm);	
						}						
					});						
				});
			});
				
		}
	});
	$('#ITP_SYSTEM02010_TREEVIEW').treed();
	
	const item = $('#ITP_SYSTEM02010_TREEVIEW').find('.list-group-item').first();
	const txt = item.attr('data-itp-param-key');
	item.addClass('active');
	itp_fn_SYSTEM02010_detail(txt, 1);
};

var itp_fn_SYSTEM02010_detail = function(key, level) {
	$('#ITP_FORM_SYSTEM02010_LEVEL').val(level); 
	
	var menuNo = {menuNo: key};
	var param = $.param(menuNo);						
	fn_ajax_call(MENU_INFO_MENU, param, itp_fn_SYSTEM02010_detail_callback, 'GET');	
};

var itp_fn_SYSTEM02010_detail_callback = function(result) {	
	
	// 정보표시
	itp_fn_form_clear_validate(null, '#ITP_FORM_SYSTEM02010_DETAIL');
	$('#ITP_FORM_SYSTEM02010_VIEW_CD').val('U');
	$('#ITP_FORM_SYSTEM02010_TOP_MENU_NO').val(result.topMenuNo);
	$('#ITP_FORM_SYSTEM02010_TOP_MENU_NM').val(result.topMenuNm);
	$('#ITP_FORM_SYSTEM02010_MENU_NO').val(result.menuNo);
	$('#ITP_FORM_SYSTEM02010_MENU_ID').val(result.menuId);
	$('#ITP_FORM_SYSTEM02010_USE_YN').val(result.useYn);
	$('#ITP_FORM_SYSTEM02010_MENU_NM_KR').val(result.menuNm);
	$('#ITP_FORM_SYSTEM02010_CONNECT_URL').val(result.connUrl);
	$('#ITP_FORM_SYSTEM02010_OPEN_CD').val(result.openCd);
	$('#ITP_FORM_SYSTEM02010_ORD_NO').val(result.viewNum);
	$('#ITP_FORM_SYSTEM02010_ICON_YN').val(result.iconYn);
	$('#ITP_FORM_SYSTEM02010_ROLE_DESC').val(result.roleDesc);
	$('#ITP_FORM_SYSTEM02010_MENU_ID').attr('readonly', true);
	$('#ITP_BTN_SYSTEM02010_DUP').attr('disabled', true);
	
	//LEVEL5 메뉴 아래로 하위메뉴를 생성할 수 없다.
	if($('#ITP_FORM_SYSTEM02010_LEVEL').val() == '5') {
		$('#ITP_BTN_SYSTEM02010_ADD').hide();
	} else {
		$('#ITP_BTN_SYSTEM02010_ADD').show();
	}
	
	ITP_FORM_SYSTEM02010_IS_WRITING = false;
	ITP_FORM_SYSTEM02010_IS_DUPLICATE = false;
	$('#ITP_FORM_SYSTEM02010_DETAIL').on('change', 'input, select, textarea', function() {
		if (!ITP_FORM_SYSTEM02010_IS_WRITING) {
			ITP_FORM_SYSTEM02010_IS_WRITING = true;
		}
		if ($(this).attr('id') == 'ITP_FORM_SYSTEM02010_MENU_ID') {
			ITP_FORM_SYSTEM02010_IS_DUPLICATE = true;
		}
	});
	
};