require(['config'], function() {
	require([
		'jquery.bootstrap.fileUpload' //파일 업로드가 있는 페이지에서 로딩
	], function($) {
		pageObj.init();
	});
});

/********************************************************
 --------------------------------------------------------
 - Object 목록								            -
 --------------------------------------------------------
 * const gridColModel = {}	: 화면 Grid Object			*
 * let pageObj = {}			: 화면공통 Object			*
 * let listObj = {}			: 목록화면 Object			*
 * let viewObj = {}			: 상세화면 Object			*
 * let modifyObj = {}		: 수정화면 Object			*
 * let createObj = {}		: 신규화면 Object			*
 *******************************************************/

/*화면 변수*/
const STINFO05010_GRID_ID = '#ITP_STINFO05010_jqGrid';
const STINFO05010_GRID_LIST = '#ITP_STINFO05010_jqGrid_list';
const STINFO05010_GRID_PAGER = '#ITP_STINFO05010_jqGridPager';
const STINFO05010_GRID_EMPTY = '#ITP_STINFO05010_jqGridEmpty';

let ITP_FORM_STINFO05010_DETAIL_IS_DUPLICATE = false;
let ITP_FORM_STINFO05010_DETAIL_IS_WRITING = false;

/*API URL*/
let API_DETAIL_GRID_LIST 	= '/api/itemunit/detail-grid-list';	    // 목록
let API_DETAIL_INFO 		= '/api/itemunit/info-itemunit';		// 상세
let API_DETAIL_SAVE 		= '/api/itemunit/save-itemunit';		// 저장
let API_ITEM_CLASS_LEVEL	= '/api/item/class-level-list';			//품목분류 level 검색

/*화면 Grid ColModel*/
const gridColModel = {
	list: [
		{
			index: 'BRAND_ID', name: 'brandId',
			label: ITP_MSG_LOCALE.label.brandId, //브랜드ID
			width: '0', fixed: false, align: 'center',
			sortable: false, hidden: true ,frozen:true
		},
		{
			index: 'BRAND_UNIT_UNQ_NO', name: 'brandUnitUnqNo',
			label: ITP_MSG_LOCALE.label.brandUnitUnqNo, //브랜드ID
			width: '0', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'ITEM_ID', name: 'itemId',
			label: ITP_MSG_LOCALE.label.itemId, //품목번호
			width: '13', fixed: false, align: 'center',
			sortable: false, hidden: false ,frozen:true
		},
		{
			index: 'ITEM_NM', name: 'itemNm',
			label: ITP_MSG_LOCALE.label.itemNm, //품목명
			width: '9', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'SPPLY_ID_NM', name: 'spplyNm',
			label: ITP_MSG_LOCALE.label.spplyNm, //공급업체명
			width: '9', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'UNIT', name: 'unit',
			label: ITP_MSG_LOCALE.label.unit, //기본단위
			width: '5', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'CURR', name: 'curr',
			label: ITP_MSG_LOCALE.label.curr, //통화
			width: '3', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'UNIT_AMT', name: 'unitAmt',
			label: ITP_MSG_LOCALE.label.unitAmt, //단가
			width: '6', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: {thousandsSeparator: ','}
		},
		{
			index: 'CONT_STT_DT', name: 'contSttDt',
			label: ITP_MSG_LOCALE.label.contSttDt, //시작일
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'CONT_END_DT', name: 'contEndDt',
			label: ITP_MSG_LOCALE.label.contEndDt, //종료일
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'LEAD_TIME', name: 'leadTime',
			label: '리드타임', //리드타임 (Day)
			width: '6', fixed: false, align: 'right',
			sortable: false, hidden: false
		},
		{
			index: 'FILE_PATH', name: 'filePath',
			label: ITP_MSG_LOCALE.label.itemFilePath, //단가계약파일
			width: '9', fixed: false, align: 'center',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'UNIT_ST_NM', name: 'unitStNm',
			label: ITP_MSG_LOCALE.label.stNm, //상태
			width: '3', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'SPPLY_ITEM_ID', name: 'spplyItemId',
			label: ITP_MSG_LOCALE.label.spplyItemId, //공급사품번
			width: '13', fixed: false, align: 'center',
			sortable: false, hidden: false
		}
	]
};

