
google.charts.load('current', {'packages':['corechart']});
// google.charts.setOnLoadCallback(drawChart1);
// google.charts.setOnLoadCallback(drawChart2);
 
function drawChart1(chartData) {
	var data = google.visualization.arrayToDataTable(chartData);

   	var options = {
    	title: '1년전 구매기준',
     	legend: { position: 'bottom' }
   	};

   	var chart = new google.visualization.LineChart(document.getElementById('ITP_PCH_STDD'));
   	chart.draw(data, options);
}

function drawChart2(chartData) {
	var data = google.visualization.arrayToDataTable(chartData);

	var options = {
		title: '최근 구매기준',
	    legend: { position: 'bottom' }
	};

	var chart = new google.visualization.LineChart(document.getElementById('ITP_PCH_NEW_STDD'));
	chart.draw(data, options);
}

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

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

/*화면 변수*/

let ITP_FORM_STINFO07010_DETAIL_IS_DUPLICATE = false;
let ITP_FORM_STINFO07010_DETAIL_IS_WRITING = false;

/*API URL*/
let API_DETAIL_INFO 		= '/api/dmand/frcst/status-dmand-frcst';		// 상세

/*화면공통 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('STINFO07010');

			// 공통코드 표시
			
			// 브랜드코드
			$('#ITP_FORM_STOCKMNG07010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
			$('#ITP_FORM_STOCKMNG07010_DETAIL .fnBrandNm').text(fn_make_user_info.get('brandNm'));
			
		},
		grid: function() {
		},
		ready: function() {
		}
	},
	event: {
		init: function () {
			this.button();
			this.change();
		},
		button: function () {
			// 버튼 클릭 이벤트
			$('button').each(function() {
				var id = $(this).attr('id');
				$(this).on('click', function() {
					switch (id) {
						case 'ITP_BTN_STOCKMNG07010_SRH'				: viewObj.search();		break;	// 조회 버튼 클릭
						case 'ITP_BTN_STOCKMNG07010_DETAIL_SRH_ITEM'	: pageObj.popItem();	break;	// 품목선택
						case 'ITP_BTN_STOCKMNG07010_DETAIL_ITEM_DELETE'	: pageObj.eraseItem();	break;	// 품목삭제
					}
				});
			});

		}, 
		change: function() {
			$('#ITP_FORM_STOCKMNG07010_DETAIL_TERM_CNT').on('change', function() {
				
				if($('#ITP_FORM_STOCKMNG07010_DETAIL_SCH_ITEM_ID').val() == '') {
					return;
				}				
				viewObj.search();
			});
		}
	},
	switchScreen: function(mode) {
		
	}, 
	popItem: function() {
		// 팝업
		var popFn = function(rowDataPop) {
			// console.log('>>>> ' + JSON.stringify(rowDataPop[0]));
			
			$('#ITP_FORM_STOCKMNG07010_DETAIL_SCH_ITEM_ID').val( rowDataPop[0].itemId );
			$('#ITP_FORM_STOCKMNG07010_DETAIL_SCH_ITEM_NM').val( rowDataPop[0].itemNm );
						
			viewObj.search();			
		};
		const key = {brandId:fn_make_user_info.get('brandId')};
		fn_call_popup('biz', 'BIZPOP_ITEM', '#ITP_ASIDE', popFn, key, 'S'); 
	}, 
	eraseItem: function() {
		$('#ITP_FORM_STOCKMNG07010_DETAIL_SCH_ITEM_ID').val('');
		$('#ITP_FORM_STOCKMNG07010_DETAIL_SCH_ITEM_NM').val('');
	}
};

/*상세화면 Object*/
let viewObj = {
	init: function () {
		// this.load(param);
	},
	search: function() {
		
		const formId = '#ITP_FORM_STOCKMNG07010_DETAIL';
		let param = $(formId).serializeObject();
		// console.log(JSON.stringify(param));
		fn_ajax_call(API_DETAIL_INFO, JSON.stringify(param), this.callback, 'POST');
	},
	callback: function (result) {
		// console.log(JSON.stringify('>>>> ' + JSON.stringify(result)));
		
		drawChart1( eval( result.poData1Year ) );
		drawChart2( eval( result.poDataLast ) );
		
		// pageObj.switchScreen('VIEW');
		// $('#ITP_FORM_STINFO07010_VIEW #ITP_FORM_STINFO07010_VIEW_ITEM_ID').val(result.itemId);
		// $('#ITP_FORM_STINFO07010_VIEW .fnBrandNm').text(result.brandNm);
	}
};
