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

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

const OPER01010_GRID_ID = '#ITP_OPER01010_jqGrid';
const OPER01010_GRID_LIST = '#ITP_OPER01010_jqGrid_list';
const OPER01010_GRID_PAGER = '#ITP_OPER01010_jqGridPager';
const OPER01010_GRID_EMPTY = '#ITP_OPER01010_jqGridEmpty';
/*
const OPER01010_VIEW_GRID_ID = '#ITP_OPER01010_VIEW_jqGrid';
const OPER01010_VIEW_GRID_LIST = '#ITP_OPER01010_VIEW_jqGrid_list';
const OPER01010_VIEW_GRID_PAGER = '#ITP_OPER01010_VIEW_jqGridPager';
const OPER01010_VIEW_GRID_EMPTY = '#ITP_OPER01010_VIEW_jqGridEmpty';

const OPER01010_DETAIL_GRID_ID = '#ITP_OPER01010_DETAIL_jqGrid';
const OPER01010_DETAIL_GRID_LIST = '#ITP_OPER01010_DETAIL_jqGrid_list';
const OPER01010_DETAIL_GRID_PAGER = '#ITP_OPER01010_DETAIL_jqGridPager';
const OPER01010_DETAIL_GRID_EMPTY = '#ITP_OPER01010_DETAIL_jqGridEmpty';

const OPER01010_NEW_GRID_ID = '#ITP_OPER01010_NEW_jqGrid';
const OPER01010_NEW_GRID_LIST = '#ITP_OPER01010_NEW_jqGrid_list';
const OPER01010_NEW_GRID_PAGER = '#ITP_OPER01010_NEW_jqGridPager';
const OPER01010_NEW_GRID_EMPTY = '#ITP_OPER01010_NEW_jqGridEmpty';
*/
let ITP_FORM_OPER01010_DETAIL_IS_DUPLICATE = false;
let ITP_FORM_OPER01010_DETAIL_IS_WRITING = false;

let API_DETAIL_GRID_LIST 	= '/api/user/detail-grid-list';
let API_USERAUTH_GRID_LIST 	= '/api/user/userauth-grid-list';
let API_DETAIL_INFO 		= '/api/user/info-user';
let API_DETAIL_SAVE 		= '/api/user/save-user';
let API_CHECK_DUPLICATE 	= '/api/user/check-duplicate';
let API_INTI_USERPW 		= '/api/user/inti-userpw';
let API_POP_GRID_LIST 		= '/api/brand/pop-grid-list';
 
$(window).on('resize', function(e) {
	setTimeout(()=>{
		const left=$(OPER01010_GRID_PAGER+'_left'), pageing=$(OPER01010_GRID_PAGER+'_center').find('table');
		left.width(60);
		pageing.width(150);
	}, 100);
});
 
