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

import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.hr.entity.UserInfo;
import com.jeeplus.modules.hr.service.UserInfoService;
import com.jeeplus.modules.sys.entity.Certificate;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.UserService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.sysuseroffice.entity.Useroffice;
import com.jeeplus.modules.sysuseroffice.service.UserofficeService;
import org.apache.shiro.authz.annotation.Logical;
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.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

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

/**
 * 人员证书信息Controller
 * @author fgy
 * @version 2017-12-06
 */
@SuppressWarnings("all")
@Controller
@RequestMapping(value = "${adminPath}/hr/certificate")
public class CertificateController extends BaseController {

	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private UserofficeService userofficeService;
	@Autowired
	private UserService userService;

	@ModelAttribute
	public UserInfo get(@RequestParam(value ="id", required=false) String id,
						@RequestParam(value = "userOfficeId", required = false)String userOfficeId ,
						@RequestParam(value = "userId", required = false)String userId,
						@RequestParam(value = "companyId", required = false)String companyId) {
		UserInfo userInfo = new UserInfo();
		Useroffice useroffice = new Useroffice();
		User user = new User();
		if (StringUtils.isNotBlank(id)){
			userInfo = userInfoService.get(id);
		}else{
			if(StringUtils.isNotBlank(userOfficeId)){
				useroffice = userofficeService.get(userOfficeId);
				userInfo.setUserOffice(useroffice);
			}
			if(StringUtils.isNotBlank(userId)){
				user = userService.get(userId);
				userInfo.setUserId(userId);
				userInfo.setUser(user);
			}
			if(StringUtils.isNotBlank(companyId)){
				userInfo.setCompanyId(companyId);
			}
		}
		if(StringUtils.isBlank(userInfo.getGender())){
			userInfo.setGender(user.getSex());
		}
		if(StringUtils.isBlank(userInfo.getName())){
			userInfo.setName(user.getName());
		}
		if(StringUtils.isBlank(userInfo.getMobilePhone())){
			userInfo.setMobilePhone(user.getMobile());
		}
		return userInfo;
	}

	/**
	 * 人员证书信息列表页面
	 */
	@RequiresPermissions("hr:certificate:list")
	@RequestMapping(value = {"list",""})
	public String certificateList(UserInfo userInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<UserInfo> page = userInfoService.findPage(new Page<UserInfo>(request, response), userInfo);
		model.addAttribute("page", page);

		User user = UserUtils.getUser();
		if(UserUtils.isManager()){
			return "modules/hr/certificateList";
		}
		return "modules/hr/certificateListSimple";

	}
	/**
	 * 查看，增加，编辑表单页面
	 */
	@RequiresPermissions(value={"hr:certificate:view","hr:certificate:add","hr:certificate:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(UserInfo userInfo, Model model, HttpServletRequest request) {
		model.addAttribute("userInfo", userInfo);

		if("certificateEdit".equals(request.getParameter("dialog"))){
			return "modules/hr/certificateForm";
		}
		if("certificateView".equals(request.getParameter("dialog"))){
			return "modules/hr/certificateView";
		}
		return "modules/hr/certificateView";
	}
	/**
	 * 打开人员证书添加页面
	 */
	@RequiresPermissions(value={"hr:certificate:view","hr:certificate:add","hr:certificate:edit"},logical=Logical.OR)
	@RequestMapping(value = "certificateAdd")
	public String certificateAdd(UserInfo userInfo, Model model, HttpServletRequest request) {
		Certificate certificate = new Certificate();
		model.addAttribute("certificate", certificate);
		model.addAttribute("userInfoId",userInfo.getId());
		model.addAttribute("userId",userInfo.getUserId());
		return "modules/hr/certificateAdd";
	}


	/**
	 * 保存人员证书信息
	 */
	@RequiresPermissions(value={"hr:certificate:add","hr:certificate:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Certificate certificate, Model model, RedirectAttributes redirectAttributes, @RequestParam(value = "this_upload_files",required = false) MultipartFile this_upload_files) throws Exception{
		userInfoService.saveCertificate(certificate,this_upload_files);//保存证书
		addMessage(redirectAttributes, "保存人员证书信息成功");
		return "redirect:"+Global.getAdminPath()+"/hr/certificate/?repage";
	}


}