package com.iamberry.wechat.handles.wechat.menu;

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

import com.iamberry.wechat.tools.WeixinUtil;
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 com.iamberry.wechat.core.entity.wx.AccessToken;
import com.iamberry.wechat.core.entity.wx.MenuInfo;
import com.iamberry.wechat.face.wechat.MenuInfoService;
import com.iamberry.wechat.tools.NameUtils;
/**
 * @company	深圳爱贝源科技有限公司
 * @website www.iamberry.com
 * @author 	献
 * @tel		18271840547
 * @date	2016年11月3日
 * @explain	微信后台管理-菜单相关
 */
@Controller
@RequestMapping("/admin/menu")
public class MenuInfoHandler {
	@Autowired
	private MenuInfoService menuInfoService;
	public void setMenuInfoService(MenuInfoService menuInfoService) {
		this.menuInfoService = menuInfoService;
	}
	
	/**
	 * 自定义菜单页面
	 * @return
	 */
	@RequestMapping("/showmenu")
	public ModelAndView showMenu(){
		String treedata = null;
		MenuInfo menuInfo = menuInfoService.selectOne(NameUtils.pubNo);
		if(menuInfo != null) {
			treedata = menuInfo.getVchar1();
		}else{
			treedata = "[{id:0,pid:0,name:'自定义菜单列表',url:'',content:'',type:0}]";
		}
		ModelAndView mv = new ModelAndView();
		mv.addObject("treedata", treedata);
		mv.setViewName("admin/wx/menu/wxtree");
		return mv;
	}

	/**
	 * 保存菜单
	 * @return
	 */
	@RequestMapping("/savemenu")
	public ModelAndView saveMenu(HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		if(request.getParameter("treedata") !=null){
			mv.addObject("treedata", request.getParameter("treedata"));
			int ok = menuInfoService.saveMenu(request.getParameter("treedata"), request.getParameter("str"));
			if(ok == 1){
				mv.setViewName("forward:/admin/menu/showmenu");
			}
		}
		return mv;
	}
	
	/**
	 * 创建菜单
	 * @param request
	 */
	@RequestMapping("/createmenu")
	public ModelAndView createMenu(HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		int result = -1;
		AccessToken accessToken = WeixinUtil.getAccessToken(NameUtils.appId, NameUtils.appSecret);
		if(accessToken != null){
			String jsondata = request.getParameter("jsondata").replaceAll("},]", "}]");
			if(jsondata != null && accessToken.getToken() != null){
				result = WeixinUtil.createMenu(jsondata, accessToken.getToken());
				if(result == 0){
					mv.addObject("rs", "yes");
				}else{
					mv.addObject("rs", "no");
				}
				mv.addObject("treedata", request.getParameter("treedata"));
				mv.setViewName("admin/wx/menu/wxtree");
			}
		}
		return mv;
	}
	
	/**
	 * 查询图文
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/html")
	public void doFindImgTxtHtml(HttpServletRequest request,HttpServletResponse response) throws Exception {
		response.setCharacterEncoding("utf-8");
		if(request.getParameter("menuId")!=null){
			MenuInfo menuInfoVO = menuInfoService.selectByMenuIdAndPubNo(request.getParameter("menuId"), NameUtils.pubNo);
			if (menuInfoVO != null){
				response.getWriter().print(menuInfoVO.getVchar3());
			}
		}
		response.getWriter().close();
	}
	
}
