package com.style24.scm.biz.web;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gagaframework.web.parameter.GagaMap;
import com.style24.core.biz.service.TscOrderService;
import com.style24.persistence.TscPageRequest;
import com.style24.persistence.domain.Order;
import com.style24.scm.biz.service.TssRendererService;
import com.style24.scm.support.controller.TssBaseController;
import com.style24.scm.support.security.session.TssSession;

import lombok.extern.slf4j.Slf4j;


/**
 * 주문 Controller
 *
 * @author jsh77b
 * @since 2021. 05. 18
 */
@Controller
@RequestMapping("/orderChange")
@Slf4j
public class TssOrderChangeController extends TssBaseController {

	@Autowired
	private TscOrderService coreOrderService;
	
	@Autowired
	private TssRendererService rendererService;
	
	@Autowired
	private Environment env;

	/**
	 * 반품요청 화면 (관리자)
	 * 
	 * @param ordNo - 주문번호
	 * @param cncWait - 취소대기요청 구분
	 * @return ModelAndView
	 * @author jsh77b
	 * @since 2020. 12. 22
	 */
	@GetMapping("/seller/withdraw/confirm/form")
	public ModelAndView rtnReqForm(@RequestParam(value = "ordNo") int ordNo, @RequestParam(value = "ordDtlNo") int ordDtlNo
			, @RequestParam(value = "delvFeeCd") String delvFeeCd, @RequestParam(value = "delvFeeCdGrp") String delvFeeCdGrp
			, @RequestParam(value = "ordChgSq") int ordChgSq) {
		
		Order order = new Order();
		order.setOrdChgSq(ordChgSq);
		
		// 2021.06.25 페이징 처리
		order.setPageable(new TscPageRequest(order.getPageNo() - 1, order.getPageSize()));
		order.getPageable().setTotalCount(1000);
		
		List<Order> orderWithdrawList = coreOrderService.getOrderSellerClaimList(order);
		int index 			= 0;
		String chgGb 		= "";
		String wdGb 		= "";
		String chgReason 	= "";
		String chgMemo 		= "";
		
		for (Order orderWithdraw : orderWithdrawList) {
			if (index == 0) {
				chgGb 		= orderWithdraw.getChgGb();
				wdGb 		= orderWithdraw.getWdGb();
				ordChgSq	= orderWithdraw.getOrdChgSq();
				ordNo		= orderWithdraw.getOrdNo();
				chgReason 	= orderWithdraw.getChgReason();
				chgMemo 	= orderWithdraw.getChgMemo();
			}
			index++;
		}
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("orderWithdrawList"	, orderWithdrawList);									// 회수컨펌대상목록
		mav.addObject("chgGb"				, chgGb);												// 변경구분 
		mav.addObject("wdGb"				, wdGb);												// 회수구분
		mav.addObject("ordChgSq"			, ordChgSq);											// 변경번호
		mav.addObject("ordNo"				, ordNo);												// 주문번호
		mav.addObject("chgReason"			, chgReason);											// 변경사유
		mav.addObject("chgMemo"				, chgMemo);												// 변경메모
		mav.addObject("chgReasonList1"		, rendererService.getAvailCommonCodeList("G688"));		// 반품사유
		mav.addObject("chgReasonList2"		, rendererService.getAvailCommonCodeList("G689"));		// 교환사유
		mav.setViewName("order/OrderWithdrawConfirmForm");
		
		return mav;
	}
	
	/**
	 * 입점업체 상품검수중 목록 회수컨펌 처리
	 * @param order - 주문정보
	 * @return Collection<Order>
	 * @author jsh77b
	 * @since 2020. 11. 16
	 */
	@PostMapping("/seller/widthdraw/confirm/update")
	@ResponseBody
	public GagaMap sellerWidthdrawConfirm(@RequestBody Order order) throws Exception {
		
		// 1. 회수컨펌
		int userNo = TssSession.getInfo().getUserNo();
		String result = coreOrderService.withdrawConfirm(order, userNo);
		
		GagaMap map = new GagaMap();
		map.set("msg", result);
		return map;
	}
	
	/**
	 * 입점업체 상품검수중 목록 회수컨펌목록 처리
	 * @param order - 주문정보
	 * @return Collection<Order>
	 * @author jsh77b
	 * @since 2020. 11. 16
	 */
	@PostMapping("/seller/widthdrawList/confirm/update")
	@ResponseBody
	public GagaMap sellerWidthdrawListConfirm(@RequestBody Collection<Order> orderList) throws Exception {
		
		// 1. 회수컨펌
		int userNo = TssSession.getInfo().getUserNo();
		String result = coreOrderService.withdrawConfirmList(orderList, userNo);
		
		GagaMap map = new GagaMap();
		map.set("msg", result);
		return map;
	}
}
