package com.style24.scm.biz.web;

import java.util.Collection;

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 com.style24.core.support.message.TscMessageByLocale;
import com.style24.persistence.domain.Brand;
import com.style24.persistence.domain.DeliveryLoc;
import com.style24.persistence.domain.DelvFeePolicy;
import com.style24.persistence.domain.SupplyCompany;
import com.style24.scm.biz.service.TssBusinessService;
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;

import com.gagaframework.web.rest.server.GagaResponse;

/**
 * 영업관리 Controller
 *
 * @author gagamel
 * @since 2020. 10. 14
 */
@Controller
@RequestMapping("/business")
@Slf4j
public class TssBusinessController extends TssBaseController {

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private TssBusinessService businessService;

	@Autowired
	private TssRendererService rendererService;

	/**
	 * 공급업체관리 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 14
	 */
	@GetMapping("/supply/company/form")
	public ModelAndView supplyCompanyForm() {
		ModelAndView mav = new ModelAndView();

		// 공급업체
		mav.addObject("supplyCompList", rendererService.getSupplyCompanyList(TssSession.getInfo().getSupplyCompCd()));

		// 입점상태
		mav.addObject("supplyStatList", rendererService.getAvailCommonCodeList("G010"));

		// 은행
		mav.addObject("bankList", rendererService.getAvailCommonCodeList("G940"));

		// 정산일
		mav.addObject("settleDayList", rendererService.getAvailCommonCodeList("G075"));

		mav.setViewName("/business/SupplyCompanyForm");

		return mav;
	}

	/**
	 * 공급업체 목록
	 * @param supplyComp - 공급업체 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 14
	 */
	@PostMapping("/supply/company/list")
	@ResponseBody
	public Collection<SupplyCompany> getSupplyCompanyList(@RequestBody SupplyCompany supplyComp) {
		if ("G001_B000".equals(TssSession.getInfo().getRoleCd())) {
			supplyComp.setSupplyCompCd(TssSession.getInfo().getSupplyCompCd());
		}
		return businessService.getSupplyCompanyList(supplyComp);
	}

	/**
	 * 공급업체 저장 처리
	 * @param supplyComp - 공급업체 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 14
	 */
	@PostMapping("/supply/company/save")
	@ResponseBody
	public GagaResponse saveSupplyCompany(@RequestBody SupplyCompany supplyComp) {
		supplyComp.setUpdNo(TssSession.getInfo().getUserNo());

		businessService.updateSupplyCompany(supplyComp);

		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 공급업체출고처관리 화면
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 16
	 **/
	@GetMapping("/supply/company/delvloc/form")
	public ModelAndView deliveryLocForm() {
		ModelAndView mav = new ModelAndView();

		// 공급업체
		mav.addObject("supplyCompList", rendererService.getSupplyCompanyList(TssSession.getInfo().getSupplyCompCd()));

		mav.setViewName("business/DeliveryLocForm");

		return mav;
	}

	/**
	 * 공급업체출고처 목록
	 * @param delvLoc - 출고처 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 16
	 **/
	@PostMapping("/supply/company/delvloc/list")
	@ResponseBody
	public Collection<DeliveryLoc> getDeliveryLocList(@RequestBody DeliveryLoc delvLoc) {
		delvLoc.setSupplyCompCd(TssSession.getInfo().getSupplyCompCd());
		return businessService.getDeliveryLocList(delvLoc);
	}

	/**
	 * 공급업체출고처 저장 처리
	 * @param delvLoc - 출고처 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 10. 16
	 */
	@PostMapping("/supply/company/delvloc/save")
	@ResponseBody
	public GagaResponse saveDeliveryLoc(@RequestBody DeliveryLoc delvLoc) {
		businessService.saveDeliveryLoc(delvLoc);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 배송비정책 목록
	 * @param supplyCompCd - 공급업체코드
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 24
	 **/
	@GetMapping("/delivery/fee/policy/list/{supplyCompCd}")
	@ResponseBody
	public Collection<DelvFeePolicy> getDeliveryFeePolicyList(@PathVariable String supplyCompCd) {
		DelvFeePolicy delvFeePolicy = new DelvFeePolicy();
		delvFeePolicy.setSupplyCompCd(supplyCompCd);
		return businessService.getDeliveryFeePolicyList(delvFeePolicy);
	}

	/**
	 * 배송비정책 등록/수정 처리
	 * @param delvFeePolicyList - 배송비정책 목록
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 24
	 */
	@PostMapping("/delivery/fee/policy/list/save")
	@ResponseBody
	public GagaResponse saveDeliveryFeePolicyList(@RequestBody Collection<DelvFeePolicy> delvFeePolicyList) {
		if (delvFeePolicyList == null || delvFeePolicyList.isEmpty()) {
			throw new IllegalStateException(message.getMessage("FAIL_1001"));
		}

		businessService.saveDeliveryFeePolicyList(delvFeePolicyList);
		return super.ok(message.getMessage("SUCC_0001"));
	}

	/**
	 * 브랜드 목록
	 * @param brand - 브랜드 정보
	 * @return
	 * @author eskim
	 * @since 2021. 04. 21
	 */
	@PostMapping("/brand/list")
	@ResponseBody
	public Collection<Brand> getBrandList(Brand brand) {

		return businessService.getBrandList(brand);
	}

	/**
	 * 공급업체 목록 화면(팝업)
	 * @param brandMd - 담당MD 정보
	 * @return
	 * @author xodud1202
	 * @since 2020. 12. 23
	 */
	@GetMapping("/comapny/search/form")
	public ModelAndView companySearchForm(SupplyCompany company) {
		ModelAndView mav = new ModelAndView();
		log.info("company={}", company);
		mav.addObject("params", company);
		mav.setViewName("business/SupplyCompanyPopupForm");
		return mav;
	}

	/**
	 * 브랜드 목록 화면(팝업)
	 * @param brand - 담당MD 정보
	 * @return
	 * @author gagamel
	 * @since 2020. 11. 4
	 */
	@GetMapping("/brand/search/form")
	public ModelAndView brandSearchForm(Brand brand) {
		ModelAndView mav = new ModelAndView();
		if ("G001_B000".equals(TssSession.getInfo().getRoleCd())) {
			brand.setSupplyCompCd(TssSession.getInfo().getSupplyCompCd());
		}
		mav.addObject("params", brand);
		mav.setViewName("business/BrandPopupForm");
		return mav;
	}

}