/*화면 Grid ColModel*/
const gridColModel = {
	list: [
		{
			index: 'AFFL_SHOP_NM', name: 'afflShopNm',
			label: ITP_MSG_LOCALE.label.afflShopNm, //브랜드그룹
			width: '10', fixed: false, align: 'left',
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'BRAND_NM', name: 'brandNm',
			label: ITP_MSG_LOCALE.label.brandNm, //브랜드
			width: '13', fixed: false, align: 'left',
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'AUTH_TP_NM', name: 'authTpNm',
			label: ITP_MSG_LOCALE.label.authTpNm, //권한명
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'SHOP_NM', name: 'storeId',
			label: ITP_MSG_LOCALE.label.storeId, //매장아이디
			width: '10', fixed: false, align: 'left',
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: true
		},
		{
			index: 'STORE_NM', name: 'storeNm',
			label: ITP_MSG_LOCALE.label.storeNm, //매장명
			width: '13', fixed: false, align: 'left',
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'SPPLY_ID', name: 'spplyId',
			label: ITP_MSG_LOCALE.label.spplyId, //공급사아이디
			width: '10', fixed: false, align: 'left',
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: true, classes: 'ui-ellipsis'
		},
		{
			index: 'SPPLY_NM', name: 'spplyNm',
			label: ITP_MSG_LOCALE.label.spplyNm, //공급사명
			width: '13', fixed: false, align: 'left',
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
		},
		{
			index: 'AUTH_TP_CD', name: 'authTpCd',
			label: ITP_MSG_LOCALE.label.authTpNm, //권한코드
			width: '13', fixed: false, align: 'left',
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: true
		},
		{
			index: 'USER_ID', name: 'userId',
			label: ITP_MSG_LOCALE.label.userId, //사용자ID
			width: '10', fixed: false, align: 'center',
			minwidth: 120, maxwidth: 200,
			sortable: false, hidden: false
		},
		{
			index: 'USER_NM', name: 'userNm',
			label: ITP_MSG_LOCALE.label.userName, //닉네임
			width: '13', fixed: false, align: 'left',
			minwidth: 120,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'EMAIL', name: 'email',
			label: ITP_MSG_LOCALE.label.idEmail, //아이디(이메일)
			width: '15', fixed: false, align: 'left',
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'TEL_NO', name: 'telNo',
			label: ITP_MSG_LOCALE.label.telephone, //휴대폰 번호
			width: '10', fixed: false, align: 'center',
			minwidth: 150, maxwidth: 250,
			sortable: false, hidden: false
		},
		{
			index: 'USER_STAT_NM', name: 'userStatNm',
			label: ITP_MSG_LOCALE.label.status, //상태
			width: '6', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'USER_STAT_DT', name: 'userStatDt',
			label: ITP_MSG_LOCALE.label.statusDt, //상태일시
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'ADD_DT', name: 'addDt',
			label: ITP_MSG_LOCALE.label.regDt, //등록일
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'LAST_LOGIN_DT', name: 'lastLoginDt',
			label: ITP_MSG_LOCALE.label.lastLogin, //마지막 로그인
			width: '12', fixed: false, align: 'center',
			minwidth: 150, maxwidth: 250,
			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: 'left',
			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: 'left',
			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();
		afflShopObj.init();
	},
	pageMode: '',
	ui: {
		init: function () {
			this.button();
			this.form();
			this.ready();
		},
		button: function () {
			// 버튼 권한설정
			fn_proc_btn_auth('OPER01010');

			// 공통코드 표시
			$('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_OPER01010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_OPER01010_SEARCH_FROM_DT').datepicker('setDate', new Date(now.setMonth(now.getMonth() - 1)));
			$('#ITP_FORM_OPER01010_SEARCH_TO_DT').datepicker('setDate', new Date());
		},
		form: function() {
			itp_fn_form_event.onKeyup('#ITP_FORM_OPER01010_DETAIL');
			itp_fn_form_event.onKeyup('#ITP_FORM_OPER01010_NEW');
		},
		afflInfo: {
			init: function() {
				this.authTpCd 		= fn_make_user_info.get('authTpCd');
				if(this.authTpCd === '30' || this.authTpCd === '40') {
					this.afflShopId 	= fn_make_user_info.get('afflShopId');
					this.afflShopNm 	= fn_make_user_info.get('afflShopNm');
					this.brandId 		= fn_make_user_info.get('brandId');
					this.brandNm 		= fn_make_user_info.get('brandNm');
					this.setAffl();
				}
			},
			setAffl: function() {
				var _this = this;
				var afflShopId = '#ITP_FORM_OPER01010_' + (pageObj.pageMode === 'ADD' ? 'DETAIL' : 'SEARCH') + '_AFFL_SHOP_ID';
				var afflShopNm = '#ITP_FORM_OPER01010_' + (pageObj.pageMode === 'ADD' ? 'DETAIL' : 'SEARCH') + '_AFFL_SHOP_NM';
				//var brandId = '#ITP_FORM_OPER01010_' + (pageObj.pageMode === 'ADD' ? 'DETAIL' : 'SEARCH') + '_BRAND_ID';
				$(afflShopId).val(_this.afflShopId);
				$(afflShopNm).val(_this.afflShopNm);
				//$(brandId).val(_this.brandId);
				
				$(afflShopNm).attr('readonly', true);
				
				
				if(_this.authTpCd === '30') {
					var callbackFn = (result) => {
						var brandList = [];
						$.each(result.gridRows, function(i, item) {
							brandList.push({'brandId': item.brandId, 'brandNm': item.brandNm});
						});
						_this.setBrand(brandList);
					};
					var param = {
						'afflShopId': _this.afflShopId
					};
					fn_ajax_call(API_POP_GRID_LIST, JSON.stringify(param), callbackFn, 'POST');
				} else {
					var brandList = [{'brandId': _this.brandId, 'brandNm': _this.brandNm}];
					this.setBrand(brandList);
				}
			},
			setBrand: function(AFFL_BRAND) {
				var _this = this;
				if(AFFL_BRAND.length > 0) {
					var brandId = 'ITP_FORM_OPER01010_' + (pageObj.pageMode === 'ADD' ? 'DETAIL_BRAND_ID' : 'SEARCH_BRAND_ID');
					(_this.authTpCd === '30') ?  fn_make_select_brand(AFFL_BRAND, brandId, true, '선택') : fn_make_select_brand(AFFL_BRAND, brandId, false);
					$('#' + brandId).val(_this.brandId);
				} else {
				}
			}
		},
		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_OPER01010_SRH'				: listObj.button.search();		break;	// 조회 버튼 클릭
						case 'ITP_BTN_OPER01010_NEWREG'				: listObj.button.create(); 		break;	// 신규등록 버튼
						case 'ITP_BTN_OPER01010_MODIFY'				: modifyObj.button.modify();	break;	// 수정 버튼
						case 'ITP_BTN_OPER01010_CANCELLIST'			: createObj.button.cancel(); 	break;	// 취소/목록 버튼
						case 'ITP_BTN_OPER00210_DELETE'				: 								break;	// 삭제 버튼
						case 'ITP_BTN_OPER01010_SAVE'				: createObj.button.save(); 		break;	// 저장 버튼
						case 'ITP_FORM_OPER01010_DETAIL_DUP'		: createObj.button.duplicate();	break;	// 중복체크
						case 'ITP_BTN_OPER01010_NEW_RESET_PW'		: createObj.button.resetPass();	break;	// 상세:비밀번호 초기화
						//case 'ITP_BTN_OPER01010_DETAIL_ADDROW'		: pageObj.grid.button.addRow();	break;	// 상세:추가버튼
						//case 'ITP_BTN_OPER01010_DETAIL_DELROW'		: pageObj.grid.button.delRow();	break;	// 상세:삭제버튼
						//case 'ITP_BTN_OPER01010_NEW_ADDROW'			: pageObj.grid.button.addRow();	break;	// 상세:추가버튼
						//case 'ITP_BTN_OPER01010_NEW_DELROW'			: pageObj.grid.button.delRow();	break;	// 상세:삭제버튼
					}
				});
			});

		}
	},
	switchScreen: function(mode) {
		pageObj.pageMode = mode;
		$('.itp_det_head').find('button[id^="ITP_BTN_OPER01010_"]').each(function(i) {
			$(this).hide();
		});
		$('#ITP_TAB_OPER01010').find('div[id$="_CONTAINER"]').each(function(i) {
			$(this).hide();
		});
		if(mode === 'LIST') { // 목록
			itp_fn_fire_window_resize();
			$('#ITP_AJAX_OPER01010_LIST_CONTAINER').show();
			if(fn_make_user_info.get('authTpCd') === '10') {
				$('#ITP_FORM_OPER01010_SEARCH_AFFL_SHOP_NM').attr('readonly', false);
			} else {
				pageObj.ui.afflInfo.init();
				$('#ITP_FORM_OPER01010_SEARCH_AFFL_SHOP_ID').val(fn_make_user_info.get('afflShopId'));
				$('#ITP_FORM_OPER01010_SEARCH_AFFL_SHOP_NM').val(fn_make_user_info.get('afflShopNm'));
				$('#ITP_FORM_OPER01010_SEARCH_AFFL_SHOP_NM').attr('readonly', true);
			}
			fn_show_btn_auth_array(['#ITP_BTN_OPER01010_SRH', '#ITP_BTN_OPER01010_NEWREG']);
		} else if(mode == 'ADD') { // 등록
			$('#ITP_AJAX_OPER01010_DETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_OPER01010_CANCELLIST', '#ITP_BTN_OPER01010_SAVE']);

			$('#ITP_FORM_OPER01010_DETAIL').find('input, textarea, select, checkbox').each(function(i, elem) {
				$(this).attr('id') === 'ITP_FORM_OPER01010_DETAIL_VIEW_CD' ? $(this).val('C') : $(this).val('');
				if(elem.type === 'select') {
					$(this).val('').prop('selected', true);
				}
			});
			//console.log(fn_make_user_info.get('afflShopId]'));
			//$('#ITP_FORM_OPER01010_DETAIL_AFFL_SHOP_ID').val(fn_make_user_info.get('afflShopId]'));
			//$('#ITP_FORM_OPER01010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId]'));
			
			if (fn_make_user_info.get('authTpCd') == "10" || fn_make_user_info.get('authTpCd') == "20") {
				$('#ITP_FORM_OPER01010_DETAIL_DELETE_AFFL_SHOP').attr("disabled", false);
				$('#ITP_FORM_OPER01010_DETAIL_AFFL_SHOP').attr("disabled", false);
			} else if (fn_make_user_info.get('authTpCd') == "30") {
				$('#ITP_FORM_OPER01010_DETAIL_DELETE_AFFL_SHOP').attr("disabled", true);
				$('#ITP_FORM_OPER01010_DETAIL_AFFL_SHOP').attr("disabled", true);
				$('#ITP_FORM_OPER01010_DETAIL_BRAND_ID').attr("disabled", false);
			} else {
				$('#ITP_FORM_OPER01010_DETAIL_DELETE_AFFL_SHOP').attr("disabled", true);
				$('#ITP_FORM_OPER01010_DETAIL_AFFL_SHOP').attr("disabled", true);
				$('#ITP_FORM_OPER01010_DETAIL_BRAND_ID').attr("disabled", true);
			}
			pageObj.ui.afflInfo.init();
		} else if(mode === 'MODIFY') { // 수정
			$('#ITP_AJAX_OPER01010_NEW_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_OPER01010_CANCELLIST', '#ITP_BTN_OPER01010_SAVE', '#ITP_BTN_OPER01010_RESET_PW']);
			// $('#ITP_FORM_OPER01010_DETAIL #ITP_FORM_OPER01010_DETAIL_AFFL_SHOP_NM').prop('readonly', true);
		} else if(mode === 'VIEW') { // 보기
			$('#ITP_AJAX_OPER01010_VIEW_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_OPER01010_MODIFY', '#ITP_BTN_OPER01010_CANCELLIST']);
		}
	}
};

