
let SYSTEM05010_SCH_NUM_NO; //
let ITP_FORM_SYSTEM05010_IS_WRITING = false;

let NUMB_NUMB_SEARCH = '/api/numb/numb-search';
let NUMB_SAVE_NUMB = '/api/numb/save-numb';
let NUMB_INFO_NUMB = '/api/numb/info-numb';

require(['config'], function() {
	require([
		'jquery'
	], function($) {
		// 버튼 권한설정
		fn_proc_btn_auth('SYSTEM05010');
		// 공통코드 표시
		fn_make_select(CODE_LIST, 'USE_YN', 'ITP_SYSTEM05010_ADV_SRH_FORM_USE_YN', true, '전체');
		fn_make_select(CODE_LIST, 'USE_YN', 'ITP_FORM_SYSTEM05010_USE_YN', true, '선택');
		
		itp_fn_SYSTEM05010_search();

		$('#ITP_BTN_SYSTEM05010_SRH').on('click', function() {
			itp_fn_SYSTEM05010_search();
		});

		$('#ITP_AJAX_SYSTEM05010_SRH_CONTAINER').on('click', '.list-group .list-group-item', function() {
			const $this = $(this);
			var itemFn = {
				callBack: function(args) {
					$('#ITP_AJAX_SYSTEM05010_SRH_CONTAINER .list-group .list-group-item').removeClass('active');
					$this.addClass('active');
					const txt = $this.find('.itp_param_key').text();
					itp_fn_SYSTEM05010_detail(txt);
				}
			};
			if (ITP_FORM_SYSTEM05010_IS_WRITING) {
				itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, itemFn, $this);
			} else {
				itemFn.callBack($this);
			}
		});

		$('#ITP_BTN_SYSTEM05010_NEW').on('click', function() {
			var newFn = {
    			callBack: function(args) {
					itp_fn_form_clear_validate(null, '#ITP_FORM_SYSTEM05010_DETAIL');
    				const item = $('#ITP_AJAX_SYSTEM05010_SRH_CONTAINER .list-group .list-group-item');
    				item.removeClass('active');

					// 정보 초기화					
					$('#ITP_FORM_SYSTEM05010_DETAIL input[name=viewCd]').val('C');
					$('#ITP_FORM_SYSTEM05010_NUM_NO').val('');
					$('#ITP_FORM_SYSTEM05010_USE_YN').val('');
					$('#ITP_FORM_SYSTEM05010_DEFI_INFO').val('');
					$('#ITP_FORM_SYSTEM05010_NUM_NM').val('');
					$('#ITP_FORM_SYSTEM05010_NUM_ST_NO').val('');
					$('#ITP_FORM_SYSTEM05010_NUM_ED_NO').val('');
					$('#ITP_FORM_SYSTEM05010_NUM_USE_NO').val('');
					$('#ITP_FORM_SYSTEM05010_NUM_CU_NO').val('');
					$('#ITP_FORM_SYSTEM05010_YEAR_CHK_YN').attr('checked', false); // 
					$('#ITP_FORM_SYSTEM05010_MONTH_CHK_YN').attr('checked', false); // 
					$('#ITP_FORM_SYSTEM05010_DAY_CHK_YN').attr('checked', false); // 
					$('#ITP_FORM_SYSTEM05010_YEAR_CHK').val('N'); // 
					$('#ITP_FORM_SYSTEM05010_MONTH_CHK').val('N'); // 
					$('#ITP_FORM_SYSTEM05010_DAY_CHK').val('N'); //	
    			}
    		};
			if (ITP_FORM_SYSTEM05010_IS_WRITING) {
				itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, newFn, null);
			} else {
				newFn.callBack();
			}
		});

		$('#ITP_BTN_SYSTEM05010_SAVE').on('click', function() {
			const formId = '#ITP_FORM_SYSTEM05010';
			itp_fn_form_clear_validate(null, formId + '_DETAIL');
			var vali_useYn = itp_fn_form_validate(formId + '_DETAIL', formId + '_USE_YN', ['empty'], undefined);
			var vali_numNm = itp_fn_form_validate(formId + '_DETAIL', formId + '_NUM_NM', ['empty'], undefined);
			var vali_numStNo = itp_fn_form_validate(formId + '_DETAIL', formId + '_NUM_ST_NO', ['empty', 'number'], undefined);
			var vali_numEdNo = itp_fn_form_validate(formId + '_DETAIL', formId + '_NUM_ED_NO', ['empty', 'number'], undefined);

			if (vali_useYn && vali_numNm && vali_numStNo && vali_numEdNo) {
				let param = $(formId + '_DETAIL').serializeObject();
				//console.log(JSON.stringify(param)); return;
				var searhFn = function() {
					var keyword = {keyword: $('#ITP_KEYWORD_SYSTEM05010_INPUT').val()};
					var param = $('#ITP_SYSTEM05010_ADV_SRH_FORM').serialize() + '&' + $.param(keyword);
					fn_ajax_call(NUMB_NUMB_SEARCH, param, itp_fn_SYSTEM05010_search_callback, 'GET');
				};
				if($('#ITP_FORM_SYSTEM05010_NUM_NO').val() != '') {
					SYSTEM05010_SCH_NUM_NO = $('#ITP_FORM_SYSTEM05010_NUM_NO').val();
				}
				fn_ajax_call(NUMB_SAVE_NUMB, JSON.stringify(param), searhFn, 'POST');
			}
		});
	});
});