/*화면공통 Object*/
let pageObj = {
	init: function () {
		this.ui.init();
		this.event.init();
	},
	ui: {
		init: function () {
			this.view();
			this.form();
			this.grid();
			this.ready();
		},
		view: function() {
			// 버튼 권한설정
			fn_proc_btn_auth('STINFO05010');

			// 공통코드 표시
			$('select').each(function() {
				if($(this).data('select-code')) {
					fn_make_select(CODE_LIST, $(this).data('select-code'), $(this).attr('id'));
				}
			});

			itemClassLevel.list(1);

			// 조회일자 지정
			var now = new Date();
			var defaultToDate = new Date(now);
			defaultToDate.setDate(now.getDate() - 30);
			$('#ITP_TAB_STINFO05010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_STINFO05010_SEARCH_FROM_DT').datepicker('setDate', defaultToDate);
			$('#ITP_FORM_STINFO05010_SEARCH_TO_DT').datepicker('setDate', now);

			// form alert message 주입
			$('input[data-alert-required="ONLYCHK"]').each(function() {
				if($(this).data('alert-msg') === 'empty') {
					$(this).data('alert-msg', ITP_MSG_LOCALE.message.form.needAll);
				}
			});
		},
		form: function() {
			itp_fn_form_event.onKeyup('#ITP_FORM_STINFO05010_DETAIL');
		},
		grid: function() {
			itp_fn_jqgrid_resize(STINFO05010_GRID_ID, STINFO05010_GRID_LIST, 'sm');
			//listObj.empty.init();
			
		},
		ready: function() {
			listObj.init();
		}
	},
	event: {
		init: function () {
			this.button();
		},
		button: function () {
			// 버튼 클릭 이벤트
			$('button').each(function() {
				var id = $(this).attr('id');
				$(this).on('click', function() {
					switch (id) {
						case 'ITP_BTN_STINFO05010_SRH'				: listObj.button.search();		break;	// 조회 버튼 클릭
						case 'ITP_BTN_STINFO05010_NEWREG'			: listObj.button.create(); 		break;	// 신규등록 버튼
						case 'ITP_BTN_STINFO05010_MODIFY'			: modifyObj.button.modify();	break;	// 수정 버튼
						case 'ITP_BTN_STINFO05010_CANCELLIST'		: createObj.button.cancel(); 	break;	// 취소/목록 버튼
						case 'ITP_BTN_STINFO05010_DELETE'			: 								break;	// 삭제 버튼
						case 'ITP_BTN_STINFO05010_SAVE'				: createObj.button.save(); 		break;	// 저장 버튼
						case 'ITP_FORM_STINFO05010_DETAIL_DUP'		: createObj.button.duplicate();	break;	// 중복체크
						case 'ITP_BTN_STINFO05010_RESET_PW'			: createObj.button.resetPass();	break;	// 상세:비밀번호 초기화
						case 'ITP_BTN_STINFO05010_EXCEL'			: listObj.excelDown(); 			break;	// 엑셀 다운로드 버튼
					}
				});
			});

			$('button[id^="ITP_BTN_STINFO05010_SEARCH"]').on('click', function() {
				popSearch.init($(this).attr('id'), 'SPPLY');
			});
			$('button[id^="ITP_BTN_STINFO05010_DETAIL"]').on('click', function() {
				var id = $(this).attr('id');
				popSearch.init(id, id.indexOf('ITEM') > -1 ? 'ITEM' : 'SPPLY');
			});
		}
	},
	fileUpload: function() {
		//파일업로드(등록/수정화면)
		var id = 'STINFO05010';
		$('#ITP_' + id + '_DETAIL_UPLOAD').empty();
		const fileArgs = {
			'fileNo': $('#ITP_FORM_' + id + '_DETAIL_FILE_NO').val(),
			'viewCd': $('#ITP_FORM_' + id + '_DETAIL_VIEW_CD').val(),
			'showYn': $('#ITP_' + id + '_DETAIL_UPLOAD').attr('data-show-yn'),
			'fileId': '#ITP_FORM_' + id + '_DETAIL_FILE_NO' //파일번호를 서버에서 받아서 넣을 파일번호 인풋박스 아이디
		};
		itp_fn_set_file_upload('ITP_' + id + '_DETAIL_UPLOAD', 'notice', true, false, id, fileArgs);
	},
	switchScreen: function(mode) {
		$('.itp_det_head').find('button[id^="ITP_BTN_STINFO05010_"]').each(function(i) {
			$(this).hide();
		});
		$('#ITP_TAB_STINFO05010').find('div[id$="_CONTAINER"]').each(function(i) {
			$(this).hide();
		});
		if(mode == 'LIST') { // 목록
			itp_fn_fire_window_resize();
			$('#ITP_AJAX_STINFO05010_LIST_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_STINFO05010_SRH', '#ITP_BTN_STINFO05010_NEWREG']);
			$('#ITP_FORM_STINFO05010_SEARCH_BRAND_ID').val(fn_make_user_info.get('brandId'));
			$('#ITP_BTN_STINFO05010_EXCEL').show();
		} else if(mode == 'ADD') { // 등록
			$('#ITP_AJAX_STINFO05010_DETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_STINFO05010_CANCELLIST', '#ITP_BTN_STINFO05010_SAVE']);

			$('#ITP_FORM_STINFO05010_DETAIL').find('input, textarea, select, checkbox').each(function(i, elem) {
				$(this).attr('id') === 'ITP_FORM_STINFO05010_DETAIL_VIEW_CD' ? $(this).val('C') : $(this).val('');
				if(elem.type === 'select') {
					$(this).val('').prop('selected', true);
				}
			});

			// 브랜드그룹 정보 설정
			$('#ITP_FORM_STINFO05010_DETAIL_AFFL_SHOP_ID').val(fn_make_user_info.get('afflShopId'));
			$('#ITP_FORM_STINFO05010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));

			$('#ITP_FORM_STINFO05010_DETAIL_STORE_ID').removeAttr('readonly');
			$('#ITP_FORM_STINFO05010_DETAIL_DUP').removeAttr('disabled');

			$('#ITP_FORM_STINFO05010_DETAIL .itp_form_info').hide();
			$('#ITP_FORM_STINFO05010_DETAIL .itp_form_change').show();
			
			$('#ITP_FORM_STINFO05010_DETAIL .fnStoreNm').text('(자동부여)');
			$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_CONT_STT_DT').datepicker('setDate', new Date());
			$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_CONT_END_DT').datepicker('setDate', itp_fn_date_add('Y', 1));
		} else if(mode == 'MODIFY') { // 수정
			$('#ITP_AJAX_STINFO05010_DETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_STINFO05010_CANCELLIST', '#ITP_BTN_STINFO05010_SAVE', '#ITP_BTN_STINFO05010_RESET_PW']);

			$('#ITP_FORM_STINFO05010_DETAIL_AFFL_SHOP_ID').val(fn_make_user_info.get('afflShopId'));
			$('#ITP_FORM_STINFO05010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
		
			$('#ITP_FORM_STINFO05010_DETAIL .itp_form_info').show();
			$('#ITP_FORM_STINFO05010_DETAIL .itp_form_change').hide();

			$('#ITP_FORM_STINFO05010_DETAIL_STORE_ID').attr('readonly', true);
			$('#ITP_FORM_STINFO05010_DETAIL_DUP').attr('disabled', true);
			$('#ITP_FORM_STINFO05010_DETAIL_SEARCH_AFFL_SHOP_NM').attr('readonly', true);

			$('#ITP_FORM_STINFO05010_DETAIL_SEARCH_AFFL_SHOP').hide();
			$('#ITP_FORM_STINFO05010_DETAIL_DELETE_AFFL_SHOP').hide();
		} else if(mode == 'VIEW') { // 보기
			$('#ITP_AJAX_STINFO05010_VIEW_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_STINFO05010_MODIFY', '#ITP_BTN_STINFO05010_CANCELLIST']);
		}
	}
};

