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 VENDINFO02010_GRID_ID = '#ITP_VENDINFO02010_jqGrid';
const VENDINFO02010_GRID_LIST = '#ITP_VENDINFO02010_jqGrid_list';
const VENDINFO02010_GRID_PAGER = '#ITP_VENDINFO02010_jqGridPager';
const VENDINFO02010_GRID_EMPTY = '#ITP_VENDINFO02010_jqGridEmpty';

const VENDINFO02010_VIEW_GRID_ID = '#ITP_VENDINFO02010_VIEW_jqGrid';
const VENDINFO02010_VIEW_GRID_LIST = '#ITP_VENDINFO02010_VIEW_jqGrid_list';
const VENDINFO02010_VIEW_GRID_PAGER = '#ITP_VENDINFO02010_VIEW_jqGridPager';
const VENDINFO02010_VIEW_GRID_EMPTY = '#ITP_VENDINFO02010_VIEW_jqGridEmpty';

const VENDINFO02010_DETAIL_GRID_ID = '#ITP_VENDINFO02010_DETAIL_jqGrid';
const VENDINFO02010_DETAIL_GRID_LIST = '#ITP_VENDINFO02010_DETAIL_jqGrid_list';
const VENDINFO02010_DETAIL_GRID_PAGER = '#ITP_VENDINFO02010_DETAIL_jqGridPager';
const VENDINFO02010_DETAIL_GRID_EMPTY = '#ITP_VENDINFO02010_DETAIL_jqGridEmpty';

let ITP_FORM_VENDINFO02010_DETAIL_IS_DUPLICATE = false;
let ITP_FORM_VENDINFO02010_DETAIL_IS_WRITING = false;

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