var itp_fn_SYSTEM05010_search = function() {
	var searhFn = {
		callBack: function(args) {
			var sNumNm = {sNumNm: $('#ITP_KEYWORD_SYSTEM05010_INPUT').val()};
			var param = $('#ITP_SYSTEM05010_ADV_SRH_FORM').serialize() + '&' + $.param(sNumNm);
			fn_ajax_call(NUMB_NUMB_SEARCH, param, itp_fn_SYSTEM05010_search_callback, 'GET');
		}
	};
	if (ITP_FORM_SYSTEM05010_IS_WRITING) {
		itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, searhFn, null);
	} else {
		searhFn.callBack();
	}
};

var itp_fn_SYSTEM05010_search_callback = function(result) {
	
	$('#ITP_AJAX_SYSTEM05010_SRH_CONTAINER > .list-group').empty(); // 초기화
	
	$.each(result.searchList, function(index, item) {
		$('#ITP_AJAX_SYSTEM05010_SRH_CONTAINER > .list-group').append($('#ITP_AJAX_SYSTEM05010_SRH_CONTAINER_ROWCOPY').val());
		$('#ITP_AJAX_SYSTEM05010_SRH_CONTAINER > .list-group > .list-group-item:last').find('.fnNumNo').text(item.numNo);
		$('#ITP_AJAX_SYSTEM05010_SRH_CONTAINER > .list-group > .list-group-item:last').find('.fnNumNm').text(item.numNm);
		$('#ITP_AJAX_SYSTEM05010_SRH_CONTAINER > .list-group > .list-group-item:last').find('.fnUseYnNm').text(item.useYnNm);
		$('#ITP_AJAX_SYSTEM05010_SRH_CONTAINER > .list-group > .list-group-item:last').find('.fnDefiInfo').text(item.defiInfo);
		if(SYSTEM05010_SCH_NUM_NO && SYSTEM05010_SCH_NUM_NO == item.numNo) {
			$('#ITP_AJAX_SYSTEM05010_SRH_CONTAINER > .list-group > .list-group-item:last').addClass('active');	
		}		
	});
	
	// 첫번째 데이터 상세조회
	if($('#ITP_AJAX_SYSTEM05010_SRH_CONTAINER .list-group .list-group-item').length > 0) {
		if(SYSTEM05010_SCH_NUM_NO && SYSTEM05010_SCH_NUM_NO !== undefined) {
			itp_fn_SYSTEM05010_detail(SYSTEM05010_SCH_NUM_NO);	
		} else {
			const item = $('#ITP_AJAX_SYSTEM05010_SRH_CONTAINER .list-group .list-group-item').first();
			const numNo = item.find('.itp_param_key').text();
			item.addClass('active');
			itp_fn_SYSTEM05010_detail(numNo);	
		}	
	}
};

