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

const OPER02010_GRID_ID = '#ITP_OPER02010_jqGrid';
const OPER02010_GRID_LIST = '#ITP_OPER02010_jqGrid_list';
const OPER02010_GRID_PAGER = '#ITP_OPER02010_jqGridPager';
const OPER02010_GRID_EMPTY = '#ITP_OPER02010_jqGridEmpty';

// const OPER02010_VIEW_GRID_ID = '#ITP_OPER02010_VIEW_jqGrid';
// const OPER02010_VIEW_GRID_LIST = '#ITP_OPER02010_VIEW_jqGrid_list';
// const OPER02010_VIEW_GRID_PAGER = '#ITP_OPER02010_VIEW_jqGridPager';
// const OPER02010_VIEW_GRID_EMPTY = '#ITP_OPER02010_VIEW_jqGridEmpty';
//
// const OPER02010_DETAIL_GRID_ID = '#ITP_OPER02010_DETAIL_jqGrid';
// const OPER02010_DETAIL_GRID_LIST = '#ITP_OPER02010_DETAIL_jqGrid_list';
// const OPER02010_DETAIL_GRID_PAGER = '#ITP_OPER02010_DETAIL_jqGridPager';
// const OPER02010_DETAIL_GRID_EMPTY = '#ITP_OPER02010_DETAIL_jqGridEmpty';

let ITP_FORM_OPER02010_DETAIL_IS_WRITING = false;

let NTICE_SAVE_NTICE = '/api/ntice/save-ntice';
let NTICE_DETAIL_GRID_LIST = '/api/ntice/detail-grid-list';
let NTICE_INFO_NTICE = '/api/ntice/info-ntice';
let NTICE_TARGET_GRID_LIST = '/api/ntice/target-grid-list';
let BRAND_POP_GRID_LIST = '/api/brand/pop-grid-list';


//공지사항 텍스트 제한
$(document).ready(function() {
	$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_NTICE_DESC').on('keyup', function() {
		$('#ITP_FORM_OPER02010_DETAIL_NTICE_DESC_CNT').html('(' + $(this).val().length + ' / 999)');
		console.log($(this).val().length);
		if ($(this).val().length > 999) {
			$(this).val($(this).val().substring(0, 999));
			$('#ITP_FORM_OPER02010_DETAIL_NTICE_DESC_CNT').html('(999 / 999)');
		}
	});
});

