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.Culture;
import com.yc.education.service.CultureService;
import com.yc.education.service.HistoryService;
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.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * huangpan
 * 2019/9/26
 * energy
 */
@Controller
public class CultureController {
    @Autowired
    CultureService cultureService;
    @Autowired
    HistoryService historyService;
    public int codid = 1;

    /*公司简介*/
    @RequestMapping("admin/about-list.html")
    public ModelAndView getAbout(int id) {
        ModelAndView modelAndView = new ModelAndView("/admin/culture-list");
        Culture cultures = cultureService.getCulture(id);
        modelAndView.addObject("cultures", cultures);
        return modelAndView;
    }
   /* *//*公司历程*//*
    @RequestMapping("admin/historys-list.html")
    public ModelAndView getHistorys(@Param("id") int id,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                    @RequestParam(value = "pageSize",defaultValue = "12") Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView("/admin/culture-list");
        Culture cultures = cultureService.getCulture(id);
        modelAndView.addObject("cultures", cultures);
        List<History> list=historyService.getHistory(pageNum,pageSize);
        modelAndView.addObject("pageInfo", new PageInfo<>(list));
        return modelAndView;*/
  /*  }*/
    /*公司文化*/
    @RequestMapping("admin/culture-list.html")
    public ModelAndView getculture(@Param("id") int id) {
        //System.out.println(id+"=========");
        ModelAndView modelAndView = new ModelAndView();
        Culture cultures = cultureService.getCulture(id);
        modelAndView.addObject("cultures", cultures);
        return modelAndView;
    }

    /*跳转至修改页面*/
    @RequestMapping("admin/getCultureId.html")
    public ModelAndView getCultureId(@Param("id") int id) {
        ModelAndView modelAndView = new ModelAndView("");
        if (id==2){
            modelAndView.setViewName("admin/cultures-update");
        }else{
            modelAndView.setViewName("/admin/culture-update");
        }
        Culture culture = cultureService.getCultureId(id);
        modelAndView.addObject("culture", culture);
        return modelAndView;
    }
    /*通过id修改信息*/
    @RequestMapping("admin/culture-update.html")
    public ModelAndView updateCulture(HttpServletResponse response, HttpServletRequest request, Culture culture) 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("")) {
            culture.setCimg(url);
        }
        cultureService.updateNotNull(culture);
        if (culture.getCfpid()==2){
            return new ModelAndView("redirect:history-list.html?id="+ culture.getCfpid());
        }else {
            return new ModelAndView("redirect:culture-list.html?id=" + culture.getCfpid());
        }
    }

}
