
const KOPER05010_GRID_ID = '#ITP_KOPER05010_jqGrid';
const KOPER05010_GRID_LIST = '#ITP_KOPER05010_jqGrid_list';
const KOPER05010_GRID_PAGER = '#ITP_KOPER05010_jqGridPager';
const KOPER05010_GRID_EMPTY = '#ITP_KOPER05010_jqGridEmpty';

let ITP_FORM_KOPER05010_DETAIL_IS_DUPLICATE = false;
let ITP_FORM_KOPER05010_DETAIL_IS_WRITING = false;

let VAN_GRID_LIST = '/api/vanInfo/grid-list';
let VAN_INFO = '/api/vanInfo/info-vaninfo';
let VAN_SAVE = '/api/vanInfo/save-vaninfo';
let VAN_CHECK_DUPLICATE = '/api/vanInfo/check-duplicate';

require(['config'], function() {
	require([
		'jquery'
	], function($) {
		// 버튼 권한설정
		fn_proc_btn_auth('KOPER05010');
		// 공통코드 표시
		//fn_make_select(CODE_LIST, 'USE_STAT_CD', 'ITP_FORM_KOPER05010_SEARCH_USE_STAT_CD');
		//fn_make_select(CODE_LIST, 'USE_STAT_CD', 'ITP_FORM_KOPER05010_DETAIL_USE_STAT_CD', true, '선택');
		//fn_make_select(CODE_LIST, 'SALES_TYPE_CD', 'ITP_FORM_KOPER05010_DETAIL_SALES_TYPE_CD', true, '선택');
		
		// 조회일자 지정
		// var now = new Date();
		//$('#ITP_TAB_KOPER05010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
		// $('#ITP_FORM_KOPER05010_SEARCH_FROM_DT').datepicker('setDate', new Date(now.setMonth(now.getMonth() - 1)));
		// $('#ITP_FORM_KOPER05010_SEARCH_TO_DT').datepicker('setDate', new Date());
				
		// 그리드				
		itp_fn_jqgrid_resize(KOPER05010_GRID_ID, KOPER05010_GRID_LIST, 'lg');
		itp_fn_KOPER05010_grid.init();
    	itp_fn_fire_window_resize();

		// 조회    	
    	itp_fn_KOPER05010_search();
    	
		// 조회 버튼 클릭
		$('#ITP_BTN_KOPER05010_SRH').on('click', function() {
			itp_KOPER05010_search = true;
			let param = $('#ITP_FORM_KOPER05010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.gridPage = $.jgrid.defaults.page;
			$(KOPER05010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
		});
		
		// 수정 버튼
		$('#ITP_BTN_KOPER05010_MODIFY').on('click', function() {
			itp_fn_form_clear_validate(null, '#ITP_FORM_KOPER05010_DETAIL');			
			const vanCdVal = $('#ITP_FORM_KOPER05010_VIEW_VAN_CD').val();
			const key = {vanCd: vanCdVal, viewCd: 'R'};
			var param = $.param(key);
			itp_fn_KOPER05010_detail(param);	
		});
		
		// 신규등록 버튼
		$('#ITP_BTN_KOPER05010_NEWREG').on('click', function() {
			itp_fn_KOPER05010_switch_screen('ADD');			
			itp_fn_form_clear_validate(null, '#ITP_FORM_KOPER05010_DETAIL');		
		});
		
		// 취소/목록 버튼
		$('#ITP_BTN_KOPER05010_CANCELLIST').on('click', function() {
			itp_fn_KOPER05010_search();
    	});
		
		// 삭제 버튼
		$('#ITP_BTN_KOPER05010_DELETE').on('click', function() {
			
		});
		
		// 저장 버튼
		$('#ITP_BTN_KOPER05010_SAVE').on('click', function() {			
			const formId = '#ITP_FORM_KOPER05010_DETAIL';
			itp_fn_form_clear_validate(null, formId);			
			var vali_vanCd = itp_fn_form_validate(formId, formId + '_VAN_CD', ['empty'], undefined);
			var vali_vanNm = itp_fn_form_validate(formId, formId + '_VAN_NM', ['empty'], undefined);
			var vali_vanIp = itp_fn_form_validate(formId, formId + '_VAN_IP', ['empty'], undefined);
			var vali_vanPort = itp_fn_form_validate(formId, formId + '_VAN_PORT', ['empty'], undefined);		
			
			if (vali_vanCd && vali_vanNm && vali_vanIp && vali_vanPort) {
				
				if (ITP_FORM_KOPER05010_DETAIL_IS_DUPLICATE) {
					itp_fn_modal_duplicate('VAN코드 중복체크를 해주세요.', formId + '_VAN_CD');
				} else {
				
					let param = $(formId).serializeObject();
					var searhFn = function() {
						ITP_FORM_KOPER05010_DETAIL_IS_WRITING = false;
						itp_fn_KOPER05010_search();
					};
					console.log(JSON.stringify(param));
					fn_ajax_call(VAN_SAVE, JSON.stringify(param), searhFn, 'POST');	
				}
			}
		});
		
		// 중복체크
		$('#ITP_FORM_KOPER05010_DETAIL_DUP').on('click', function() {
			const formId = '#ITP_FORM_KOPER05010_DETAIL';
			itp_fn_form_clear_validate(null, formId + '_DETAIL');
			var vali_vanCd = itp_fn_form_validate(formId + '_DETAIL', formId + '_VAN_CD', ['empty'], undefined);
			if (vali_vanCd) {
				var dupFn = function(result) {
					if (result.code == 'N') {
						ITP_FORM_KOPER05010_DETAIL_IS_DUPLICATE = false;
					} else {
						ITP_FORM_KOPER05010_DETAIL_IS_DUPLICATE = true;
					}
				};
				const key = {'vanCd':  $(formId + '_VAN_CD').val()};
				const param = $.param(key);
				fn_ajax_call(VAN_CHECK_DUPLICATE, param, dupFn, 'GET');
			}
		});
		
	});
});


var itp_fn_KOPER05010_switch_screen = function(mode) {
	if(mode == 'LIST') { // 목록
		$('#ITP_BTN_KOPER05010_MODIFY').hide();
		$('#ITP_BTN_KOPER05010_CANCELLIST').hide();
		$('#ITP_BTN_KOPER05010_DELETE').hide();
		$('#ITP_BTN_KOPER05010_SAVE').hide();
		fn_show_btn_auth('#ITP_BTN_KOPER05010_SRH');
		fn_show_btn_auth('#ITP_BTN_KOPER05010_NEWREG');
		
		$('#ITP_AJAX_KOPER05010_LIST_CONTAINER').show();
		$('#ITP_AJAX_KOPER05010_DETAIL_CONTAINER').hide();		
		$('#ITP_AJAX_KOPER05010_VIEW_CONTAINER').hide();
	} else if(mode == 'ADD') { // 등록
		ITP_FORM_KOPER05010_DETAIL_IS_DUPLICATE = true;
		$('#ITP_BTN_KOPER05010_SRH').hide();
		$('#ITP_BTN_KOPER05010_MODIFY').hide();
		$('#ITP_BTN_KOPER05010_NEWREG').hide();
		$('#ITP_BTN_KOPER05010_DELETE').hide();
		fn_show_btn_auth('#ITP_BTN_KOPER05010_CANCELLIST');
		fn_show_btn_auth('#ITP_BTN_KOPER05010_SAVE');
		
		$('#ITP_AJAX_KOPER05010_LIST_CONTAINER').hide();
		$('#ITP_AJAX_KOPER05010_DETAIL_CONTAINER').show();
		$('#ITP_AJAX_KOPER05010_VIEW_CONTAINER').hide();		
		$('#ITP_FORM_KOPER05010_DETAIL_VAN_CD').removeAttr('readonly');
		$('#ITP_FORM_KOPER05010_DETAIL_DUP').removeAttr('disabled');
		
		$('#ITP_FORM_KOPER05010_DETAIL_VIEW_CD').val('C');
		$('#ITP_FORM_KOPER05010_DETAIL_VAN_CD').val('');
		$('#ITP_FORM_KOPER05010_DETAIL_VAN_NM').val('');
		$('#ITP_FORM_KOPER05010_DETAIL_VAN_IP').val('');
		$('#ITP_FORM_KOPER05010_DETAIL_VAN_PORT').val('');
		
	} else if(mode == 'MODIFY') { // 수정
		$('#ITP_BTN_KOPER05010_SRH').hide();
		$('#ITP_BTN_KOPER05010_MODIFY').hide();
		$('#ITP_BTN_KOPER05010_NEWREG').hide();		
		$('#ITP_BTN_KOPER05010_DELETE').hide();
		fn_show_btn_auth('#ITP_BTN_KOPER05010_CANCELLIST');
		fn_show_btn_auth('#ITP_BTN_KOPER05010_SAVE');
		
		$('#ITP_AJAX_KOPER05010_LIST_CONTAINER').hide();
		$('#ITP_AJAX_KOPER05010_DETAIL_CONTAINER').show();
		$('#ITP_AJAX_KOPER05010_VIEW_CONTAINER').hide();
		$('#ITP_FORM_KOPER05010_DETAIL_VAN_CD').attr('readonly', true);
		$('#ITP_FORM_KOPER05010_DETAIL_DUP').attr('disabled', true);
	} else if(mode == 'VIEW') { // 보기
		$('#ITP_BTN_KOPER05010_SRH').hide();
		$('#ITP_BTN_KOPER05010_NEWREG').hide();		
		$('#ITP_BTN_KOPER05010_DELETE').hide();
		$('#ITP_BTN_KOPER05010_SAVE').hide();
		fn_show_btn_auth('#ITP_BTN_KOPER05010_MODIFY');
		fn_show_btn_auth('#ITP_BTN_KOPER05010_CANCELLIST');
		
		$('#ITP_AJAX_KOPER05010_LIST_CONTAINER').hide();
		$('#ITP_AJAX_KOPER05010_DETAIL_CONTAINER').hide();
		$('#ITP_AJAX_KOPER05010_VIEW_CONTAINER').show();
	}	
};

var itp_fn_KOPER05010_grid = {
	init: function() {
		// 데이터 없을때
		itp_fn_KOPER05010_empty.push();
		$(KOPER05010_GRID_EMPTY).on('click', function() {
			itp_fn_KOPER05010_empty.back();
			itp_KOPER05010_param.gridSize = $.jgrid.defaults.rowNum;
			$(KOPER05010_GRID_ID).setGridParam({'postData': JSON.stringify(itp_KOPER05010_param)}).trigger('reloadGrid');
		});	
	},
	callBack: function() {
		let param = $('#ITP_FORM_KOPER05010_SEARCH').serializeObject();
		param.gridSize = $.jgrid.defaults.rowNum;	
		$(KOPER05010_GRID_ID).jqGrid({
			colModel: [
				{
					index: 'VAN_CD', name: 'vanCd',
					label: 'VAN코드', 
					width: '6', fixed: false, align: 'center',
					sortable: false, hidden: false
				},
				{
					index: 'VAN_NM', name: 'vanNm',
					label: 'VAN명', 
					width: '10', fixed: false, align: 'center',
					sortable: false, hidden: false
				},
				{
					index: 'VAN_IP', name: 'vanIp',
					label: 'IP', 
					width: '6', fixed: false, align: 'center',
					sortable: false, hidden: false
				},
				{
					index: 'VAN_PORT', name: 'vanPort',
					label: 'PORT', 
					width: '6', fixed: false, align: 'center',
					sortable: false, hidden: false
				},
				{
					index: 'ADD_DT', name: 'addDt',
					label: '등록일자', 
					width: '8', fixed: false, align: 'center',
					sortable: false, hidden: false
				}
			],
			loadBeforeSend: function(jqXHR) {
			    jqXHR.setRequestHeader('X-AUTH-TOKEN', CONN_KEY);
			},
			postData: JSON.stringify(param),
			url: DOMAIN + VAN_GRID_LIST,
			pager: KOPER05010_GRID_PAGER,
			multiselect: false,
			rownumbers: true, 
			onCellSelect: function(rowId, cellIdx, cellValue) {
				var cm = $(this).jqGrid('getGridParam', 'colModel');
				var colNm = cm[cellIdx].name;
				if (colNm == 'vanCd') {
					const vanCdVal = $(this).jqGrid('getCell', rowId, 'vanCd');
					const key = {vanCd: vanCdVal, viewCd: 'R'};
					var param = $.param(key);
					itp_fn_KOPER05010_view(param);
				}
		   	},
			loadComplete: function(data) {
				console.log(JSON.stringify(data));
				itp_fn_grid_load_complete(data, KOPER05010_GRID_ID, true, 'number', 'KOPER05010', itp_KOPER05010_search, itp_fn_KOPER05010_empty, true, data.gridRecords, true);
				var ids = $(KOPER05010_GRID_ID).getDataIDs();
				$.each(ids, function(idx, rowId) {
					$(KOPER05010_GRID_ID).jqGrid('setCell', rowId, 'vanCd', '', ITP_GRID_COL_STYLE.link);
				});	
		    },
		    loadError: function(jqXHR, textStatus, errorThrown) {
		    	itp_fn_grid_load_error(jqXHR, textStatus, errorThrown);
		    },
		    onPaging: function(action) {
		    	itp_fn_grid_paging(KOPER05010_GRID_ID, action, param);
		    }
		}).navGrid(KOPER05010_GRID_PAGER, ITP_GRID_NAV_DEFAULTS.navGrid);
	},
	clearData : function() {
		$(KOPER05010_GRID_ID).jqGrid('clearGridData', true);
		$(KOPER05010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
		$(KOPER05010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
		$(KOPER05010_GRID_EMPTY).show();
	}, 
	unload : function() {
		$.jgrid.gridUnload(KOPER05010_GRID_ID); 
	}
};

var itp_fn_KOPER05010_search = function() {
	itp_fn_KOPER05010_switch_screen('LIST');	
	itp_fn_KOPER05010_grid.unload();
	itp_fn_KOPER05010_grid.callBack();
};

let itp_KOPER05010_param = {};
let itp_KOPER05010_search = false;
var itp_fn_KOPER05010_empty = {
	push: function() {
		let param = $('#ITP_FORM_KOPER05010_SEARCH').serializeObject();
		itp_KOPER05010_param = param;
	}
};

var itp_fn_KOPER05010_view = function(param) {
	fn_ajax_call(VAN_INFO, param, itp_fn_KOPER05010_view_callback, 'GET');
};

var itp_fn_KOPER05010_view_callback = function(result) {
	// console.log(result);
	itp_fn_KOPER05010_switch_screen('VIEW');
	$('#ITP_FORM_KOPER05010_VIEW #ITP_FORM_KOPER05010_VIEW_VAN_CD').val(result.vanCd);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanCd').text(result.vanCd);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanNm').text(result.vanNm);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanIp').text(result.vanIp);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanPort').text(result.vanPort);
	$('#ITP_FORM_KOPER05010_VIEW .fnAddDt').text(result.addDt);
	$('#ITP_FORM_KOPER05010_VIEW .fnChgDt').text(result.chgDt);
	
	$('#ITP_FORM_KOPER05010_VIEW .fnVanData1').text(result.vanData1);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanData2').text(result.vanData2);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanData3').text(result.vanData3);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanData4').text(result.vanData4);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanData5').text(result.vanData5);
	
};