/*목록화면 Object*/
let listObj = {
	init: function () {
		this.form();
		this.grid.init();
	},
	itp_STINFO05010_search: false,
	button: {
		search: function() {
			listObj.itp_STINFO05010_search = true;
			let param = $('#ITP_FORM_STINFO05010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.gridPage = $.jgrid.defaults.page;
			$(STINFO05010_GRID_ID).data('grid-param',param);
			$(STINFO05010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
		},
		create: function() {
			if(!fn_make_user_info.isEmpty()) {
				createObj.init();
			}
		}
	},
	form: function() {
		$('#ITP_FORM_STINFO05010_SEARCH_ITEM_CLASS1').on('change', function() {
			($(this).val() === '') ? itemClassLevel.clear(2): itemClassLevel.list(2);
		});
		$('#ITP_FORM_STINFO05010_SEARCH_ITEM_CLASS2').on('change', function() {
			($(this).val() === '') ? itemClassLevel.clear(3): itemClassLevel.list(3);
		});
		$('#ITP_FORM_STINFO05010_SEARCH_ITEM_CLASS3').on('change', function() {
			($(this).val() === '') ? itemClassLevel.clear(4): itemClassLevel.list(4);
		});
	},
	empty: {
		init: function() {
			
		},
		itp_STINFO05010_param: {},
		push: function() {
		},
		back: function() {
		}
	},
	grid: {
		init: function () {
			// 데이터 없을때
			//listObj.empty.push();
			this.search();
		},
		search: function() {
			pageObj.switchScreen('LIST');
			this.unload();
			this.load();
		},
		load: function() {
			let param = $('#ITP_FORM_STINFO05010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.sbrandId = fn_make_user_info.get('brandId');

			var option = {
				gridId: STINFO05010_GRID_ID,
				colModel: gridColModel.list,
				param: param,
				autowidth: false,
				shrinkToFit :true,
				forceFit: true,
				url: DOMAIN + API_DETAIL_GRID_LIST,
				pager: STINFO05010_GRID_PAGER,
				multiselect: true,
				rownumbers: false,
				onCellSelect: function(rowId, cellIdx, cellValue) {
					var cm = $(this).jqGrid('getGridParam', 'colModel');
					var colNm = cm[cellIdx].name;
					if (colNm == 'itemId') {
						const selectVal = $(this).jqGrid('getCell', rowId, 'brandUnitUnqNo');
						const key = {brandId:fn_make_user_info.get('brandId'), brandUnitUnqNo: selectVal, viewCd: 'R'};
						var param = $.param(key);
						viewObj.init(param);
					}
				},
				loadComplete: function(data) {
					console.log(data);
					itp_fn_grid_load_complete(data, STINFO05010_GRID_ID, true, 'number', 'STINFO05010', listObj.itp_STINFO05010_search, listObj.empty, true, data.gridRecords, true);
					var ids = $(STINFO05010_GRID_ID).getDataIDs();
					$.each(ids, function(idx, rowId) {
						$(STINFO05010_GRID_ID).jqGrid('setCell', rowId, 'itemId', '', ITP_GRID_COL_STYLE.link);
					});
				},
				onPaging: function(action) {
					itp_fn_grid_paging(STINFO05010_GRID_ID, action, param);
				}
			};
			itp_fn_grid_make_remote(option);
		},
		clearData : function() {
			$(STINFO05010_GRID_ID).jqGrid('clearGridData', true);
			$(STINFO05010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(STINFO05010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(STINFO05010_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(STINFO05010_GRID_ID);
		}
	},
	excelDown: function() {
		
		var exparam = $('#ITP_FORM_STINFO05010_SEARCH').serializeObject() ;
		exparam.sbrandId = fn_make_user_info.get('brandId');
		var param = {
			'url': API_DETAIL_GRID_LIST,
			'param': exparam,
			'gridColumns': $(STINFO05010_GRID_ID).jqGrid('getGridParam', 'colModel'),
			'fileName': '단가관리.xlsx',
			'sheetName': '단가관리'
		};
		itp_fn_remote_to_excel(param);
	}
};

/*상세화면 Object*/
let viewObj = {
	init: function (param) {
		this.load(param);
	},
	load: function(param) {
		fn_ajax_call(API_DETAIL_INFO, param, this.callback, 'GET');
	},
	callback: function (result) {
		console.log(result);
		pageObj.switchScreen('VIEW');
		$('#ITP_FORM_STINFO05010_VIEW #ITP_FORM_STINFO05010_VIEW_ITEM_ID').val(result.itemId);
		$('#ITP_FORM_STINFO05010_VIEW #ITP_FORM_STINFO05010_VIEW_BRAND_UNIT_UNQ_NO').val(result.brandUnitUnqNo);
		$('#ITP_FORM_STINFO05010_VIEW .fnBrandNm').text(result.brandNm);
		$('#ITP_FORM_STINFO05010_VIEW .fnStCd').text(result.unitStNm);
		$('#ITP_FORM_STINFO05010_VIEW .fnItemId').text(result.itemId);
		$('#ITP_FORM_STINFO05010_VIEW .fnItemNm').text(result.itemNm);
		$('#ITP_FORM_STINFO05010_VIEW .fnSpplyId').text(result.spplyId);
		$('#ITP_FORM_STINFO05010_VIEW .fnSpplyNm').text(result.spplyNm);
		$('#ITP_FORM_STINFO05010_VIEW .fnSpplyItemId').text(result.spplyItemId);
		$('#ITP_FORM_STINFO05010_VIEW .fnContSttDt').text(result.contSttDt);
		$('#ITP_FORM_STINFO05010_VIEW .fnContEndDt').text(result.contEndDt);
		$('#ITP_FORM_STINFO05010_VIEW .fnLeadTime').text(result.leadTime);
		$('#ITP_FORM_STINFO05010_VIEW .fnUnit').text(result.unit);
		$('#ITP_FORM_STINFO05010_VIEW .fnCurr').text(result.curr);
		$('#ITP_FORM_STINFO05010_VIEW .fnUnitAmt').text(result.unitAmt.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','));
		$('#ITP_FORM_STINFO05010_VIEW .fnStoreSaleUnitAmt').text(result.storeSaleUnitAmt.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','));
		$('#ITP_FORM_STINFO05010_VIEW .fnRegId').text(result.sysRegId);
		$('#ITP_FORM_STINFO05010_VIEW .fnRegDt').text(result.sysRegDttm);
		$('#ITP_FORM_STINFO05010_VIEW .fnChgId').text(result.sysChgId);
		$('#ITP_FORM_STINFO05010_VIEW .fnChgDt').text(result.sysChgDttm);
		fileUploadInfo5010(result.imageFileNoList);
		/*
		itp_fn_attach_file.view('ITP_FORM_STINFO05010_VIEW_FILE', result.imageFileNoList
			, 'form-control itp_input', 'border:none; top:12px; left:12px; height:100px; width:125px; position:absolute;');
		*/
	}
};


function fileUploadInfo5010(arr) {
	//파일업로드(등록/수정화면)
	var uid='#ITP_FORM_STINFO05010_VIEW_FILE'; 
    $(uid).empty();
	if(Array.isArray(arr) && arr.length ) {
		var sid='ITP_FORM_STINFO05010_VIEW';
		var fileNo=arr[0].fileNo;
		$(uid).bootstrapFileUpload({
			url: DOMAIN + '/api/file/upload',
			inputName: 'uploadFile',
			folderName: 'affl',
			fileNumber:  fileNo,
			fileInput: null,
			fileTypes: ITP_UPLOAD_DEFAULTS.fileTypes,
			viewCode: 'R',
			formMethod: 'post',
			multiFile: true,
			multiUpload: false,
			maxSize: 100,
			maxFiles: 5,
			showThumb: false,
			thumbWidth: 80,
			thumbHeight: 80,
			debug: true, 
			screen_id: sid,
			showYn: true 
		});
		setTimeout(function() {
			$('#'+sid+'_UPLOAD_FORM').hide();
		},20);
	}
}


/*수정화면 Object*/
let modifyObj = {
	init: function () {
	},
	button: {
		modify: function (param) {
			itp_fn_form_clear_validate(null, '#ITP_FORM_STINFO05010_DETAIL');
			const selectVal = $('#ITP_FORM_STINFO05010_VIEW_BRAND_UNIT_UNQ_NO').val();
			const key = {brandId:fn_make_user_info.get('brandId'), brandUnitUnqNo: selectVal, viewCd: 'R'};
			var param = $.param(key);
			modifyObj.load(param);
		}
	},
	load: function(param) {
		fn_ajax_call(API_DETAIL_INFO, param, this.callback, 'GET');
	},
	callback: function (result) {
		console.log(result);
		pageObj.switchScreen('MODIFY');

		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_VIEW_CD').val('U');
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_BRAND_UNIT_UNQ_NO').val(result.brandUnitUnqNo);
		$('#ITP_FORM_STINFO05010_DETAIL .fnBrandNm').text(result.brandNm);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_UNIT_ST_CD').val(result.unitStCd);
		$('#ITP_FORM_STINFO05010_DETAIL .fnItemId').text(result.itemId);
		$('#ITP_FORM_STINFO05010_DETAIL .fnItemNm').text(result.itemNm);
		$('#ITP_FORM_STINFO05010_DETAIL .fnSpplyId').text(result.spplyId);
		$('#ITP_FORM_STINFO05010_DETAIL .fnSpplyNm').text(result.spplyNm);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_ITEM_ID').val(result.itemId);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_ITEM_NM').val(result.itemNm);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_SPPLY_ID').val(result.spplyId);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_SPPLY_NM').val(result.spplyNm);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_SPPLY_ITEM_ID').val(result.spplyItemId);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_CONT_STT_DT').val(result.contSttDt === '' ? today : result.contSttDt);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_CONT_END_DT').val(result.contEndDt === '' ? today : result.contEndDt);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_LEAD_TIME').val(result.leadTime);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_UNIT').val(result.unit);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_CURR').val(result.curr);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_UNIT_AMT').val(result.unitAmt.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','));
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_STORE_SALE_UNIT_AMT').val(result.storeSaleUnitAmt.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','));
		$('#ITP_FORM_STINFO05010_DETAIL .fnRegId').text(result.sysRegId);
		$('#ITP_FORM_STINFO05010_DETAIL .fnRegDt').text(result.sysRegDttm);
		$('#ITP_FORM_STINFO05010_DETAIL .fnChgId').text(result.sysChgId);
		$('#ITP_FORM_STINFO05010_DETAIL .fnChgDt').text(result.sysChgDttm);

		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_FILE_NO').val(result.shortContFileNo);
		pageObj.fileUpload();
	}
};

