package com.yc.education.controller.admin;

import com.baidu.ueditor.ActionEnter;
import com.baidu.ueditor.define.ActionMap;
import com.baidu.ueditor.define.BaseState;
import com.github.pagehelper.PageInfo;
import com.yc.education.model.Team;
import com.yc.education.service.TeamService;
import org.apache.ibatis.annotations.Param;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * huangpan
 * 2019/9/26
 * energy
 */
@Controller
public class TeamController {
    @Autowired
    TeamService teamService;

    /*查询所有*/
    @RequestMapping("admin/team-list.html")
    public ModelAndView findTeam(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                 @RequestParam(value = "pageSize",defaultValue = "12") Integer pageSize,@Param("userName") String userName, HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        List<Team> list= teamService.getTeam(pageNum,pageSize,userName);
        session.removeAttribute("userName");
        if(userName!=null&&!userName.equals("")){
            session.setAttribute("userName", userName);
        }
        modelAndView.addObject("pageInfo", new PageInfo<>(list));
        return modelAndView;
    }
    /*修改*/
    @RequestMapping("admin/getTeamId.html")
    public ModelAndView showTeam(@Param("id") int id){
        ModelAndView modelAndView=new ModelAndView("/admin/team-update");
        Team classi= teamService.getTeamId(id);
        modelAndView.addObject("classi", classi);
        return modelAndView;
    }
    /*新增*/
    @RequestMapping("admin/team-add.html")
    public ModelAndView reqTeam(){
        return  new ModelAndView();
    }
    /*执行修改*/
    @RequestMapping(value = "admin/team-update.html",method = RequestMethod.POST)
    public void  updateTeam(HttpServletRequest request, HttpServletResponse response, Team team)
            throws Exception{
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getSession().getServletContext().getContextPath();
        BaseState state = (BaseState) new ActionEnter(request, rootPath, contextPath, "img").invoke(ActionMap.UPLOAD_FILE);
        String url = state.getInfo("url");
        if(url!=null&&!"".equals(url)){
            // journalism.setPicture(url);
            team.setTimg(url);
        }
       teamService.updateNotNull(team);

        response.sendRedirect("team-list.html");
    }
    /*执行新增*/
    @RequestMapping(value ="admin/team-save.html")
    public void TeamSave(HttpServletRequest request, HttpServletResponse response, Team team)throws Exception{
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getSession().getServletContext().getContextPath();
        BaseState state = (BaseState) new ActionEnter(request, rootPath, contextPath, "img").invoke(ActionMap.UPLOAD_FILE);
        String url = state.getInfo("url");
        if (team.getSex()==null){
            team.setSex("1");
        }
        team.setTimg(url);
        team.setTfpid(3);
        if (team.getReleasetime().equals("")){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String commentdatemin = df.format(new Date());
            team.setReleasetime(commentdatemin);
        }
        teamService.save(team);
        response.sendRedirect("team-list.html");
    }
    /*删除*/
    @RequestMapping(value = "admin/deleteTeamById.html",method = RequestMethod.POST)
    @ResponseBody
    public void deleteGetId(@Param("id") int id){
        teamService.deleteTeamId(id);
    }
}
