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

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

/*화면 변수*/
const INOUTMNG03010_GRID_ID = '#ITP_INOUTMNG03010_jqGrid';
const INOUTMNG03010_GRID_LIST = '#ITP_INOUTMNG03010_jqGrid_list';
const INOUTMNG03010_GRID_PAGER = '#ITP_INOUTMNG03010_jqGridPager';
const INOUTMNG03010_GRID_EMPTY = '#ITP_INOUTMNG03010_jqGridEmpty';

const INOUTMNG03010_DETAIL_GRID_ID = '#ITP_INOUTMNG03010_DETAIL_jqGrid';
const INOUTMNG03010_DETAIL_GRID_LIST = '#ITP_INOUTMNG03010_DETAIL_jqGrid_list';
const INOUTMNG03010_DETAIL_GRID_PAGER = '#ITP_INOUTMNG03010_DETAIL_jqGridPager';
const INOUTMNG03010_DETAIL_GRID_EMPTY = '#ITP_INOUTMNG03010_DETAIL_jqGridEmpty';


let INOUTMNG03010_GRID_LAST_ROW_ID;


/*API URL*/
let API_DETAIL_GRID_LIST = '/api/rtnmng/inoutmng/col-target-grid-list';	// 목록
let API_DETAIL_SAVE 	 = '/api/rtnmng/inoutmng/save-inst-col';			//수거지시