/*화면 Grid ColModel*/
const gridColModel = {
	list: [
	
			{
					index: 'ITEM_ID', name: 'itemId',
					label: ITP_MSG_LOCALE.label.itemId, //품목번호
					width: '12', fixed: false, align: 'center',
					sortable: false, hidden: false
			},
			{
					index: 'ITEM_NM', name: 'itemNm',
					label: ITP_MSG_LOCALE.label.itemNm, //품목명
					width: '13', fixed: false, align: 'center',
					sortable: false, hidden: false
			},
			{
					index: 'ITEM_TYPE_NM', name: 'itemTypeNm',
					label: ITP_MSG_LOCALE.label.itemTypeNm, //품목유형
					width: '10', fixed: false, align: 'center',
					sortable: false, hidden: false
			},
			{
					index: 'ITEM_CLASS_NM', name: 'itemClassNm',
					label: ITP_MSG_LOCALE.label.itemClassNm, //품목분류
					width: '15', fixed: false, align: 'center',
					sortable: false, hidden: false
			},
			{
					index: 'ITEM_ST_CD', name: 'itemStCd',
					label: ITP_MSG_LOCALE.label.stNm, //상태
					width: '6', fixed: false, align: 'center',
					sortable: false, hidden: true
			},
			{
					index: 'PODR_PSSBLDVSN_NM', name: 'podrPssblDvsnNm',
					label: ITP_MSG_LOCALE.label.podrPssblDvsnNm, //발주여부
					width: '6', 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: 'PCH_ITEM_YN', name: 'pchItemYn',
					label: ITP_MSG_LOCALE.label.pchItemYn, //구매품목여부
					width: '6', fixed: false, align: 'center',
					sortable: false, hidden: false
			},
			{
					index: 'SALE_ITEM_YN', name: 'saleItemYn',
					label: ITP_MSG_LOCALE.label.saleItemYn, //판매품목여부
					width: '10', fixed: false, align: 'center',
					sortable: false, hidden: false
			},
	],
	detail: [
			{
					index: 'VIEW_CD', name: 'viewCd',
					label: ITP_MSG_LOCALE.label.viewCd,
					width: '10', fixed: false, align: 'center',
					sortable: false, hidden: true
			},
			{
					index: 'AUTH_NO', name: 'authNo',
					label: ITP_MSG_LOCALE.label.permitCd,
					width: '20', fixed: false, align: 'center',
					sortable: false, editable: false, edittype: 'text',
					editrules: {required: true}
			},
			{
					index: 'AUTH_NM', name: 'authNm',
					label: ITP_MSG_LOCALE.label.permitName,
					width: '30', fixed: false, align: 'center',
					sortable: true, editable: false, edittype: 'text',
					editrules: {required: true}
			},
			{
					index: 'USE_YN_NM', name: 'useYnNm',
					label: ITP_MSG_LOCALE.label.status,
					width: '10', fixed: false, align: 'center',
					sortable: false, editable: false, edittype: 'text'
			},
			{
					index: 'ADD_DT', name: 'addDt',
					label: ITP_MSG_LOCALE.label.regDt,
					width: '10', fixed: false, align: 'center',
					sortable: false, editable: false, edittype: 'text'
			}
	]
};

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

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

					itemClassLevel.make(1, 'LIST');
			},
			grid: function() {
					itp_fn_jqgrid_resize(VENDINFO02010_GRID_ID, VENDINFO02010_GRID_LIST, 'lg');
					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_VENDINFO02010_SRH'				: listObj.button.search();		break;	// 조회 버튼 클릭
											case 'ITP_BTN_VENDINFO02010_NEWREG'			: listObj.button.create(); 		break;	// 신규등록 버튼
											case 'ITP_BTN_VENDINFO02010_MODIFY'			: modifyObj.button.modify();	break;	// 수정 버튼
											case 'ITP_BTN_VENDINFO02010_CANCELLIST'		: createObj.button.cancel(); 	break;	// 취소/목록 버튼
											case 'ITP_BTN_VENDINFO02010_DELETE'			: 								break;	// 삭제 버튼
											case 'ITP_BTN_VENDINFO02010_SAVE'				: createObj.button.save(); 		break;	// 저장 버튼
											case 'ITP_FORM_VENDINFO02010_DETAIL_DUP'		: createObj.button.duplicate();	break;	// 중복체크
											case 'ITP_BTN_VENDINFO02010_RESET_PW'			: createObj.button.resetPass();	break;	// 상세:비밀번호 초기화
									}
							});
					});

					$('body').on('click', function(e) {
							var gridId = VENDINFO02010_DETAIL_GRID_ID.replace('#', '');
							var rids = $(VENDINFO02010_DETAIL_GRID_ID).jqGrid('getDataIDs');
							var last_row_id = rids[rids.length - 1];
							itp_fn_grid_reset_selection(e, last_row_id, gridId, 'VENDINFO02010_DETAIL');
					});

			}
	},
	fileUpload: function() {
			//파일업로드(등록/수정화면)
			var id = 'VENDINFO02010';
			$('#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_VENDINFO02010_"]').each(function(i) {
					$(this).hide();
			});
			$('#ITP_TAB_VENDINFO02010').find('div[id$="_CONTAINER"]').each(function(i) {
					$(this).hide();
			});
			if(mode == 'LIST') { // 목록
					$('#ITP_AJAX_VENDINFO02010_LIST_CONTAINER').show();
					if(fn_make_user_info.get('authTpCd') !== '60') {
						$('#ITP_FORM_VENDINFO02010_SEARCH_STORE_SPPLY').show();
						$('#ITP_FORM_VENDINFO02010_DELETE_STORE_SPPLY').show();
					} else {
						$('#ITP_FORM_VENDINFO02010_SEARCH_STORE_SPPLY').hide();
						$('#ITP_FORM_VENDINFO02010_DELETE_STORE_SPPLY').hide();
					}
					
					fn_show_btn_auth_array(['#ITP_BTN_VENDINFO02010_SRH']);
			} else if(mode == 'ADD') { // 등록
					$('#ITP_AJAX_VENDINFO02010_DETAIL_CONTAINER').show();
					fn_show_btn_auth_array(['#ITP_BTN_VENDINFO02010_CANCELLIST', '#ITP_BTN_VENDINFO02010_SAVE']);

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

					$('#ITP_FORM_VENDINFO02010_DETAIL_STORE_ID').removeAttr('readonly');
					$('#ITP_FORM_VENDINFO02010_DETAIL_DUP').removeAttr('disabled');

					// 가맹점 정보 설정
					$('#ITP_FORM_VENDINFO02010_DETAIL_AFFL_SHOP_ID').val(fn_make_user_info.get('afflShopId'));
					$('#ITP_FORM_VENDINFO02010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
					$('#ITP_FORM_VENDINFO02010_DETAIL .fnBrandNm').text(fn_make_user_info.get('brandNm'));
					$('#ITP_FORM_VENDINFO02010_DETAIL .fnItemClassMgntNo').text('(자동부여)');
			} else if(mode == 'MODIFY') { // 수정
					$('#ITP_AJAX_VENDINFO02010_DETAIL_CONTAINER').show();
					fn_show_btn_auth_array(['#ITP_BTN_VENDINFO02010_CANCELLIST', '#ITP_BTN_VENDINFO02010_SAVE', '#ITP_BTN_VENDINFO02010_RESET_PW']);

					$('#ITP_FORM_VENDINFO02010_DETAIL_AFFL_SHOP_ID').val(fn_make_user_info.get('afflShopId'));
					$('#ITP_FORM_VENDINFO02010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
					$('#ITP_FORM_VENDINFO02010_DETAIL .fnBrandNm').text(fn_make_user_info.get('brandNm'));
					$('#ITP_FORM_VENDINFO02010_DETAIL_STORE_ID').attr('readonly', true);
					$('#ITP_FORM_VENDINFO02010_DETAIL_DUP').attr('disabled', true);
					$('#ITP_FORM_VENDINFO02010_DETAIL_SEARCH_AFFL_SHOP_NM').attr('readonly', true);

					$('#ITP_FORM_VENDINFO02010_DETAIL_SEARCH_AFFL_SHOP').hide();
					$('#ITP_FORM_VENDINFO02010_DETAIL_DELETE_AFFL_SHOP').hide();
			} else if(mode == 'VIEW') { // 보기
					$('#ITP_AJAX_VENDINFO02010_VIEW_CONTAINER').show();
					$('#ITP_FORM_VENDINFO02010_VIEW .fnBrandNm').text(fn_make_user_info.get('brandNm'));
					fn_show_btn_auth_array(['#ITP_BTN_VENDINFO02010_MODIFY', '#ITP_BTN_VENDINFO02010_CANCELLIST']);
			}
	},
	grid: {
		init: function(mode) {
			this.mode = mode;
			this.gridId = (mode === 'VIEW') ? VENDINFO02010_VIEW_GRID_ID : VENDINFO02010_NEW_GRID_ID;
			this.unload();
			this.load(mode);
		},
		mode: 'DETAIL',
		gridId: '',
		button: {
			addRow: function() {
				// 팝업
				var gridId = (pageObj.pageMode === 'MODIFY') ? VENDINFO02010_NEW_GRID_ID : VENDINFO02010_DETAIL_GRID_ID;
				var popFn = function(rowDataPop) {
					console.log(rowDataPop);
					// 기존등록 사용자ID
					var exists = '';
					const rowData = $(gridId).getRowData();
					$.each(rowData, function(key, value) {
						exists = exists + value.authNo + ';';
					});

					$.each(rowDataPop, function(key, value) {
						// console.log(JSON.stringify(value));
						if(exists.indexOf(value.authNo) < 0) {
							value['viewCd'] =  "C";
							$(gridId).jqGrid('addRowData', value.authNo, value, 'last');
						}
					});
					$((pageObj.pageMode === 'MODIFY') ? VENDINFO02010_NEW_GRID_EMPTY : VENDINFO02010_DETAIL_GRID_EMPTY).hide();
				};
				fn_call_popup('comm', 'STORE_SPPLY', '#ITP_ASIDE', popFn, null, 'S');
			},
			delRow: function() {
				itp_fn_grid_del_row((pageObj.pageMode === 'MODIFY') ? VENDINFO02010_NEW_GRID_ID : VENDINFO02010_DETAIL_GRID_ID);
			}
		},
		load : function() {
			var gridId = (pageObj.pageMode === 'MODIFY') ? VENDINFO02010_NEW_GRID_ID : VENDINFO02010_VIEW_GRID_ID;
			var formId = (pageObj.pageMode === 'MODIFY') ? '#ITP_FORM_VENDINFO02010_NEW' : '#ITP_FORM_VENDINFO02010_VIEW';
			let param = $(formId).serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.pagingYn = false; // 페이지안함

			var option = {
				gridId: gridId,
				colModel: gridColModel.detail,
				param: param,
				url: DOMAIN + API_USERAUTH_GRID_LIST,
				multiselect: (this.mode !== 'VIEW'),
				loadComplete: function(data) {
					console.log(data);
					itp_fn_grid_load_complete(data, gridId, true, undefined, 'VENDINFO02010_' + this.mode);
				},
				onSortCol: function(index, columnIndex, sortOrder) {
					var sortingFn = {
						callBack: function(args) {
							$(args).trigger('reloadGrid');
						}
					};
					if (itp_fn_check_grid_is_writing(gridId)) {
						itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, sortingFn, gridId);
						return 'stop';
					} else {
						itp_fn_grid_sorting(gridId, index, sortOrder);
					}
				}
			};
			itp_fn_grid_make_not_paging(option);
		},
		unload : function() {
			$.jgrid.gridUnload(this.gridId);
		}
	}
};

