package com.style24.admin.biz.web;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
//import org.apache.commons.collections.CollectionUtils;
//import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.parameter.GagaMap;
import com.gagaframework.web.rest.server.GagaResponse;
import com.style24.persistence.TscPageRequest;
import com.style24.persistence.domain.CommonCode;
import com.style24.persistence.domain.Delivery;
import com.style24.persistence.domain.Pos;
import com.style24.persistence.domain.Withdraw;
import com.style24.persistence.domain.WithdrawExc;
import com.style24.persistence.domain.WmsWithdraw;
import com.style24.persistence.domain.OrderChange;
import com.style24.admin.biz.service.TsaDeliveryService;
import com.style24.admin.biz.service.TsaRendererService;
import com.style24.admin.biz.service.TsaWithdrawService;
import com.style24.admin.biz.service.TsaWmsWithdrawService;
import com.style24.admin.support.controller.TsaBaseController;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.core.biz.service.TscOrderChangeService;


/**
 * 회수관리 Controller
 * 
 * @author moon
 * @since 2020. 11. 16
 */
@Controller
@RequestMapping("/withdraw")
@Slf4j
public class TsaWithdrawController extends TsaBaseController {

	@Autowired
	private TsaWithdrawService withdrawService;

	@Autowired
	private TsaRendererService rendererService;

	@Autowired
	private TsaDeliveryService deliveryService;

	@Autowired
	private TscMessageByLocale message;
	
	@Autowired
	private TsaWmsWithdrawService wmsWithdrawService;
	
	@Autowired
	private TscOrderChangeService orderChangeService;	
	
	

	
	/**
	 * 환불관리 목록 화면
	 * 
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2020. 11. 16
	 */
	@RequestMapping(value = "/refund/form")
	public ModelAndView withdrawRefundForm() throws Exception {
		ModelAndView mav = new ModelAndView();

		// 사이트구분
		mav.addObject("siteCdList", rendererService.getAvailCommonCodeList("G000"));

		mav.setViewName("withdraw/RefundRegisterListForm");

		return mav;
	}

	/**
	 * 환불관리 목록
	 * 
	 * @param  withdraw
	 * @return Collection<Withdraw>
	 * @author moon
	 * @since 2020. 11. 16
	 */
	@PostMapping("/refund/list")
	@ResponseBody
	public GagaMap getRefundList(@RequestBody Withdraw withdraw) {
		
		GagaMap result = new GagaMap();
		withdraw.setPageable(new TscPageRequest(withdraw.getPageNo() - 1, withdraw.getPageSize()));
		withdraw.getPageable().setTotalCount(withdrawService.getRefundListCount(withdraw));
		if ("N".equals(withdraw.getPageingYn())) {
			withdraw.setPageable(null);
		}
		result.set("pageing", withdraw);
		result.set("refundList", withdrawService.getRefundList(withdraw));
		
		
		return result;
	}
	
	/**
	 * 환불관리 상세 화면
	 * 
	 * @param ordNo
	 * @return 회수 상세 화면
	 * @throws Exception
	 * @author moon
	 * @since 2020. 11. 16
	 */
	@GetMapping("/refund/detail/form")
	@ResponseBody
	public ModelAndView refundDetailForm(Withdraw withdraw) {
		ModelAndView mav = new ModelAndView();
		log.info("withdraw {}"+withdraw.toString());
		mav.addObject("ordChgSq", withdraw.getOrdChgSq());
		mav.addObject("ordNo", withdraw.getOrdNo());
		mav.addObject("userNo", withdraw.getUserNo());
		
		OrderChange orderChange= new OrderChange();
		orderChange.setOrdChgSq(withdraw.getOrdChgSq());
		orderChange.setUserNo(withdraw.getUserNo());
		//orderChangeService.refundConfirmPreInfo(orderChange);
		//mav.addObject("refundConfirmPreInfo", orderChangeService.refundConfirmPreInfo(orderChange));
		//mav.addObject("dataList", withdrawService.getRefundDetailList(withdraw));
		
		mav.setViewName("withdraw/RefundDetailForm");
		return mav;
	}