var itp_fn_SYSTEM05010_detail = function(key) {
	var numNo  = {numNo: key};
	var param = $.param(numNo);						
	fn_ajax_call(NUMB_INFO_NUMB, param, itp_fn_SYSTEM05010_detail_callback, 'GET');	
};

var itp_fn_SYSTEM05010_detail_callback = function(result) {
	ITP_FORM_SYSTEM05010_IS_WRITING = false;
	
	// 상세정보 지정
	$('#ITP_FORM_SYSTEM05010_DETAIL input[name=viewCd]').val('U');
	$('#ITP_FORM_SYSTEM05010_NUM_NO').val(result.numNo);
	$('#ITP_FORM_SYSTEM05010_USE_YN').val(result.useYn);
	$('#ITP_FORM_SYSTEM05010_DEFI_INFO').val(result.defiInfo);
	$('#ITP_FORM_SYSTEM05010_NUM_NM').val(result.numNm);
	$('#ITP_FORM_SYSTEM05010_NUM_ST_NO').val(result.numStNo);
	$('#ITP_FORM_SYSTEM05010_NUM_ED_NO').val(result.numEdNo);
	$('#ITP_FORM_SYSTEM05010_NUM_USE_NO').val(result.numUseNo);
	$('#ITP_FORM_SYSTEM05010_NUM_CU_NO').val(result.numCuNo);
	$('#ITP_FORM_SYSTEM05010_YEAR_CHK_YN').attr('checked', result.yearChk == 'Y' ? true : false); // 
	$('#ITP_FORM_SYSTEM05010_MONTH_CHK_YN').attr('checked', result.monthChk == 'Y' ? true : false); // 
	$('#ITP_FORM_SYSTEM05010_DAY_CHK_YN').attr('checked', result.dayChk == 'Y' ? true : false); // 
	$('#ITP_FORM_SYSTEM05010_YEAR_CHK').val(result.yearChk); // 
	$('#ITP_FORM_SYSTEM05010_MONTH_CHK').val(result.monthChk); // 
	$('#ITP_FORM_SYSTEM05010_DAY_CHK').val(result.dayChk); // 

	$('#ITP_FORM_SYSTEM05010_DETAIL').on('change', 'input, select, textarea', function() {
		if (!ITP_FORM_SYSTEM05010_IS_WRITING) {
			ITP_FORM_SYSTEM05010_IS_WRITING = true;
		}
		if ($(this).attr('id') == 'ITP_FORM_SYSTEM05010_YEAR_CHK_YN') {
			if ($(this).is(':checked') == true) {
				$('#ITP_FORM_SYSTEM05010_YEAR_CHK').val('Y');
			} else {
				$('#ITP_FORM_SYSTEM05010_YEAR_CHK').val('N');
			}
		} else if ($(this).attr('id') == 'ITP_FORM_SYSTEM05010_MONTH_CHK_YN') {
			if ($(this).is(':checked') == true) {
				$('#ITP_FORM_SYSTEM05010_MONTH_CHK').val('Y');
			} else {
				$('#ITP_FORM_SYSTEM05010_MONTH_CHK').val('N');
			}
		} else if ($(this).attr('id') == 'ITP_FORM_SYSTEM05010_DAY_CHK_YN') {
			if ($(this).is(':checked') == true) {
				$('#ITP_FORM_SYSTEM05010_DAY_CHK').val('Y');
			} else {
				$('#ITP_FORM_SYSTEM05010_DAY_CHK').val('N');
			}
		}
	});
	
};