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
		},
		{
			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
		},
		{
			index: 'ITEM_NM', name: 'itemNm',
			label: ITP_MSG_LOCALE.label.itemNm, //품목명
			width: '15', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'SPPLY_ID_NM', name: 'spplyNm',
			label: ITP_MSG_LOCALE.label.spplyNm, //공급업체명
			width: '13', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'UNIT', name: 'unit',
			label: ITP_MSG_LOCALE.label.unit, //기본단위
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'STDD_QYT', name: 'stddQty',
			label: ITP_MSG_LOCALE.label.stddQty, //기준수량
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'CURR', name: 'curr',
			label: ITP_MSG_LOCALE.label.curr, //통화
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'UNIT_AMT', name: 'unitAmt',
			label: ITP_MSG_LOCALE.label.unitAmt, //단가
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'CONT_STT_DT', name: 'contSttDt',
			label: ITP_MSG_LOCALE.label.contSttDt, //시작일
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'CONT_END_DT', name: 'contEndDt',
			label: ITP_MSG_LOCALE.label.contEndDt, //종료일
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'LEAD_TIME', name: 'leadTime',
			label: ITP_MSG_LOCALE.label.leadTime, //리드타임 (Day)
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'FILE_PATH', name: 'filePath',
			label: ITP_MSG_LOCALE.label.itemFilePath, //단가계약파일
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'SYS_REG_DT', name: 'sysRegDt',
			label: ITP_MSG_LOCALE.label.regDt, //등록일
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'UNIT_ST_NM', name: 'unitStNm',
			label: ITP_MSG_LOCALE.label.stNm, //상태
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'SPPLY_ITEM_ID', name: 'spplyItemId',
			label: ITP_MSG_LOCALE.label.spplyItemId, //공급사품번
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		}
	]
};

/*화면공통 Object*/
let pageObj = {
	init: function () {
		this.ui.init();
		this.event.init();
		this.fileUpload();
	},
	ui: {
		init: function () {
			this.view();
			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();
			$('#ITP_TAB_STINFO05010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_STINFO05010_SEARCH_CONT_STT_DT').datepicker('setDate', new Date(now.setMonth(now.getMonth() - 1)));
			$('#ITP_FORM_STINFO05010_SEARCH_CONT_END_DT').datepicker('setDate', new Date());
		},
		grid: function() {
			itp_fn_jqgrid_resize(STINFO05010_GRID_ID, STINFO05010_GRID_LIST, 'sm');
			listObj.empty.init();
			itp_fn_fire_window_resize();
		},
		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;	// 상세:비밀번호 초기화
					}
				});
			});

			$('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_AJAX_STINFO05010_LIST_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_STINFO05010_SRH', '#ITP_BTN_STINFO05010_NEWREG']);
		} 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('(자동부여)');
		} 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).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() {
			var _this = this;
			this.push();
			$(STINFO05010_GRID_EMPTY).on('click', function() {
				_this.back();
				_this.itp_STINFO05010_param.gridSize = $.jgrid.defaults.rowNum;
				$(STINFO05010_GRID_ID).setGridParam({'postData': JSON.stringify(_this.itp_STINFO05010_param)}).trigger('reloadGrid');
			});
		},
		itp_STINFO05010_param: {},
		push: function() {
			let param = $('#ITP_FORM_STINFO05010_SEARCH').serializeObject();
			listObj.itp_STINFO05010_param = param;
		},
		back: function() {
			$('#ITP_FORM_STINFO05010_SERVICE_ST_CD').val(this.itp_STINFO05010_param.sitemStCd);
			$('#ITP_FORM_STINFO05010_SEARCH_PODR_PSSBL_DVSN').val(this.itp_STINFO05010_param.contSttDt);
			$('#ITP_FORM_STINFO05010_SEARCH_ITEM_KIND').val(this.itp_STINFO05010_param.contEndDt);
			$('#ITP_FORM_STINFO05010_SEARCH_ITEM_TYPE').val(this.itp_STINFO05010_param.sspplyNm);
			$('#ITP_FORM_STINFO05010_SEARCH_ITEM_CLASS1').val(this.itp_STINFO05010_param.sitemClass1);
			$('#ITP_FORM_STINFO05010_SEARCH_ITEM_CLASS2').val(this.itp_STINFO05010_param.sitemClass2);
			$('#ITP_FORM_STINFO05010_SEARCH_ITEM_CLASS3').val(this.itp_STINFO05010_param.sitemClass3);
			$('#ITP_FORM_STINFO05010_SEARCH_ITEM_CLASS4').val(this.itp_STINFO05010_param.sitemClass4);
			$('#ITP_FORM_STINFO05010_SEARCH_ITEM_NM').val(this.itp_STINFO05010_param.sitemNm);
		}
	},
	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,
				url: DOMAIN + API_DETAIL_GRID_LIST,
				pager: STINFO05010_GRID_PAGER,
				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);
		}
	}
};

