package com.jeeplus.modules.API.works.approval;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.google.common.collect.Maps;
import com.jeeplus.modules.utils.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.oa.dao.LeaveDao;
import com.jeeplus.modules.oa.entity.Leave;
import com.jeeplus.modules.sys.entity.User;

@Controller
@RequestMapping(value = "${frontPath}/leave")
public class LeaveInterfaceController extends BaseController {

	@Autowired
	private LeaveDao leaveDao;

	/**
	 * 查询 我的审批
	 *
	 * @param
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "myLeaveList", method = RequestMethod.GET)
	public AjaxJson myLeaveList(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		String userId = request.getParameter("userId");

		User user = new User();
		user.setId(userId);
		Leave leave = new Leave();
		leave.setUser(new User(userId));
		leave.setCreateBy(user);
		List<Leave> list = leaveDao.findList(leave);
		j.put("data", list);
		j.setErrorCode(ErrorCode.code_1004);
		return j;

	}

	/**
	 * 启动请假申请
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping(value = "applyForLeave", method = RequestMethod.POST)
	public AjaxJson applyForLeave(HttpServletRequest request, HttpServletResponse response) throws ParseException {
		AjaxJson json = new AjaxJson();
		HashMap<String, String> requestMap = findRequestMap(request);
		//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String start = request.getParameter("startTime");
		String end = request.getParameter("endTime");
		//Date startTime = sdf.parse(start);//格式化时间2017-07-22 13:15:16
		//Date endTime = sdf.parse(end);//格式化时间
		String id = request.getParameter("userId");
		String reason = request.getParameter("reason");
		String leaveTime = request.getParameter("leaveTime"); // 新加字段 请假时长
		String leaveDays = request.getParameter("leaveDays"); // 新加字段 请假天数
		String leaveType = request.getParameter("leaveType");
		String approverId = request.getParameter("approverId");//审批人
		String CCId = request.getParameter("CCId");//抄送人7
		String delFlag="0"; //审核流程状态标记 0 待审核 1 审核完成
		User user = new User();
		Leave leave = new Leave();
		User approver =new User();
		approver.setId(approverId);
		User CC = new User();
		CC.setId(CCId);
		user.setId(id);
		leave.setUser(user);
		leave.setReason(reason);
		leave.setStartTime(start);
		leave.setEndTime(end);
		leave.setLeaveTime(leaveTime);
		leave.setLeaveDays(leaveDays);
		leave.setLeaveType(leaveType);
		leave.setCreateBy(user);
		leave.setUpdateBy(user);
		leave.setDelFlag(delFlag);
		leave.preInsert();
		leaveDao.insert(leave);
		json.setMsg("提交成功");
		json.setErrorCode(ErrorCode.code_1004);
		return json;
	}

	/**
	 * 修改请假申请
	 *
	 * @param leaveId
	 * @param request
	 * @param response
	 * @return
	 * @throws ParseException
	 * locahost8080/jee/f/leave/modifyLeave
	 */
	@ResponseBody
	@RequestMapping(value = "modifyLeave", method = RequestMethod.POST)
	public AjaxJson modifyLeave(String leaveId, HttpServletRequest request, HttpServletResponse response)
			throws ParseException {
		AjaxJson json = new AjaxJson();
		//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
		String id = request.getParameter("userId");
		String reason = request.getParameter("reason");
		String leaveTime = request.getParameter("leaveTime"); // 新加字段 请假时长
		String leaveDays = request.getParameter("leaveDays"); // 新加字段 请假天数
		String leaveType = request.getParameter("leaveType");
		User user = new User();
		Leave leave = new Leave();
		user.setId(id);
		if (leaveDao.get(leaveId) != null) {
			leave.setReason(reason);
			leave.setStartTime(startTime);
			leave.setEndTime(endTime);
			leave.setLeaveTime(leaveTime);
			leave.setLeaveDays(leaveDays);
			leave.setLeaveType(leaveType);
			leave.setUpdateBy(user);
			leave.preInsert();
			leaveDao.update(leave);
			json.setMsg("提交请假申请成功");
			json.setErrorCode(ErrorCode.code_1004);
		}

		return json;
	}

	/**
	 * 删除
	 *
	 * @param leaveId
	 */
	@ResponseBody
	@RequestMapping(value = "delLeave", method = RequestMethod.GET)
	public AjaxJson delLeave(String leaveId, HttpServletRequest request, HttpServletResponse response) {
		AjaxJson json = new AjaxJson();

		Leave leave = new Leave();
		leave.setId(leaveId);
		leaveDao.delete(leave);

		if (leaveDao.get(leave) == null) {
			json.setMsg("删除成功");
			json.setErrorCode(ErrorCode.code_1004);
		} else {
			json.setMsg("删除失败");
			json.setErrorCode(ErrorCode.code_2004);
		}
		return json;
	}

	/**
	 * 查询
	 */
	@ResponseBody
	@RequestMapping(value = "approverIdList", method = RequestMethod.GET)
	public void approverIdList(String leaveId,HttpServletRequest request, HttpServletResponse response) {

	}
	/**
	 * 请假详情
	 */

	@ResponseBody
	@RequestMapping(value = "getleave", method = RequestMethod.GET)
	public AjaxJson getleave(String leaveId,HttpServletRequest request, HttpServletResponse response) {
		AjaxJson json = new AjaxJson();
		Leave leave = new Leave();
		leave.setId(leaveId);
		Leave leave1=leaveDao.get(leaveId);//get底层方法问题，获取不到，get获取null
		json.setSuccess(true);
		json.setErrorCode(ErrorCode.code_1004);
		json.put("data",leave1);
		return json;
	}
	/**
	 *审批 (*审批成功)
	 *
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "auditSave", method = RequestMethod.POST)
	public AjaxJson auditSave(HttpServletRequest request, HttpServletResponse response) {
		String delFlag=request.getParameter("delFlag");
		String id = request.getParameter("userId");
		String leaveDetails = request.getParameter("leaveDetails");
		String leaveId = request.getParameter("leaveId");
		Leave leave=new Leave();
		leave.setId(leaveId);
		leave.setDelFlag(delFlag);
		leave.setLeaveDetails(leaveDetails);
		User user = new User();
		user.setId(id);
		leave.setUpdateBy(user);
		leaveDao.update(leave);
		AjaxJson json = new AjaxJson();
		json.setSuccess(true);
		json.setMsg("审核成功");
		json.setErrorCode(ErrorCode.code_1004);
		return json;
	}

}
