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.Classification;
import com.yc.education.model.Journalism;
import com.yc.education.service.ClassificationService;
import com.yc.education.service.JournalismService;
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/24
 * energy
 */
@RequestMapping("/admin/")
@Controller
public class JournalismController {
    @Autowired
    JournalismService journalismService;

    @Autowired
    ClassificationService classificationService;




    @RequestMapping(value = "tolist.html")
    public void toList(HttpServletRequest request,HttpServletResponse response)throws Exception{
        request.getRequestDispatcher("journalism-list.html").forward(request, response);
    }

    /*根据分页/标题查询所以数据*/
    @RequestMapping("journalism-list.html")
    public ModelAndView queryAll(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
                                 @RequestParam(value = "pageSize",defaultValue = "12") Integer pageSize, String titleName, Integer typesort, HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        List<Journalism> list = journalismService.getJournalism(pageNum,pageSize,titleName,typesort);
        PageInfo<List> pageInfo= new PageInfo(list);
        session.setAttribute("titleName", "");
        if(titleName!=null&&!titleName.equals("")){
            session.setAttribute("titleName", titleName);
        }
        session.setAttribute("typesort", "");
        if(typesort!=null&&!typesort.equals("")){
            session.setAttribute("typesort", typesort);
            System.out.println(session.getAttribute("typesort"));
        }
        session.setAttribute("Idcount", pageInfo.getTotal());
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.addObject("typeList", getClassifications());
        return modelAndView;

    }

    /*跳转新闻添加*/
    @RequestMapping(value = "journalism-save.html")
    public ModelAndView journamlismSave(){
      ModelAndView modelAndView  = new ModelAndView("/admin/journalism-add");
      modelAndView.addObject("typeList", getrecommendId());

        return modelAndView;
    }

    /*新闻添加*/
    @RequestMapping(value = "journalism-add")
    public void journamlistAdd(HttpServletRequest request, HttpServletResponse response,
                               Journalism journalism)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");
        journalism.setPicture(url);
        if (journalism.getReleaseTime().equals("")){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String commentdatemin = df.format(new Date());
            journalism.setReleaseTime(commentdatemin);
        }
       /* SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String commentdatemin = df.format(new Date());*/
       /* journalism.setReleaseTime(commentdatemin);*/
        journalismService.save(journalism);
        request.getRequestDispatcher("journalism-list.html").forward(request, response);
    }

    /*跳转新闻修改*/
    @RequestMapping(value = "getJournalismById.html")
    public ModelAndView getJournalismById(@RequestParam( value = "id")int id){
        ModelAndView modelAndView =new ModelAndView("/admin/journalism-update");
        Journalism journalism=journalismService.getJournalismId(id);
        modelAndView.addObject("journalism", journalism);
        modelAndView.addObject("typeList", getClassifications());
        return modelAndView;
    }

    /*修改单条新闻内容*/
    @RequestMapping("updateJournalismById.html")
    public void updateJournalismById(HttpServletRequest request, HttpServletResponse response,
                                     Journalism journalism) throws  Exception{
     /*   ModelAndView result = new ModelAndView("redirect:/admin/journalism-list.html");*/
        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);
        }
        journalismService.updateNotNull(journalism);
        request.getRequestDispatcher("journalism-list.html").forward(request, response);

    }

    /*删除单条新闻*/
    @RequestMapping( value = "deleteJournalism",method = RequestMethod.POST)
    @ResponseBody
    public void deleteJournalismId(@RequestParam( value = "id")int id){
        journalismService.deleteJournalismId(id);
    }

    /*批量删除*/
    @RequestMapping(value ="deleteSomeJournalism")
    public void deleteSomeJournalism(@RequestParam( value = "chkvalue") String chkvalue,HttpServletResponse response)throws  Exception{
        String[] skt=chkvalue.split(",");
        for (int i=0;i<skt.length;i++){
            journalismService.deleteJournalismId(Integer.parseInt(skt[i]));
        }
        response.sendRedirect("journalism-list.html");
    }

    /*查询所有的分类*/
    public List<Classification> getClassifications(){
        List<Classification> cl=classificationService.getClassification();

        return cl;
    }

    public List<Classification> getrecommendId(){
        List<Classification> classifications = classificationService.recommendId();
        return classifications;
    }
}
