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

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

/*화면 변수*/
const OPER03010_GRID_ID = '#ITP_OPER03010_jqGrid';
const OPER03010_GRID_LIST = '#ITP_OPER03010_jqGrid_list';
const OPER03010_GRID_PAGER = '#ITP_OPER03010_jqGridPager';
const OPER03010_GRID_EMPTY = '#ITP_OPER03010_jqGridEmpty';

const OPER03010_VIEW_GRID_ID = '#ITP_OPER03010_VIEW_jqGrid';
const OPER03010_VIEW_GRID_LIST = '#ITP_OPER03010_VIEW_jqGrid_list';
const OPER03010_VIEW_GRID_PAGER = '#ITP_OPER03010_VIEW_jqGridPager';
const OPER03010_VIEW_GRID_EMPTY = '#ITP_OPER03010_VIEW_jqGridEmpty';

const OPER03010_DETAIL_GRID_ID = '#ITP_OPER03010_DETAIL_jqGrid';
const OPER03010_DETAIL_GRID_LIST = '#ITP_OPER03010_DETAIL_jqGrid_list';
const OPER03010_DETAIL_GRID_PAGER = '#ITP_OPER03010_DETAIL_jqGridPager';
const OPER03010_DETAIL_GRID_EMPTY = '#ITP_OPER03010_DETAIL_jqGridEmpty';

let ITP_FORM_OPER03010_DETAIL_IS_DUPLICATE = false;
let ITP_FORM_OPER03010_DETAIL_IS_WRITING = false;

/*API URL*/
let API_DETAIL_GRID_LIST = '/api/affl/detail-grid-list';
let API_DETAIL_INFO = '/api/affl/info-affl';
let COMPANY_INFO_COMPANY = '/api/affl/info-affl';
let API_DETAIL_SAVE = '/api/affl/save-affi';
let API_CHECK_DUPLICATE = '/api/affl/check-duplicate';
let API_INTI_USERPW = '/api/user/inti-userpw';

