package com.sys.bus.setting;


import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.aop.Enhancer;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.sys.bus.hr.service.HrService;
import com.sys.bus.setting.service.ApproverSettingService;
import com.sys.bus.sys.model.DtBean;
import com.sys.core.model.DataBean;
import com.sys.core.utils.ConstUtil;
import com.sys.core.utils.Tool;

public class ApproverSettingController extends Controller{
	
	static ApproverSettingService service = Enhancer.enhance(ApproverSettingService.class);//需要事务处理的service，必须使用增强方法

	static HrService hrService = Enhancer.enhance(HrService.class);
	public void index(){
		
		this.renderJsp("setting/approverSetting.jsp");//加斜杠不加斜杠有区别，不加到配置的路径下去找视图；加则到根目录下找视图
	}
	
	public void department() {
		
		DataBean dBean = new DataBean();
		dBean.setCode(0);
		dBean.setMsg("Ok");
		dBean.setData(service.department());
		this.renderJson(dBean);
	}
	public void selectUserUi() {
		String orgId = this.getPara("orgid", "");
		
		this.renderJsp("setting/selectUser.jsp");
		//this.render(view, status);
	}
	
	public void selectUserDialogUi() {
		String type = this.getPara("type", "");
		this.setAttr("type", type);
		this.renderJsp("setting/selectUserDialog.jsp");
	}
	
    @ActionKey("/jx/approveSetting")
    public void approveSetting() {
    	
    	String msg = "";
    	List<DtBean> list = service.listLeftApproveUser();
    	for(DtBean bean:list){
    		if(msg.equals(""))msg+="以下审批人员已不在职，请核实后修改！<br>";
    		msg+=bean.getStr("NAME")+"-（节点："+bean.getStr("DEPTNAME")+"）<br>";
    	}
    	this.setAttr("msg", msg);
    	
        this.render("/_view/jx/approveSetting.html");
    }
	@ActionKey("/jx/approvorCommission")
    public void approvorCommission() {
		String personId = this.getSessionAttr(ConstUtil.SESSION_USER);
		DtBean  person = 	service.getPersonById(personId);
		String personName = person.getStr("fname_l2");
		
		this.setAttr("personId", personId);
		this.setAttr("personName", personName);
		
    	this.render("/_view/jx/approvorCommission.html");
    }
	public void searchUser() {
		String orgId = this.getPara("key[orgId]", "");
		String qName = this.getPara("key[qName]", ""); 
		int pageNumber = this.getParaToInt("page", 1);
		int pageSize = this.getParaToInt("limit", 10);
		DataBean dBean = new DataBean();
		dBean.setCode(0);
		dBean.setMsg("Ok");
		Page<DtBean> pdata = service.searchUser(pageNumber,pageSize,orgId, qName);
		dBean.setData(pdata.getList());
		dBean.setCount(pdata.getTotalRow());
		this.renderJson(dBean);
		
	}
	
	public void listApproveUser() {
		String orgId = this.getPara("key[orgId]", "");
		int pageNumber = this.getParaToInt("page", 1);
		int pageSize = this.getParaToInt("limit", 10);
		DataBean dBean = new DataBean();
		dBean.setCode(0);
		dBean.setMsg("Ok");
		Page<DtBean> pdata = service.listApproveUser(pageNumber,pageSize,orgId);
		dBean.setData(pdata.getList());
		dBean.setCount(pdata.getTotalRow());
		this.renderJson(dBean);
	}

	public void doAction() {
		int type = this.getParaToInt("type", 1);
		String rowid = this.getPara("rowid", "");
		//1-置顶 ，2-up，3-down，4-del
		DataBean dBean = new DataBean();
		service.doAction(type, rowid);
		dBean.setCode(0);
		dBean.setMsg("操作成功");
		this.renderJson("data",dBean);
	}
	

	public void getNodeNames() {
		DataBean dBean = new DataBean();
		dBean.setCode(0);
		dBean.setData(service.getNodeNames());
		dBean.setMsg("保存成功");
		this.renderJson("data",dBean);
	}

}
