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

/*화면 변수*/
const STOCKMNG04010_GRID_ID = '#ITP_STOCKMNG04010_jqGrid';
const STOCKMNG04010_GRID_LIST = '#ITP_STOCKMNG04010_jqGrid_list';
const STOCKMNG04010_GRID_PAGER = '#ITP_STOCKMNG04010_jqGridPager';
const STOCKMNG04010_GRID_EMPTY = '#ITP_STOCKMNG04010_jqGridEmpty';

const STOCKMNG04010_VIEW_GRID_ID = '#ITP_STOCKMNG04010_VIEW_jqGrid';
const STOCKMNG04010_VIEW_GRID_LIST = '#ITP_STOCKMNG04010_VIEW_jqGrid_list';
const STOCKMNG04010_VIEW_GRID_PAGER = '#ITP_STOCKMNG04010_VIEW_jqGridPager';
const STOCKMNG04010_VIEW_GRID_EMPTY = '#ITP_STOCKMNG04010_VIEW_jqGridEmpty';

const STOCKMNG04010_DETAIL_GRID_ID = '#ITP_STOCKMNG04010_DETAIL_jqGrid';
const STOCKMNG04010_DETAIL_GRID_LIST = '#ITP_STOCKMNG04010_DETAIL_jqGrid_list';
const STOCKMNG04010_DETAIL_GRID_PAGER = '#ITP_STOCKMNG04010_DETAIL_jqGridPager';
const STOCKMNG04010_DETAIL_GRID_EMPTY = '#ITP_STOCKMNG04010_DETAIL_jqGridEmpty';

let ITP_FORM_STOCKMNG04010_DETAIL_IS_DUPLICATE = false;
let ITP_FORM_STOCKMNG04010_DETAIL_IS_WRITING = false;
let STOCKMNG04010_DETAIL_GRID_LAST_ROW_ID;

/* 공통코드 리스트 */
let ITP_COMMON_CD_STCK_DVSN = fn_make_common_cd_list(CODE_LIST, 'STCK_DVSN', false);
let ITP_COMMON_CD_LOC_ST_CD = fn_make_common_cd_list(CODE_LIST, 'LOC_ST_CD', false);

/*API URL*/
let WHS_MNG_DETAIL_GRID_LIST = '/api/whs/mng/detail-grid-list';
let WHS_MNG_LOCATION_GRID_LIST = '/api/whs/mng/location-grid-list';
let WHS_MNG_INFO_WHS = '/api/whs/mng/info-whs';
let WHS_MNG_ADD_WHS = '/api/whs/mng/add-whs';
let WHS_MNG_MIDIFY_WHS = '/api/whs/mng/modify-whs';
let WHS_MNG_REMOVE_WHS = '/api/whs/mng/remove-whs'; 

/*화면 Grid ColModel*/
const gridColModel = {
	list: [
		{
			index: 'BRAND_ID', name: 'brandId',
			label: "브랜드ID", 
			width: '0', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'STORE_ID', name: 'storeId',
			label: "매장ID", 
			width: '0', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'BRAND_NM', name: 'brandNm',
			label: ITP_MSG_LOCALE.label.brandNm, //브랜드
			width: '13', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'STORE_NM', name: 'storeNm',
			label: "매장명", 
			width: '13', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'WHS_ID', name: 'whsId',
			label: "창고번호", 
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'WHS_NM', name: 'whsNm',
			label: "창고명", 
			width: '13', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'WHS_ST_NM', name: 'whsStNm',
			label: ITP_MSG_LOCALE.label.status, //상태
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'MGR_NM', name: 'mgrNm',
			label: "담당자명", 
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'MGR_TEL_NO', name: 'mgrTelNo',
			label: "연락처", 
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: false
		}
	],
	view: [
		{
			index: 'VIEW_CD', name: 'viewCd',
			label: ITP_MSG_LOCALE.label.viewCd,
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},		
		{
			index: 'LOCATION', name: 'location',
			label: "로케이션",
			width: '7', fixed: false, align: 'left',
			sortable: false, editable: false, edittype: 'text',
			editrules: {required: true}
		},
		{
			index: 'LOCATION_NM', name: 'locationNm',
			label: "로케이션명",
			width: '25', fixed: false, align: 'left',
			sortable: true, editable: false, edittype: 'text',
			editrules: {required: true}, classes: 'ui-ellipsis'
		},
		{
			index: 'STCK_DVSN_NM', name: 'stckDvsnNm',
			label: "관리등록",
			width: '6', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'LOC_ST_NM', name: 'locStNm',
			label: "상태",
			width: '6', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		}
	],
	detail: [
		{
			index: 'VIEW_CD', name: 'viewCd',
			label: ITP_MSG_LOCALE.label.viewCd,
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'LOCATION', name: 'location',
			label: "로케이션",
			width: '7', fixed: false, align: 'left',
			sortable: false, editable: true, edittype: 'text',
			editrules: {required: false}
		},
		{
			index: 'LOCATION_NM', name: 'locationNm',
			label: "로케이션명",
			width: '25', fixed: false, align: 'left',
			classes: 'input_color',
			sortable: false, editable: true, edittype: 'text',
			editrules: {required: true}, classes: 'ui-ellipsis',
			editoptions: {maxlength: 50}
		},
		{
			index: 'STCK_DVSN', name: 'stckDvsn',
			label: "관리등록",
			width: '6', fixed: false, align: 'center',
			sortable: false, editable: true, formatter: 'select', edittype: 'select',
			editoptions: {value: ITP_COMMON_CD_STCK_DVSN},
			classes: 'input_color',
			editrules: {required: true}
		},
		{
			index: 'LOC_ST_CD', name: 'locStCd',
			label: "상태",
			width: '6', fixed: false, align: 'center',
			sortable: false, editable: true, formatter: 'select', edittype: 'select',
			editoptions: {value: ITP_COMMON_CD_LOC_ST_CD},
			classes: 'input_color',
			editrules: {required: true}
		}
	]
};

