package com.controller.admin;


import com.controller.CommonController;
import com.entity.Apply;
import com.service.ApplyService;
import com.service.UsersService;
import com.entity.Users;
import com.util.ImgCodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 *  资源申请控制类
 * @ignore
 * @Author:Alcedo QQ:815657032
 * @Date 2020/4/15 11:05
 */
@Controller
@RequestMapping("/admin/")
public class ApplyController {
    @Resource
    private ApplyService applyService;
    @Resource
    private UsersService usersService;

    @Resource
    private CommonController commonController;
    /*设置访问路径*/
    private String path = "admin/views/apply/";


    public static final String TABLE_NAME = "资源申请";


    /**
     * ===后台资源申请管理===
     *
     * @return
     */

    /*处理添加资源申请*/
    @RequestMapping("doAddApply.do")
    public String doAddApply(HttpServletRequest request, ModelMap map) {
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "apply_add";
    }

    // 添加资源申请
    @RequestMapping("addApply.do")
    public String addApply(HttpServletRequest request, Apply apply, HttpSession session) {
        Users users = (Users) session.getAttribute("auser");
        if (users == null) {
            return "admin/login";
        } else {
            apply.setUid(users.getId());
            apply.setStatus("待审核");
            //apply.setPubtime(new Timestamp(System.currentTimeMillis()).toString().substring(0,19));
            applyService.add(apply);
            return "redirect:applyList.do";
        }
    }


    /*处理修改资源申请*/
    @RequestMapping("doUpdateApply.do")
    public String doUpdateApply(HttpServletRequest request, int id, ModelMap map) {
        map.put("apply", applyService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "apply_update";
    }

    /*通过*/
    @RequestMapping("apply_tg.do")
    public String apply_tg(HttpServletRequest request, int id, ModelMap map) {
        Apply apply = applyService.getById(id);
        apply.setStatus("通过");
        applyService.update(apply);
        return "redirect:applyList.do";
    }

    /*拒绝*/
    @RequestMapping("apply_btg.do")
    public String apply_btg(HttpServletRequest request, int id, ModelMap map) {
        Apply apply = applyService.getById(id);
        apply.setStatus("拒绝");
        applyService.update(apply);
        return "redirect:applyList.do";
    }

    /*查看资源申请*/
    @RequestMapping("doLookApply.do")
    public String doLookApply(HttpServletRequest request, int id, ModelMap map) {
        map.put("apply", applyService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path + "apply_look";
    }

    /*保存资源申请*/
    @RequestMapping("updateApply.do")
    public String updateApply(HttpServletRequest request, Apply apply) {
        applyService.update(apply);
        return "redirect:applyList.do";
    }


    // 分页查询资源申请信息的列表
    @RequestMapping("applyList.do")
    public String applyList(@RequestParam(value = "page", defaultValue = "0") Integer page, ModelMap map,
                            HttpSession session) {
        Map<String, Object> bMap = new HashMap<>();
        Users users = (Users) session.getAttribute("auser");
        if (users == null) {
            return "admin/login";
        } else {
            if (users.getUtype().equals("普通用户")) {
                bMap.put("uid", users.getId());
            }

            List<Apply> list = applyService.getAll(bMap);
            map.put("list", list);
            /*通用方法*/
            commonController.commonMethod(map);
            return path + "apply_list";
        }
    }

    // 分页查询资源申请信息的列表--模糊查询
    @RequestMapping("applyListSearch.do")
    public String applyListSearch(@RequestParam(value = "page", defaultValue = "0") Integer page, ModelMap map,
                                  HttpSession session, Apply apply) {
        Map<String, Object> bMap = new HashMap<>();
        Users users = (Users) session.getAttribute("auser");
        if (users == null) {
            return "admin/login";
        } else {
            if (users.getUtype().equals("普通用户")) {
                bMap.put("uid", users.getId());
            }

            if (apply.getName() != null && !apply.getName().equals("")) {
                bMap.put("name", apply.getName());
            }
            if (apply.getUid() != null && !apply.getUid().equals("")) {
                bMap.put("uid", apply.getUid());
            }
            if (apply.getFid() != null && !apply.getFid().equals("")) {
                bMap.put("fid", apply.getFid());
            }
            if (apply.getStatus() != null && !apply.getStatus().equals("")) {
                bMap.put("status", apply.getStatus());
            }
            List<Apply> list = applyService.getAll(bMap);
            map.put("list", list);
            /*通用方法*/
            commonController.commonMethod(map);
            return path + "apply_list";
        }
    }

    /*删除资源申请*/
    @RequestMapping("deleteApply.do")
    public String deleteApply(int id) {
        applyService.delete(id);
        return "redirect:applyList.do";
    }


    /**
     * ajax获取验证码
     */
    @ResponseBody
    @RequestMapping(value = "autoCode", produces = {"application/json;charset=UTF-8"})
    public void autoCode(HttpServletResponse response, HttpServletRequest request){
        // 通知浏览器不要缓存
        response.setHeader("Expires", "-1");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "-1");
        ImgCodeUtil util = ImgCodeUtil.Instance();
        // 将验证码输入到session中，用来验证
        String code = util.getString();
        request.getSession().setAttribute("code", code);
        // 输出打web页面
        try {
            ImageIO.write(util.getImage(), "jpg", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

