package com.lemon.lifecenter.controller;

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

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.LifeCenterPaging;
import com.lemon.lifecenter.dto.PatientDTO;
import com.lemon.lifecenter.service.PatientService;

@Controller
@RequestMapping("/patient")
//@DependsOn(value = {"LifeCenterPaging"})
public class PatientController extends LifeCenterController {
    @Autowired
    private PatientService patientService;
    
    @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);
        
        mv.addObject("inputState", inputState);
        mv.addObject("patientName", patientName);
        mv.addObject("total", total);
        mv.addObject("item", result);
        mv.addObject("paging", paging);
        
        return mv;
    }

    @RequestMapping("/new")
    public ModelAndView patientNew() {
        ModelAndView mv = setMV("patient/new");

        return mv;
    }

    @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;
    }
}
