/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.iim.web;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.iim.entity.MyCalendar;
import com.jeeplus.modules.iim.service.MyCalendarService;
import com.jeeplus.modules.iim.utils.DateUtil;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
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.mvc.support.RedirectAttributes;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 日历Controller
 * 
 * @author liugf
 * @version 2016-04-19
 */
@Controller
@RequestMapping(value = "${adminPath}/iim/myCalendar")
public class MyCalendarController extends BaseController {

	@Autowired
	private MyCalendarService myCalendarService;

	@ModelAttribute
	public MyCalendar get(@RequestParam(required = false) String id) {
		MyCalendar entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = myCalendarService.get(id);
		}
		if (entity == null) {
			entity = new MyCalendar();
		}
		return entity;
	}

	/**
	 * 日历页面
	 */
	@RequestMapping(value = { "index", "" })
	public String index(MyCalendar myCalendar, HttpServletRequest request, HttpServletResponse response, Model model) {
		myCalendar.setUser(UserUtils.getUser());
		myCalendar.setCompanyId(UserUtils.getUser().getComId());
		return "modules/iim/myCalendar";
	}

	/**
	 * 打开添加框
	 */
	@RequestMapping(value = "addform")
	public String addform(@RequestParam("date") String date ,HttpServletRequest request,HttpServletResponse response,Model model){

		MyCalendar myCalendar = new MyCalendar();
		myCalendar.setUser(UserUtils.getUser());
		myCalendar.setCompanyId(UserUtils.getUser().getComId());

		String endDate = date;
		String noticeDate = date;

		//将鼠标点击的这一天作为日程开始的第一天
		model.addAttribute("date",date);
		model.addAttribute("enddate",endDate);
		model.addAttribute("noticedate",noticeDate);
		model.addAttribute("myCalendar",myCalendar);
		return "modules/iim/myCalendarForm-add";
	}

	/**
	 *   打开编辑框
	 * @param myCalendar
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editform")
	public String editform(MyCalendar myCalendar, HttpServletRequest request, HttpServletResponse response, Model model) {

		String title = myCalendar.getTitle();// 事件标题
		String  location = myCalendar.getLocation();
		String start = myCalendar.getStart();// 事件开始时间
		String end = myCalendar.getEnd();// 结束时间
		String notice = myCalendar.getNotice();//提醒时间
		//String allDay = myCalendar.getAdllDay();// 是否为全天事件
		String color = myCalendar.getColor();// 事件的背景

		myCalendar.setUser(UserUtils.getUser());
		myCalendar.setCompanyId(UserUtils.getUser().getComId());

		String start_d = "";
		String start_h = "";
		String start_m = "";

		String end_d = "";
		String end_h = "";
		String end_m = "";

		String notice_d = "";
		String notice_h = "";
		String notice_m = "";

		if(!StringUtils.isBlank(start) && start.length()>=16){
			start_d = start.substring(5,7)+"/"+start.substring(8,10)+"/"+start.substring(0,4);
			start_h = start.substring(11, 13);
			start_m = start.substring(14);
		}
		if(!StringUtils.isBlank(end) && end.length()>=16){
			end_d = end.substring(5,7)+"/"+end.substring(8,10)+"/"+end.substring(0,4);
			end_h = end.substring(11, 13);
			end_m = end.substring(14);
		}
		if(!StringUtils.isBlank(notice) && notice.length()>=16){
			notice_d = notice.substring(5,7)+"/"+notice.substring(8,10)+"/"+notice.substring(0,4);
			notice_h = notice.substring(11,13);
			notice_m = notice.substring(14);
		}

		model.addAttribute("title", title);
		model.addAttribute("location",location);
		model.addAttribute("color", color);
		model.addAttribute("start",start);
		model.addAttribute("start_d", start_d);
		model.addAttribute("start_h", start_h);
		model.addAttribute("start_m", start_m);
		model.addAttribute("end", end);
		model.addAttribute("end_d", end_d);
		model.addAttribute("end_h", end_h);
		model.addAttribute("end_m", end_m);
		model.addAttribute("notice",notice);
		model.addAttribute("notice_d",notice_d);
		model.addAttribute("notice_h",notice_h);
		model.addAttribute("notice_m",notice_m);
		//model.addAttribute("allDay", allDay);
		return "modules/iim/myCalendarForm-edit";
	}

	/**
	 * 初始化日历界面，显示出当月的所有日程
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "findList")
	protected List<MyCalendar> doPost(MyCalendar myCalendar, HttpServletRequest request, HttpServletResponse response,Model model) throws ServletException, IOException {

		myCalendar.setUser(UserUtils.getUser());
		myCalendar.setCompanyId(UserUtils.getUser().getComId());
		List<MyCalendar> list = myCalendarService.findList(myCalendar);

		return list;
	}

	/**
	 * 保存新建的日程 (是否“全天”暂且不论)
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "add")
	public String add(MyCalendar myCalendar, HttpServletRequest request, HttpServletResponse response, Model model) {

		String events = request.getParameter("event");// 事件内容
		String location = request.getParameter("location");
		//String isallday = request.getParameter("allDay").equals("1")?"1":"0";// 是否是全天事件
		String startdate = request.getParameter("startdate");
		String enddate = request.getParameter("enddate");
		String noticedate = request.getParameter("noticedate");

		String s_time = request.getParameter("s_hour") + ":"
				+ request.getParameter("s_minute") /*+ ":00"*/;
		String e_time = request.getParameter("e_hour") + ":"
				+ request.getParameter("e_minute") /*+ ":00"*/;
		String n_time = request.getParameter("n_hour") + ":"
				+ request.getParameter("n_minute") ;
		String start = "";
		String end = "";
		String notice = "";

		start = startdate + " " + s_time;
		end = enddate + " " + e_time;
		notice = noticedate + " " +n_time;

		start = transTo(start);
		end = transTo(end);
		notice = transTo(notice);

		String[] colors = { "#360", "#f30", "#06c" };
		int index = (int) (Math.random() * colors.length);
		myCalendar.setUser(UserUtils.getUser());
		myCalendar.setTitle(events);
		myCalendar.setLocation(location);
		myCalendar.setStart(start);
		myCalendar.setEnd(end);
		myCalendar.setNotice(notice);
		myCalendar.setCompanyId(UserUtils.getUser().getComId());
		//myCalendar.setAdllDay(isallday);
		myCalendar.setColor(colors[index]);
		myCalendarService.saveCalender(myCalendar);

		return "1";
	}

	/**
	 * 修改日程
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "edit")
	public String edit(MyCalendar myCalendar, HttpServletRequest request, HttpServletResponse response, Model model) {

		String events = request.getParameter("event");// 事件内容
		String location = request.getParameter("location");
		//String isallday = request.getParameter("isallday");// 是否是全天事件
		String startdate = request.getParameter("startdate");
		String enddate = request.getParameter("enddate");
		String noticedate = request.getParameter("noticedate");

		String s_time = request.getParameter("s_hour") + ":"
				+ request.getParameter("s_minute") ;
		String e_time = request.getParameter("e_hour") + ":"
				+ request.getParameter("e_minute");
		String n_time = request.getParameter("n_hour") + ":"
				+ request.getParameter("n_minute") ;

		String start = startdate + " " + s_time;
		String end = enddate + " " + e_time;
		String notice = noticedate + " " +n_time;

		start = transTo(start);
		end = transTo(end);
		notice = transTo(notice);


		String[] colors = { "#360", "#f30", "#06c" };
		int index = (int) (Math.random() * colors.length);
		myCalendar.setTitle(events);
		myCalendar.setLocation(location);
		myCalendar.setStart(start);
		myCalendar.setEnd(end);
		myCalendar.setNotice(notice);
		myCalendar.setColor(colors[index]);
		myCalendar.setUser(UserUtils.getUser());
		myCalendar.setCompanyId(UserUtils.getUser().getComId());
		myCalendarService.saveCalender(myCalendar);

		model.addAttribute("myCalendar", myCalendar);

		return "1";
	}

	/**
	 * 删除日历
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "del")
	public String del(MyCalendar myCalendar,
			RedirectAttributes redirectAttributes) {

		myCalendarService.delete(myCalendar);
		return "1";

	}

	/**
	 * 縮放日歷
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "resize")
	public String resize(MyCalendar myCalendar, HttpServletRequest request, HttpServletResponse response, Model model) {

		myCalendar.setUser(UserUtils.getUser());
		myCalendar.setCompanyId(UserUtils.getUser().getComId());
		Integer daydiff = Integer.parseInt(request.getParameter("daydiff")) * 24 * 60 * 60;
		Integer minudiff = Integer.parseInt(request.getParameter("minudiff")) * 60;
		String start = myCalendar.getStart();
		long lstart = DateUtil.string2long(start);
		String end = myCalendar.getEnd();
		Integer difftime = daydiff + minudiff;
		if ("".equals(end)) {
			myCalendar.setEnd(DateUtil.long2string(lstart + difftime));
			myCalendarService.save(myCalendar);
		} else {
			long lend = DateUtil.string2long(end);
			myCalendar.setEnd(DateUtil.long2string(lend + difftime));
			myCalendarService.save(myCalendar);
		}
		return "1";
	}

	/**
	 * 拖拽日历
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "drag")
	public String drag(MyCalendar myCalendar, HttpServletRequest request, HttpServletResponse response, Model model) {

		myCalendar.setUser(UserUtils.getUser());
		myCalendar.setCompanyId(UserUtils.getUser().getComId());
		Integer daydiff = Integer.parseInt(request.getParameter("daydiff")) * 24 * 60 * 60;
		Integer minudiff = Integer.parseInt(request.getParameter("minudiff")) * 60;
		String allday = request.getParameter("allday");
		//String start = myCalendar.getStart();
		String start = myCalendar.getStart()+":00"; //时间转换工具类不能转换2017-09-07 19:09 的格式
		long lstart = DateUtil.string2long(start);

		String end = myCalendar.getEnd();
		if ("true".equals(allday)) {
			if ("".equals(end)) {
				myCalendar.setStart(DateUtil.long2string(lstart + daydiff));
				myCalendarService.save(myCalendar);
			} else {
				long lend = DateUtil.string2long(end);
				myCalendar.setStart(DateUtil.long2string(lstart + daydiff));
				myCalendar.setEnd(DateUtil.long2string(lend + daydiff));
				myCalendarService.save(myCalendar);
			}
		} else {
			Integer difftime = daydiff + minudiff;
			if ("".equals(end)) {
				myCalendar.setStart(DateUtil.long2string(lstart + difftime));
				myCalendarService.save(myCalendar);
			} else {
				long lend = DateUtil.string2long(end);
				myCalendar.setStart(DateUtil.long2string(lstart + difftime));
				myCalendar.setEnd(DateUtil.long2string(lend + difftime));
				myCalendarService.save(myCalendar);
			}
		}
		return "1";
	}

	//将 09/10/2017 08:20 转换为 2017-09-10 08:20
	public String transTo(String dateStr){
		if(dateStr.indexOf("/")>0){
			String str1 = dateStr.substring(0,10);
			String str2 = "";
			if(dateStr.length()>10){
				str2 = dateStr.substring(10);
			}
			String[] arr = str1.split("/");
			dateStr = arr[2]+"-"+arr[0]+"-"+arr[1]+str2;
			return dateStr;
		}
		return dateStr;
	}

}