package com.jeeplus.modules.oa.web;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.modules.oa.entity.Leave;
import com.jeeplus.modules.oa.service.LeaveService;
import com.jeeplus.modules.oa.service.ManageCalendarService;
import com.jeeplus.modules.oa_evection.entity.oa_evection.OaEvection;
import com.jeeplus.modules.oa_evection.service.oa_evection.OaEvectionService;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.work.entity.report.WorkReport;
import com.jeeplus.modules.workborrowmangement.entity.WorkBorrowMangement;
import com.jeeplus.modules.workgoout.entity.GoOut;
import com.jeeplus.modules.workgoout.service.GoOutService;
import com.jeeplus.modules.workovertimeform.entity.WorkOvertimeForm;
import com.jeeplus.modules.workovertimeform.service.WorkOvertimeFormService;
import com.jeeplus.modules.worksealform.entity.WorkSealForm;
import com.jeeplus.modules.worksealform.service.WorkSealFormService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2017/8/24.
 */
@Controller
@RequestMapping("${adminPath}/oa/manageCalendar")
public class OaManageCalendarController {

    @Autowired
    private ManageCalendarService manageCalendarService;
    @Autowired
    private WorkSealFormService workSealFormService ;
    @Autowired
    private LeaveService leaveService;
    @Autowired
    private OaEvectionService evectionService;
    @Autowired
    private WorkOvertimeFormService workOvertimeFormService;
    @Autowired
    private GoOutService goOutService;

    @RequestMapping(value={"","getManageCalendarInfoByMonth"})
    public ModelAndView manageCalendarInfo(@RequestParam(value = "date",required = false) String date ,HttpServletRequest request, @RequestParam(value="userId",required = false) String userId){
        ModelAndView mv = new ModelAndView("modules/oa/manageCalendar");
        if(!UserUtils.isManager()){
            request.setAttribute("message","您在本公司非管理员，无权查看管理日志");
            return mv;
        }
        if(date==null||"".equals(date)){
            date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        }
        User user;
        if(UserUtils.getUser()!=null){
            user =UserUtils.getUser();
        }else {
            user =UserUtils.get(userId);
        }
        String companyId = null ;
        if(!user.isAdmin()){
            companyId = user.getComId();
        }
        mv.addObject("user",user);
        String yearmonthStr = date.substring(0,7);
        //本公司当月的记录   某一天 --- n条记录
        List thisMonthRecordList =  manageCalendarService.getManageCalendarByMonth2(companyId,yearmonthStr);

        mv.addObject("thisMonthRecordList",thisMonthRecordList);
        return mv;
    }
    @RequestMapping("/getManageCalendarInfoByDate")
    public ModelAndView getManageCalendarInfoByDate(HttpServletRequest request,@RequestParam(value="userId",required = false)String userId,@RequestParam(value="date",required = false)String date){
        ModelAndView mv = new ModelAndView("modules/oa/manageCalendarDetails");
        User login_user = UserUtils.get(userId);
        List manageCalendarDetailsList =  manageCalendarService.getReportUserList(date);

        mv.addObject("login_user",login_user);
        mv.addObject("manageCalendarDetailsList",manageCalendarDetailsList);

        return mv;
    }
    @RequestMapping("workSealForm")
    public String showWorkSealForm(@RequestParam("id")String id ,Model model){
        WorkSealForm workSealForm = workSealFormService.get(id);
        model.addAttribute("workSealForm",workSealForm);
        return "modules/oa/audits-workSealForm";
    }

    @RequestMapping("leaveForm")
    public String showLeaveForm(@RequestParam("id")String id , Model model){
        Leave leave = leaveService.get(id);
        model.addAttribute("leave",leave);
        return "modules/oa/audits-leaveForm";
    }

    @RequestMapping("evectionForm")
    public String showEvectionForm(@RequestParam("id")String id , Model model){
        OaEvection oaEvection = evectionService.get(id);
        model.addAttribute("oaEvection",oaEvection);
        return "modules/oa/audits-evectionForm";
    }

    @RequestMapping("workOvertimeForm")
    public String showWorkOvertimeForm(@RequestParam("id")String id,Model model){
        WorkOvertimeForm workOvertimeForm = workOvertimeFormService.get(id);
        model.addAttribute("workOvertimeForm",workOvertimeForm);
        return "modules/oa/audits-workOvertimeForm";
    }

    @RequestMapping("goOutForm")
    public String showGoOutForm(@RequestParam("id")String id ,Model model){
        GoOut goOut = goOutService.get(id);
        model.addAttribute("goOut",goOut);
        return "modules/oa/audits-goOutForm";
    }


}