require(['config'], function() {
	require([
		'kakao.map.util'
	], function($) {
		pageObj.init();
	});
});

/*화면공통 Object*/
let pageObj = {
	init: function () {
		this.ui.init();
		this.event.init();
		locInfoObj.init();
	},
	ui: {
		init: function () {
			this.view();
			this.form();
			this.grid();
			this.ready();
		},
		view: function() {
			// 버튼 권한설정
			fn_proc_btn_auth('STOCKMNG04010');
			// 공통코드 표시
			fn_make_select(CODE_LIST, 'WHS_DVSN', 'ITP_FORM_STOCKMNG04010_WHS_DVSN');			
			fn_make_select(CODE_LIST, 'WHS_ST_CD', 'ITP_FORM_STOCKMNG04010_WHS_ST_CD');
			fn_make_select(CODE_LIST, 'WHS_ST_CD', 'ITP_FORM_STOCKMNG04010_DETAIL_WHS_ST_CD', true, '선택');

			// 조회일자 지정
			$('#ITP_TAB_STOCKMNG04010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);		
			
			
			$('#ITP_FORM_STOCKMNG04010_WHS_DVSN').val("W01");
		},
		form: function() {
			itp_fn_form_event.onKeyup('#ITP_FORM_STOCKMNG04010_DETAIL');
		},
		grid: function() {
			itp_fn_jqgrid_resize(STOCKMNG04010_GRID_ID, STOCKMNG04010_GRID_LIST, 'lg');
			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_STOCKMNG04010_SRH'				: listObj.button.search();		break;	// 조회 버튼 클릭
						case 'ITP_BTN_STOCKMNG04010_NEWREG'				: listObj.button.create(); 		break;	// 신규등록 버튼
						case 'ITP_BTN_STOCKMNG04010_MODIFY'				: modifyObj.button.modify();	break;	// 수정 버튼
						case 'ITP_BTN_STOCKMNG04010_CANCELLIST'			: createObj.button.cancel(); 	break;	// 취소/목록 버튼
						case 'ITP_BTN_STOCKMNG04010_DELETE'				: 								break;	// 삭제 버튼
						case 'ITP_BTN_STOCKMNG04010_SAVE'				: createObj.button.save(); 		break;	// 저장 버튼
						case 'ITP_BTN_STOCKMNG04010_DETAIL_ADDROW'		: locInfoObj.button.addRow();	break;	// 상세:추가버튼
						case 'ITP_BTN_STOCKMNG04010_DETAIL_DELROW'		: locInfoObj.button.delRow();	break;	// 상세:삭제버튼
						case 'ITP_BTN_STOCKMNG04010_DETAIL_ADDRESS_SEARCH'		: createObj.button.address();	break;	// 신규등록 주소 찾기
						case 'ITP_BTN_STOCKMNG04010_SEARCH_SRH'			: createObj.button.popupStore();	break;	// 매장선택 팝업
						case 'ITP_BTN_STOCKMNG04010_SEARCH_MODIFY'		: createObj.button.eraseStore();	break;	// 매장선택 삭제
					}
				});
			});

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

		}
	},
	switchScreen: function(mode) {
		if(mode == 'LIST') { // 목록
			itp_fn_fire_window_resize();
			$('#ITP_BTN_STOCKMNG04010_MODIFY').hide();
			$('#ITP_BTN_STOCKMNG04010_CANCELLIST').hide();
			$('#ITP_BTN_STOCKMNG04010_DELETE').hide();
			$('#ITP_BTN_STOCKMNG04010_SAVE').hide();
			
			fn_show_btn_auth('#ITP_BTN_STOCKMNG04010_SRH');
			fn_show_btn_auth('#ITP_BTN_STOCKMNG04010_NEWREG');

			$('#ITP_AJAX_STOCKMNG04010_LIST_CONTAINER').show();
			$('#ITP_AJAX_STOCKMNG04010_DETAIL_CONTAINER').hide();
			$('#ITP_AJAX_STOCKMNG04010_VIEW_CONTAINER').hide();
		} else if(mode == 'ADD') { // 등록
			ITP_FORM_STOCKMNG04010_DETAIL_IS_DUPLICATE = true;

			$('#ITP_BTN_STOCKMNG04010_SRH').hide();
			$('#ITP_BTN_STOCKMNG04010_MODIFY').hide();
			$('#ITP_BTN_STOCKMNG04010_NEWREG').hide();
			$('#ITP_BTN_STOCKMNG04010_DELETE').hide();
			
			fn_show_btn_auth('#ITP_BTN_STOCKMNG04010_CANCELLIST');
			fn_show_btn_auth('#ITP_BTN_STOCKMNG04010_SAVE');

			$('#ITP_AJAX_STOCKMNG04010_LIST_CONTAINER').hide();
			$('#ITP_AJAX_STOCKMNG04010_DETAIL_CONTAINER').show();
			$('#ITP_AJAX_STOCKMNG04010_VIEW_CONTAINER').hide();

			$('#ITP_FORM_STOCKMNG04010_DETAIL_VIEW_CD').val('C');
			$('#ITP_FORM_STOCKMNG04010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
			$('#ITP_FORM_STOCKMNG04010_DETAIL .fnBrandNm').text(fn_make_user_info.get('brandNm'));
			$('#ITP_FORM_STOCKMNG04010_DETAIL .fnWhsId').text('');
			$('#ITP_FORM_STOCKMNG04010_DETAIL .fnRegDtm').text('');
			$('#ITP_FORM_STOCKMNG04010_DETAIL_STORE_ID').val('');
			$('#ITP_FORM_STOCKMNG04010_DETAIL_WHS_ID').val('');
			$('#ITP_FORM_STOCKMNG04010_DETAIL_WHS_ST_CD').val('');
			$('#ITP_FORM_STOCKMNG04010_DETAIL_WHS_NM').val('');
			$('#ITP_FORM_STOCKMNG04010_DETAIL_MGR_TEL_NO').val('');
			$('#ITP_FORM_STOCKMNG04010_DETAIL_MGR_NM').val('');
			$('#ITP_FORM_STOCKMNG04010_DETAIL_STORE_NM').val('');
			$('#ITP_FORM_STOCKMNG04010_DETAIL_ZIPNO').val('');
			$('#ITP_FORM_STOCKMNG04010_DETAIL_ADRES').val('');
			$('#ITP_FORM_STOCKMNG04010_DETAIL_ADRES_DTL').val('');
			
		} else if(mode == 'MODIFY') { // 수정
			$('#ITP_BTN_STOCKMNG04010_SRH').hide();
			$('#ITP_BTN_STOCKMNG04010_MODIFY').hide();
			$('#ITP_BTN_STOCKMNG04010_NEWREG').hide();
			$('#ITP_BTN_STOCKMNG04010_DELETE').hide();
			fn_show_btn_auth('#ITP_BTN_STOCKMNG04010_CANCELLIST');
			fn_show_btn_auth('#ITP_BTN_STOCKMNG04010_SAVE');
			fn_show_btn_auth('#ITP_BTN_STOCKMNG04010_RESET_PW');

			$('#ITP_AJAX_STOCKMNG04010_LIST_CONTAINER').hide();
			$('#ITP_AJAX_STOCKMNG04010_DETAIL_CONTAINER').show();
			$('#ITP_AJAX_STOCKMNG04010_VIEW_CONTAINER').hide();
			// $('#ITP_FORM_STOCKMNG04010_DETAIL_USER_ID').attr('readonly', true);
			// $('#ITP_FORM_STOCKMNG04010_DETAIL_DUP').attr('disabled', true);
			$('#ITP_FORM_STOCKMNG04010_DETAIL_SEARCH_AFFL_SHOP_NM').attr('readonly', true);

			$('#ITP_FORM_STOCKMNG04010_DETAIL_SEARCH_AFFL_SHOP').hide();
			$('#ITP_FORM_STOCKMNG04010_DETAIL_DELETE_AFFL_SHOP').hide();
		} else if(mode == 'VIEW') { // 보기
			$('#ITP_BTN_STOCKMNG04010_SRH').hide();
			$('#ITP_BTN_STOCKMNG04010_NEWREG').hide();
			$('#ITP_BTN_STOCKMNG04010_DELETE').hide();
			$('#ITP_BTN_STOCKMNG04010_SAVE').hide();
			
			fn_show_btn_auth('#ITP_BTN_STOCKMNG04010_MODIFY');
			fn_show_btn_auth('#ITP_BTN_STOCKMNG04010_CANCELLIST');

			$('#ITP_AJAX_STOCKMNG04010_LIST_CONTAINER').hide();
			$('#ITP_AJAX_STOCKMNG04010_DETAIL_CONTAINER').hide();
			$('#ITP_AJAX_STOCKMNG04010_VIEW_CONTAINER').show();
		}
	}
};