/*상세화면 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 .fnStddQyt').text(result.stddQty);
		$('#ITP_FORM_STINFO05010_VIEW .fnCurr').text(result.curr);
		$('#ITP_FORM_STINFO05010_VIEW .fnUnitAmt').text(result.unitAmt);
		$('#ITP_FORM_STINFO05010_VIEW .fnStoreSaleStddQty').text(result.storeSaleStddQty);
		$('#ITP_FORM_STINFO05010_VIEW .fnStoreSaleUnitAmt').text(result.storeSaleUnitAmt);
		$('#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);

		$.each(result.imageFileNoList, function(index, item) {
			$('#ITP_FORM_STINFO05010_VIEW .fnFilePath').append('<img src=" + item.filePath +" />');
		});
	}
};

/*수정화면 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 #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);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_CONT_END_DT').val(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_STDD_QYT').val(result.stddQty);
		$('#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);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_STORE_SALE_STDD_QTY').val(result.storeSaleStddQty);
		$('#ITP_FORM_STINFO05010_DETAIL #ITP_FORM_STINFO05010_DETAIL_STORE_SALE_UNIT_AMT').val(result.storeSaleUnitAmt);
		$('#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);
		$.each(result.imageFileNoList, function(index, item) {
			$('#ITP_FORM_STINFO05010_VIEW .ITEM_IMAGE').append('<img src=" + item.filePath +" />');
		});
	}
};

/*신규화면 Object*/
let createObj = {
	init: function () {
		pageObj.switchScreen('ADD');
		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() {
			listObj.grid.search();
		},
		save: function() {
			const formId = '#ITP_FORM_STINFO05010_DETAIL';
			itp_fn_form_clear_validate(null, formId);
			if (createObj.isValid(formId)) {
				let param = $(formId).serializeObject();
				console.log(JSON.stringify(param));
				var searhFn = function() {
					ITP_FORM_STINFO05010_DETAIL_IS_WRITING = false;
					listObj.grid.search();
				};
				fn_ajax_call(API_DETAIL_SAVE, JSON.stringify(param), searhFn, 'POST');
			}
		}
	},
	isValid: function(formId) {
		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;
	}
};

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_STORE_ITEM' : 'BIZPOP_STORE_SPPLY';
		var popFn = function(rowDataPop) {
			console.log(rowDataPop);
			$.each(rowDataPop, function(key, value) {
				if(id.indexOf('SEARCH') > -1) {
					$('#ITP_FORM_STINFO05010_SEARCH_SPPLY_NM').val(rowDataPop.spplyNm);
				} else {
					if(id.lastIndexOf('ITEM') > -1) {
						$('#ITP_FORM_STINFO05010_DETAIL_ITEM_ID').val(rowDataPop.itemId);
						$('#ITP_FORM_STINFO05010_DETAIL_ITEM_NM').val(rowDataPop.itemNm);
					} else {
						$('#ITP_FORM_STINFO05010_DETAIL_SPPLY_ID').val(rowDataPop.spplyId);
						$('#ITP_FORM_STINFO05010_DETAIL_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('');
		} else {
			if(id.lastIndexOf('ITEM') > -1) {
				$('#ITP_FORM_STINFO05010_DETAIL_ITEM_ID').val('');
				$('#ITP_FORM_STINFO05010_DETAIL_ITEM_NM').val('');
			} else {
				$('#ITP_FORM_STINFO05010_DETAIL_SPPLY_ID').val('');
				$('#ITP_FORM_STINFO05010_DETAIL_SPPLY_NM').val('');
			}
		}
	}
};
