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

/*화면 변수*/

let ITP_FORM_STOCKMNG05010_DETAIL_IS_DUPLICATE = false;
let ITP_FORM_STOCKMNG05010_DETAIL_IS_WRITING = false;
let STOCKMNG05010_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 DMAND_FRCST_INFO_DMAND_FRCST = '/api/dmand/frcst/info-dmand-frcst';
let DMAND_FRCST_SAVE_DMAND_FRCST = '/api/dmand/frcst/save-dmand-frcst';

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

/*화면공통 Object*/
let pageObj = {
	init: function () {
		this.ui.init();
		this.event.init();
	},
	ui: {
		init: function () {
			this.view();
			this.grid();
			this.ready();
		},
		view: function() {
			// 버튼 권한설정
			fn_proc_btn_auth('STOCKMNG05010');
			
			// 공통코드 표시
			// fn_make_select(CODE_LIST, 'WHS_DVSN', 'ITP_FORM_STOCKMNG05010_WHS_DVSN');			
			// fn_make_select(CODE_LIST, 'WHS_ST_CD', 'ITP_FORM_STOCKMNG05010_WHS_ST_CD');
			// fn_make_select(CODE_LIST, 'WHS_ST_CD', 'ITP_FORM_STOCKMNG05010_DETAIL_WHS_ST_CD', true, '선택');
			
			// 브랜드코드
			$('#ITP_FORM_STOCKMNG05010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
			$('#ITP_FORM_STOCKMNG05010_DETAIL .fnBrandNm').text(fn_make_user_info.get('brandNm'));

			// 조회일자 지정
			// $('#ITP_TAB_STOCKMNG05010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);			
		},
		grid: function() {			
		},
		ready: function() {
			// 조회버튼 클릭		
			modifyObj.button.modify();
		}
	},
	event: {
		init: function () {
			this.button();
		},
		button: function () {
			// 버튼 클릭 이벤트
			$('button').each(function() {
				var id = $(this).attr('id');
				$(this).on('click', function() {
					switch (id) {
						case 'ITP_BTN_STOCKMNG05010_SRH'				: modifyObj.button.modify();	break;	// 조회 버튼 클릭
						case 'ITP_BTN_STOCKMNG05010_SAVE'				: modifyObj.button.save(); 		break;	// 저장 버튼
					}
				});
			});

		}
	},
	switchScreen: function(mode) {
		
	}
};

/*수정화면 Object*/
let modifyObj = {
	init: function () {
	},
	button: {
		modify: function (param) {
			itp_fn_form_clear_validate(null, '#ITP_FORM_STOCKMNG05010_DETAIL');
			const brandIdVal = $('#ITP_FORM_STOCKMNG05010_DETAIL_BRAND_ID').val();
			const key = {brandId: brandIdVal, viewCd: 'R'};
			var param = $.param(key);
			modifyObj.load(param);
		}, 
		save: function() {
			
			const formId = '#ITP_FORM_STOCKMNG05010_DETAIL';
			itp_fn_form_clear_validate(null, formId);
			
			let param = $(formId).serializeObject();
			//console.log(JSON.stringify(param));
			var searhFn = function() {
				modifyObj.button.modify();
			};
			fn_ajax_call(DMAND_FRCST_SAVE_DMAND_FRCST, JSON.stringify(param), searhFn, 'POST');
						
		}
	},
	load: function(param) {
		fn_ajax_call(DMAND_FRCST_INFO_DMAND_FRCST, param, this.callback, 'GET');
	},
	callback: function (result) {
		// console.log(result);
		// pageObj.switchScreen('MODIFY');
		$('#ITP_FORM_STOCKMNG05010_DETAIL #ITP_FORM_STOCKMNG05010_DETAIL_VIEW_CD').val('U');
		$('#ITP_FORM_STOCKMNG05010_DETAIL #ITP_FORM_STOCKMNG05010_DETAIL_BRAND_ID').val(result.brandId);
		$('#ITP_FORM_STOCKMNG05010_DETAIL #ITP_FORM_STOCKMNG05010_DETAIL_TERM_CNT').val(result.termCnt);
		$('#ITP_FORM_STOCKMNG05010_DETAIL #ITP_FORM_STOCKMNG05010_DETAIL_WEIGHT_VAL1').val(result.weightVal1);
		$('#ITP_FORM_STOCKMNG05010_DETAIL #ITP_FORM_STOCKMNG05010_DETAIL_WEIGHT_VAL2').val(result.weightVal2);
		$('#ITP_FORM_STOCKMNG05010_DETAIL #ITP_FORM_STOCKMNG05010_DETAIL_WEIGHT_VAL3').val(result.weightVal3);
		$('#ITP_FORM_STOCKMNG05010_DETAIL #ITP_FORM_STOCKMNG05010_DETAIL_WEIGHT_VAL4').val(result.weightVal4);
		$('#ITP_FORM_STOCKMNG05010_DETAIL #ITP_FORM_STOCKMNG05010_DETAIL_WEIGHT_VAL5').val(result.weightVal5);
		$('#ITP_FORM_STOCKMNG05010_DETAIL #ITP_FORM_STOCKMNG05010_DETAIL_WEIGHT_VAL6').val(result.weightVal6);
		$('#ITP_FORM_STOCKMNG05010_DETAIL #ITP_FORM_STOCKMNG05010_DETAIL_COEFFICIENT').val(result.coefficient);

	}
};