	/**
	 * 환불관리상세 정보
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2020. 11. 16
	 */
	@PostMapping("/detail/list")
	@ResponseBody
	public GagaMap getRefundDetailList(@RequestBody Withdraw withdraw) {
		
		OrderChange orderChange= new OrderChange();
		orderChange.setOrdChgSq(withdraw.getOrdChgSq());
		orderChange.setUserNo(withdraw.getUserNo());
		//orderChangeService.refundConfirmPreInfo(orderChange);
		
		GagaMap result = new GagaMap();
		
		result.set("refundConfirmPreInfo"	, orderChangeService.refundConfirmPreInfo(orderChange));
		result.set("dataList"				, withdrawService.getRefundDetailList(withdraw));
		
	//	Collection<Withdraw> dataList = withdrawService.getRefundDetailList(withdraw);
		return result;
	}

	/**
	 * 환불관리상세 - 환불
	 * 
	 * @param withdraw
	 * @return GagaResponse
	 * @author moon
	 * @since 2021. 05. 31
	 */
	@PostMapping("/detail/confirm")
	@ResponseBody
	public GagaResponse refundDetailConfirm(@RequestBody GagaMap orderChange) {
		log.info("orderChange {}"+orderChange);

		withdrawService.refundDetailConfirm(orderChange);
		return super.ok(message.getMessage("SUCC_0004"));
	}	
	
	/**
	 * 회수예외관리 화면 
	 *
	 * @param
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2021. 02. 19
	 */
	@GetMapping("/exception/form")
	@ResponseBody
	public ModelAndView withdrawExceptionListForm() throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.addObject("reasonCodeList", rendererService.getAvailCommonCodeList("G325"));
		mav.setViewName("/withdraw/WithdrawExceptionListForm");