var itp_fn_KOPER05010_detail = function(param) {
	fn_ajax_call(VAN_INFO, param, itp_fn_KOPER05010_detail_callback, 'GET');
};

var itp_fn_KOPER05010_detail_callback = function(result) {
	 console.log(result);
	itp_fn_KOPER05010_switch_screen('MODIFY');
	$('#ITP_FORM_KOPER05010_DETAIL #ITP_FORM_KOPER05010_DETAIL_VIEW_CD').val('U');
	
	//$('#ITP_FORM_KOPER05010_VIEW #ITP_FORM_KOPER05010_VIEW_VAN_CD').val(result.vanCd);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanCd').text(result.vanCd);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanNm').text(result.vanNm);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanIp').text(result.vanIp);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanPort').text(result.vanPort);
	$('#ITP_FORM_KOPER05010_VIEW .fnAddDt').text(result.addDt);
	$('#ITP_FORM_KOPER05010_VIEW .fnChgDt').text(result.chgDt);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanData1').text(result.vanData1);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanData2').text(result.vanData2);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanData3').text(result.vanData3);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanData4').text(result.vanData4);
	$('#ITP_FORM_KOPER05010_VIEW .fnVanData5').text(result.vanData5);
	
	
	
	$('#ITP_FORM_KOPER05010_DETAIL #ITP_FORM_KOPER05010_DETAIL_VAN_CD').val(result.vanCd);
	$('#ITP_FORM_KOPER05010_DETAIL #ITP_FORM_KOPER05010_DETAIL_VAN_NM').val(result.vanNm);
	$('#ITP_FORM_KOPER05010_DETAIL #ITP_FORM_KOPER05010_DETAIL_VAN_IP').val(result.vanIp);
	$('#ITP_FORM_KOPER05010_DETAIL #ITP_FORM_KOPER05010_DETAIL_VAN_PORT').val(result.vanPort);	
	$('#ITP_FORM_KOPER05010_DETAIL .fnAddDt').text(result.addDt);
	$('#ITP_FORM_KOPER05010_DETAIL .fnChgDt').text(result.chgDt);
	
	
	$('#ITP_FORM_KOPER05010_DETAIL #ITP_FORM_KOPER05010_DETAIL_VAN_DATA1').val(result.vanData1);
	$('#ITP_FORM_KOPER05010_DETAIL #ITP_FORM_KOPER05010_DETAIL_VAN_DATA2').val(result.vanData2);
	$('#ITP_FORM_KOPER05010_DETAIL #ITP_FORM_KOPER05010_DETAIL_VAN_DATA3').val(result.vanData3);
	$('#ITP_FORM_KOPER05010_DETAIL #ITP_FORM_KOPER05010_DETAIL_VAN_DATA4').val(result.vanData4);
	$('#ITP_FORM_KOPER05010_DETAIL #ITP_FORM_KOPER05010_DETAIL_VAN_DATA5').val(result.vanData5);
	
	
	
};