/*목록화면 Object*/
let listObj = {
	init: function () {
		// Grid
		this.grid.init();
	},
	itp_OPER01010_param: {},
	itp_OPER01010_search: false,
	button: {
		search: function () {
			listObj.itp_OPER01010_search = true;
			let param = $('#ITP_FORM_OPER01010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.gridPage = $.jgrid.defaults.page;
			$(OPER01010_GRID_ID).data('grid-param',param);
			$(OPER01010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
		},
		create:  function () {
			pageObj.switchScreen('ADD');
			itp_fn_form_clear_validate(null, '#ITP_FORM_OPER01010_DETAIL');
			createObj.init();
		}
	},
	grid: {
		init: function() {
			// 데이터 없을때
			this.itp_fn_OPER01010_empty.push();
			itp_fn_jqgrid_resize(OPER01010_GRID_ID, OPER01010_GRID_LIST, 'lg'); 
			this.search();
		},
		colModel: gridColModel.list,
		search: function() {
			pageObj.switchScreen('LIST');
			this.unload();
			this.load();
		},
		load: function() {
			var _this = this;
			let param = $('#ITP_FORM_OPER01010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			var option = {
				gridId: OPER01010_GRID_ID,
				colModel: this.colModel,
				param: param,
				url: DOMAIN + API_DETAIL_GRID_LIST,
				pager: OPER01010_GRID_PAGER,
				autowidth: true,
				shrinkToFit :false,
				onCellSelect: function(rowId, cellIdx, cellValue) {
					var cm = $(this).jqGrid('getGridParam', 'colModel');
					var colNm = cm[cellIdx].name;
					if (colNm === 'userNm') {
						const selectVal = $(this).jqGrid('getCell', rowId, 'userId');
						const key = {userId: selectVal, viewCd: 'R'};
						var param = $.param(key);
						viewObj.init(param);
					}
				},
				loadComplete: function(data) {
					console.log(data);
					itp_fn_grid_load_complete(data, OPER01010_GRID_ID, true, 'number', 'OPER01010', listObj.itp_OPER01010_search, _this.itp_fn_OPER01010_empty, true, data.gridRecords, true);
					var ids = $(OPER01010_GRID_ID).getDataIDs();
					$.each(ids, function(idx, rowId) {
						$(OPER01010_GRID_ID).jqGrid('setCell', rowId, 'userNm', '', ITP_GRID_COL_STYLE.link);
					});
				}
			};
			itp_fn_grid_make_remote(option);
		},
		itp_fn_OPER01010_empty: {
			push: function() {
				let param = $('#ITP_FORM_OPER01010_SEARCH').serializeObject();
				listObj.itp_OPER01010_param = param;
			},
			back: function() {
				$('#ITP_FORM_OPER01010_DEVI_TP_CD').val(listObj.itp_OPER01010_param.safflShopId);
				$('#ITP_FORM_OPER01010_ORDER_YN').val(listObj.itp_OPER01010_param.safflShopNm);
				$('#ITP_FORM_OPER01010_ADT_AUTH_CD').val(listObj.itp_OPER01010_param.sbrandId);
				$('#ITP_FORM_OPER01010_EMAIL_RCV_YN').val(listObj.itp_OPER01010_param.snticeTitl);
				$('#ITP_FORM_OPER01010_FROM_DT').val(listObj.itp_OPER01010_param.fromDt);
				$('#ITP_FORM_OPER01010_TO_DT').val(listObj.itp_OPER01010_param.toDt);
			}
		},
		reload : function() {
			$(OPER01010_GRID_EMPTY).off('click').on('click', function() {
				_this.itp_fn_OPER01010_empty.back();
				listObj.itp_OPER01010_param.gridSize = $.jgrid.defaults.rowNum;
				$(OPER01010_GRID_ID).setGridParam({'postData': JSON.stringify(listObj.itp_OPER01010_param)}).trigger('reloadGrid');
			});
		},
		clearData : function() {
			$(OPER01010_GRID_ID).jqGrid('clearGridData', true);
			$(OPER01010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(OPER01010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(OPER01010_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(OPER01010_GRID_ID);
		}
	}
};

/*상세화면 Object*/
let viewObj = {
	init: function (param) {
		this.load(param);
	},
	button: {
	},
	load: function(param) {
		fn_ajax_call(API_DETAIL_INFO, param, this.callback, 'GET');
	},
	callback: function (result) {
		console.log(result);
		pageObj.switchScreen('VIEW');
		$('#ITP_FORM_OPER01010_VIEW #ITP_FORM_OPER01010_VIEW_USER_ID').val(result.userId);
		$('#ITP_FORM_OPER01010_VIEW .fnAfflShopNm').text(result.afflShopNm);
		$('#ITP_FORM_OPER01010_VIEW .fnBrandNm').text(result.brandNm);
		$('#ITP_FORM_OPER01010_VIEW .fnUserNm').text(result.userNm);
		$('#ITP_FORM_OPER01010_VIEW .fnAuthTpNm').text(result.authTpNm);
		$('#ITP_FORM_OPER01010_VIEW .fnUserStatNm').text(result.userStatNm);
		$('#ITP_FORM_OPER01010_VIEW .fnUserId').text(result.userId);
		$('#ITP_FORM_OPER01010_VIEW .fnEmail').text(result.email);
		$('#ITP_FORM_OPER01010_VIEW .fnTelNo').text(result.telNo);
		$('#ITP_FORM_OPER01010_VIEW .fnAddDt').text(result.addDt);
		$('#ITP_FORM_OPER01010_VIEW .fnLastLoginDt').text(result.lastLoginDt);
		$('#ITP_FORM_OPER01010_VIEW .fnStoreNm').text(result.storeNm);
		$('#ITP_FORM_OPER01010_VIEW .fnSpplyNm').text(result.spplyNm);
		
		
		// 권한정보 리스트 조회
		//pageObj.grid.init('VIEW');
	}
};

/*수정화면 Object*/
let modifyObj = {
	init: function (param) {
	},
	button: {
		modify: function () {
			itp_fn_form_clear_validate(null, '#ITP_FORM_OPER01010_NEW');
			const userIdVal = $('#ITP_FORM_OPER01010_VIEW_USER_ID').val();
			const key = {userId: userIdVal, 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_OPER01010_NEW #ITP_FORM_OPER01010_NEW_VIEW_CD').val('U');
		$('#ITP_FORM_OPER01010_NEW #ITP_FORM_OPER01010_NEW_AFFL_SHOP_ID').val(result.afflShopId);
		$('#ITP_FORM_OPER01010_NEW .fnAfflShopNm').text(result.afflShopNm);
		$('#ITP_FORM_OPER01010_NEW .fnBrandNm').text(result.brandNm);
		$('#ITP_FORM_OPER01010_NEW #ITP_FORM_OPER01010_NEW_BRAND_ID').val(result.brandId);
		$('#ITP_FORM_OPER01010_NEW #ITP_FORM_OPER01010_NEW_USER_NM').val(result.userNm);
		$('#ITP_FORM_OPER01010_NEW #ITP_FORM_OPER01010_NEW_USER_STAT_CD').val(result.userStatCd);
		$('#ITP_FORM_OPER01010_NEW #ITP_FORM_OPER01010_NEW_USER_ID').val(result.userId);
		$('#ITP_FORM_OPER01010_NEW .fnBrandKey').text(result.brandKey);
		$('#ITP_FORM_OPER01010_NEW .fnUserId').text(result.userId);
		$('#ITP_FORM_OPER01010_NEW #ITP_FORM_OPER01010_NEW_USER_PW').val(result.userPw);
		$('#ITP_FORM_OPER01010_NEW #ITP_FORM_OPER01010_NEW_TEL_NO').val(result.telNo);
		$('#ITP_FORM_OPER01010_NEW #ITP_FORM_OPER01010_NEW_EMAIL').val(result.email);
		$('#ITP_FORM_OPER01010_NEW .fnAddDt').text(result.userStatDt);
		
		$('#ITP_FORM_OPER01010_NEW .fnAuthTpNm').text(result.authTpNm);
		$('#ITP_FORM_OPER01010_NEW .fnStoreNm').text(result.storeNm);
		$('#ITP_FORM_OPER01010_NEW .fnSpplyNm').text(result.spplyNm);
		
		// $('#ITP_FORM_OPER01010_NEW .fnAddDt').text(result.addDt);
		// $('#ITP_FORM_OPER01010_NEW .fnLastLoginDt').text(result.lastLoginDt);
		//ITP_FORM_OPER01010_DETAIL_USER_ID

		// 리스트 조회
		//pageObj.grid.init('NEW');
	}
};

/*신규화면 Object*/
let createObj = {
	init: function () {
	},
	button: {
		duplicate: function() {
			const formId = '#ITP_FORM_OPER01010_DETAIL';
			itp_fn_form_clear_validate(null, formId);
			var vali_userId = itp_fn_form_validate(formId, formId + '_USER_ID', ['empty'], undefined);
			if (vali_userId) {
				var dupFn = function(result) {
					if (result.code == 'N') {
						ITP_FORM_OPER01010_DETAIL_IS_DUPLICATE = false;
					} else {
						ITP_FORM_OPER01010_DETAIL_IS_DUPLICATE = true;
					}
				};
				const key = {'userId':  $(formId + '_USER_ID').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_OPER01010_NEW_USER_ID').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);
		},
		save: function() {
			const formId = (pageObj.pageMode === 'MODIFY') ? '#ITP_FORM_OPER01010_NEW' : '#ITP_FORM_OPER01010_DETAIL';
			itp_fn_form_clear_validate(null, formId);

			if (itp_fn_form_event.isValid(formId)) {
				if($(formId + ' #ITP_FORM_OPER01010_DETAIL_USER_PW').val() !== $(formId + ' #ITP_FORM_OPER01010_DETAIL_USER_PW_CONFIRM').val()) {
					var element = '#ITP_FORM_OPER01010_DETAIL_USER_PW';
					$(element).closest('.itp_ip').addClass('has-error');
					var error_msg = $('<span class="help-block">' + ITP_MSG_LOCALE.message.form.wrongPwd + '</span>');
					error_msg.insertAfter($(element));
					return;
				}
				if (ITP_FORM_OPER01010_DETAIL_IS_DUPLICATE) {
					itp_fn_modal_duplicate(ITP_MSG_LOCALE.label.userId, formId + '_USER_ID');
				} else {
					
					let param = $(formId).serializeObject();
					
					if (fn_make_user_info.get('authTpCd') == "40") {
						param.brandId = fn_make_user_info.get('brandId');
					}
					
					var searhFn = function() {
						ITP_FORM_OPER01010_DETAIL_IS_WRITING = false;
						listObj.grid.search();
					};
					fn_ajax_call(API_DETAIL_SAVE, JSON.stringify(param), searhFn, 'POST');
				}
			} else {
				console.log('유효성 실패');
			}
		},
		cancel: function() {
			listObj.grid.search();
		}
	}
};

/*브랜드그룹 Object*/
let afflShopObj = {
	init: function () {
		this.action();
	},
	action: function () {
		var _this = this;
		// 브랜드그룹 조회 버튼 클릭
		$('#ITP_FORM_OPER01010_SEARCH_AFFL_SHOP').on('click', function() {
			_this.popup('L');
		});
		// 브랜드그룹 텍스트 삭제 버튼 클릭
		$('#ITP_FORM_OPER01010_DELETE_AFFL_SHOP').on('click', function() {
			$('#ITP_FORM_OPER01010_SEARCH_AFFL_SHOP_NM').val('');
			$('select#ITP_FORM_OPER01010_SEARCH_BRAND_ID option').remove();
		});
		// 신규등록 브랜드그룹 조회 버튼 클릭
		$('#ITP_FORM_OPER01010_DETAIL_AFFL_SHOP').on('click', function() {
			_this.popup('C');
		});
		// 신규등록 브랜드그룹 텍스트 삭제 버튼 클릭
		$('#ITP_FORM_OPER01010_DETAIL_DELETE_AFFL_SHOP').on('click', function() {
			$('#ITP_FORM_OPER01010_DETAIL_AFFL_SHOP_NM').val('');
			$('select#ITP_FORM_OPER01010_DETAIL_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_OPER01010_SEARCH_AFFL_SHOP_ID').val(rowDataPop.AFFL_SHOP.afflShopId);
				$('#ITP_FORM_OPER01010_SEARCH_AFFL_SHOP_NM').val(rowDataPop.AFFL_SHOP.afflShopNm);
			}
			if(rowDataPop.AFFL_BRAND) {
				fn_make_select_brand(rowDataPop.AFFL_BRAND, 'ITP_FORM_OPER01010_SEARCH_BRAND_ID', true, '선택');
			}
		},
		create: function(rowDataPop) {
			console.log(rowDataPop);
			if(rowDataPop.AFFL_SHOP) {
				$('#ITP_FORM_OPER01010_DETAIL_AFFL_SHOP_ID').val(rowDataPop.AFFL_SHOP.afflShopId);
				$('#ITP_FORM_OPER01010_DETAIL_AFFL_SHOP_NM').val(rowDataPop.AFFL_SHOP.afflShopNm);
			}
			if(rowDataPop.AFFL_BRAND) {
				fn_make_select_brand(rowDataPop.AFFL_BRAND, 'ITP_FORM_OPER01010_DETAIL_BRAND_ID', true, '선택');
			}
		}
	}
};