/*화면 Grid ColModel*/
const gridColModel = {
	// 목록
	list: [
		{
			index: 'AFFL_SHOP_ID', name: 'afflShopId',
			label: '가맹점코드',
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'AFFL_SHOP_NM', name: 'afflShopNm',
			label: '가맹점명',
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'CORP_TYPE_NM', name: 'corpTypeNm',
			label: '기업형태',
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'ST_NM', name: 'stNm',
			label: '기업상태',
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'OWNER_NM', name: 'ownerNm',
			label: '대표자명',
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'OWNER_TEL', name: 'ownerTel',
			label: '대표연락처',
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'JOIN_DT', name: 'joinDt',
			label: '가입일자',
			width: '8', 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: 'SALE_MGR_ID', name: 'saleMgrId',
			label: '영업담당자ID',
			width: '20', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text',
			editrules: {required: true}
		},
		{
			index: 'SALE_MGR_NM', name: 'saleMgrNm',
			label: '영업담당자명',
			width: '30', fixed: false, align: 'center',
			sortable: true, editable: false, edittype: 'text',
			editrules: {required: true}
		},
		{
			index: 'RPRST_TEL_NO', name: 'rprstTelNo',
			label: ITP_MSG_LOCALE.label.rprstTelNo,
			width: '10', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'RPRST_EMAIL', name: 'rprstEmai',
			label: ITP_MSG_LOCALE.label.rprstEmal,
			width: '10', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'ST_CD', name: 'stCd',
			label: ITP_MSG_LOCALE.label.status,
			width: '10', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text', hidden: true
		},
		{
			index: 'ST_NM', name: 'stNm',
			label: ITP_MSG_LOCALE.label.status,
			width: '10', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'AFFL_SHOP_ID', name: 'afflShopId',
			label: '가맹점코드',
			width: '10', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text', hidden: true
		}
	]
};

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

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

			// 조회일자 지정
			var now = new Date();
			$('#ITP_TAB_OPER03010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_OPER03010_SEARCH_FROM_DT').datepicker('setDate', new Date(now.setMonth(now.getMonth() - 1)));
			$('#ITP_FORM_OPER03010_SEARCH_TO_DT').datepicker('setDate', new Date());

			// form alert message 주입
			$('input[data-alert-required="ONLYCHK"]').each(function() {
				if($(this).data('alert-msg') === 'address') {
					$(this).data('alert-msg', ITP_MSG_LOCALE.message.form.emptyAddr);
				}
			});
		},
		form: function() {
			itp_fn_form_event.onKeyup('#ITP_FORM_OPER03010_DETAIL');
		},
		grid: function() {
			itp_fn_jqgrid_resize(OPER03010_GRID_ID, OPER03010_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_OPER03010_SRH'					: listObj.button.search();		break;	// 조회 버튼 클릭
						case 'ITP_BTN_OPER03010_NEWREG'					: listObj.button.create(); 		break;	// 신규등록 버튼
						case 'ITP_BTN_OPER03010_MODIFY'					: modifyObj.button.modify();	break;	// 수정 버튼
						case 'ITP_BTN_OPER03010_CANCELLIST'				: createObj.button.cancel(); 	break;	// 취소/목록 버튼
						case 'ITP_BTN_OPER03010_DELETE'					: 								break;	// 삭제 버튼
						case 'ITP_BTN_OPER03010_SAVE'					: createObj.button.save(); 		break;	// 저장 버튼
						case 'ITP_BTN_OPER03010_DETAIL_AFFL_SHOP_KEY'	: createObj.button.duplicate();	break;	// 중복체크
						case 'ITP_BTN_OPER03010_RESET_PW'				: createObj.button.resetPass();	break;	// 상세:비밀번호 초기화
						case 'ITP_FORM_OPER03010_DETAIL_ADRES_SCH'		: createObj.button.address();	break;	// 신규등록 주소 찾기
						case 'ITP_BTN_OPER03010_DETAIL_ADDROW'			: pageObj.grid.button.addRow();	break;	// 신규등록 영업담당자 행 추가
						case 'ITP_BTN_OPER03010_DETAIL_DELROW'			: pageObj.grid.button.delRow();	break;	// 상세:삭제버튼
					}
				});
			});

			$('body').on('click', function(e) {
				// afflUserObj.grid.resetGrid(e);
			});

		}
	},
	fileUpload: function() {
		//파일업로드(수정화면)
		var id = 'OPER03010';
		$('#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_OPER03010_"]').each(function(i) {
			$(this).hide();
		});
		$('#ITP_TAB_OPER03010').find('div[id$="_CONTAINER"]').each(function(i) {
			$(this).hide();
		});
		if(mode === 'LIST') { // 목록
			$('#ITP_AJAX_OPER03010_LIST_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_OPER03010_SRH', '#ITP_BTN_OPER03010_NEWREG']);
		} else if(mode == 'ADD') { // 등록
			$('#ITP_AJAX_OPER03010_DETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_OPER03010_CANCELLIST', '#ITP_BTN_OPER03010_SAVE']);

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

			$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_FILE_NO').val('');
			$('#ITP_FORM_OPER03010_DETAIL_AFFL_SHOP_KEY').removeAttr('readonly');
			$('#ITP_BTN_OPER03010_DETAIL_AFFL_SHOP_KEY').removeAttr('disabled');
			$('.fnAfflShopId').text('(자동부여)');
			$('#ITP_FORM_OPER03010_DETAIL .itp_form_info').hide();

		} else if(mode === 'MODIFY') { // 수정
			$('#ITP_AJAX_OPER03010_DETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_OPER03010_CANCELLIST', '#ITP_BTN_OPER03010_SAVE', '#ITP_BTN_OPER03010_RESET_PW']);

			// $('#ITP_FORM_OPER03010_DETAIL_AFFL_SHOP_NM').prop('readonly', true);
			$('#ITP_FORM_OPER03010_DETAIL_AFFL_SHOP_KEY').prop('readonly', true);
			$('#ITP_BTN_OPER03010_DETAIL_AFFL_SHOP_KEY').prop('disabled', true);
			$('#ITP_FORM_OPER03010_DETAIL .itp_form_info').show();

		} else if(mode === 'VIEW') { // 보기
			$('#ITP_AJAX_OPER03010_VIEW_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_OPER03010_MODIFY', '#ITP_BTN_OPER03010_CANCELLIST']);
		}
	},
	grid: {
		init: function(mode, gridData) {
			this.mode = mode;
			this.gridId = (mode === 'VIEW') ? OPER03010_VIEW_GRID_ID : OPER03010_DETAIL_GRID_ID;
			this.unload();
			this.load(gridData);
		},
		mode: 'DETAIL',
		gridId: '',
		button: {
			addRow: function () {
				// 팝업
				var popFn = function(rowDataPop) {
					console.log(rowDataPop);
					// 기존등록 데이터
					var exists = '';
					const rowData = $(OPER03010_DETAIL_GRID_ID).getRowData();
					$.each(rowData, function(key, value) {
						exists = exists + value.saleMgrId + ';';
					});

					$.each(rowDataPop, function(key, value) {
						if(exists.indexOf(value.saleMgrId) < 0) {
							value['viewCd'] =  "C";
							$(OPER03010_DETAIL_GRID_ID).jqGrid('addRowData', value.saleMgrId, value, 'last');
						}
					});
					$(OPER03010_DETAIL_GRID_EMPTY).hide();
				};
				fn_call_popup('biz', 'BIZPOP_APPL_SALES_USER_SCH', '#ITP_ASIDE', popFn, null, 'S');
			},
			delRow: function () {
				itp_fn_grid_del_row(OPER03010_DETAIL_GRID_ID);
			}
		},
		load: function(gridData) {
			var _this = this;
			var option = {
				gridId: this.gridId,
				colModel: gridColModel.detail,
				data: gridData,
				multiselect: (this.mode !== 'VIEW'),
				loadComplete: function(data) {
					$(_this.mode === 'VIEW' ? OPER03010_VIEW_GRID_LIST : OPER03010_DETAIL_GRID_LIST).find('.ui-jqgrid .ui-jqgrid-bdiv').css('overflow-x', 'hidden');
					if(data.records === 0) {
						_this.clearData();
						var gridEmpty = (this.mode === 'VIEW') ? OPER03010_VIEW_GRID_EMPTY : OPER03010_DETAIL_GRID_EMPTY;
						$(gridEmpty).html(ITP_MSG_LOCALE.message.grid.noData);
						$(gridEmpty).show();
					}
				}
			};
			itp_fn_grid_make_local(option);
		},
		clearData : function() {
			$(this.gridId).jqGrid('clearGridData', true);
			$(this.mode === 'VIEW' ? OPER03010_VIEW_GRID_LIST : OPER03010_DETAIL_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(this.mode === 'VIEW' ? OPER03010_VIEW_GRID_EMPTY : OPER03010_DETAIL_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(this.mode === 'VIEW' ? OPER03010_VIEW_GRID_EMPTY : OPER03010_DETAIL_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(this.gridId);
		}
	}
};

/*목록화면 Object*/
let listObj = {
	init: function () {
		this.grid.init();
	},
	itp_OPER03010_search: false,
	button: {
		search: function() {
			listObj.itp_OPER03010_search = true;
			let param = $('#ITP_FORM_OPER03010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.gridPage = $.jgrid.defaults.page;
			$(OPER03010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
		},
		create: function() {
			pageObj.switchScreen('ADD');
			itp_fn_form_clear_validate(null, '#ITP_FORM_OPER03010_DETAIL');
			// 권한정보 리스트 조회
			createObj.init();
		}
	},
	empty: {
		init: function() {
			var _this = this;
			this.push();
			$(OPER03010_GRID_EMPTY).on('click', function() {
				_this.back();
				_this.itp_OPER03010_param.gridSize = $.jgrid.defaults.rowNum;
				$(OPER03010_GRID_ID).setGridParam({'postData': JSON.stringify(_this.itp_OPER03010_param)}).trigger('reloadGrid');
			});
		},
		itp_OPER03010_param: {},
		push: function() {
			let param = $('#ITP_FORM_OPER03010_SEARCH').serializeObject();
			pageObj.itp_OPER03010_param = param;
		},
		back: function() {
			$('#ITP_FORM_OPER03010_SEARCH_FROM_DT').val(pageObj.itp_OPER03010_param.fromDt);
			$('#ITP_FORM_OPER03010_SEARCH_TO_DT').val(pageObj.itp_OPER03010_param.toDt);
			$('#ITP_FORM_OPER03010_SEARCH_AFFL_SHOP_NM').val(pageObj.itp_OPER03010_param.safflShopNm);
			$('#ITP_FORM_OPER03010_SEARCH_ST_CD').val(pageObj.itp_OPER03010_param.sstCd);
		}
	},
	grid: {
		init: function () {
			// 데이터 없을때
			listObj.empty.push();
			itp_fn_jqgrid_resize(OPER03010_GRID_ID, OPER03010_GRID_LIST, 'lg');
			itp_fn_fire_window_resize();
			this.reload();
			this.search();
		},
		colModel: gridColModel.list,
		search: function() {
			pageObj.switchScreen('LIST');
			this.unload();
			this.load();
		},
		load: function() {
			var _this = this;
			let param = $('#ITP_FORM_OPER03010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			var option = {
				gridId: OPER03010_GRID_ID,
				colModel: gridColModel.list,
				param: param,
				url: DOMAIN + API_DETAIL_GRID_LIST,
				pager: OPER03010_GRID_PAGER,
				onCellSelect: function(rowId, cellIdx, cellValue) {
					var cm = $(this).jqGrid('getGridParam', 'colModel');
					var colNm = cm[cellIdx].name;
					if (colNm == 'afflShopNm') {
						const afflCdVal = $(this).jqGrid('getCell', rowId, 'afflShopId');
						const key = {afflShopId: afflCdVal, viewCd: 'R'};
						var param = $.param(key);
						viewObj.init(param);
					}
				},
				loadComplete: function(data) {
					console.log(data);
					itp_fn_grid_load_complete(data, OPER03010_GRID_ID, true, 'number', 'OPER03010', pageObj.itp_OPER03010_search, listObj.empty, true, data.gridRecords, true);
					var ids = $(OPER03010_GRID_ID).getDataIDs();
					$.each(ids, function(idx, rowId) {
						$(OPER03010_GRID_ID).jqGrid('setCell', rowId, 'afflShopNm', '', ITP_GRID_COL_STYLE.link);
					});
				}
			};
			itp_fn_grid_make_remote(option);
		},
		reload : function() {
			var _this = this;
			$(OPER03010_GRID_EMPTY).off('click').on('click', function() {
				pageObj.itp_OPER03010_param.gridSize = $.jgrid.defaults.rowNum;
				$(OPER03010_GRID_ID).setGridParam({'postData': JSON.stringify(pageObj.itp_OPER03010_param)}).trigger('reloadGrid');
			});
		},
		clearData : function() {
			$(OPER03010_GRID_ID).jqGrid('clearGridData', true);
			$(OPER03010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(OPER03010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(OPER03010_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(OPER03010_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_OPER03010_VIEW #ITP_FORM_OPER03010_VIEW_AFFL_SHOP_ID').val(result.afflShopId);

		$('#ITP_FORM_OPER03010_VIEW .fnAfflShopId').text(result.afflShopId);
		$('#ITP_FORM_OPER03010_VIEW .fnAfflShopNm').text(result.afflShopNm);
		$('#ITP_FORM_OPER03010_VIEW .fnAfflAbbr').text(result.afflAbbr);
		$('#ITP_FORM_OPER03010_VIEW .fnStCd').text(result.stNm);
		$('#ITP_FORM_OPER03010_VIEW .fnCorpTypeNm').text(result.authTypeNm);
		$('#ITP_FORM_OPER03010_VIEW .fnBsnsRegNo').text(result.bsnsRegNo);
		$('#ITP_FORM_OPER03010_VIEW .fnCorpRegNo').text(result.corpRegNo);
		$('#ITP_FORM_OPER03010_VIEW .fnTelNo').text(result.ownerTelNo);
		$('#ITP_FORM_OPER03010_VIEW .fnFaxNo').text(result.rprstFaxNo);
		$('#ITP_FORM_OPER03010_VIEW .fnEmalAddr').text(result.rprstEmai);
		$('#ITP_FORM_OPER03010_VIEW .fnBsnsCndt').text(result.bsnsCndt);
		$('#ITP_FORM_OPER03010_VIEW .fnOwnerNm').text(result.ownerNm);
		$('#ITP_FORM_OPER03010_VIEW .fnCtgrBsns').text(result.ctgrBsns);
		$('#ITP_FORM_OPER03010_VIEW .fnOwnerTelNo').text(result.ownerTelNo);
		$('#ITP_FORM_OPER03010_VIEW .fnJoinSttDt').text(result.joinSttDt);
		$('#ITP_FORM_OPER03010_VIEW .fnJoinEndDt').text(result.joinEndDt);
		$('#ITP_FORM_OPER03010_VIEW .fnZipNo').text(result.zipNo);
		$('#ITP_FORM_OPER03010_VIEW .fnLoadAddr').text(result.addr1);
		$('#ITP_FORM_OPER03010_VIEW .fnDtlAddr').text(result.addr2);

		let locate = result.addr1 + ' ' + result.addr2;
		kakaoMap.init('ITP_FORM_OPER03010_VIEW', locate);

		// 영업담당자정보  리스트 조회
		pageObj.grid.init('VIEW', result.saleList);
	}
};

/*수정화면 Object*/
let modifyObj = {
	button: {
		modify: function (param) {
			itp_fn_form_clear_validate(null, '#ITP_FORM_OPER03010_DETAIL');
			const afflShopIdVal = $('#ITP_FORM_OPER03010_VIEW_AFFL_SHOP_ID').val();
			const key = {afflShopId: afflShopIdVal, 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_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_VIEW_CD').val('U');

		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_AFFL_SHOP_ID').val(result.afflShopId);
		$('#ITP_FORM_OPER03010_DETAIL .fnAfflShopId').text(result.afflShopId);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_AFFL_SHOP_NM').val(result.afflShopNm);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_AFFL_SHOP_KEY').val(result.afflAbbr);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_ST_CD').val(result.stCd);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_AUTH_TYPE').val(result.authType);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_CORP_TYPE').val(result.corpType);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_BSNS_REG_NO').val(result.bsnsRegNo);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_CORP_REG_NO').val(result.corpRegNo);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_TEL_NO').val(result.ownerTelNo);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_FAX_NO').val(result.rprstFaxNo);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_EMAL_ADDR').val(result.rprstEmai);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_BSNS_CNDT').val(result.bsnsCndt);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_OWNER_NM').val(result.ownerNm);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_CTGR_BSNS').val(result.ctgrBsns);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_OWNER_TEL_NO').val(result.ownerTelNo);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_SEARCH_JOIN_STT_DT').val(result.joinSttDt);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_SEARCH_JOIN_END_DT').val(result.joinEndDt);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_ZIPNO').val(result.zipNo);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_ADRES').val(result.addr1);
		$('#ITP_FORM_OPER03010_DETAIL #ITP_FORM_OPER03010_DETAIL_ADRES_DTL').val(result.addr2);

		$('#ITP_FORM_OPER03010_DETAIL .fnAddDt').text(result.regDt);
		$('#ITP_FORM_OPER03010_DETAIL .fnChgDt').text(result.chgDt);

		kakaoMap.init('ITP_FORM_OPER03010_DETAIL', $('#ITP_FORM_OPER03010_DETAIL_ADRES').val());

		// 권한정보 리스트 조회
		pageObj.grid.init('MODIFY', result.saleList);
	}
};

/*신규화면 Object*/
let createObj = {
	init: function () {
		kakaoMap.init('ITP_FORM_OPER03010_DETAIL', '');
		pageObj.grid.init('DETAIL', []);
	},
	button: {
		duplicate: function() {
			const formId = '#ITP_FORM_OPER03010_DETAIL';
			itp_fn_form_clear_validate(null, formId);
			if (itp_fn_form_validate(formId, formId + '_AFFL_SHOP_KEY', ['empty'], undefined)) {
				var dupFn = function(result) {
					if (result.code === 'N') {
						ITP_FORM_OPER03010_DETAIL_IS_DUPLICATE = false;
					} else {
						ITP_FORM_OPER03010_DETAIL_IS_DUPLICATE = true;
					}
				};
				const key = {'afflAbbr':  $(formId + '_AFFL_SHOP_KEY').val()};
				const param = $.param(key);
				fn_ajax_call(API_CHECK_DUPLICATE, param, dupFn, 'GET');
			}
		},
		resetPass: function() {
			var resetPwFn = {
				callBack: function(args) {
					const key = {'userId':  $('#ITP_FORM_OPER03010_DETAIL_CMPY_CD').val()};
					const param = $.param(key);
					fn_ajax_call(API_INTI_USERPW, param, undefined, 'GET');
				}
			};
			itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.askResetUserPw, resetPwFn, null);
		},
		address: function() {
			kakaoMap.address('ITP_FORM_OPER03010_DETAIL');
		},
		cancel: function() {
			listObj.grid.search();
		},
		save: function() {
			const formId = '#ITP_FORM_OPER03010_DETAIL';
			itp_fn_form_event.removeChar('#ITP_FORM_OPER03010_DETAIL');
			itp_fn_form_clear_validate(null, formId);

			if (itp_fn_form_event.isValid(formId)) {

				if($('#ITP_FORM_OPER03010_DETAIL_CMPY_ZIPNO').val() === '' || $('#ITP_FORM_OPER03010_DETAIL_CMPY_ADRES').val() === '') {
					itp_fn_modal_alert('주소 검색을 통해 회사 주소를 입력하세요.');
					return;
				}

				if (ITP_FORM_OPER03010_DETAIL_IS_DUPLICATE) {
					itp_fn_modal_duplicate('회사코드 중복체크를 해주세요.', formId + '_AFFL_SHOP_ID');
				} else {
					let gridInsertData = [];
					let gridUpdateData = [];
					let gridDeleteData = [];
					const rowData = $(OPER03010_DETAIL_GRID_ID).getRowData();
					const keyValue = $(formId + '_AFFL_SHOP_ID').val();
					$.each(rowData, function(key, value) {
						if (value.viewCd !== 'R') {
							value.afflShopId = 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_OPER03010_DETAIL_IS_WRITING = false;
						listObj.grid.search();
					};
					fn_ajax_call(API_DETAIL_SAVE, JSON.stringify(param), searhFn, 'POST');
				}
			}
		}
	}
};