// Grid Column Model
const gridColModel = {
	// 목록
	list: [
		{
			index: 'NTICE_NO', name: 'nticeNo',
			label: ITP_MSG_LOCALE.label.notiNo, //공지번호
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'NTICE_TITL', name: 'nticeTitl',
			label: ITP_MSG_LOCALE.label.titlet, //공지제목
			width: '15', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'AFFL_SHOP_NM', name: 'afflShopNm',
			label: ITP_MSG_LOCALE.label.afflShopNm, //브랜드그룹
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			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: 'TARGET_NM', name: 'targetNm',
			label: ITP_MSG_LOCALE.label.targetNm, //공지대상
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'NTICE_STAT_NM', name: 'nticeStatNm',
			label: ITP_MSG_LOCALE.label.status, //상태
			width: '7', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'NOTI_ST_DAY', name: 'notiStDay',
			label: ITP_MSG_LOCALE.label.viewDt, //노출일
			width: '12', 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
		}
	]/*,
	detail: [
		{
			index: 'VIEW_CD', name: 'viewCd',
			label: ITP_MSG_LOCALE.label.viewCd,
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'NTICE_NO', name: 'nticeNo',
			label: ITP_MSG_LOCALE.label.notiNo,
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'BRAND_ID', name: 'brandId',
			label: ITP_MSG_LOCALE.label.brandId,
			width: '20', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'BRAND_NM', name: 'brandNm',
			label: ITP_MSG_LOCALE.label.brandNm,
			width: '30', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		}
	],
	brand: [
		{
			index: 'VIEW_CD', name: 'viewCd',
			label: ITP_MSG_LOCALE.label.viewCd,
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'BRAND_ID', name: 'brandId',
			label: ITP_MSG_LOCALE.table.brandId,
			width: '20', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'BRAND_NM', name: 'brandNm',
			label: ITP_MSG_LOCALE.table.brandNm,
			width: '30', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		}
	]*/
};

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

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

			// 공통코드 표시
			$('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_OPER02010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_OPER02010_SEARCH_FROM_DT').datepicker('setDate', new Date());
		},
		afflInfo: {
			init: function() {
				this.authTpCd = fn_make_user_info.get('authTpCd');
				if(this.authTpCd === '30' || this.authTpCd === '40' || this.authTpCd === '50') {
					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();
				}
				if (this.authTpCd === '40' || this.authTpCd === '50') {
					$('#ITP_FORM_OPER02010_SEARCH_AFFL_SHOP_BTN').attr('disabled', true);
					$('#ITP_FORM_OPER02010_DELETE_AFFL_SHOP_BTN').attr('disabled', true);
				}
				if (this.authTpCd === '50') {
					$('#ITP_BTN_OPER02010_NEWREG').hide();					
				}
			},
			setAffl: function() {
				var _this = this;
				var afflShopId = '#ITP_FORM_OPER02010_' + (pageObj.pageMode === 'LIST' ? 'SEARCH' : 'DETAIL') + '_AFFL_SHOP_ID';
				var afflShopNm = '#ITP_FORM_OPER02010_' + (pageObj.pageMode === 'LIST' ? 'SEARCH' : 'DETAIL') + '_AFFL_SHOP_NM';
				// var afflShopId = '#ITP_FORM_OPER02010_' + (pageObj.pageMode === 'ADD' ? 'DETAIL' : 'SEARCH') + '_AFFL_SHOP_ID';
				// var afflShopNm = '#ITP_FORM_OPER02010_' + (pageObj.pageMode === 'ADD' ? 'DETAIL' : 'SEARCH') + '_AFFL_SHOP_NM';
				$(afflShopId).val(_this.afflShopId);
				$(afflShopNm).val(_this.afflShopNm);
				$(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(BRAND_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_OPER02010_' + (pageObj.pageMode === 'LIST' ? 'SEARCH' : 'DETAIL') + '_BRAND_ID';
					(_this.authTpCd === '30') ?  fn_make_select_brand(AFFL_BRAND, brandId, true, '선택') : fn_make_select_brand(AFFL_BRAND, brandId, false);
				} else {
				}
			}
		},
		ready: function () {
			listObj.init();
			// 브랜드그룹 조회
			afflShopObj.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_OPER02010_SRH'				: listObj.button.search();		break;	// 조회 버튼 클릭
						case 'ITP_BTN_OPER02010_NEWREG'				: listObj.button.create(); 		break;	// 신규등록 버튼
						case 'ITP_BTN_OPER02010_MODIFY'				: modifyObj.button.modify();	break;	// 수정 버튼
						case 'ITP_BTN_OPER02010_CANCELLIST'			: createObj.button.cancel(); 	break;	// 취소/목록 버튼
						case 'ITP_BTN_OPER00210_DELETE'				: 								break;	// 삭제 버튼
						case 'ITP_BTN_OPER02010_SAVE'				: createObj.button.save(); 		break;	// 저장 버튼
						// case 'ITP_BTN_OPER02010_DETAIL_ADDROW'		: pageObj.grid.button.addRow();	break;	// 상세:추가버튼
						// case 'ITP_BTN_OPER02010_DETAIL_DELROW'		: pageObj.grid.button.delRow();	break;	// 상세:삭제버튼
					}
				});
			});

		},
		notiTarget: function(isDisabled) {
			$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_TARGET_CD').val('A');
			$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_TARGET_CD').attr('disabled', isDisabled);
		}
	},
	fileUpload: function() {
		//파일업로드(등록/수정화면)
		var id = 'OPER02010';
		$('#ITP_' + id + '_DETAIL_UPLOAD').empty();
		$('#ITP_' + id + '_DETAIL_LOGO_FILE').empty();
		var removeFn = (param) => {
			$('#ITP_' + id + '_DETAIL_LOGO_FILE').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, removeFn);
	},
	switchScreen: function(mode) {
		pageObj.pageMode = mode;
		$('.itp_det_head').find('button[id^="ITP_BTN_OPER02010_"]').each(function(i) {
			$(this).hide();
		});
		$('#ITP_TAB_OPER02010').find('div[id$="_CONTAINER"]').each(function(i) {
			$(this).hide();
		});
		if(mode === 'LIST') { // 목록
			$('#ITP_AJAX_OPER02010_LIST_CONTAINER').show();
			$('#ITP_FORM_OPER02010_DELETE_AFFL_SHOP_BTN').show();
			fn_show_btn_auth_array(['#ITP_BTN_OPER02010_SRH', '#ITP_BTN_OPER02010_NEWREG']);
			pageObj.ui.afflInfo.init();
		} else if(mode == 'ADD') { // 등록
			$('#ITP_AJAX_OPER02010_DETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_OPER02010_CANCELLIST', '#ITP_BTN_OPER02010_SAVE']);

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

			$('#ITP_FORM_OPER02010_FROM_DT').datepicker('setDate', new Date());
			$('#ITP_FORM_OPER02010_TO_DT').datepicker('setDate', new Date());

			$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_AFFL_SHOP_ID').val('');
			$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_AFFL_SHOP_NM').val('').prop('readonly', true);
			$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_SEARCH_BRAND_ID option').remove();
			$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_FILE_NO').val('');

			pageObj.ui.afflInfo.init();
			pageObj.event.notiTarget(fn_make_user_info.get('authTpCd') === '10');
		} else if(mode === 'MODIFY') { // 수정
			$('#ITP_AJAX_OPER02010_DETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_OPER02010_CANCELLIST', '#ITP_BTN_OPER02010_SAVE', '#ITP_BTN_OPER02010_RESET_PW']);
			$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_AFFL_SHOP_NM').prop('readonly', true);
			if(fn_make_user_info.get('authTpCd') === '30') {
				pageObj.ui.afflInfo.init();
			}
		} else if(mode === 'VIEW') { // 보기
			$('#ITP_AJAX_OPER02010_VIEW_CONTAINER').show();
		}
	}
};

