/********************************************************
 --------------------------------------------------------
 - Object 목록								            -
 --------------------------------------------------------
 * const gridColModel = {}	: 화면 Grid Object			*
 * let pageObj = {}			: 화면공통 Object			*
 * let listObj = {}			: 목록화면 Object			*
 * let modifyObj = {}			: 상세화면 Object			*
 *******************************************************/

/*화면 변수*/
const VSETTMNG01010_GRID_ID = '#ITP_VSETTMNG01010_jqGrid';
const VSETTMNG01010_GRID_LIST = '#ITP_VSETTMNG01010_jqGrid_list';
const VSETTMNG01010_GRID_PAGER = '#ITP_VSETTMNG01010_jqGridPager';
const VSETTMNG01010_GRID_EMPTY = '#ITP_VSETTMNG01010_jqGridEmpty';

const VSETTMNG01010_DETAIL_GRID_ID = '#ITP_VSETTMNG01010_DETAIL_jqGrid';
const VSETTMNG01010_DETAIL_GRID_LIST = '#ITP_VSETTMNG01010_DETAIL_jqGrid_list';
const VSETTMNG01010_DETAIL_GRID_PAGER = '#ITP_VSETTMNG01010_DETAIL_jqGridPager';
const VSETTMNG01010_DETAIL_GRID_EMPTY = '#ITP_VSETTMNG01010_DETAIL_jqGridEmpty';

let ITP_FORM_VSETTMNG01010_DETAIL_IS_DUPLICATE = false;
let ITP_FORM_VSETTMNG01010_DETAIL_IS_WRITING = false;
let VSETTMNG01010_DETAIL_GRID_LAST_ROW_ID;

/*API URL*/
let STTL_REQ_DETAIL_GRID_LIST = '/api/sttl/req/detail-grid-list';
let STTL_REQ_REQUEST_STTL = '/api/sttl/req/request-sttl';
let STTL_REQ_REQUEST_STTL_PROC = '/api/sttl/req/request-sttl-proc';
let STTL_REQ_INFO_STTL = '/api/sttl/req/info-sttl';
let STTL_REQ_STL_GRID_LIST = '/api/sttl/req/stl-grid-list';

let LOGIN_AUTH_TYPE_CD;