/*목록화면 Object*/
let listObj = {
	init: function () {
			this.form();
			this.grid.init();
	},
    itp_VENDINFO02010_param: {},
	itp_VENDINFO02010_search: false,
	button: {
			search: function() {
					listObj.itp_VENDINFO02010_search = true;
					var url = DOMAIN + API_DETAIL_GRID_LIST;
					let param = $('#ITP_FORM_VENDINFO02010_SEARCH').serializeObject();
					param.gridSize = $.jgrid.defaults.rowNum;
					param.gridPage = $.jgrid.defaults.page;
					$(VENDINFO02010_GRID_ID).setGridParam({'url': url,'data': JSON.stringify(param)}).trigger('reloadGrid');
			},
			create: function() {
					if(!fn_make_user_info.isEmpty()) {
						createObj.init();
					}
			}
	},
	form: function() {
			$('#ITP_FORM_VENDINFO02010_SEARCH_ITEM_CLASS1').on('change', function() {
					($(this).val() === '') ? itemClassLevel.clear(2): itemClassLevel.change(2);
			});
			$('#ITP_FORM_VENDINFO02010_SEARCH_ITEM_CLASS2').on('change', function() {
					($(this).val() === '') ? itemClassLevel.clear(3): itemClassLevel.change(3);
			});
			$('#ITP_FORM_VENDINFO02010_SEARCH_ITEM_CLASS3').on('change', function() {
					($(this).val() === '') ? itemClassLevel.clear(4): itemClassLevel.change(4);
			});
	},
	empty: {
			init: function() {
					var _this = this;
					this.push();
					$(VENDINFO02010_GRID_EMPTY).on('click', function() {
							_this.back();
							_this.itp_VENDINFO02010_param.gridSize = $.jgrid.defaults.rowNum;
							$(VENDINFO02010_GRID_ID).setGridParam({'postData': JSON.stringify(_this.itp_VENDINFO02010_param)}).trigger('reloadGrid');
					});
			},
			itp_VENDINFO02010_param: {},
			push: function() {
					let param = $('#ITP_FORM_VENDINFO02010_SEARCH').serializeObject();
					listObj.itp_VENDINFO02010_param = param;
			},
			back: function() {
//					$('#ITP_FORM_VENDINFO02010_SERVICE_ST_CD').val(this.itp_VENDINFO02010_param.sitemStCd);
					$('#ITP_FORM_VENDINFO02010_SEARCH_PODR_PSSBL_DVSN').val(listObj.itp_VENDINFO02010_param.podrPssblDvsnNm);
					$('#ITP_FORM_VENDINFO02010_SEARCH_ITEM_KIND').val(listObj.itp_VENDINFO02010_param.sitemKind);
					$('#ITP_FORM_VENDINFO02010_SEARCH_ITEM_NM').val(listObj.itp_VENDINFO02010_param.itemNm);
			}
	},
	grid: {
			init: function () {
					// 데이터 없을때
					listObj.empty.push();
					this.search();
			},
			colModel: gridColModel.list,
			search: function() {
					pageObj.switchScreen('LIST');
					this.unload();
					this.localLoad();
//					this.load();
			},
			localLoad: function() {
				var option = {
					gridId: VENDINFO02010_GRID_ID,
					colModel: gridColModel.list,
					data: [],
					multiselect: true,
					pager: VENDINFO02010_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, 'itemId');
								const key = {brandId:fn_make_user_info.get('brandId'), itemId: selectVal, viewCd: 'R'};
								var param = $.param(key);
								viewObj.init(param);
						}
					},	
					loadComplete: function(data) {
						console.log(data);
						if(data && data.length > 0) {
							itp_fn_grid_load_complete(data, VENDINFO02010_GRID_ID, true, 'number', 'VENDINFO02010', listObj.itp_VENDINFO02010_search, listObj.empty, true, data.gridRecords, true);
							var ids = $(VENDINFO02010_GRID_ID).getDataIDs();
							$.each(ids, function(idx, rowId) {
									$(VENDINFO02010_GRID_ID).jqGrid('setCell', rowId, 'itemId', '', ITP_GRID_COL_STYLE.link);
							});
						}
					},
					onPaging: function(action) {
						itp_fn_grid_paging(VENDINFO02010_GRID_ID, action, param);
					}
				};
				itp_fn_grid_make_local(option);
			},
			load: function() {
					$('#ITP_FORM_VENDINFO02010_SEARCH_SPPLY_ID').val(fn_make_user_info.get('spplyId'));
					$('#ITP_FORM_VENDINFO02010_SEARCH_BRAND_ID').val(fn_make_user_info.get('brandId'));
					$('#ITP_FORM_VENDINFO02010_SEARCH_SPPLY_NM').val(fn_make_user_info.get('spplyNm'));

					let param = $('#ITP_FORM_VENDINFO02010_SEARCH').serializeObject();
					param.gridSize = $.jgrid.defaults.rowNum;
					param.sbrandId = sessionStorage.getItem('itp_brand_id') !== null ? sessionStorage.getItem('itp_brand_id') : 'BRD221000001';

					var option = {
							gridId: VENDINFO02010_GRID_ID,
							colModel: gridColModel.list,
							param: param,
							url: DOMAIN + API_DETAIL_GRID_LIST,
							pager: VENDINFO02010_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, 'itemId');
											const key = {brandId:fn_make_user_info.get('brandId'), itemId: selectVal, viewCd: 'R'};
											var param = $.param(key);
											viewObj.init(param);
									}
							},
							loadComplete: function(data) {
									console.log(data);
									itp_fn_grid_load_complete(data, VENDINFO02010_GRID_ID, true, 'number', 'VENDINFO02010', listObj.itp_VENDINFO02010_search, listObj.empty, true, data.gridRecords, true);
									var ids = $(VENDINFO02010_GRID_ID).getDataIDs();
									$.each(ids, function(idx, rowId) {
											$(VENDINFO02010_GRID_ID).jqGrid('setCell', rowId, 'itemId', '', ITP_GRID_COL_STYLE.link);
									});
							},
							onPaging: function(action) {
									itp_fn_grid_paging(VENDINFO02010_GRID_ID, action, param);
							}
					};
					itp_fn_grid_make_remote(option);
			},
			clearData : function() {
					$(VENDINFO02010_GRID_ID).jqGrid('clearGridData', true);
					$(VENDINFO02010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
					$(VENDINFO02010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
					$(VENDINFO02010_GRID_EMPTY).show();
			},
			unload : function() {
					$.jgrid.gridUnload(VENDINFO02010_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_VENDINFO02010_VIEW #ITP_FORM_VENDINFO02010_VIEW_ITEM_ID').val(result.itemId);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnStCd').text(result.itemStCd);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnItemClass1').text(result.itemClass1);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnItemClass2').text(result.itemClass2);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnItemClass3').text(result.itemClass3);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnItemClass4').text(result.itemClass4);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnItemClass1Nm').text(result.itemClass1Nm);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnItemClass2Nm').text(result.itemClass2Nm);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnItemClass3Nm').text(result.itemClass3Nm);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnItemClass4Nm').text(result.itemClass4Nm);
			
			$('#ITP_FORM_VENDINFO02010_VIEW .fnItemClassMgntNo').text(result.itemClassMgntNo);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnItemId').text(result.itemId);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnItemTypeNm').text(result.itemTypeNm);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnItemNm').text(result.itemClassNm);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnImpDvsnNm').text(result.impDvsnNm);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnPodrPssblDvsn').text(result.podrPssblDvsnNm);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnUnit').text(result.unit);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnWeightUnit').text(result.weightUnit);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnVolumeUnit').text(result.volumeUnit);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnWidthUnit').text(result.widthUnit);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnHeightUnit').text(result.heightUnit);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnHighUnit').text(result.highUnit);
			
			$('#ITP_FORM_VENDINFO02010_VIEW .fnPchItemYn').text(result.pchItemYn);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnViewPchUnit').text(result.pchUnit);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnSaleItemYn').text(result.saleItemYn);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnSaleUnit').text(result.saleUnit);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnAssetInfo').text(result.assetInfo);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnStckUnit').text(result.stckUnit);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnWeight').text(result.weight);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnVolume').text(result.volume);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnWidth').text(result.width);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnHeight').text(result.height);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnHigh').text(result.high);
			
			$('#ITP_FORM_VENDINFO02010_VIEW .fnOneBoxQty').text(result.oneBoxQty);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnExpryYn').text(result.expryYn);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnExpryPeriod').text(result.expryPeriod);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnRtnPssblYn').text(result.rtnPssblYn);
			$('#ITP_FORM_VENDINFO02010_VIEW .fnRtnDvsn').text(result.rtnDvsnNm);
			// $('#ITP_FORM_VENDINFO02010_VIEW .fnItemImage').text(result.imageFileNoList);
			$.each(result.imageFileNoList, function(index, item) {
					$('#ITP_FORM_VENDINFO02010_VIEW .fnItemImage').append('<img src=" + item.filePath +" />');
			});
	}
};

