package com.yc.education.controller.admin;/**
 * huangpan
 * 2019/10/15
 * 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.Personnel;
import com.yc.education.service.CoomService;
import com.yc.education.service.CoomclassifyService;
import com.yc.education.service.OrdersService;
import com.yc.education.service.PersonnelService;
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 javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *@ClassName PersonnelController
 *@Description TODO
 *@Author QuZhangJing
 *@Date 2019/10/15 13:50
 *@Version 1.0
 */
@Controller
public class PersonnelController {
    @Autowired
    PersonnelService personnelService;

    @Autowired
    OrdersService ordersService;

    @Autowired
    CoomService coomService;
    @Autowired
    CoomclassifyService coomclassifyService;

    /*员工登录判断*/
    @RequestMapping("center.html")
    public ModelAndView getPensonl(String name, String password, HttpServletResponse response, HttpSession session, HttpServletRequest request)throws  Exception{
    ModelAndView modelAndView=new ModelAndView();

       Personnel sesPer= (Personnel) session.getAttribute("sesionPer");
    if(sesPer!=null&&!sesPer.equals("")){
        modelAndView.setViewName("xingtai/center");
        Personnel personnelLogins = personnelService.getPersonnelLogin(sesPer.getUsername(), sesPer.getPassword());
        modelAndView.addObject("personnelLogin", personnelLogins);
    }else {
        Personnel personnelLogin = personnelService.getPersonnelLogin(name, password);
        if (personnelLogin != null) {
            modelAndView.setViewName("xingtai/center");
            modelAndView.addObject("personnelLogin", personnelLogin);
            session.setAttribute("sesionPer", personnelLogin);
            session.setAttribute("userName", personnelLogin.getName());
            session.setAttribute("urls", personnelLogin.getHportrail());
        } else {

            return  new ModelAndView("redirect:index.html?meageests=1");
        }
    }
        return modelAndView;
    }
    /*员工页面修改*/
    @RequestMapping("pensonl-update.html")
    public ModelAndView updatePensonl(Personnel personnel, HttpServletRequest request,HttpSession session){
        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("")) {
        personnel.setHportrail(url);
        session.setAttribute("urls", personnel.getHportrail());
        }
        session.setAttribute("userName", personnel.getName());
        personnelService.updateNotNull(personnel);
        return new ModelAndView("redirect:center.html");
    }


    /*后台查询员工表*/
    @RequestMapping("admin/personnel-list.html")
    public ModelAndView selPersonnel(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
            @RequestParam(value = "pageSize",defaultValue = "12") Integer pageSize,String titleName){
        List<Personnel> personnels = personnelService.getPersonnelPage(pageNum, pageSize, titleName);
      /*  personnels.remove(0);*/
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("personnelPage", new PageInfo<>(personnels));
        modelAndView.addObject("titleName", titleName);
        return modelAndView;
    }
    /*新增员工*/
    @RequestMapping("admin/personnel-add.html")
    public ModelAndView addPersonnel(){return new ModelAndView();}
    /*执行新增*/
    @RequestMapping("admin/personnel-save.html")
    public void savePersonnel(Personnel personnel,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");
        personnel.setHportrail(url);
        if (personnel.getReleasetime().equals("")){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String commentdatemin = df.format(new Date());
            personnel.setReleasetime(commentdatemin);
        }
        personnelService.save(personnel);
        response.sendRedirect("personnel-list.html");

    }
    /*查看员工信息*/
    @RequestMapping("admin/personnelById.html")
    public ModelAndView personnelById(Integer id){
        Personnel personnelById = personnelService.getPersonnelById(id);
        ModelAndView modelAndView=new ModelAndView("admin/personnel-update");
        modelAndView.addObject("personnelById", personnelById);
        return modelAndView;
    }
    /*执行修改*/
    @RequestMapping("admin/personnel-update.html")
    public void updatePersonnel(Personnel personnel,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(""))
        personnel.setHportrail(url);
        personnelService.updateNotNull(personnel);
       /* session.setAttribute("userName",personnel.getName());*/
        response.sendRedirect("personnel-list.html");

    }
    /*执行删除*/
    @RequestMapping("admin/delpersonnel.html")
    @ResponseBody
    public void delpersonnel(Integer id){
        personnelService.delPersonnel(id);
    }
    /*密码修改*/
    @RequestMapping("safe.html")
    public ModelAndView safe(String magess,HttpSession session){
        Personnel sesionPer =(Personnel) session.getAttribute("sesionPer");
        if(sesionPer==null||sesionPer.equals("")){
            return new ModelAndView("redirect:index.html");
        }else {
        ModelAndView modelAndView=new ModelAndView("xingtai/safe");
        modelAndView.addObject("magess", magess);
        return modelAndView;}
    }
    /*执行密码修改*/
    @RequestMapping("safes.html")
    public ModelAndView updateSafe(HttpSession session,String password,String password1,String password2,HttpServletResponse response)throws Exception{
        String urls="";
        if (!password1.equals(password2)){
            System.err.println("两次密码不正确");
           urls="redirect:safe.html?magess=1";
        }else{
          Personnel personnel=(Personnel)  session.getAttribute("sesionPer");
          if (password!=personnel.getPassword()&&!password.equals(personnel.getPassword())){
              System.err.println("原密码错误");
             urls="redirect:safe.html?magess=2";
          }else{
              personnel.setPassword(password1);
              personnelService.updateNotNull(personnel);
              session.setAttribute("sesionPer", personnel);
            urls="redirect:center.html";
          }

        }
      return new ModelAndView(urls);
    }
    /*退出登录*/
        @RequestMapping("out.html")
    public void out(HttpSession session,HttpServletResponse response)throws Exception{
        session.removeAttribute("sesionPer");
        session.removeAttribute("userName");
        session.removeAttribute("urls");
        response.sendRedirect("index.html");
    }
    /*前端注销*/
    @RequestMapping("delUserName.html")
    @ResponseBody
    public void delUserName(HttpSession session,HttpServletResponse response)throws Exception {
        session.removeAttribute("sesionPer");
        session.removeAttribute("userName");
    }
}