/*화면 Grid ColModel*/
const gridColModel = {
	list: [
		{
			index: 'VIEW_CD', name: 'viewCd',
			label: ITP_MSG_LOCALE.label.viewCd,
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'STTL_MGNT_UNQ_NO', name: 'sttlMgntUnqNo',
			label: "정산관리고유번호", 
			width: '0', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'STTL_MGNT_DTL_NO', name: 'sttlMgntDtlNo',
			label: '정산관리상세번호',
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'MGNT_UNQ_NO', name: 'mgntUnqNo',
			label: '관리고유번호',
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'BRAND_NM', name: 'brandNm',
			label: "브랜드명",
			width: '14', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'STORE_NM', name: 'storeNm',
			label: "매장명",
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'LOAN_DVSN', name: 'loanDvsn',
			label: "정산타입", 
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'LOAN_DVSN_NM', name: 'loanDvsnNm',
			label: "정산타입", 
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'STTL_ST_CD', name: 'sttlStCd',
			label: "정산상태코드", 
			width: '8', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'STTL_ST_NM', name: 'sttlStNm',
			label: "정산상태", 
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},		
		{
			index: 'ITEM_ID', name: 'itemId',
			label: "품목코드",
			width: '20', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'ITEM_NM', name: 'itemNm',
			label: "품목명",
			width: '14', fixed: false, align: 'left',
			sortable: false, editable: false, edittype: 'text',
			classes: 'ui-ellipsis'
		},
		{
			index: 'UNIT', name: 'unit',
			label: "단위",
			width: '6', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'UNIT_AMT', name: 'unitAmt',
			label: "단가",
			width: '13', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text', 
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'PODR_QTY', name: 'podrQty',
			label: "발주수량",
			width: '13', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text', 
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'DLV_QTY', name: 'dlvQty',
			label: "납품수량",
			width: '13', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text', 
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'WHS_COL_QTY', name: 'whsColQty',
			label: "입고/수거수량",
			width: '13', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text', 
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		}, 
		{
			index: 'WHS_COL_DT', name: 'whsColDt',
			label: "입고/수거일자",
			width: '10', fixed: false, align: 'center',
			minwidth: 100,
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'WHS_COL_AMT', name: 'whsColAmt',
			label: "입고/수거금액",
			width: '16', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text', 
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'STTL_DVSN', name: 'sttlDvsn',
			label: "정산구분",
			width: '9', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'STTL_DVSN_NM', name: 'sttlDvsnNm',
			label: "정산구분",
			width: '10', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'PODR_UNQ_NO', name: 'podrUnqNo',
			label: "발주번호",
			width: '18', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'PODR_DTL_NO', name: 'podrDtlNo',
			label: "발주항번",
			width: '10', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		}	
	],
	view: [
		{
			index: 'VIEW_CD', name: 'viewCd',
			label: ITP_MSG_LOCALE.label.viewCd,
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'STTL_MGNT_DTL_NO', name: 'sttlMgntDtlNo',
			label: '정산관리상세번호',
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},		
		{
			index: 'BRAND_NM', name: 'brandNm',
			label: "브랜드",
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'STORE_NM', name: 'storeNm',
			label: "매장명",
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'ITEM_ID', name: 'itemId',
			label: "품목코드",
			width: '14', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'ITEM_NM', name: 'itemNm',
			label: "품목명",
			width: '10', fixed: false, align: 'left',
			sortable: false, editable: false, edittype: 'text',
			classes: 'ui-ellipsis'
		},
		{
			index: 'UNIT', name: 'unit',
			label: "단위",
			width: '6', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'UNIT_AMT', name: 'unitAmt',
			label: "단가",
			width: '11', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text', 
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'PODR_QTY', name: 'podrQty',
			label: "발주수량",
			width: '11', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text', 
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'DLV_QTY', name: 'dlvQty',
			label: "납품수량",
			width: '11', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text', 
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'WHS_COL_QTY', name: 'whsColQty',
			label: "입고/수거수량",
			width: '11', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text', 
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		}, 
		{
			index: 'WHS_COL_DT', name: 'whsColDt',
			label: "입고/수거일자",
			width: '10', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'WHS_COL_AMT', name: 'whsColAmt',
			label: "입고/수거금액",
			width: '11', fixed: false, align: 'right',
			sortable: false, editable: false, edittype: 'text', 
			formatter:'integer',formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'STTL_DVSN_NM', name: 'sttlDvsnNm',
			label: "정산구분",
			width: '8', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'PODR_UNQ_NO', name: 'podrUnqNo',
			label: "발주번호",
			width: '13', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		},
		{
			index: 'PODR_DTL_NO', name: 'podrDtlNo',
			label: "발주항번",
			width: '7', fixed: false, align: 'center',
			sortable: false, editable: false, edittype: 'text'
		}
	]
};

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('VSETTMNG01010');
			
			// 공통코드 표시
			$('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_VSETTMNG01010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);			
			$('#ITP_FORM_VSETTMNG01010_SEARCH_FROM_DT').datepicker('setDate', new Date(now.setMonth(now.getMonth() - 1)));
			$('#ITP_FORM_VSETTMNG01010_SEARCH_TO_DT').datepicker('setDate', new Date());
			
			// 권한에 따라 매장값 설정
			$('#ITP_FORM_VSETTMNG01010_SEARCH_BRAND_ID').val(fn_make_user_info.get('brandId'));
			$('#ITP_FORM_VSETTMNG01010_SEARCH_SPPLY_ID').val(fn_make_user_info.get('spplyId'));
			LOGIN_AUTH_TYPE_CD = fn_make_user_info.get('authTpCd'); // 권한타입
			if(LOGIN_AUTH_TYPE_CD == '50') {
				$('#ITP_FORM_VSETTMNG01010_SEARCH_STORE_ID').val(fn_make_user_info.get('storeId'));
				$('#ITP_FORM_VSETTMNG01010_SEARCH_STORE_NM').val(fn_make_user_info.get('storeNm'));	
			}
			
		},
		grid: function() {
			itp_fn_jqgrid_resize(VSETTMNG01010_GRID_ID, VSETTMNG01010_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_VSETTMNG01010_SRH'				: listObj.button.search();		break;	// 조회 버튼 클릭
						case 'ITP_BTN_VSETTMNG01010_CANCELLIST'		: modifyObj.button.cancel(); 	break;	// 취소/목록 버튼
						case 'ITP_BTN_VSETTMNG01010_NEWREG'			: listObj.button.reqSttl();	break;	// 정산요청 버튼
						case 'ITP_BTN_VSETTMNG01010_REQUEST'			: modifyObj.button.request();	break;	// 요청 버튼
						case 'ITP_BTN_VSETTMNG01010_STORE_SEARCH'	: listObj.button.storePop();	break;	// 매장검색 팝업
						case 'ITP_BTN_VSETTMNG01010_STORE_ERASE'		: listObj.button.storeErase();	break;	// 매장 지움
						case 'ITP_BTN_VSETTMNG01010_EXCEL'			: listObj.excelDown();				break;	// 엑셀 다운로드
					}
				});
			});

			$('body').on('click', function(e) {
				var gridId = VSETTMNG01010_DETAIL_GRID_ID.replace('#', '');
				var rids = $(VSETTMNG01010_DETAIL_GRID_ID).jqGrid('getDataIDs');
				var last_row_id = rids[rids.length - 1];
				itp_fn_grid_reset_selection(e, last_row_id, gridId, 'VSETTMNG01010_DETAIL');
			});

		}
	},
	switchScreen: function(mode) {
		if(mode == 'LIST') { // 목록
			$('#ITP_BTN_VSETTMNG01010_MODIFY').hide();
			$('#ITP_BTN_VSETTMNG01010_CANCELLIST').hide();
			$('#ITP_BTN_VSETTMNG01010_DELETE').hide();
			$('#ITP_BTN_VSETTMNG01010_REQUEST').hide();
			
			if(LOGIN_AUTH_TYPE_CD == '50') { // 매장관리자
				$('#ITP_BTN_VSETTMNG01010_STORE_SEARCH').hide();
				$('#ITP_BTN_VSETTMNG01010_STORE_ERASE').hide();
			} else {
				$('#ITP_BTN_VSETTMNG01010_STORE_SEARCH').show();
				$('#ITP_BTN_VSETTMNG01010_STORE_ERASE').show();
			}
			
			fn_show_btn_auth('#ITP_BTN_VSETTMNG01010_SRH');
			fn_show_btn_auth('#ITP_BTN_VSETTMNG01010_NEWREG');
			
			$('#ITP_AJAX_VSETTMNG01010_LIST_CONTAINER').show();
			$('#ITP_AJAX_VSETTMNG01010_DETAIL_CONTAINER').hide();
			
			$('#ITP_BTN_VSETTMNG01010_EXCEL').show();			
		} else if(mode == 'DETAIL') { // 보기
			$('#ITP_BTN_VSETTMNG01010_SRH').hide();
			$('#ITP_BTN_VSETTMNG01010_NEWREG').hide();
			$('#ITP_BTN_VSETTMNG01010_DELETE').hide();
			
			fn_show_btn_auth('#ITP_BTN_VSETTMNG01010_REQUEST');
			fn_show_btn_auth('#ITP_BTN_VSETTMNG01010_CANCELLIST');

			$('#ITP_AJAX_VSETTMNG01010_LIST_CONTAINER').hide();
			$('#ITP_AJAX_VSETTMNG01010_DETAIL_CONTAINER').show();
		}
	}
};