/*수정화면 Object*/
let modifyObj = {
	init: function () {
	},
	button: {
			modify: function (param) {
					itp_fn_form_clear_validate(null, '#ITP_FORM_VENDINFO02010_DETAIL');
					const selectVal = $('#ITP_FORM_VENDINFO02010_VIEW_ITEM_ID').val();
					const key = {brandId:fn_make_user_info.get('brandId'), itemId: 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_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_VIEW_CD').val('U');
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_ITEM_ID').val(result.itemId);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_STORE_ST_CD').val(result.itemStCd);
			$('#ITP_FORM_VENDINFO02010_DETAIL .fnItemClassMgntNo').text(result.itemId);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_ITEM_TYPE').val(result.itemType);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_ITEM_NM').val(result.itemClassNm);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_IMP_DVSN').val(result.impDvsn);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_PODR_PSSBL_DVSN').val(result.podrPssblDvsn);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_UNIT').val(result.unit);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_PCH_ITEM_YN').val(result.pchItemYn);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_PCH_UNIT').val(result.pchUnit);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_SALE_ITEM_YN').val(result.saleItemYn);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_SALE_UNIT').val(result.saleUnit);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_ASSET_INFO').val(result.assetInfo);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_STCK_UNIT').val(result.stckUnit);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_WEIGHT').val(result.weight);
			$('#ITP_FORM_VENDINFO02010_DETAIL .item_unit').text(result.unit);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_VOLUME').val(result.volume);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_WIDTH').val(result.width);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_HEIGHT').val(result.height);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_HIGH').val(result.high);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_ONE_BOX_QTY').val(result.oneBoxQty);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_EXPRY_YN').val(result.expryYn);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_EXPRY_PERIOD').val(result.expryPeriod);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_RTN_PSSBL_YN').val(result.rtnPssblYn);
			$('#ITP_FORM_VENDINFO02010_DETAIL #ITP_FORM_VENDINFO02010_DETAIL_RTN_DVSN').val(result.rtnDvsn);
			$.each(result.imageFileNoList, function(index, item) {
					$('#ITP_FORM_VENDINFO02010_VIEW .ITEM_IMAGE').append('<img src=" + item.filePath +" />');
			});

			itemClassLevel.data.push(result.itemClass1);
			itemClassLevel.data.push(result.itemClass2);
			itemClassLevel.data.push(result.itemClass3);
			itemClassLevel.data.push(result.itemClass4);
			itemClassLevel.make(1, 'MODIFY');
	}
};

