package com.yc.education.controller.admin;/**
 * huangpan
 * 2019/10/16
 * energy
 */

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.Coom;
import com.yc.education.model.Coomclassify;
import com.yc.education.service.CoomService;
import com.yc.education.service.CoomclassifyService;
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.bind.annotation.ResponseBody;
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;

/**
 *@ClassName CoomController
 *@Description TODO
 *@Author QuZhangJing
 *@Date 2019/10/16 11:02
 *@Version 1.0
 */
@Controller
public class CoomController {
    @Autowired
    CoomService coomService;
    @Autowired
    CoomclassifyService coomclassifyService;

    /*后台查询*/
    @RequestMapping("admin/coom-list.html")
    public ModelAndView selCoom(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                @RequestParam(value = "pageSize",defaultValue = "12") Integer pageSize){
        List<Coom> cooms = coomService.getCoomsPage(pageNum, pageSize);
        ModelAndView modelAndView=new ModelAndView();
        List<Coomclassify> coomFys = coomclassifyService.getCoomFys();
        modelAndView.addObject("cooms", new PageInfo<>(cooms));
        modelAndView.addObject("coomFys", coomFys);
        return modelAndView;
    }
    /*新增会议室*/
    @RequestMapping("admin/coom-add.html")
    public ModelAndView addcoom(){
        ModelAndView modelAndView=new ModelAndView();
        List<Coomclassify> coomFys = coomclassifyService.getCoomFys();
        modelAndView.addObject("coomFys", coomFys);
        return modelAndView;}
    /*执行新增*/
    @RequestMapping("admin/coom-save.html")
    public void savecoom(Coom coom,HttpServletResponse response,HttpServletRequest request)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 (coom.getReleasetime().equals("")){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String commentdatemin = df.format(new Date());
            coom.setReleasetime(commentdatemin);
        }
        coom.setSurfaceplot(url);
        coomService.save(coom);
        response.sendRedirect("coom-list.html");

    }
    /*查看会议室信息*/
    @RequestMapping("admin/coomById.html")
    public ModelAndView coomById(Integer id){
        Coom coomById = coomService.getCoomsById(id);
        ModelAndView modelAndView=new ModelAndView("admin/coom-update");
        List<Coomclassify> coomFys = coomclassifyService.getCoomFys();
        modelAndView.addObject("coomFys", coomFys);
        modelAndView.addObject("coomById", coomById);
        return modelAndView;
    }
    /*执行修改*/
    @RequestMapping("admin/coom-update.html")
    public void updatecoom(Coom coom, HttpServletResponse response, HttpServletRequest request)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&&!url.equals(""))
         coom.setSurfaceplot(url);
        coomService.updateNotNull(coom);
        response.sendRedirect("coom-list.html");

    }
    /*执行删除*/
    @RequestMapping("admin/delCoom.html")
    @ResponseBody
    public void delCoom(Integer id){
        coomService.delCoomById(id);
    }
}
