package com.dbnt.faisp.faStatistics.crackdownStatus;

import com.dbnt.faisp.authMgt.service.AuthMgtService;
import com.dbnt.faisp.faStatistics.crackdownStatus.model.CrackdownStatus;
import com.dbnt.faisp.faStatistics.crackdownStatus.model.FishingBoat;
import com.dbnt.faisp.faStatistics.crackdownStatus.model.ProcessResult;
import com.dbnt.faisp.faStatistics.crackdownStatus.service.CrackdownStatusService;
import com.dbnt.faisp.faStatistics.internationalCrimeArrest.model.InternationalCrimeArrest;
import com.dbnt.faisp.userInfo.model.UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/faStatistics")
public class CrackdownStatusController {

    private final AuthMgtService authMgtService;
    private final CrackdownStatusService crackdownStatusService;

    @RequestMapping("/crackdownStatus")
    public ModelAndView crackdownStatus(@AuthenticationPrincipal UserInfo loginUser, CrackdownStatus crackdownStatus) {
        ModelAndView mav = new ModelAndView("faStatistics/crackdownStatus/crackdownStatus");

        //메뉴권한 확인
        String accessAuth = authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/faStatistics/crackdownStatus").get(0).getAccessAuth();

        mav.addObject("accessAuth", accessAuth);

        crackdownStatus.setQueryInfo();
        List<CrackdownStatus> crackdownStatusList = crackdownStatusService.selectCrackdownStatusList(crackdownStatus);

        mav.addObject("crackdownStatusList", crackdownStatusList);
        crackdownStatus.setContentCnt(crackdownStatusService.selectCrackdownStatusListCnt(crackdownStatus));
        crackdownStatus.setPaginationInfo();
        mav.addObject("searchParams", crackdownStatus);
        return mav;
    }

    @GetMapping("/crackdownStatus/crackdownStatusEditModal")
    public ModelAndView crackdownStatusEditModal(@AuthenticationPrincipal UserInfo loginUser, CrackdownStatus crackdownStatus){
        ModelAndView mav = new ModelAndView("faStatistics/crackdownStatus/crackdownStatusEditModal");
        if(crackdownStatus.getCdsKey()!=null){
            crackdownStatus = crackdownStatusService.selectCrackdownStatus(crackdownStatus.getCdsKey());
        }else{
            crackdownStatus.setWrtOrgan(loginUser.getOgCd());
            crackdownStatus.setWrtNm(loginUser.getUserNm());
            crackdownStatus.setWrtDt(LocalDateTime.now());
        }
        mav.addObject("crackdownStatus", crackdownStatus);
        return mav;
    }

    @PostMapping("/crackdownStatus/saveCrackdownStatus")
    public Integer saveCrackdownStatus(@AuthenticationPrincipal UserInfo loginUser,
                                       CrackdownStatus crackdownStatus,
                                       FishingBoat fishingBoat,
                                       ProcessResult processResult){
        crackdownStatus.setWrtUserSeq(loginUser.getUserSeq());
        crackdownStatus.setFishingBoat(fishingBoat);
        crackdownStatus.setProcessResult(processResult);
        return crackdownStatusService.saveCrackdownStatus(crackdownStatus);
    }
}