/*신규화면 Object*/
let createObj = {
	init: function () {
			pageObj.switchScreen('ADD');
			itp_fn_form_clear_validate(null, '#ITP_FORM_VENDINFO02010_DETAIL');
			this.button.init();
			itemClassLevel.make(1, 'ADD');
	},
	button: {
			init: function() {
					$('#ITP_FORM_VENDINFO02010_DETAIL_ITEM_CLASS1').off('change').on('change', function() {
							($(this).val() === '') ? itemClassLevel.clear(2): itemClassLevel.change(2);
					});
					$('#ITP_FORM_VENDINFO02010_DETAIL_ITEM_CLASS2').off('change').on('change', function() {
							($(this).val() === '') ? itemClassLevel.clear(3): itemClassLevel.change(3);
					});
					$('#ITP_FORM_VENDINFO02010_DETAIL_ITEM_CLASS3').off('change').on('change', function() {
							($(this).val() === '') ? itemClassLevel.clear(4): itemClassLevel.change(4);
					});
			},
			cancel: function() {
					listObj.grid.search();
			},
			save: function() {
					const formId = '#ITP_FORM_VENDINFO02010_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_VENDINFO02010_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: '',
	data: [],
	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;
	},
	make: function(level, mode) {
			this.level = level;
			this.levelID = (mode === 'LIST' ? 'ITP_FORM_VENDINFO02010_SEARCH_ITEM_CLASS' : 'ITP_FORM_VENDINFO02010_DETAIL_ITEM_CLASS');
			if(mode !== 'MODIFY') {
					this.data.length = 0;
					this.clear(1);
			}
			this.call($.param(this.keys()), mode === 'MODIFY');
	},
	change: function(level) {
			this.level = level;
			this.clear(level);
			this.call($.param(this.keys()), false);
	},
	call: function(param, isModify) {
			var _this = this;
			var callbackFn = function(result) {
					var id = _this.levelID + _this.level;
					fn_make_select_item_class(result.searchList, id, true, _this.emptyText());
					if(isModify) {
							$('#' + id).val(_this.data[ _this.level - 1]);
							if(_this.level < _this.data.length) {
									if(_this.data[_this.level] !== '') {
											_this.level++;
											_this.call( $.param(_this.keys()), isModify);
									}
							}
					}
			};
			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();
			}
	}
};
let storeSpplyObj = {
		init: function () {
			this.action();
		},
		action: function () {
			var _this = this;
			// 가맹점 조회  클릭
			$('#ITP_FORM_VENDINFO02010_SEARCH_STORE_SPPLY').on('click', function() {
				_this.popup('L');
			});
			// 가맹점 텍스트 삭제 버튼 클릭
			$('#ITP_FORM_VENDINFO02010_DELETE_STORE_SPPLY').on('click', function() {
				$('#ITP_FORM_VENDINFO02010_SEARCH_SPPLY_NM').val('');
				$('select#ITP_FORM_VENDINFO02010_SEARCH_BRAND_ID option').remove();
			});
		},
		popup: function (view) {
			// 팝업
			var popFn = (view === 'C') ? this.callback.create : this.callback.list;
			fn_call_popup('biz', 'BIZPOP_STORE_SPPLY', '#ITP_ASIDE', popFn, null, 'S');
		},
		callback: {
			list: function(rowDataPop) {
				 console.log(rowDataPop);
				if(rowDataPop) {
					$('#ITP_FORM_VENDINFO02010_SEARCH_SPPLY_ID').val(rowDataPop.spplyId);
					$('#ITP_FORM_VENDINFO02010_SEARCH_SPPLY_NM').val(rowDataPop.spplyNm);
				}
				if(rowDataPop.AFFL_BRAND) {
					fn_make_select_brand(rowDataPop.AFFL_BRAND, 'ITP_FORM_VENDINFO02010_SEARCH_BRAND_ID', true, '선택');
				}
			},
			create: function(rowDataPop) {
				console.log(rowDataPop);
				if(rowDataPop) {
					$('#ITP_FORM_VENDINFO02010_DETAIL_SEARCH_STORE_SPPLY_ID').val(rowDataPop.spplyId);
					$('#ITP_FORM_VENDINFO02010_DETAIL_SEARCH_STORE_SPPLY_NM').val(rowDataPop.spplyNm);
				}
				if(rowDataPop.AFFL_BRAND) {
					fn_make_select_brand(rowDataPop.AFFL_BRAND, 'ITP_FORM_VENDINFO02010_DETAIL_BRAND_NM', true, '선택');
				}
			}
		}
	};
