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.yc.education.model.Aboutus;
import com.yc.education.service.AboutusService;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * huangpan
 * 2019/9/26
 * energy
 */
@Controller
public class AboutusController {
    @Autowired
    AboutusService aboutusService;


    /*查看*/
    @RequestMapping("admin/aboutus-list.html")
    public ModelAndView getAboutus(){
        ModelAndView modelAndView=new ModelAndView();
           List<Aboutus> getlist=aboutusService.getAboutus();
           modelAndView.addObject("aboutusList", getlist);
           return  modelAndView;
    }
    /*修改*/
    @RequestMapping("admin/getAboutusId.html")
    public ModelAndView getAcoutusTd(@Param("id") int id){
        ModelAndView modes=new ModelAndView("/admin/aboutus-update");
        Aboutus aboutus =aboutusService.getAboutusId(id);
        modes.addObject("aboutus", aboutus);
        return  modes;
    }
    /*执行修改*/
    @RequestMapping(value = "admin/aboutus-update.html",method = RequestMethod.POST )
    public void updateAbcoutus(HttpServletResponse response, HttpServletRequest request, Aboutus aboutus, HttpSession session)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)){
        aboutus.setUsimg(url);
        }
        aboutusService.updateNotNull(aboutus);
        List<Aboutus> aboutuses=aboutusService.getAboutus();
        session.setAttribute("aboutusSession", aboutuses);
        response.sendRedirect("aboutus-list.html");
    }
    /*新增*/
    @RequestMapping("admin/aboutus-add.html")
    public ModelAndView abcoutusAdd(){
        return new ModelAndView();
    }
    /*执行新增*/
    @RequestMapping("admin/aboutus-save.html")
    public void saveAboutus(HttpServletResponse response, HttpServletRequest request,Aboutus aboutus)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");
        aboutus.setUsimg(url);
        aboutus.setUrltests("#");
        aboutusService.save(aboutus);
        response.sendRedirect("aboutus-list.html");

    }
    /*删除*/
    @RequestMapping(value = "admin/deleteAoutusById.html",method = RequestMethod.POST)
    @ResponseBody
    public void deleteGetId(@Param("id") int id){
        aboutusService.delAboutusId(id);
    }

}