/*목록화면 Object*/
let listObj = {
	init: function () {
		// Grid
		this.grid.init();
	},
	button: {
		search: function () {
			listObj.grid.itp_OPER02010_search = true;
			let param = $('#ITP_FORM_OPER02010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.gridPage = $.jgrid.defaults.page;
			console.log(JSON.stringify(param));
			$(OPER02010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
		},
		create:  function () {
			pageObj.switchScreen('ADD');
			itp_fn_form_clear_validate(null, '#ITP_FORM_OPER02010_DETAIL');
			createObj.init();
		}
	},
	grid: {
		init: function() {
			// 데이터 없을때
			this.itp_fn_OPER02010_empty.push();
			itp_fn_jqgrid_resize(OPER02010_GRID_ID, OPER02010_GRID_LIST, 'lg');
			// itp_fn_fire_window_resize();
			this.search();
		},
		itp_OPER02010_param: {},
		itp_OPER02010_search: false,
		colModel: gridColModel.list,
		search: function() {
			pageObj.switchScreen('LIST');
			this.unload();
			this.load();
		},
		load: function() {
			var _this = this;
			let param = $('#ITP_FORM_OPER02010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			var option = {
				gridId: OPER02010_GRID_ID,
				colModel: this.colModel,
				param: param,
			    autowidth:true,    
		        shrinkToFit:false,
				url: DOMAIN + NTICE_DETAIL_GRID_LIST,
				pager: OPER02010_GRID_PAGER,
				onCellSelect: function(rowId, cellIdx, cellValue) {
					var cm = $(this).jqGrid('getGridParam', 'colModel');
					var colNm = cm[cellIdx].name;
					if (colNm == 'nticeTitl') {
						const nticeNoVal = $(this).jqGrid('getCell', rowId, 'nticeNo');
						const key = {nticeNo: nticeNoVal, viewCd: 'R'};
						var param = $.param(key);
						viewObj.init(param);
					}
				},
				loadComplete: function(data) {
					itp_fn_grid_load_complete(data, OPER02010_GRID_ID, true, 'number', 'OPER02010', listObj.grid.itp_OPER02010_search, _this.itp_fn_OPER02010_empty, true, data.gridRecords, true);
					var ids = $(OPER02010_GRID_ID).getDataIDs();
					$.each(ids, function(idx, rowId) {
						$(OPER02010_GRID_ID).jqGrid('setCell', rowId, 'nticeTitl', '', ITP_GRID_COL_STYLE.link);
						// $(OPER02010_GRID_ID).jqGrid('setCell', rowId, 'afflShopNm', '', ITP_GRID_COL_STYLE.link);
					});

					if ($('#ITP_CONTAINER').data('openYn') == 'Y') {
						$('#ITP_CONTAINER').data('openYn', 'N');
						const keyNew = {nticeNo: $('#ITP_CONTAINER').data('nticeNo'), viewCd: 'R'};
						var paramNew = $.param(keyNew);
						viewObj.init(paramNew);
					}
				}
			};
			itp_fn_grid_make_remote(option);
		},
		itp_fn_OPER02010_empty: {
			push: function() {
				let param = $('#ITP_FORM_OPER02010_SEARCH').serializeObject();
				listObj.grid.itp_OPER02010_param = param;
			},
			back: function() {
				var _this = listObj.grid;
				$('#ITP_FORM_OPER02010_DEVI_TP_CD').val(_this.itp_OPER02010_param.safflShopId);
				$('#ITP_FORM_OPER02010_ORDER_YN').val(_this.itp_OPER02010_param.safflShopNm);
				$('#ITP_FORM_OPER02010_ADT_AUTH_CD').val(_this.itp_OPER02010_param.sbrandId);
				$('#ITP_FORM_OPER02010_EMAIL_RCV_YN').val(_this.itp_OPER02010_param.snticeTitl);
				$('#ITP_FORM_OPER02010_FROM_DT').val(_this.itp_OPER02010_param.fromDt);
				$('#ITP_FORM_OPER02010_TO_DT').val(_this.itp_OPER02010_param.toDt);
			}
		},
		reload : function() {
			var _this = this;
			$(OPER02010_GRID_EMPTY).off('click').on('click', function() {
				_this.itp_fn_OPER02010_empty.back();
				_this.itp_OPER02010_param.gridSize = $.jgrid.defaults.rowNum;
				$(OPER02010_GRID_ID).setGridParam({'postData': JSON.stringify(_this.itp_OPER02010_param)}).trigger('reloadGrid');
			});
		},
		clearData : function() {
			$(OPER02010_GRID_ID).jqGrid('clearGridData', true);
			$(OPER02010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(OPER02010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(OPER02010_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(OPER02010_GRID_ID);
		}
	}
};

/*상세화면 Object*/
let viewObj = {
	init: function (param) {
		this.load(param);
	},
	button: {
	},
	load: function(param) {
		fn_ajax_call(NTICE_INFO_NTICE, param, this.callback, 'GET');
	},
	callback: function (result) {
		/*
		pageObj.switchScreen('VIEW');
		$('#ITP_FORM_OPER02010_VIEW #ITP_FORM_OPER02010_VIEW_NTICE_NO').val(result.nticeNo);
		$('#ITP_FORM_OPER02010_VIEW #ITP_FORM_OPER02010_VIEW_USER_ID').val(fn_make_user_info.get('userId'));
		$('#ITP_FORM_OPER02010_VIEW #ITP_FORM_OPER02010_VIEW_ADD_ID').val(result.addId);
		$('#ITP_FORM_OPER02010_VIEW .fnNticeTitl').text(result.nticeTitl);
		$('#ITP_FORM_OPER02010_VIEW .fnNotiDay').text(result.notiStDay+' ~ '+result.notiEdDay);
		$('#ITP_FORM_OPER02010_VIEW .fnDelYn').text(result.delYn);
		$('#ITP_FORM_OPER02010_VIEW .fnNticeDesc').html(result.nticeDesc.replace(/(\n|\r\n)/g, '<br>'));
		$('#ITP_FORM_OPER02010_VIEW .fnTargetNm').text(result.targetNm);
		$('#ITP_FORM_OPER02010_VIEW_FILE_NO').val(result.fileNo); //파일번호 셋팅
		$('#ITP_FORM_OPER02010_VIEW .fnAddId').text(result.addId);
		$('#ITP_FORM_OPER02010_VIEW .fnAddNm').text(result.addNm);

		itp_fn_attach_file.view('ITP_OPER02010_VIEW_UPLOAD', result.fileNoList
			, 'form-control itp_input', 'border:none; top:0px; left:12px; height:100px; width:125px; position:absolute;');

		// if(result.addId === fn_make_user_info.get('userId')) {
		if(fn_make_user_info.get('authTpCd') === '10' || result.addId === fn_make_user_info.get('userId')) {
			fn_show_btn_auth_array(['#ITP_BTN_OPER02010_MODIFY', '#ITP_BTN_OPER02010_CANCELLIST']);
		} else {
			fn_show_btn_auth_array(['#ITP_BTN_OPER02010_CANCELLIST']);
		}
		
		// 권한정보 리스트 조회
		// pageObj.grid.init('VIEW');
		*/
		pageObj.switchScreen('VIEW');
		$('#ITP_FORM_OPER02010_VIEW_NTICE_NO').val(result.nticeNo);
		$('#ITP_FORM_OPER02010_VIEW_NTICE_TITL').val(result.nticeTitl);
		$('#ITP_FORM_OPER02010_VIEW_FROM_DT').val(result.notiStDay);
		$('#ITP_FORM_OPER02010_VIEW_TO_DT').val(result.notiEdDay);
		$('#ITP_FORM_OPER02010_VIEW_NTICE_DESC').val(result.nticeDesc);
		$('#ITP_FORM_OPER02010_VIEW_TARGET_CD').val(result.targetCd);
		$('#ITP_FORM_OPER02010_VIEW_FILE_NO').val(result.fileNo); //파일번호 셋팅

		// 브랜드그룹 셋팅
		$('#ITP_FORM_OPER02010_VIEW_AFFL_SHOP_ID').val(result.afflShopId);
		$('#ITP_FORM_OPER02010_VIEW_AFFL_SHOP_NM').val(result.afflShopNm);

		pageObj.ui.afflInfo.init();
		if(result.brandId && result.brandId !== '') {
			var brandList = [{'brandId':result.brandId, 'brandNm':result.brandNm}];
			fn_make_select_brand(brandList, 'ITP_FORM_OPER02010_VIEW_SEARCH_BRAND_ID');
		}

		pageObj.event.notiTarget(result.afflShopId === '');
		//파일업로드(수정화면)
		setFileUploadInfo('OPER02010','VIEW');
		
		formReadonly("#ITP_FORM_OPER02010_VIEW .form-control");
		
		// if(result.addId === fn_make_user_info.get('userId')) {
		if(fn_make_user_info.get('authTpCd') === '10' || result.addId === fn_make_user_info.get('userId')) {
			fn_show_btn_auth_array(['#ITP_BTN_OPER02010_MODIFY', '#ITP_BTN_OPER02010_CANCELLIST']);
		} else {
			fn_show_btn_auth_array(['#ITP_BTN_OPER02010_CANCELLIST']);
		}

	  }
};


function formReadonly(qid) {
	if(!qid) qid='.form-readonly .form-control';
	setTimeout(function() {
		const formInputs=document.querySelectorAll(qid);
		formInputs.forEach(el=> {
			if( el.tagName=='SELECT') {
				$(el).css('pointer-events','none');
			} else {
				el.readOnly=true;
				el.classList.add("form-readonly");
			}
		});
	},100);
}

function setFileUploadInfo(id, type ) {
	//파일업로드(등록/수정화면)
	if(!id) id = 'OPER02010';
	var code=id + '_'+type;
	$('#ITP_' + code+'_UPLOAD').empty();
	$('#ITP_' + code+'_LOGO_FILE').empty();
	var removeFn = (param) => {
		$('#ITP_' + code+'_LOGO_FILE').empty();
	}; 
	$('#ITP_' + code+'_UPLOAD').bootstrapFileUpload({
		url: DOMAIN + '/api/file/upload',
		inputName: 'uploadFile',
		folderName: 'notice',
		fileNumber:  $('#ITP_FORM_' + code+'_FILE_NO').val(),
		fileInput: '#ITP_FORM_' + code+'_FILE_NO',
		fileTypes: ITP_UPLOAD_DEFAULTS.fileTypes,
		viewCode: $('#ITP_FORM_' + code+'_VIEW_CD').val(),
		formMethod: 'post',
		multiFile: true,
		multiUpload: false,
		maxSize: 100,
		maxFiles: 5,
		showThumb: false,
		thumbWidth: 0,
  		thumbHeight: 0,
		debug: true, 
		screen_id: code,
		showYn: false,
		onInit: function() {
			 
		}
	});

	setTimeout(function() {
		$('#ITP_' + code+'_UPLOAD .fileupload-add').hide();
		$('#ITP_' + code+'_UPLOAD .filedownload-remove').hide();
	},20);

}

/*수정화면 Object*/
let modifyObj = {
	init: function (param) {
	},
	button: {
		modify: function () {
			itp_fn_form_clear_validate(null, '#ITP_FORM_OPER02010_DETAIL');
			const nticeNoVal = $('#ITP_FORM_OPER02010_VIEW_NTICE_NO').val();
			const userIdVal = $('#ITP_FORM_OPER02010_VIEW_USER_ID').val();
			const addIdVal = $('#ITP_FORM_OPER02010_VIEW_ADD_ID').val(); 
			const key = {nticeNo: nticeNoVal, viewCd: 'R'};
			if(addIdVal != userIdVal){
				itp_fn_modal_alert_ajax(ITP_MSG_LOCALE.message.ajax.diffWrite);
				return false;
			}
			var param = $.param(key);
			modifyObj.load(param);
		}
	},
	load: function(param) {
		fn_ajax_call(NTICE_INFO_NTICE, param, this.callback, 'GET');
	},
	callback: function (result) {
		pageObj.switchScreen('MODIFY');
		$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_VIEW_CD').val('U');
		$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_NTICE_NO').val(result.nticeNo);
		$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_NTICE_TITL').val(result.nticeTitl);
		$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_FROM_DT').val(result.notiStDay);
		$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_TO_DT').val(result.notiEdDay);
		$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DEL_YN').val(result.delYn);
		$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_NTICE_DESC').val(result.nticeDesc);
		$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_TARGET_CD').val(result.targetCd);
		// $('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_BRAND').prop('checked', result.brandChk === 'Y');
		// $('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_STORE').prop('checked', result.shopChk === 'Y');
		// $('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_SPPLY').prop('checked', result.supplyChk === 'Y');
		// $('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_BRAND_CHK').val(result.brandChk);
		// $('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_STORE_CHK').val(result.shopChk);
		// $('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_SPPLY_CHK').val(result.supplyChk);
		$('#ITP_FORM_OPER02010_DETAIL_FILE_NO').val(result.fileNo); //파일번호 셋팅

		// 브랜드그룹 셋팅
		$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_AFFL_SHOP_ID').val(result.afflShopId);
		$('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_AFFL_SHOP_NM').val(result.afflShopNm);
		// $('#ITP_FORM_OPER02010_DETAIL_SEARCH_AFFL_SHOP').attr('disabled', true);
		// $('#ITP_FORM_OPER02010_DETAIL_DELETE_AFFL_SHOP').attr('disabled', true);
		// $('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_SEARCH_AFFL_SHOP_NM').attr('readonly', true);
		// $('#ITP_FORM_OPER02010_DETAIL #ITP_FORM_OPER02010_DETAIL_SEARCH_BRAND_ID').attr('readonly', true);

		pageObj.ui.afflInfo.init();
		if(result.brandId && result.brandId !== '') {
			var brandList = [{'brandId':result.brandId, 'brandNm':result.brandNm}];
			fn_make_select_brand(brandList, 'ITP_FORM_OPER02010_DETAIL_SEARCH_BRAND_ID');
		}

		pageObj.event.notiTarget(result.afflShopId === '');
		// if(result.targetCd && result.targetCd != '') {
		// 	$('#ITP_BTN_OPER02010_DETAIL_ADDROW').attr('disabled', false);
		// 	$('#ITP_BTN_OPER02010_DETAIL_DELROW').attr('disabled', false);
		// }

		//파일업로드(수정화면)
		pageObj.fileUpload();

		// 리스트 조회
		// createObj.form.init();
		// pageObj.grid.init('DETAIL');
	}
};

/*신규화면 Object*/
let createObj = {
	init: function () {
		pageObj.fileUpload();
		
	},
	button: {
		save: function() {
			const formId = '#ITP_FORM_OPER02010_DETAIL';
			itp_fn_form_clear_validate(null, formId);

			$('#ITP_FORM_OPER02010_DETAIL_TARGET_CD').attr('disabled', false);
			if(fn_make_user_info.get('authTpCd') === '30') {
				$('#ITP_FORM_OPER02010_DETAIL_TARGET_CD').val('A');
			}
			if (itp_fn_form_event.isValid(formId)) {
				// let gridInsertData = [];
				// let gridUpdateData = [];
				// let gridDeleteData = [];
				// const rowData = $(OPER02010_DETAIL_GRID_ID).getRowData();
				// const keyValue = $(formId + '_USER_ID').val();
				// $.each(rowData, function(key, value) {
				// 	if (value.viewCd != 'R') {
				// 		value.nticeNo = 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_OPER02010_DETAIL_IS_WRITING = false;
					listObj.grid.search();
				};
				fn_ajax_call(NTICE_SAVE_NTICE, JSON.stringify(param), searhFn, 'POST');
			}
		},
		cancel: function() {
			listObj.grid.search();
		}
	}
};

/*브랜드그룹 조회 Object*/
let afflShopObj = {
	init: function() {
		this.action();
	},
	button: function() {
		var _this = this;
		// 브랜드그룹 조회 버튼 클릭
		$('#ITP_FORM_OPER02010_SEARCH_AFFL_SHOP_BTN').on('click', function() {
			_this.popup('L');
		});
		// 브랜드그룹 텍스트 삭제 버튼 클릭
		$('#ITP_FORM_OPER02010_DELETE_AFFL_SHOP_BTN').on('click', function() {
			$('#ITP_FORM_OPER02010_SEARCH_AFFL_SHOP_ID').val('');
			$('#ITP_FORM_OPER02010_SEARCH_AFFL_SHOP_NM').val('');
			$('select#ITP_FORM_OPER02010_SEARCH_BRAND_ID option').remove();
		});
		// 신규등록 브랜드그룹 조회 버튼 클릭
		$('#ITP_FORM_OPER02010_DETAIL_SEARCH_AFFL_SHOP').on('click', function() {
			_this.popup('C');
		});
		// 신규등록 브랜드그룹 텍스트 삭제 버튼 클릭
		$('#ITP_FORM_OPER02010_DETAIL_DELETE_AFFL_SHOP').on('click', function() {
			$('#ITP_FORM_OPER02010_DETAIL_AFFL_SHOP_ID').val('');
			$('#ITP_FORM_OPER02010_DETAIL_AFFL_SHOP_NM').val('');
			$('select#ITP_FORM_OPER02010_DETAIL_BRAND_ID option').remove();
			$('#ITP_FORM_OPER02010_DETAIL_TARGET_CD option:eq(0)').prop('selected', true);
			// $('#ITP_FORM_OPER02010_DETAIL_TARGET_CD').trigger('change');
			pageObj.event.notiTarget(true);
		});
	},
	action: function() {
		this.button();
	},
	popup: function (view) {
		// 팝업
		var popFn = function(rowDataPop) {
			var id = (view === 'C') ? 'ITP_FORM_OPER02010_DETAIL' : 'ITP_FORM_OPER02010_SEARCH';
			if(rowDataPop.AFFL_SHOP) {
				$('#'+ id + '_AFFL_SHOP_ID').val(rowDataPop.AFFL_SHOP.afflShopId);
				$('#'+ id + '_AFFL_SHOP_NM').val(rowDataPop.AFFL_SHOP.afflShopNm);
				if(view === 'C') pageObj.event.notiTarget(false);
			}
			if(rowDataPop.AFFL_BRAND) {
				fn_make_select_brand(rowDataPop.AFFL_BRAND, id + '_BRAND_ID', true, '선택');
			}
		};
		fn_call_popup('biz', 'BIZPOP_AFFL_SHOP', '#ITP_ASIDE', popFn, null, 'S');
	}
};