/*화면 Grid ColModel*/
const gridColModel = {
	list: [
		{
			index: 'BRAND_ID', name: 'brandId',
			label: ITP_MSG_LOCALE.label.brandId, //브랜드아이디
			width: '13', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'BRAND_NM', name: 'brandNm',
			label: ITP_MSG_LOCALE.label.brandNm, //브랜드명
			width: '10', fixed: false, align: 'left',
			minwidth: 100,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'RTN_PODR_UNQ_NO', name: 'rtnPodrUnqNo',
			label: ITP_MSG_LOCALE.label.rtnPodrUnqNo, //반품발주번호
			width: '10', fixed: false, align: 'center',
			minwidth: 140,
			sortable: false, hidden: false
		},
		{
			index: 'RTN_PODR_DTL_NO', name: 'rtnPodrDtlNo',
			label: ITP_MSG_LOCALE.label.rtnPodrDtlNo, //반품발주항번
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'RTN_REQ_UNQ_NO', name: 'rtnReqUnqNo',
			label: ITP_MSG_LOCALE.label.rtnReqUnqNo, //반품요청번호
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'RTN_REQ_DTL_NO', name: 'rtnReqDtlNo',
			label: ITP_MSG_LOCALE.label.rtnReqDtlNo, //반품요청항번
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'ITEM_ID', name: 'itemId',
			label: ITP_MSG_LOCALE.label.itemId, //품목아이디
			width: '10', fixed: false, align: 'center',
			minwidth: 140,
			sortable: false, hidden: false
		},
		{
			index: 'ITEM_NM', name: 'itemNm',
			label: ITP_MSG_LOCALE.label.itemNm, //품목명
			width: '10', fixed: false, align: 'left',
			minwidth: 100,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'RTN_WHS_ID', name: 'rtnWhsId',
			label: ITP_MSG_LOCALE.label.rtnWhsId, //수거창고아이디
			width: '13', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'RTN_LOCATION', name: 'rtnLocation',
			label: ITP_MSG_LOCALE.label.rtnLocation, //수거LOCATION
			width: '15', fixed: false, align: 'left',
			sortable: false, hidden: true
		},
		{
			index: 'RTN_WHS_NM', name: 'rtnWhsNm',
			label: ITP_MSG_LOCALE.label.rtnWhsNm, //수거창고명
			width: '15', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'UNIT_AMT', name: 'unitAmt',
			label: ITP_MSG_LOCALE.label.unitAmt, //단가
			width: '6', fixed: false, align: 'right',
			sortable: false, hidden: false,
			minwidth: 60,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'RTN_REQ_QTY', name: 'rtnReqQty',
			label: ITP_MSG_LOCALE.label.rtnReqQty, //반품요청수량
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: true,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'COL_QTY', name: 'colQty',
			label: ITP_MSG_LOCALE.label.colQty, //반품요청수량
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: true,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'RTN_ODR_QTY', name: 'rtnOdrQty',
			label: ITP_MSG_LOCALE.label.rtnOdrQty, //반품발주수량
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'RTN_AMT', name: 'rtnAmt',
			label: ITP_MSG_LOCALE.label.rtnAmt, //반품발주금액
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'COL_REQ_DT', name: 'colReqDt',
			label: ITP_MSG_LOCALE.label.colReqDt, //수거요청일
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'STORE_ID', name: 'storeId',
			label: ITP_MSG_LOCALE.label.storeId, //매장아이디
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: true
		},
		{
			index: 'WHS_ID', name: 'whsId',
			label: ITP_MSG_LOCALE.label.whsId, //입고창고아이디
			width: '13', fixed: false, align: 'left',
			sortable: false, hidden: true
		},
		{
			index: 'LOCATION', name: 'location',
			label: ITP_MSG_LOCALE.label.location, //입고LOCATION
			width: '15', fixed: false, align: 'left',
			sortable: false, hidden: true
		},
		{
			index: 'WHS_NM', name: 'whsNm',
			label: ITP_MSG_LOCALE.label.locationNm, //입고창고명
			width: '15', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		}
	],
	detail: [
		{
			index: 'BRAND_ID', name: 'brandId',
			label: ITP_MSG_LOCALE.label.brandId, //브랜드아이디
			width: '13', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'BRAND_NM', name: 'brandNm',
			label: ITP_MSG_LOCALE.label.brandNm, //브랜드명
			width: '10', fixed: false, align: 'center',
			minwidth: 100,
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'RTN_PODR_UNQ_NO', name: 'rtnPodrUnqNo',
			label: ITP_MSG_LOCALE.label.rtnPodrUnqNo, //반품발주번호
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'RTN_PODR_DTL_NO', name: 'rtnPodrDtlNo',
			label: ITP_MSG_LOCALE.label.rtnPodrDtlNo, //반품발주항번
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'RTN_REQ_UNQ_NO', name: 'rtnReqUnqNo',
			label: ITP_MSG_LOCALE.label.rtnReqUnqNo, //반품요청번호
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'RTN_REQ_DTL_NO', name: 'rtnReqDtlNo',
			label: ITP_MSG_LOCALE.label.rtnReqDtlNo, //반품요청항번
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'ITEM_ID', name: 'itemId',
			label: ITP_MSG_LOCALE.label.itemId, //품목아이디
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'ITEM_NM', name: 'itemNm',
			label: ITP_MSG_LOCALE.label.itemNm, //품목명
			width: '10', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'RTN_WHS_ID', name: 'rtnWhsId',
			label: ITP_MSG_LOCALE.label.rtnWhsId, //수거창고아이디
			width: '13', fixed: false, align: 'center',
			sortable: false, hidden: true
		},
		{
			index: 'RTN_LOCATION', name: 'rtnLocation',
			label: ITP_MSG_LOCALE.label.rtnLocation, //수거LOCATION
			width: '15', fixed: false, align: 'left',
			sortable: false, hidden: true
		},
		{
			index: 'RTN_WHS_NM', name: 'rtnWhsNm',
			label: ITP_MSG_LOCALE.label.rtnWhsNm, //수거창고명
			width: '15', fixed: false, align: 'center',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		},
		{
			index: 'UNIT_AMT', name: 'unitAmt',
			label: ITP_MSG_LOCALE.label.unitAmt, //단가
			width: '6', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'RTN_REQ_QTY', name: 'rtnReqQty',
			label: ITP_MSG_LOCALE.label.rtnReqQty, //반품요청수량
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'RTN_ODR_QTY', name: 'rtnOdrQty',
			label: ITP_MSG_LOCALE.label.rtnOdrQty, //반품발주수량
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'COL_QTY', name: 'colQty',
			label: ITP_MSG_LOCALE.label.colQty, //수거수량
			width: '10', fixed: false, align: 'right',
			sortable: true, editable: true, edittype: 'text',
			editrules: { required: true, number: true },
			editoptions: {"onKeyup": "this.value=this.value.replace(/[^0-9]/g,'');"}, 
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'RTN_AMT', name: 'rtnAmt',
			label: ITP_MSG_LOCALE.label.rtnAmt, //수거발주금액
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: false,
			formatter: 'integer', formatoptions: { defaultValue: '', thousandsSeparator: ',' }
		},
		{
			index: 'COL_REQ_DT', name: 'colReqDt',
			label: ITP_MSG_LOCALE.label.colReqDt, //수거요청일
			width: '10', fixed: false, align: 'center',
			sortable: false, hidden: false
		},
		{
			index: 'STORE_ID', name: 'storeId',
			label: ITP_MSG_LOCALE.label.storeId, //매장아이디
			width: '10', fixed: false, align: 'right',
			sortable: false, hidden: true
		},
		{
			index: 'WHS_ID', name: 'whsId',
			label: ITP_MSG_LOCALE.label.whsId, //입고창고아이디
			width: '13', fixed: false, align: 'left',
			sortable: false, hidden: true
		},
		{
			index: 'LOCATION', name: 'location',
			label: ITP_MSG_LOCALE.label.location, //입고LOCATION
			width: '15', fixed: false, align: 'left',
			sortable: false, hidden: true
		},
		{
			index: 'WHS_NM', name: 'whsNm',
			label: ITP_MSG_LOCALE.label.whsNm, //입고창고명
			width: '15', fixed: false, align: 'left',
			sortable: false, hidden: false, classes: 'ui-ellipsis'
		}
	]
};

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

			// 공통코드 표시
			$('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_INOUTMNG03010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_INOUTMNG03010_SEARCH_FROM_DT').datepicker('setDate', new Date(now.setDate(now.getDate())));
			$('#ITP_FORM_INOUTMNG03010_SEARCH_TO_DT').datepicker('setDate', new Date(now.setDate(now.getDate() + 30)));
		},
		form: function() {
			itp_fn_form_event.onKeyup('#ITP_FORM_INOUTMNG03010_DETAIL');
		},
		grid: function() {
			itp_fn_jqgrid_resize(INOUTMNG03010_GRID_ID, INOUTMNG03010_GRID_LIST, 'lg');
			listObj.empty.init();
			
		},
		ready: function() {
			listObj.init();
		}
	},
	action: function () {
		var _this = this;
		// 반품입고창고 조회 버튼 클릭
		$('#ITP_FORM_INOUTMNG03010_SEARCH_WHS_NM').on('click', function() {
			_this.popup('R');
		});
		// 수거창고 텍스트 삭제 버튼 클릭
		$('#ITP_FORM_INOUTMNG03010_DELETE_WHS_NM').on('click', function() {
			$('#ITP_FORM_INOUTMNG03010_SEARCH_SWHS_NM').val('');
			$('#ITP_FORM_INOUTMNG03010_SEARCH_SWHS_ID').val('');

		});

		// 수거장소 조회 버튼 클릭
		$('#ITP_FORM_INOUTMNG03010_SEARCH_RTN_WHS_NM').on('click', function() {
			_this.popup('W');
		});
		// 수거장소 텍스트 삭제 버튼 클릭
		$('#ITP_FORM_INOUTMNG03010_DELETE_RTN_WHS_NM').on('click', function() {
			$('#ITP_FORM_INOUTMNG03010_SEARCH_SRTN_WHS_NM').val('');
			$('#ITP_FORM_INOUTMNG03010_SEARCH_SRTN_WHS_ID').val('');
		});
	},
	popup: function (arg) {
		var popFn;
		// 팝업
		if (arg == "R") {
			popFn = this.callback.whs;
		} else {
			popFn = this.callback.rtnwhs;
		}

		var whsDvsn = "";

		if (arg == "R") {
			whsDvsn = "W01";
		} else {
			whsDvsn = "W02";
		}

		const key = { brandId: fn_make_user_info.get('brandId'), storeId: fn_make_user_info.get('storeId'), whsDvsn: whsDvsn };



		fn_call_popup('biz', 'BIZPOP_WHS_LOCATION', '#ITP_ASIDE', popFn, key, 'S');
		
		
	},
	callback: {
		whs: function(rowDataPop) {
			if (rowDataPop) {
				$('#ITP_FORM_INOUTMNG03010_SEARCH_SWHS_NM').val(rowDataPop.whsNm);
				$('#ITP_FORM_INOUTMNG03010_SEARCH_SWHS_ID').val(rowDataPop.whsId);
			}

		},
		rtnwhs: function(rowDataPop) {
			if (rowDataPop) {
				$('#ITP_FORM_INOUTMNG03010_SEARCH_SRTN_WHS_NM').val(rowDataPop.whsNm);
				$('#ITP_FORM_INOUTMNG03010_SEARCH_SRTN_WHS_ID').val(rowDataPop.whsId);
			}

		}
	},
	event: {
		init: function () {
			this.button();
		},
		button: function () {
			// 버튼 클릭 이벤트
			$('button').each(function() {
				var id = $(this).attr('id');
				$(this).on('click', function() {
					switch (id) {
						case 'ITP_BTN_INOUTMNG03010_SRH'				: listObj.button.search();			break;	// 조회 버튼 클릭
						case 'ITP_BTN_INOUTMNG03010_CUC'				: listObj.button.colcreat();		break;	// 수거생성 버튼
						case 'ITP_BTN_INOUTMNG03010_CUD'				: modifyObj.button.save(); 			break;	// 수거지시 버튼
						case 'ITP_BTN_INOUTMNG03010_CANCELLIST'			: modifyObj.button.cancel(); 		break;	// 취소/목록 버튼
						case 'ITP_BTN_INOUTMNG03010_EXCEL'				: listObj.excelDown(); 				break;	// 엑셀다운로드
					}
				});
			});
			
			$('body').on('click', function(e) {
				
				var gridId = (INOUTMNG03010_DETAIL_GRID_ID).replace('#', '');
				var rids = $(INOUTMNG03010_DETAIL_GRID_ID).jqGrid('getDataIDs');
				var last_row_id = rids[rids.length - 1];
				
				itp_fn_grid_reset_selection(e, last_row_id, gridId, 'INOUTMNG03010_DETAIL');	
			});

		}
	},
	switchScreen: function(mode) {
		$('.itp_det_head').find('button[id^="ITP_BTN_INOUTMNG03010_"]').each(function(i) {
			$(this).hide();
		});
		$('#ITP_TAB_INOUTMNG03010').find('div[id$="_CONTAINER"]').each(function(i) {
			$(this).hide();
		});
		if(mode == 'LIST') { // 목록
			itp_fn_fire_window_resize();
			$('#ITP_AJAX_INOUTMNG03010_LIST_CONTAINER').show();
			$('#ITP_FORM_INOUTMNG03010_DELETE_WHS_NM').show();
			$('#ITP_FORM_INOUTMNG03010_DELETE_RTN_WHS_NM').show();
			fn_show_btn_auth_array(['#ITP_BTN_INOUTMNG03010_SRH', '#ITP_BTN_INOUTMNG03010_CUC']);
			$('#ITP_FORM_INOUTMNG03010_DETAIL_SBRAND_ID').val(fn_make_user_info.get('brandId'));
			
			$('#ITP_BTN_INOUTMNG03010_EXCEL').show();
		} else if(mode == 'DETAIL') { // 출하지시 상세
			$('#ITP_AJAX_INOUTMNG03010_DETAIL_CONTAINER').show();
			fn_show_btn_auth_array(['#ITP_BTN_INOUTMNG03010_CUD', '#ITP_BTN_INOUTMNG03010_CANCELLIST']);
			$('#ITP_FORM_INOUTMNG03010_DETAIL_BRAND_ID').val(fn_make_user_info.get('brandId'));
			
			//pageObj.grid.init(param);
		} 
	},
	grid: {
		init: function(param) {
			this.gridId = INOUTMNG03010_DETAIL_GRID_ID;
			this.gridList = INOUTMNG03010_DETAIL_GRID_LIST;
			this.gridEmpty = INOUTMNG03010_DETAIL_GRID_EMPTY;
			var now = new Date();
			$('#ITP_TAB_INOUTMNG03010 .input-group.date').datepicker(ITP_DATE_LANGUAGE);
			$('#ITP_FORM_INOUTMNG03010_DETAIL_COL_SCH_DT').datepicker('setDate', new Date(param.colSchDt));
			$('#ITP_FORM_INOUTMNG03010_DETAIL .fnBrandNm').text(param.brandNm);
			
			this.gridRows = param.recordData;
			this.unload();
			this.load(param.recordData);
			
		},
		gridId: '',
		button: {},
		gridList: '',
		gridEmpty: '',
		gridRows: '',
		load: function(gridRows) {
			
			var _this = this;
			var option = {
				gridId: _this.gridId,
				colModel: gridColModel.detail,
				data: gridRows,
				loadComplete: function(data) {
					$(INOUTMNG03010_DETAIL_GRID_LIST).find('.ui-jqgrid .ui-jqgrid-bdiv').css('overflow-x', 'hidden');
					data.records === 0 ? _this.clearData(mode) : $(INOUTMNG03010_DETAIL_GRID_EMPTY).hide();
					
				},
				onCellSelect: function(rowid, cellIdx, cellValue) {
					INOUTMNG03010_GRID_LAST_ROW_ID = itp_fn_set_data_editable(rowid, cellIdx, INOUTMNG03010_GRID_LAST_ROW_ID, _this.gridId, ['pchReqDtlNo']);
				}
			};
			itp_fn_grid_make_local(option);
		},
		clearData : function(mode) {
			$(INOUTMNG03010_DETAIL_GRID_ID).jqGrid('clearGridData', true);
			$(INOUTMNG03010_DETAIL_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(INOUTMNG03010_DETAIL_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(INOUTMNG03010_DETAIL_GRID_EMPTY).show();
		},
		unload : function() {
			$.jgrid.gridUnload(this.gridId);
		}
	}
};

/*목록화면 Object*/
let listObj = {
	init: function () {
		this.grid.init();
	},
	itp_INOUTMNG03010_search: false,
	button: {
		search: function() {
			listObj.itp_INOUTMNG03010_search = true;
			let param = $('#ITP_FORM_INOUTMNG03010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			param.gridPage = $.jgrid.defaults.page;
			$(INOUTMNG03010_GRID_ID).data('grid-param',param);
			$(INOUTMNG03010_GRID_ID).setGridParam({'postData': JSON.stringify(param)}).trigger('reloadGrid');
		},
		colcreat: function() {
			//선택되어진 row 값 전송할예정
			let gridUpdateData = [];
			const rowKey = $(INOUTMNG03010_GRID_ID).getGridParam('selrow');
			if (!rowKey) {
				return;
		    }

			var selectedIds = $(INOUTMNG03010_GRID_ID).getGridParam('selarrrow');
			
			var brandNm = "";
			var maxColReqDt = "";
			
			for (var i=selectedIds.length-1; i>=0; i--) {
				var rowData = $(INOUTMNG03010_GRID_ID).jqGrid('getRowData', selectedIds[i]);
				
				gridUpdateData.push(rowData);	
				if (rowData.colReqDt > maxColReqDt) {
					maxColReqDt = rowData.colReqDt;
				}
				sbrandNm =rowData.brandNm;
			}
			
			let param = {brandNm: sbrandNm, colSchDt: maxColReqDt };
			param.recordData = gridUpdateData;
			pageObj.switchScreen('DETAIL');
			itp_fn_form_clear_validate(null, '#ITP_FORM_INOUTMNG03010_DETAIL');
			pageObj.grid.init(param);
		}
	},
	empty: {
		init: function() {
			var _this = this;
			this.push();
			$(INOUTMNG03010_GRID_EMPTY).on('click', function() {
				_this.back();
				_this.itp_INOUTMNG03010_param.gridSize = $.jgrid.defaults.rowNum;
				$(INOUTMNG03010_GRID_ID).setGridParam({'postData': JSON.stringify(_this.itp_INOUTMNG03010_param)}).trigger('reloadGrid');
			});
		},
		itp_INOUTMNG03010_param: {},
		push: function() {
			let param = $('#ITP_FORM_INOUTMNG03010_SEARCH').serializeObject();
			listObj.itp_INOUTMNG03010_param = param;
		},
		back: function() {
			$('#ITP_FORM_INOUTMNG03010_SEARCH_FROM_DT').val(this.itp_INOUTMNG03010_param.fromDt);
			$('#ITP_FORM_INOUTMNG03010_SEARCH_TO_DT').val(this.itp_INOUTMNG03010_param.toDt);
			$('#ITP_FORM_INOUTMNG03010_SEARCH_SWHS_ID').val(this.itp_INOUTMNG03010_param.swhsId);
			$('#ITP_FORM_INOUTMNG03010_SEARCH_SWHS_NM').val(this.itp_INOUTMNG03010_param.swhsNm);
			$('#ITP_FORM_INOUTMNG03010_SEARCH_SRTN_WHS_NM').val(this.itp_INOUTMNG03010_param.srtnWhsId);
			$('#ITP_FORM_INOUTMNG03010_SEARCH_SRTN_WHS_ID').val(this.itp_INOUTMNG03010_param.srtnWhsNm);
			$('#ITP_FORM_INOUTMNG03010_SEARCH_ITEM_NM').val(this.itp_INOUTMNG03010_param.sitemNm);
		}
	},
	grid: {
		init: function () {
			// 데이터 없을때
			listObj.empty.push();
			itp_fn_jqgrid_resize(INOUTMNG03010_GRID_ID, INOUTMNG03010_GRID_LIST, 'lg');
			itp_fn_fire_window_resize();
			this.search();
			
		},
		colModel: gridColModel.list,
		search: function() {
			pageObj.switchScreen('LIST');
			this.unload();
			this.load();
		},
		load: function() {
			let param = $('#ITP_FORM_INOUTMNG03010_SEARCH').serializeObject();
			param.gridSize = $.jgrid.defaults.rowNum;
			
			var option = {
				multiselect: true,
				rownumbers: false,
				autowidth: true,
				gridId: INOUTMNG03010_GRID_ID,
				colModel: gridColModel.list,
				param: param,
				url: DOMAIN + API_DETAIL_GRID_LIST,
				pager: INOUTMNG03010_GRID_PAGER,
				loadComplete: function(data) {
					itp_fn_grid_load_complete(data, INOUTMNG03010_GRID_ID, true, 'number', 'INOUTMNG03010', listObj.itp_INOUTMNG03010_search, listObj.empty, true, data.gridRecords, true);
				},
				onPaging: function(action) {
					itp_fn_grid_paging(INOUTMNG03010_GRID_ID, action, param);
				}
			};
			itp_fn_grid_make_remote(option);
		},
		button: {
		},
		unload : function() {
			$.jgrid.gridUnload(INOUTMNG03010_GRID_ID);
		},
		clearData : function() {
			$(INOUTMNG03010_GRID_ID).jqGrid('clearGridData', true);
			$(INOUTMNG03010_GRID_LIST).find('.ui-jqgrid-bdiv').css('min-height', '100px');
			$(INOUTMNG03010_GRID_EMPTY).html(ITP_MSG_LOCALE.message.grid.noData);
			$(INOUTMNG03010_GRID_EMPTY).show();
		}
	},
	excelDown: function() {
		var param = {
			'url': API_DETAIL_GRID_LIST,
			'param': $('#ITP_FORM_INOUTMNG03010_SEARCH').serializeObject(),
			'gridColumns': $(INOUTMNG03010_GRID_ID).jqGrid('getGridParam', 'colModel'),
			'fileName': '수거지시대상정보.xlsx',
			'sheetName': '수거지시대상리스트'
		};
		itp_fn_remote_to_excel(param);
	}
};

/*수정화면 Object*/
let modifyObj = {
	init: function (param) {
		this.load(param);
	},
	button: {
		modify: function (param) {
			itp_fn_form_clear_validate(null, '#ITP_FORM_INOUTMNG03010_DETAIL');
			//const selectVal = $('#ITP_FORM_INOUTMNG03010_VIEW_STORE_ID').val();
			//const key = {brandId:fn_make_user_info.get('brandId'), storeId: selectVal, viewCd: 'R'};
			//var param = $.param(key);
			//modifyObj.load(param);
		},
		cancel: function() {
			document.getElementById('ITP_FORM_INOUTMNG03010_DETAIL').reset();
			listObj.grid.search();
		},
		save: function() {
			$('body').trigger('click');
			const formId = ('#ITP_FORM_INOUTMNG03010_DETAIL');
			itp_fn_form_clear_validate(null, formId);

			if (itp_fn_form_event.isValid(formId)) {
				
				var modalFn = {
					callBack: () => {
						itp_fn_form_event.removeChar('#ITP_FORM_OPER03010_DETAIL');
						var chkQtyYn = "N";
						let gridInsertData = [];
						const rowData = $(INOUTMNG03010_DETAIL_GRID_ID).getRowData();
						
						$.each(rowData, function(key, value) {
							
							if (value.colQty == '' || value.colQty < 1 ) {
								alert("수거수량을 입력해주세요!");
								chkQtyYn = "Y";
								return false;
							}
							
							
							if (value.rtnOdrQty > value.colQty ) {
								 alert("수거수량이 반품 발주수량보다 클수 없습니다!");
								chkQtyYn = "Y";
								return false;
							}
							
							gridInsertData.push(value);
						});
						
						if (chkQtyYn == "Y") return;
						
						let param = $(formId).serializeObject();
						param.viewCd = "C";
						param.gridInsertData = gridInsertData;
						console.log(JSON.stringify(param));
						var searhFn = function () {
							//ITP_FORM_INOUTMNG03010_DETAIL_IS_WRITING = false;
							pageObj.grid.clearData();
							modifyObj.button.cancel();
						}
						fn_ajax_call(API_DETAIL_SAVE, JSON.stringify(param), searhFn, 'POST');
					}
				};
				itp_fn_modal_confirm('수거지시 하시겠습니까?', modalFn);
			}
		}
	},
	
	load: function(param) {
		//console.log(param);
		//pageObj.grid.load("DETAIL",param);
	}
};


