package com.lemon.lifecenter.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lemon.lifecenter.common.LifeCenterConfigVO;
import com.lemon.lifecenter.common.LifeCenterController;
import com.lemon.lifecenter.common.LifeCenterFunction;
import com.lemon.lifecenter.common.LifeCenterPaging;
import com.lemon.lifecenter.common.LifeCenterSessionController;
import com.lemon.lifecenter.dto.CenterInfoDTO;
import com.lemon.lifecenter.dto.PatientDTO;
import com.lemon.lifecenter.service.CenterService;
import com.lemon.lifecenter.service.PatientService;

@Controller
@RequestMapping("/patient")
//@DependsOn(value = {"LifeCenterPaging"})
public class PatientController extends LifeCenterController {
    @Autowired
    private PatientService patientService;
    
    @Autowired
    private CenterService centerService;
    
    @Autowired
    private LifeCenterConfigVO config;
    
    private LifeCenterPaging paging;
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping("/list")
//    @ModelAttribute("dto") final ASiboDTO dto,
//    @RequestParam( value="k", required=true ) String k
//    @RequestParam( value="type", required=false, defaultValue="" ) String type
    public ModelAndView patientList(
            @RequestParam(value="inputState", required=false, defaultValue="ALL") String inputState,
            @RequestParam(value="patientName", required=false, defaultValue="") String patientName,
            @RequestParam(value="startDate", required=false, defaultValue="") String startDate,
            @RequestParam(value="endDate", required=false, defaultValue="") String endDate,
            @RequestParam(value="page", required=false, defaultValue="1") int page ) {
        
        System.err.println( "page : " + page );
        
        PatientDTO dto = new PatientDTO();
        dto.setPatientName(patientName);
        dto.setState(inputState);
        dto.setStartDate(startDate);
        dto.setEndDate(endDate);
        dto.setLimit( ( Integer.valueOf( page ) - 1 ) * config.pageDataSize );
        dto.setLimitMax( config.pageDataSize );
        
        int total = patientService.selectPatientCount(dto);
        List<PatientDTO> result = new ArrayList<PatientDTO>();

        if (total > 0) {
            result = patientService.selectPatietList(dto);
        }
        
        ModelAndView mv = setMV("patient/list");
        
        System.out.println( "PatientController config.pageGroupSize : " + config.pageGroupSize );
        
        String param = "patientName=" + patientName + "&inputState=" + inputState + "&startDate=" + startDate + "&endDate=" + endDate;
        paging = LifeCenterPaging.getInstance();
        paging.paging(config, total, page, param);
        
        startDate = startDate.equals( "" ) ? LifeCenterFunction.getNow( "yyyy-MM-dd" ) : startDate;
        endDate   = endDate.equals( "" )   ? LifeCenterFunction.getNow( "yyyy-MM-dd" ) : endDate;
        
        mv.addObject("inputState", inputState);
        mv.addObject("patientName", patientName);
        mv.addObject("total", total);
        mv.addObject("item", result);
        mv.addObject("paging", paging);
        
        mv.addObject( "startDate", startDate );
        mv.addObject( "endDate", endDate );
        mv.addObject( "page", page );
        mv.addObject( "pageSize", dto.getLimitMax() );
        
        return mv;
    }

    @RequestMapping("/new")
    public ModelAndView patientNew( HttpServletRequest request,HttpServletResponse response ) {
        ModelAndView mv = setMV("patient/new");
        
        String sesCenterCode  = LifeCenterSessionController.getSession( request, "sesCenterCode" );
        
        System.out.println( "sesCenterCode: " + sesCenterCode );
        
        // 세션의 센터코드 들고와서 치료센터명 조회후 view 전달
        CenterInfoDTO dto = new CenterInfoDTO();
        dto.setCenterCode( Integer.valueOf( sesCenterCode ) );
        
        int centerCount   = centerService.selectCenterCount( dto );
        
        String centerName = "";
        
        if( centerCount > 0 ) {
            centerName = centerService.selectCenterInfo( dto ).getCenterName();
        }
        
        System.out.println("centerCount : " + centerCount );
        mv.addObject( "centerName", centerName );
        mv.addObject( "centerCount", centerCount );
        
        return mv;
    }
    
    @RequestMapping("/new/insert")
    public String patientNewInsert() {
        ModelAndView mv = setMV("patient/info");
        
        // 트랜잭션
        // 환자기본정보 insert
        // 환자 기저질환 여부 insert (예 선택인경우)
        // 현재증상 insert
        
        return "redirect:/patient/info";
    }

    @RequestMapping("/info")
    public ModelAndView patientInfo() {
        ModelAndView mv = setMV("patient/info");

        return mv;
    }

    @RequestMapping("/edit")
    public ModelAndView patientEdit() {
        ModelAndView mv = setMV("patient/edit");

        return mv;
    }

    @RequestMapping("/privacy")
    public ModelAndView patientPrivacy() {
        ModelAndView mv = setMV("patient/privacy");

        return mv;
    }
}