/*목록화면 Object*/
let listObj = {
	init: function () {
		this.grid.init();
	},
	itp_STOCKMNG04010_search: false,
	button: {
		search: function() {
			listObj.itp_STOCKMNG04010_search = true;
			let param = $('#ITP_FORM_STOCKMNG04010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.gridPage = $.jgrid.defaults.page;
			$(STOCKMNG04010_GRID_ID).data('grid-param',param);
			$(STOCKMNG04010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
		},
		create: function() {
			pageObj.switchScreen('ADD');			
			itp_fn_form_clear_validate(null, '#ITP_FORM_STOCKMNG04010_DETAIL');						
			// 로케이션 리스트 조회
			createObj.init();
		}
	},
	empty: {
		init: function() {
			var _this = this;
			this.push();
			$(STOCKMNG04010_GRID_EMPTY).on('click', function() {
				_this.back();
				_this.itp_STOCKMNG04010_param.gridSize = $.jgrid.defaults.rowNum;
				$(STOCKMNG04010_GRID_ID).setGridParam({'postData': JSON.stringify(_this.itp_STOCKMNG04010_param)}).trigger('reloadGrid');
			});
		},
		itp_STOCKMNG04010_param: {},
		push: function() {
			let param = $('#ITP_FORM_STOCKMNG04010_SEARCH').serializeObject();
			listObj.itp_STOCKMNG04010_param = param;
		},
		back: function() {
			$('#ITP_FORM_STOCKMNG04010_WHS_DVSN').val(listObj.itp_STOCKMNG04010_param.swhsDvsn);
			$('#ITP_FORM_STOCKMNG04010_WHS_ST_CD').val(listObj.itp_STOCKMNG04010_param.swhsStCd);
			$('#ITP_FORM_STOCKMNG04010_SEARCH_WHS_NM').val(listObj.itp_STOCKMNG04010_param.swhsNm);
		}
	},
	grid: {
		init: function () {
			// 데이터 없을때
			listObj.empty.push();
			this.search();
		},
		colModel: gridColModel.list,
		search: function() {
			pageObj.switchScreen('LIST');
			this.unload();
			this.load();
		},
		load: function() {
			$('#ITP_FORM_STOCKMNG04010_SEARCH_BRAND_ID').val(fn_make_user_info.get('brandId'));
			
			let param = $('#ITP_FORM_STOCKMNG04010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;

			var option = {
				gridId: STOCKMNG04010_GRID_ID,
				colModel: gridColModel.list,
				param: param,
				url: DOMAIN + WHS_MNG_DETAIL_GRID_LIST,
				pager: STOCKMNG04010_GRID_PAGER,
				onCellSelect: function(rowId, cellIdx, cellValue) {
					var cm = $(this).jqGrid('getGridParam', 'colModel');
					var colNm = cm[cellIdx].name;
					if (colNm == 'whsNm') {
						const brandIdVal = $(this).jqGrid('getCell', rowId, 'brandId');
						const whsIdVal = $(this).jqGrid('getCell', rowId, 'whsId');
						
						const key = {brandId: brandIdVal, whsId: whsIdVal, storeId: '', viewCd: 'R'};
						var param = $.param(key);
						viewObj.init(param);
					}
				},
				loadComplete: function(data) {
					console.log(data);
					itp_fn_grid_load_complete(data, STOCKMNG04010_GRID_ID, true, 'number', 'STOCKMNG04010', listObj.itp_STOCKMNG04010_search, listObj.empty, true, data.gridRecords, true);
					var ids = $(STOCKMNG04010_GRID_ID).getDataIDs();
					$.each(ids, function(idx, rowId) {
						$(STOCKMNG04010_GRID_ID).jqGrid('setCell', rowId, 'whsNm', '', ITP_GRID_COL_STYLE.link);
					});
				},
				onPaging: function(action) {
					itp_fn_grid_paging(STOCKMNG04010_GRID_ID, action, param);
				}
			};
			itp_fn_grid_make_remote(option);
		},
		clearData : function() {
			$(STOCKMNG04010_GRID_ID).jqGrid('clearGridData', true);
			$(STOCKMNG04010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(STOCKMNG04010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(STOCKMNG04010_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(STOCKMNG04010_GRID_ID);
		}
	}
};

/*상세화면 Object*/
let viewObj = {
	init: function (param) {
		this.load(param);
	},
	load: function(param) {
		fn_ajax_call(WHS_MNG_INFO_WHS, param, this.callback, 'GET');
	},
	callback: function (result) {
		// console.log(result);
		pageObj.switchScreen('VIEW');
		$('#ITP_FORM_STOCKMNG04010_VIEW #ITP_FORM_STOCKMNG04010_VIEW_BRAND_ID').val(result.brandId);
		$('#ITP_FORM_STOCKMNG04010_VIEW #ITP_FORM_STOCKMNG04010_VIEW_STORE_ID').val(result.storeId);
		$('#ITP_FORM_STOCKMNG04010_VIEW #ITP_FORM_STOCKMNG04010_VIEW_WHS_ID').val(result.whsId);
		
		$('#ITP_FORM_STOCKMNG04010_VIEW .fnBrandNm').text(result.brandNm);
		$('#ITP_FORM_STOCKMNG04010_VIEW .fnStCd').text(result.whsStNm);
		$('#ITP_FORM_STOCKMNG04010_VIEW .fnWhsId').text(result.whsId);
		$('#ITP_FORM_STOCKMNG04010_VIEW .fnWhsNm').text(result.whsNm);
		$('#ITP_FORM_STOCKMNG04010_VIEW .fnTelNo').text(result.mgrTelNo);
		$('#ITP_FORM_STOCKMNG04010_VIEW .fnMgrNm').text(result.mgrNm);		
		$('#ITP_FORM_STOCKMNG04010_VIEW .fnSpplyNm').text(result.storeNm ? result.storeNm : '');		
		$('#ITP_FORM_STOCKMNG04010_VIEW .fnRegDtm').text(result.sysRegDttm);
		if(result.zipNo) {
			$('#ITP_FORM_STOCKMNG04010_VIEW .fnAddress').text('('+result.zipNo+') ' + result.addr1 + ' ' + result.addr2);	
		}		

		// 권한정보 리스트 조회
		viewObj.grid.init();
	},
	grid: {
		init: function() {
			this.unload();
			this.load();
		},
		load : function() {
			let param = $('#ITP_FORM_STOCKMNG04010_VIEW').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.pagingYn = false; // 페이징안함

			var option = {
				gridId: STOCKMNG04010_VIEW_GRID_ID,
				colModel: gridColModel.view,
				param: param,
				url: DOMAIN + WHS_MNG_LOCATION_GRID_LIST,
				pager: STOCKMNG04010_VIEW_GRID_PAGER,
				loadComplete: function(data) {
					itp_fn_grid_load_complete(data, STOCKMNG04010_VIEW_GRID_ID, true, undefined, 'STOCKMNG04010_VIEW');
				},
				onPaging: function(action) {
					var pagingFn = {
						callBack: function(args) {
							$(args).trigger('reloadGrid');
						}
					};
					if (itp_fn_check_grid_is_writing(STOCKMNG04010_VIEW_GRID_ID)) {
						itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, pagingFn, STOCKMNG04010_VIEW_GRID_ID);
						return 'stop';
					} else {
						itp_fn_grid_paging(STOCKMNG04010_VIEW_GRID_ID, action, param);
					}
				},
				onSortCol: function(index, columnIndex, sortOrder) {
					var sortingFn = {
						callBack: function(args) {
							$(args).trigger('reloadGrid');
						}
					};
					if (itp_fn_check_grid_is_writing(STOCKMNG04010_VIEW_GRID_ID)) {
						itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, sortingFn, STOCKMNG04010_VIEW_GRID_ID);
						return 'stop';
					} else {
						itp_fn_grid_sorting(STOCKMNG04010_VIEW_GRID_ID, index, sortOrder);
					}
				}
			};
			itp_fn_grid_make_remote(option);
		},
		clearData : function() {
			$(STOCKMNG04010_VIEW_GRID_ID).jqGrid('clearGridData', true);
			$(STOCKMNG04010_VIEW_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(STOCKMNG04010_VIEW_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(STOCKMNG04010_VIEW_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(STOCKMNG04010_VIEW_GRID_ID);
		}
	}
};

/*수정화면 Object*/
let modifyObj = {
	init: function () {
	},
	button: {
		modify: function (param) {
			var storeId = $('#ITP_FORM_STOCKMNG04010_VIEW_STORE_ID').val();
			if (storeId != '') {
				console.log(fn_make_user_info.get('storeId'));
				if (fn_make_user_info.get('storeId') != storeId) {
					alert("매장 창고는 매장담당자만 수정 가능합니다.");
					return;
				}
			}
			
			itp_fn_form_clear_validate(null, '#ITP_FORM_STOCKMNG04010_DETAIL');
			const brandIdVal = $('#ITP_FORM_STOCKMNG04010_VIEW_BRAND_ID').val();
			const whsIdVal = $('#ITP_FORM_STOCKMNG04010_VIEW_WHS_ID').val();
			const key = {brandId: brandIdVal, whsId: whsIdVal, storeId: '', viewCd: 'R'};
			var param = $.param(key);
			
			console.log(param);
			
			modifyObj.load(param);
		}
	},
	load: function(param) {
		fn_ajax_call(WHS_MNG_INFO_WHS, param, this.callback, 'GET');
	},
	callback: function (result) {
		// console.log(result);
		pageObj.switchScreen('MODIFY');
		$('#ITP_FORM_STOCKMNG04010_DETAIL #ITP_FORM_STOCKMNG04010_DETAIL_VIEW_CD').val('U');
		$('#ITP_FORM_STOCKMNG04010_DETAIL #ITP_FORM_STOCKMNG04010_DETAIL_BRAND_ID').val(result.brandId);
		$('#ITP_FORM_STOCKMNG04010_DETAIL #ITP_FORM_STOCKMNG04010_DETAIL_STORE_ID').val(result.storeId);
		$('#ITP_FORM_STOCKMNG04010_DETAIL #ITP_FORM_STOCKMNG04010_DETAIL_WHS_ID').val(result.whsId);
				
		$('#ITP_FORM_STOCKMNG04010_DETAIL .fnBrandNm').text(result.brandNm);
		$('#ITP_FORM_STOCKMNG04010_DETAIL .fnWhsId').text(result.whsId);
		$('#ITP_FORM_STOCKMNG04010_DETAIL .fnRegDtm').text(result.sysRegDttm);
		
		$('#ITP_FORM_STOCKMNG04010_DETAIL #ITP_FORM_STOCKMNG04010_DETAIL_WHS_ST_CD').val(result.whsStCd);
		$('#ITP_FORM_STOCKMNG04010_DETAIL #ITP_FORM_STOCKMNG04010_DETAIL_WHS_NM').val(result.whsNm);
		$('#ITP_FORM_STOCKMNG04010_DETAIL #ITP_FORM_STOCKMNG04010_DETAIL_WHS_NM').val(result.whsNm);
		$('#ITP_FORM_STOCKMNG04010_DETAIL #ITP_FORM_STOCKMNG04010_DETAIL_MGR_TEL_NO').val(result.mgrTelNo);
		$('#ITP_FORM_STOCKMNG04010_DETAIL #ITP_FORM_STOCKMNG04010_DETAIL_MGR_NM').val(result.mgrNm);
		$('#ITP_FORM_STOCKMNG04010_DETAIL #ITP_FORM_STOCKMNG04010_DETAIL_STORE_NM').val(result.storeNm);
		$('#ITP_FORM_STOCKMNG04010_DETAIL #ITP_FORM_STOCKMNG04010_DETAIL_ZIPNO').val(result.zipNo);
		$('#ITP_FORM_STOCKMNG04010_DETAIL #ITP_FORM_STOCKMNG04010_DETAIL_ADRES').val(result.addr1);
		$('#ITP_FORM_STOCKMNG04010_DETAIL #ITP_FORM_STOCKMNG04010_DETAIL_ADRES_DTL').val(result.addr2);
		
		kakaoMap.init('ITP_FORM_STOCKMNG04010_DETAIL', $('#ITP_FORM_STOCKMNG04010_DETAIL #ITP_FORM_STOCKMNG04010_DETAIL_ADRES').val());
		
		// 권한정보 리스트 조회
		modifyObj.grid.init();
	},
	grid: {
		init: function() {
			this.unload();
			this.load();
		},
		load : function() {
			let param = $('#ITP_FORM_STOCKMNG04010_DETAIL').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.pagingYn = false; // 페이지안함

			var option = {
				gridId: STOCKMNG04010_DETAIL_GRID_ID,
				colModel: gridColModel.detail,
				param: param,
				url: DOMAIN + WHS_MNG_LOCATION_GRID_LIST,
				pager: STOCKMNG04010_DETAIL_GRID_PAGER,
				multiselect: true,
				rownumbers: false,
				onCellSelect: function(rowId, cellIdx, cellValue) {
					STOCKMNG04010_DETAIL_GRID_LAST_ROW_ID = itp_fn_set_data_editable(rowId, cellIdx, STOCKMNG04010_DETAIL_GRID_LAST_ROW_ID, STOCKMNG04010_DETAIL_GRID_ID, ['location']);
		   		},
				loadComplete: function(data) {
					itp_fn_grid_load_complete(data, STOCKMNG04010_DETAIL_GRID_ID, true, undefined, 'STOCKMNG04010_DETAIL');
				},
				onPaging: function(action) {
					var pagingFn = {
						callBack: function(args) {
							$(args).trigger('reloadGrid');
						}
					};
					if (itp_fn_check_grid_is_writing(STOCKMNG04010_DETAIL_GRID_ID)) {
						itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, pagingFn, STOCKMNG04010_DETAIL_GRID_ID);
						return 'stop';
					} else {
						itp_fn_grid_paging(STOCKMNG04010_DETAIL_GRID_ID, action, param);
					}
				},
				onSortCol: function(index, columnIndex, sortOrder) {
					var sortingFn = {
						callBack: function(args) {
							$(args).trigger('reloadGrid');
						}
					};
					if (itp_fn_check_grid_is_writing(STOCKMNG04010_DETAIL_GRID_ID)) {
						itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, sortingFn, STOCKMNG04010_DETAIL_GRID_ID);
						return 'stop';
					} else {
						itp_fn_grid_sorting(STOCKMNG04010_DETAIL_GRID_ID, index, sortOrder);
					}
				}
			};
			itp_fn_grid_make_remote(option);
		},
		clearData : function() {
			$(STOCKMNG04010_DETAIL_GRID_ID).jqGrid('clearGridData', true);
			$(STOCKMNG04010_DETAIL_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(STOCKMNG04010_DETAIL_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(STOCKMNG04010_DETAIL_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(STOCKMNG04010_DETAIL_GRID_ID);
		}
	}
};

/*신규화면 Object*/
let createObj = {
	init: function () {
		kakaoMap.init('ITP_FORM_STOCKMNG04010_DETAIL', '');
		this.unload();
		this.load();
	},
	button: {
		cancel: function() {
			listObj.grid.search();
		},
		save: function() {
			$('body').trigger('click');
			
			var detailViewCd = $('#ITP_FORM_STOCKMNG04010_DETAIL_VIEW_CD').val();
			const formId = '#ITP_FORM_STOCKMNG04010_DETAIL';
			itp_fn_form_clear_validate(null, formId);
			
			var vali_whsStatCd = itp_fn_form_validate(formId, formId + '_WHS_ST_CD', ['empty'], undefined); // 상태
			var vali_whsNm = itp_fn_form_validate(formId, formId + '_WHS_NM', ['empty'], undefined);
			
			if (vali_whsStatCd && vali_whsNm) {
				let gridInsertData = [];
				let gridUpdateData = [];
				let gridDeleteData = [];
				const rowData = $(STOCKMNG04010_DETAIL_GRID_ID).getRowData();
				// const keyValue = $(formId + '_WHS_ID').val();
				$.each(rowData, function(key, value) {
					if (value.viewCd != 'R') {
						// value.whsId = keyValue;
						if (value.viewCd == 'C') {
							gridInsertData.push(value);
						} else if (value.viewCd == 'U') {
							gridUpdateData.push(value);
						} else if (value.viewCd == 'D') {
							gridDeleteData.push(value);
						}
					}
				});
				let param = $(formId).serializeObject();
				param.gridInsertData = gridInsertData;
				param.gridUpdateData = gridUpdateData;
				param.gridDeleteData = gridDeleteData;
				//console.log(JSON.stringify(param));
				var searhFn = function() {
					ITP_FORM_STOCKMNG04010_DETAIL_IS_WRITING = false;
					listObj.grid.search();
				};
				fn_ajax_call(detailViewCd == 'C' ? WHS_MNG_ADD_WHS : WHS_MNG_MIDIFY_WHS, JSON.stringify(param), searhFn, 'POST');
			}
		}, 
		address: function() {
			kakaoMap.address('ITP_FORM_STOCKMNG04010_DETAIL');
		},
		popupStore: function() { // 매장선택 팝업
			
		}, 
		eraseStore: function() { // 매장선택 삭제
			$('#ITP_FORM_STOCKMNG04010_DETAIL_STORE_ID').val('');
			$('#ITP_FORM_STOCKMNG04010_DETAIL_STORE_NM').val('');
		}
	},
	load: function () {
		let param = $('#ITP_FORM_STOCKMNG04010_DETAIL').serializeObject();
		param.gridSize = $.jgrid.defaults.rowNum;
		param.pagingYn = false; // 페이지안함

		var option = {
			gridId: STOCKMNG04010_DETAIL_GRID_ID,
			colModel: gridColModel.detail,
			param: param,
			url: DOMAIN + WHS_MNG_LOCATION_GRID_LIST,
			pager: STOCKMNG04010_DETAIL_GRID_PAGER,
			multiselect: true,
			loadComplete: function(data) {
				itp_fn_grid_load_complete(data, STOCKMNG04010_DETAIL_GRID_ID, true, undefined, 'STOCKMNG04010_DETAIL');
			},
			onPaging: function(action) {
				var pagingFn = {
					callBack: function(args) {
						$(args).trigger('reloadGrid');
					}
				};
				if (itp_fn_check_grid_is_writing(STOCKMNG04010_DETAIL_GRID_ID)) {
					itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, pagingFn, STOCKMNG04010_DETAIL_GRID_ID);
					return 'stop';
				} else {
					itp_fn_grid_paging(STOCKMNG04010_DETAIL_GRID_ID, action, param);
				}
			},
			onSortCol: function(index, columnIndex, sortOrder) {
				var sortingFn = {
					callBack: function(args) {
						$(args).trigger('reloadGrid');
					}
				};
				if (itp_fn_check_grid_is_writing(STOCKMNG04010_DETAIL_GRID_ID)) {
					itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, sortingFn, STOCKMNG04010_DETAIL_GRID_ID);
					return 'stop';
				} else {
					itp_fn_grid_sorting(STOCKMNG04010_DETAIL_GRID_ID, index, sortOrder);
				}
			}
		};
		itp_fn_grid_make_remote(option);
	},
	clearData : function() {
		$(STOCKMNG04010_DETAIL_GRID_ID).jqGrid('clearGridData', true);
		$(STOCKMNG04010_DETAIL_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
		$(STOCKMNG04010_DETAIL_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
		$(STOCKMNG04010_DETAIL_GRID_EMPTY).show();
	},
	unload : function() {
		$.jgrid.gridUnload(STOCKMNG04010_DETAIL_GRID_ID);
	}
};

/*로케이션정보 Object*/
let locInfoObj = {
	init: function () {
		// if(AUTH_TYPE_CD === '10' || AUTH_TYPE_CD === '20') {
		this.action();
		// } else {
		// this.make();
		// }
	},
	button: {
		addRow: function() {
			// $(STOCKMNG04010_DETAIL_GRID_ID).jqGrid('addRow', {position: 'last'});
			// $(STOCKMNG04010_DETAIL_GRID_EMPTY).hide();
			STOCKMNG04010_DETAIL_GRID_LAST_ROW_ID = itp_fn_add_grid_row(STOCKMNG04010_DETAIL_GRID_ID, undefined, ['location'],'STOCKMNG04010_DETAIL');
		},
		delRow: function() {
			itp_fn_grid_del_row(STOCKMNG04010_DETAIL_GRID_ID);
		}
	},
	make: function() {
		$('#ITP_FORM_STOCKMNG04010_SEARCH_AFFL_SHOP').hide();
		$('#ITP_FORM_STOCKMNG04010_DELETE_AFFL_SHOP').hide();
		if(AFFL_SHOP_LIST) {
			$('#ITP_FORM_STOCKMNG04010_SEARCH_AFFL_SHOP_ID').val(AFFL_SHOP_LIST[0].afflShopId);
			$('#ITP_FORM_STOCKMNG04010_SEARCH_AFFL_SHOP_NM').val(AFFL_SHOP_LIST[0].afflShopNm);
		}
		if(AFFL_BRAND_LIST) {
			fn_make_select_brand(AFFL_BRAND_LIST, 'ITP_FORM_STOCKMNG04010_SEARCH_BRAND_ID');
		}
	},
	action: function () {
		var _this = this;
		// 로케이션정보 조회 버튼 클릭
		$('#ITP_FORM_STOCKMNG04010_SEARCH_AFFL_SHOP').on('click', function() {
			_this.popup('L');
		});
		// 로케이션정보 텍스트 삭제 버튼 클릭
		$('#ITP_FORM_STOCKMNG04010_DELETE_AFFL_SHOP').on('click', function() {
			$('#ITP_FORM_STOCKMNG04010_SEARCH_AFFL_SHOP_NM').val('');
			$('select#ITP_FORM_STOCKMNG04010_SEARCH_BRAND_ID option').remove();
		});
		// 신규등록 로케이션정보 조회 버튼 클릭
		$('#ITP_FORM_STOCKMNG04010_DETAIL_SEARCH_AFFL_SHOP').one('click', function() {
			_this.popup('C');
		});
		// 신규등록 로케이션정보 텍스트 삭제 버튼 클릭
		$('#ITP_FORM_STOCKMNG04010_DETAIL_DELETE_AFFL_SHOP').one('click', function() {
			$('#ITP_FORM_STOCKMNG04010_DETAIL_SEARCH_AFFL_SHOP_NM').val('');
			$('select#ITP_FORM_STOCKMNG04010_DETAIL_SEARCH_BRAND_ID option').remove();
		});
	},
	popup: function (view) {
		// 팝업
		var popFn = (view === 'C') ? this.callback.create : this.callback.list;
		fn_call_popup('biz', 'BIZPOP_AFFL_SHOP', '#ITP_ASIDE', popFn, null, 'S');
	},
	callback: {
		list: function(rowDataPop) {
			// console.log(rowDataPop);
			if(rowDataPop.AFFL_SHOP) {
				$('#ITP_FORM_STOCKMNG04010_SEARCH_AFFL_SHOP_ID').val(rowDataPop.AFFL_SHOP.afflShopId);
				$('#ITP_FORM_STOCKMNG04010_SEARCH_AFFL_SHOP_NM').val(rowDataPop.AFFL_SHOP.afflShopNm);
			}
			if(rowDataPop.AFFL_BRAND) {
				fn_make_select_brand(rowDataPop.AFFL_BRAND, 'ITP_FORM_STOCKMNG04010_SEARCH_BRAND_ID', true, '선택');
			}
		},
		create: function(rowDataPop) {
			// console.log(rowDataPop);
			if(rowDataPop.AFFL_SHOP) {
				// $('#ITP_FORM_STOCKMNG04010_DETAIL_SEARCH_AFFL_SHOP_ID').val(rowDataPop.AFFL_SHOP.afflShopId);
				// $('#ITP_FORM_STOCKMNG04010_DETAIL_SEARCH_AFFL_SHOP_NM').val(rowDataPop.AFFL_SHOP.afflShopNm);
			}
			if(rowDataPop.AFFL_BRAND) {
				fn_make_select_brand(rowDataPop.AFFL_BRAND, 'ITP_FORM_STOCKMNG04010_DETAIL_SEARCH_BRAND_ID', true, '선택');
			}
		}
	}
};