/*목록화면 Object*/
let listObj = {
	init: function () {
		this.grid.init();
	},
	itp_VSETTMNG01010_search: false,
	button: {
		search: function() {
			listObj.itp_VSETTMNG01010_search = true;
			let param = $('#ITP_FORM_VSETTMNG01010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.gridPage = $.jgrid.defaults.page;
			$(VSETTMNG01010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
		},
		reqSttl: function() { // 정산요청
			// 매장선택 체크
			// if($('#ITP_FORM_VSETTMNG01010_SEARCH_STORE_ID').val() == '') {
			// itp_fn_modal_alert_ajax('정산요청을 위한 매장을 선택하세요.');
			// return;
			// }
		
			// 그리드에서 최소1개는 선택해야 함
			const rowKey = $(VSETTMNG01010_GRID_ID).getGridParam('selrow');
		    if (!rowKey) {
				itp_fn_modal_alert_ajax(ITP_MSG_LOCALE.message.ajax.noData);
		    	return;
		    }

			var selectedIds = $(VSETTMNG01010_GRID_ID).getGridParam('selarrrow');
			let sttlReqDataList = [];			
			var check = true;
			$.each(selectedIds, function(key, value) {
				var sttlMgntUnqNo = $(VSETTMNG01010_GRID_ID).jqGrid('getRowData', value).sttlMgntUnqNo;				
				var sttlMgntDtlNo = $(VSETTMNG01010_GRID_ID).jqGrid('getRowData', value).sttlMgntDtlNo;
				var loanDvsn = $(VSETTMNG01010_GRID_ID).jqGrid('getRowData', value).loanDvsn;		// 여신구분
				var sttlStCd = $(VSETTMNG01010_GRID_ID).jqGrid('getRowData', value).sttlStCd;		// 정산상태코드
				var sttlDvsn = $(VSETTMNG01010_GRID_ID).jqGrid('getRowData', value).sttlDvsn;		// 정산구분
				var mgntUnqNo = $(VSETTMNG01010_GRID_ID).jqGrid('getRowData', value).mgntUnqNo;
								
				if(sttlStCd == 'ST10' || sttlStCd == 'ST20') { // 정산요청 및 정산확정 건은 정산요청이 불가능 
					check = false;	
				}
				let data = {}
				data.sttlMgntUnqNo = sttlMgntUnqNo;
				data.sttlMgntDtlNo = sttlMgntDtlNo;
				data.loanDvsn = loanDvsn;
				data.sttlDvsn = sttlDvsn;
				data.mgntUnqNo = mgntUnqNo;
				sttlReqDataList.push(data);
			});
			if(!check) {
				itp_fn_modal_alert_ajax('정산요청 및 확정된 건은 정산 요청이 불가능합니다.');
				return;
			}
			
			let param = {};
			param.brandId = $('#ITP_FORM_VSETTMNG01010_SEARCH_BRAND_ID').val();
			// param.storeId = $('#ITP_FORM_VSETTMNG01010_SEARCH_STORE_ID').val();
			param.spplyId = $('#ITP_FORM_VSETTMNG01010_SEARCH_SPPLY_ID').val();
			param.sttlReqDataList = sttlReqDataList;
			fn_ajax_call(STTL_REQ_REQUEST_STTL, JSON.stringify(param), function(result) {
				
				// alert(JSON.stringify(result));
				// $('#ITP_FORM_VSETTMNG01010_DETAIL_STTL_MGNT_UNQ_NO').val(result.sttlMgntUnqNo);
				
				const sttlMgntUnqNoVal = result.sttlMgntUnqNo;
				const key = {sttlMgntUnqNo: sttlMgntUnqNoVal, viewCd: 'R'};
				var param = $.param(key);
				modifyObj.init(param);
				
			}, 'POST');			
		}, 
		storePop: function() {
			fn_call_popup('biz', 'BIZPOP_BRAND_STORE', '#ITP_ASIDE', function(result) {
				// alert(JSON.stringify(result));
				$('#ITP_FORM_VSETTMNG01010_SEARCH_STORE_ID').val(result[0].storeId);
				$('#ITP_FORM_VSETTMNG01010_SEARCH_STORE_NM').val(result[0].storeNm);				
				listObj.button.search();
			}, null, 'S');
		}, 
		storeErase: function() {
			$('#ITP_FORM_VSETTMNG01010_SEARCH_STORE_ID').val('');
			$('#ITP_FORM_VSETTMNG01010_SEARCH_STORE_NM').val('');
			listObj.button.search();
		}
	},
	empty: {
		init: function() {
			var _this = this;
			this.push();
			$(VSETTMNG01010_GRID_EMPTY).on('click', function() {
				_this.back();
				_this.itp_VSETTMNG01010_param.gridSize = $.jgrid.defaults.rowNum;
				$(VSETTMNG01010_GRID_ID).setGridParam({'postData': JSON.stringify(_this.itp_VSETTMNG01010_param)}).trigger('reloadGrid');
			});
		},
		itp_VSETTMNG01010_param: {},
		push: function() {
			let param = $('#ITP_FORM_VSETTMNG01010_SEARCH').serializeObject();
			listObj.itp_VSETTMNG01010_param = param;
		},
		back: function() {
			$('#ITP_FORM_VSETTMNG01010_SEARCH_STORE_ID').val(listObj.itp_VSETTMNG01010_param.sstoreId);
			$('#ITP_FORM_VSETTMNG01010_SEARCH_STORE_NM').val(listObj.itp_VSETTMNG01010_param.sstoreNm);
			$('#ITP_FORM_VSETTMNG01010_SEARCH_FROM_DT').val(listObj.itp_VSETTMNG01010_param.fromDt);
			$('#ITP_FORM_VSETTMNG01010_SEARCH_TO_DT').val(listObj.itp_VSETTMNG01010_param.toDt);
			$('#ITP_FORM_VSETTMNG01010_SEARCH_ITEM_NM').val(listObj.itp_VSETTMNG01010_param.sitemNm);
		}
	},
	grid: {
		init: function () {
			// 데이터 없을때
			listObj.empty.push();
			this.search();
		},
		colModel: gridColModel.list,
		search: function() {
			pageObj.switchScreen('LIST');
			this.unload();
			this.load();
		},
		load: function() {			
			let param = $('#ITP_FORM_VSETTMNG01010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;

			var option = {
				gridId: VSETTMNG01010_GRID_ID,
				colModel: gridColModel.list,
				param: param,
				url: DOMAIN + STTL_REQ_DETAIL_GRID_LIST,
				pager: VSETTMNG01010_GRID_PAGER,
				multiselect: true,
				rownumbers: false,
				onCellSelect: function(rowId, cellIdx, cellValue) {
					var cm = $(this).jqGrid('getGridParam', 'colModel');
					var colNm = cm[cellIdx].name;
					if (colNm == 'sttlReqDt') {
						const sttlMgntUnqNoVal = $(this).jqGrid('getCell', rowId, 'sttlMgntUnqNo');
						
						const key = {sttlMgntUnqNo: sttlMgntUnqNoVal, viewCd: 'R'};
						var param = $.param(key);
						modifyObj.init(param);
					}
				},
				loadComplete: function(data) {
					console.log(data);
					itp_fn_grid_load_complete(data, VSETTMNG01010_GRID_ID, true, 'number', 'VSETTMNG01010', listObj.itp_VSETTMNG01010_search, listObj.empty, true, data.gridRecords, true);
					var ids = $(VSETTMNG01010_GRID_ID).getDataIDs();
					$.each(ids, function(idx, rowId) {
						// $(VSETTMNG01010_GRID_ID).jqGrid('setCell', rowId, 'sttlReqDt', '', ITP_GRID_COL_STYLE.link);
					});
				},
				onPaging: function(action) {
					itp_fn_grid_paging(VSETTMNG01010_GRID_ID, action, param);
				}
			};
			itp_fn_grid_make_remote(option);
		},
		clearData : function() {
			$(VSETTMNG01010_GRID_ID).jqGrid('clearGridData', true);
			$(VSETTMNG01010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(VSETTMNG01010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(VSETTMNG01010_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(VSETTMNG01010_GRID_ID);
		}
	},
	excelDown: function() {
		var param = {
			'url': STTL_REQ_DETAIL_GRID_LIST,
			'param': $('#ITP_FORM_VSETTMNG01010_SEARCH').serializeObject(),
			'gridColumns': $(VSETTMNG01010_GRID_ID).jqGrid('getGridParam', 'colModel'),
			'fileName': '공급사 정산요청.xlsx',
			'sheetName': '공급사 정산리스트'
		};
		itp_fn_remote_to_excel(param);
	}
};

/*상세화면 Object*/
let modifyObj = {
	init: function (param) {
		this.load(param);
	},
	button: {
		cancel: function() {
			listObj.grid.search();
		},
		request: function() {
			
			const formId = '#ITP_FORM_VSETTMNG01010_DETAIL';
			itp_fn_form_clear_validate(null, formId);
			
			var vali_sttlReqDt = itp_fn_form_validate(formId, formId + '_STTL_REQ_DT', ['empty'], undefined); // 정산요청일자
			
			if (vali_sttlReqDt) {
				let sttlReqDataList = [];
				const rowData = $(VSETTMNG01010_DETAIL_GRID_ID).getRowData();
				// const keyValue = $(formId + '_WHS_ID').val();
				$.each(rowData, function(key, value) {
					sttlReqDataList.push(value);
				});
				let param = $(formId).serializeObject();
				param.sttlReqDataList = sttlReqDataList;
				
				// console.log(JSON.stringify(param));
				var confirmFn= {
					callBack: function() {
						//
						var searhFn = function() {
							ITP_FORM_VSETTMNG01010_DETAIL_IS_WRITING = false;
							listObj.grid.search();
						};
						fn_ajax_call(STTL_REQ_REQUEST_STTL_PROC, JSON.stringify(param), searhFn, 'POST'); 	
					}
				};
				itp_fn_modal_confirm(ITP_MSG_LOCALE.message.form.procData, confirmFn, null);				
			}
		}
	}, 
	load: function(param) {
		fn_ajax_call(STTL_REQ_INFO_STTL, param, this.callback, 'GET');
	},
	callback: function (result) {
		// console.log(result);
		pageObj.switchScreen('DETAIL');
		$('#ITP_FORM_VSETTMNG01010_DETAIL_BRAND_ID').val(result.brandId);
		$('#ITP_FORM_VSETTMNG01010_DETAIL_STORE_ID').val(result.storeId);
		$('#ITP_FORM_VSETTMNG01010_DETAIL_SPPLY_ID').val(result.spplyId);
		$('#ITP_FORM_VSETTMNG01010_DETAIL_STTL_MGNT_UNQ_NO').val(result.sttlMgntUnqNo);
		
		$('#ITP_FORM_VSETTMNG01010_DETAIL .fnBrandNm').text(result.brandNm);
		$('#ITP_FORM_VSETTMNG01010_DETAIL .fnSpplyNm').text(result.spplyNm);
		$('#ITP_FORM_VSETTMNG01010_DETAIL_STTL_REQ_DT').val(result.sttlReqDt);
		$('#ITP_FORM_VSETTMNG01010_DETAIL .fnSttlStNm').text(result.sttlStNm);
		$('#ITP_FORM_VSETTMNG01010_DETAIL .fnSttlReqAmt').text(itp_fn_number_comma(result.sttlReqAmt));
		$('#ITP_FORM_VSETTMNG01010_DETAIL .fnAcctBal').text(itp_fn_number_comma(result.acctBal));
		$('#ITP_FORM_VSETTMNG01010_DETAIL .fnSysRegDttm').text(result.sysRegDttm);
		$('#ITP_FORM_VSETTMNG01010_DETAIL .fnSysRegNm').text(result.sysRegNm);

		// 리스트 조회
		modifyObj.grid.init();
	},
	grid: {
		init: function() {
			this.unload();
			this.load();
		},
		load : function() {
			let param = $('#ITP_FORM_VSETTMNG01010_DETAIL').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.pagingYn = false; // 페이징안함

			var option = {
				gridId: VSETTMNG01010_DETAIL_GRID_ID,
				colModel: gridColModel.view,
				param: param,
				url: DOMAIN + STTL_REQ_STL_GRID_LIST,
				pager: VSETTMNG01010_DETAIL_GRID_PAGER,
				loadComplete: function(data) {
					itp_fn_grid_load_complete(data, VSETTMNG01010_DETAIL_GRID_ID, true, undefined, 'VSETTMNG01010_DETAIL');
				},
				onPaging: function(action) {
					var pagingFn = {
						callBack: function(args) {
							$(args).trigger('reloadGrid');
						}
					};
					if (itp_fn_check_grid_is_writing(VSETTMNG01010_DETAIL_GRID_ID)) {
						itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, pagingFn, VSETTMNG01010_DETAIL_GRID_ID);
						return 'stop';
					} else {
						itp_fn_grid_paging(VSETTMNG01010_DETAIL_GRID_ID, action, param);
					}
				},
				onSortCol: function(index, columnIndex, sortOrder) {
					var sortingFn = {
						callBack: function(args) {
							$(args).trigger('reloadGrid');
						}
					};
					if (itp_fn_check_grid_is_writing(VSETTMNG01010_DETAIL_GRID_ID)) {
						itp_fn_modal_confirm(ITP_MSG_LOCALE.message.ajax.cancelData, sortingFn, VSETTMNG01010_DETAIL_GRID_ID);
						return 'stop';
					} else {
						itp_fn_grid_sorting(VSETTMNG01010_DETAIL_GRID_ID, index, sortOrder);
					}
				}
			};
			itp_fn_grid_make_remote(option);
		},
		clearData : function() {
			$(VSETTMNG01010_DETAIL_GRID_ID).jqGrid('clearGridData', true);
			$(VSETTMNG01010_DETAIL_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(VSETTMNG01010_DETAIL_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(VSETTMNG01010_DETAIL_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(VSETTMNG01010_DETAIL_GRID_ID);
		}
	}
};