/*신규화면 Object*/
let createObj = {
	init: function () {
		pageObj.switchScreen('ADD');
		pageObj.fileUpload();
		itp_fn_form_clear_validate(null, '#ITP_FORM_STINFO05010_DETAIL');
		$('#ITP_FORM_STINFO05010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
		$('#ITP_FORM_STINFO05010_DETAIL .fnBrandNm').text(fn_make_user_info.get('brandNm'));
	},
	button: {
		cancel: function() {
			let childrens = $('#ITP_FORM_STINFO05010_VIEW_FILE').children();
			childrens.remove();
			listObj.grid.search();
		},
		save: function() {
			const formId = '#ITP_FORM_STINFO05010_DETAIL';
			
			$('#ITP_FORM_STINFO05010_DETAIL_UNIT').removeAttr('disabled');
			itp_fn_form_clear_validate(null, formId);

			if (itp_fn_form_event.isValid(formId)) {
				itp_fn_form_event.removeChar(formId);
				let param = $(formId).serializeObject();
				console.log(JSON.stringify(param));
				var searhFn = function() {
					ITP_FORM_STINFO05010_DETAIL_IS_WRITING = false;
					$('#ITP_FORM_STINFO05010_DETAIL_UNIT').attr('disabled', true);
					listObj.grid.search();
				};
				fn_ajax_call(API_DETAIL_SAVE, JSON.stringify(param), searhFn, 'POST');
			}
		}
	}
};

var itemClassLevel = {
	level: 1,
	levelID: 'ITP_FORM_STINFO05010_SEARCH_ITEM_CLASS',
	keys: function() {
		var keys = {sBrandId:fn_make_user_info.get('brandId'), sItemLevel: this.level};
		for(var i=1; i<=this.level; i++) {
			var key = 'sItemClass' + i;
			var id = '#' + this.levelID + i;
			keys[key] = $(id + ' option:selected').val();
		}
		return keys;
	},
	list: function(level) {
		this.level = level;
		this.clear(level);
		this.call( $.param(this.keys()));
	},
	call: function(param) {
		var _this = this;
		var callbackFn = function(result) {
			console.log(result);
			var id = _this.levelID + _this.level;
			fn_make_select_item_class(result.searchList, id, true, _this.emptyText());
		};
		fn_ajax_call(API_ITEM_CLASS_LEVEL, param, callbackFn, 'GET');
	},
	emptyText: function() {
		return '품목분류Level' + this.level;
	},
	clear: function(level) {
		for(var i=level; i<5; i++) {
			$('#' + (this.levelID + i)).find('option:not(:first)').remove();
		}
	}
};

var popSearch = {
	init: function(id, mode) {
		(id.lastIndexOf('DELETE') > -1 ? this.delete(id) : this.popup(id));
	},
	popup: function(id) {
		var popId = id.lastIndexOf('ITEM') > -1 ? 'BIZPOP_NEW_ITEM' : 'BIZPOP_STORE_SPPLY';
		var popFn = function(rowDataPop) {
			
			$.each(rowDataPop, function(key, value) {
				if(id.indexOf('SEARCH') > -1) {
					$('#ITP_FORM_STINFO05010_SEARCH_SPPLY_NM').val(rowDataPop.spplyNm);
					$('#ITP_FORM_STINFO05010_SEARCH_SPPLY_ID').val(rowDataPop.spplyId);
				} else {
					if(id.lastIndexOf('ITEM') > -1) {
						$('#ITP_FORM_STINFO05010_DETAIL_ITEM_ID').val(rowDataPop.itemId);
						$('#ITP_FORM_STINFO05010_DETAIL_ITEM_NM').val(rowDataPop.itemNm);
						$('#ITP_FORM_STINFO05010_DETAIL_SCH_ITEM_ID').val(rowDataPop.itemId);
						$('#ITP_FORM_STINFO05010_DETAIL_SCH_ITEM_NM').val(rowDataPop.itemNm);
						$('#ITP_FORM_STINFO05010_DETAIL_UNIT').val(rowDataPop.unit);
					} else {
						$('#ITP_FORM_STINFO05010_DETAIL_SPPLY_ID').val(rowDataPop.spplyId);
						$('#ITP_FORM_STINFO05010_DETAIL_SPPLY_NM').val(rowDataPop.spplyNm);
						$('#ITP_FORM_STINFO05010_DETAIL_SCH_SPPLY_ID').val(rowDataPop.spplyId);
						$('#ITP_FORM_STINFO05010_DETAIL_SCH_SPPLY_NM').val(rowDataPop.spplyNm);
					}
				}
			});
		};
		var agrs = {'sbrandId': fn_make_user_info.get('brandId')};
		fn_call_popup('biz', popId, '#ITP_ASIDE', popFn, agrs, 'S');
		// fn_call_popup('biz', 'BIZPOP_AFFL_BRAND', '#ITP_ASIDE', popFn, agrs, 'S');
	},
	delete: function(id) {
		if(id.indexOf('SEARCH') > -1) {
			$('#ITP_FORM_STINFO05010_SEARCH_SPPLY_NM').val('');
			$('#ITP_FORM_STINFO05010_SEARCH_SPPLY_ID').val('');
		} else {
			if(id.lastIndexOf('ITEM') > -1) {
				$('#ITP_FORM_STINFO05010_DETAIL_ITEM_ID').val('');
				$('#ITP_FORM_STINFO05010_DETAIL_ITEM_NM').val('');
				$('#ITP_FORM_STINFO05010_DETAIL_SCH_ITEM_ID').val('');
				$('#ITP_FORM_STINFO05010_DETAIL_SCH_ITEM_NM').val('');
				$('#ITP_FORM_STINFO05010_DETAIL_UNIT').val('');
			} else {
				$('#ITP_FORM_STINFO05010_DETAIL_SPPLY_ID').val('');
				$('#ITP_FORM_STINFO05010_DETAIL_SPPLY_NM').val('');
				$('#ITP_FORM_STINFO05010_DETAIL_SCH_SPPLY_ID').val('');
				$('#ITP_FORM_STINFO05010_DETAIL_SCH_SPPLY_NM').val('');
			}
		}
	}
};