		return mav;
	}
	
	/**
	 * 회수예외관리 조회 
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2021. 03. 04
	 */
	@PostMapping("/exception/list")
	@ResponseBody
	public GagaMap getWithdrawExceptionList(@RequestBody WithdrawExc withdrawExc) throws Exception {
		
		GagaMap result = new GagaMap();
		withdrawExc.setPageable(new TscPageRequest(withdrawExc.getPageNo() - 1, withdrawExc.getPageSize()));
		withdrawExc.getPageable().setTotalCount(withdrawService.getWithdrawExceptionListCount(withdrawExc));
		if ("N".equals(withdrawExc.getPageingYn())) {
			withdrawExc.setPageable(null);
		}
		result.set("pageing", withdrawExc);
		result.set("withdrawExceptionList", withdrawService.getWithdrawExceptionList(withdrawExc));
		

		return result;
	}
	
	
	/**
	 * 회수예외관리 - 완료처리
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2021. 03. 04
	 */
	@PostMapping("/exception/complete")
	@ResponseBody
	public GagaResponse updateWithdrawException(@RequestBody WithdrawExc withdrawExc) {
		withdrawService.createWithdrawInfo(withdrawExc);
		
		return super.ok(message.getMessage("SUCC_0001"));
	}
	
	/**
	 * 회수예외관리 - 사유변경(팝업)
	 *
	 * @param
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2021. 05. 20
	 */
	@GetMapping("/exception/change/form")
	@ResponseBody
	public ModelAndView saveInvoiceForm(WithdrawExc withdrawExc) {
		
		ModelAndView mav = new ModelAndView();
		Collection<WithdrawExc> list  = withdrawService.getWithdrawExceptionChangeList(withdrawExc);
		WithdrawExc data = list.iterator().next();
		
		GagaMap rtnMap = withdrawService.getRefundAmt(list);
		
		
		mav.addObject("orderWithdrawList", list);
		if("G680_30".equals(withdrawExc.getChgGb())) {
			mav.addObject("chgReasonList", rendererService.getAvailCommonCodeList("G688"));
		} else {
			mav.addObject("chgReasonList", rendererService.getAvailCommonCodeList("G689"));
		}
		
		mav.addObject("ordChgSq",  data.getOrdChgSq());		
		mav.addObject("chgGb",     withdrawExc.getChgGb());		
		mav.addObject("wdGb",      data.getWdGb());
		mav.addObject("chgReason", data.getChgReason());
		mav.addObject("chgMemo",   data.getChgMemo());
		mav.addObject("delvFee",   data.getDelvFee());
		mav.addObject("rtnDelvFee",data.getRtnDelvFee());		
		mav.addObject("spanRefundAmt",   rtnMap.getInt("spanRefundAmt"));

		mav.setViewName("withdraw/WithdrawChangePopForm");
		return mav;
	} 
	
	/**
	 * 회수예외관리 - 사유변경(팝업) 저장 
	 *
	 * @param
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2021. 05. 21
	 */
	@PostMapping("/change/save")
	@ResponseBody
	public GagaResponse saveDeliveryComplete(@RequestBody WithdrawExc withdrawExc) throws Exception {

		if (withdrawExc == null) {
			throw new IllegalStateException(message.getMessage("FAIL_1002"));
		}
		
		withdrawService.saveChangeReason(withdrawExc);
		return super.ok(message.getMessage("SUCC_0004"));
	}
	
	
	/**
	 * WMS회수목록 화면 
	 *
	 * @param
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2021. 03. 10
	 */
	@GetMapping("/wms/list/form")
	@ResponseBody
	public ModelAndView wmsWithdrawListForm() throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/withdraw/WmsWithdrawListForm");

		return mav;
	}
	
	/**
	 *WMS회수목록 조회 
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2021. 03. 10
	 */
	@PostMapping("/wms/list")
	@ResponseBody
	public GagaMap getWmsWithdrawList(@RequestBody WmsWithdraw wmsWithdraw) throws Exception {
		
		GagaMap result = new GagaMap();
		wmsWithdraw.setPageable(new TscPageRequest(wmsWithdraw.getPageNo() - 1, wmsWithdraw.getPageSize()));
		wmsWithdraw.getPageable().setTotalCount(wmsWithdrawService.getWmsWithdrawListCount(wmsWithdraw));
		if ("N".equals(wmsWithdraw.getPageingYn())) {
			wmsWithdraw.setPageable(null);
		}
		result.set("pageing", wmsWithdraw);
		Collection<WmsWithdraw> wmsList = wmsWithdrawService.getWmsWithdrawList(wmsWithdraw);
		
		wmsList = withdrawService.getOptionInfo(wmsList);
		
		result.set("wmsWithdrawList", wmsList);
		

		return result;
	}	
	
	
	/**
	 * 회수지시목록 화면 
	 *
	 * @param
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2021. 05. 10
	 */
	@GetMapping("/directive/form")
	@ResponseBody
	public ModelAndView withdrawDirectiveListForm() throws Exception {
		ModelAndView mav = new ModelAndView();

		mav.addObject("wdShipStateList", rendererService.getAvailCommonCodeList("G320"));
		mav.addObject("chgStatList", rendererService.getAvailCommonCodeList("G685"));
		
		
		mav.setViewName("/withdraw/WithdrawDirectiveListForm");

		return mav;
	}
	
	/**
	 *회수목록 조회 
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2021. 05. 10
	 */
	@PostMapping("/direct/list")
	@ResponseBody
	public GagaMap withdrawDirectiveList(@RequestBody Withdraw withdraw) throws Exception {
		
		GagaMap result = new GagaMap();
		withdraw.setPageable(new TscPageRequest(withdraw.getPageNo() - 1, withdraw.getPageSize()));
		
		withdraw.getPageable().setTotalCount(withdrawService.getWithdrawDirectiveListCount(withdraw));
		
		if ("N".equals(withdraw.getPageingYn())) {
			withdraw.setPageable(null);
		}
		result.set("pageing", withdraw);
		log.info("withdraw {}"+withdraw);
		Collection<Withdraw> list = withdrawService.getWithdrawDirectiveList(withdraw);
		
		result.set("directList", list);
		

		return result;
	}	
	
	/**
	 * 출고처지정 취소 
	 *
	 * @param
	 * @return
	 * @throws Exception
	 * @author moon
	 * @since 2020. 04. 20
	 */
	@PostMapping("/direct/recallorder")
	@ResponseBody
	public GagaResponse reRecallOrder(@RequestBody Withdraw param) throws Exception {
		withdrawService.reRecallOrder(param);
		return super.ok(message.getMessage("SUCC_0001"));
	}


}
