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.Classification;
import com.yc.education.service.ClassificationService;
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 java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * huangpan
 * 2019/9/24
 * energy
 */
@Controller

public class ClassificationController {
    @Autowired
    ClassificationService classificationService;


//    @RequestMapping(value = {"","index"})
//    public ModelAndView index(){
//        return  new ModelAndView("redirect:/admin/journalism-list.html");
//    }

    /*查询所有*/
    @RequestMapping("admin/classification-list.html")
    public ModelAndView findClassification(){
        ModelAndView modelAndView=new ModelAndView();
         List<Classification> list=classificationService.getClassification();
        modelAndView.addObject("classificationList", list);
        return modelAndView;
    }
    /*修改*/
    @RequestMapping("admin/getClassificationId.html")
    public ModelAndView showClassification(@Param("id") int id){
        ModelAndView modelAndView=new ModelAndView("/admin/classification-update");
           Classification classi= classificationService.getClassificationId(id);
           modelAndView.addObject("classi", classi);
        return modelAndView;
    }
    /*新增*/
    @RequestMapping("admin/classification-add.html")
    public ModelAndView reqClassification(){
        return  new ModelAndView();
    }
    /*执行修改*/
    @RequestMapping(value = "admin/classification-update.html",method = RequestMethod.POST)
    public void  updateClassification(HttpServletRequest request, HttpServletResponse response, Classification classification)
            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);
            classification.setPresentationst(url);
        }
      /*  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String commentdatemin = df.format(new Date());
        classification.setReleasetime(commentdatemin);*/
            classificationService.updateNotNull(classification);

        response.sendRedirect("classification-list.html");
    }
    /*执行新增*/
    @RequestMapping(value = "admin/classification-save.html",method = RequestMethod.POST)
    public void classificationSave(HttpServletRequest request, HttpServletResponse response, Classification classification)
            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");
            classification.setPresentationst(url);
            if (classification.getReleasetime()==null){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String commentdatemin = df.format(new Date());
        classification.setReleasetime(commentdatemin);}
        classificationService.save(classification);
        response.sendRedirect("classification-list.html");
    }
    /*执行批量删除*//*
    @RequestMapping("admin/deleteSomeClassification")
    public void deleteSome(HttpServletRequest request, HttpServletResponse response,@Param("chkvalue") String chkvalue)throws Exception{
        String[] chkv=chkvalue.split(",");
        for (int i=0;i<chkv.length;i++){
            int item=Integer.parseInt(chkv[i]);
          classificationService.deleteClassification(item);
        }
        response.sendRedirect("/admin/classification-list.html");
    }*/
    /*删除*/
    @RequestMapping(value = "admin/deleteNewById.html",method = RequestMethod.POST)
    @ResponseBody
    public void deleteGetId(@Param("id") int id){
        classificationService.deleteClassification(id);
    }
}
