package nccloud.web.hryf.regApply.action;

import java.util.Arrays;
import java.util.HashMap;

import nc.hr.utils.ResHelper;
import nc.itf.hr.frame.IPersistenceRetrieve;
import nc.itf.uap.pf.metadata.IFlowBizItf;
import nc.md.data.access.NCObject;
import nc.vo.hr.pf.HRPFUtils;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import nc.vo.pub.pf.IPfRetCheckInfo;
import nc.vo.trn.pub.TRNConst;
import nc.vo.trn.regitem.TrnRegItemVO;
import nc.vo.trn.regmng.Glbdef1VO;
import nc.vo.trn.regmng.Glbdef2VO;
import nc.vo.trn.regmng.RegapplyVO;
import nc.vo.uif2.LoginContext;
import nccloud.commons.lang.StringUtils;
import nccloud.dto.hryf.tools.HRYFConst;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.ui.meta.PageMeta;
import nccloud.framework.web.ui.pattern.billcard.ExtSpecilAggBill;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCard;
import nccloud.framework.web.ui.pattern.form.Form;
import nccloud.web.hr.pub.HRNccTemplateUtils;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.hryf.regApply.utils.RegPageMetaUtil;
import nccloud.web.uapbd.commons.web.ParamUtils;

/**
 * 转正申请-修改按钮
 * @author lbt 20190314
 *
 */
public class RegApplyEditAction extends RegBaseAction {


	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		//解析请求中得参数，放入map中，并根据参数获取用户的登录上下文信息。
		ParamUtils param = new ParamUtils(request);
		LoginContext context = HRNccUtils.getLoginContext(param);

		String area_code = param.getString("area_code", "");//区域编码
		if(StringUtils.isBlank(area_code)){
			throw new BusinessException(ResHelper.getString("6008hrgx","06008hrgx0036")
/*@res "area_code 区域编码没传！"*/);
		}
		String page_code = param.getString("page_code", "");//页面编码
		if(StringUtils.isBlank(page_code)){
			throw new BusinessException(ResHelper.getString("6008hrgx","06008hrgx0037")
/*@res "page_code 页面编码没传！"*/);
		}
		// 是否是审批节点
		boolean isapprove = param.getBoolean("isapprove", false);
		// 获取修改的单据，并校验
		AggregatedValueObject obj = getSelectedData(param);
		if (obj == null){
			throw new BusinessException(ResHelper.getString("6001uif2", "06001uif20050") /* @res "请先选择数据！" */);
		}
		RegapplyVO regapplyVO = (RegapplyVO)obj.getParentVO();
		// 如果为审批节点，设置pk_org
		if(isapprove){
			context.setPk_org(regapplyVO.getPk_org());
		}
		TrnRegItemVO[] regItemSet = super.getItemSet(context,param,regapplyVO.getProbation_type());


        NCObject ncObj = NCObject.newInstance(obj);
        IFlowBizItf itf = ncObj.getBizInterface(IFlowBizItf.class);
        int iApproveStatus = itf.getApproveStatus();
        String stateName = HRPFUtils.getBillStateName(iApproveStatus);

        if (!isapprove){
            // 申请节点 即使当前登录人已经是当前审批人了，也是不可以修改单据的
            if (IPfRetCheckInfo.NOSTATE != iApproveStatus /* && IPfRetCheckInfo.NOPASS != iApproveStatus */) {
            	throw new BusinessException(ResHelper.getString("6001pf", "06001pf0047", stateName)
                /* @res "单据状态为{0}，不允许编辑！" */);
            }
        } else {
            // 审批节点
        	if (IPfRetCheckInfo.COMMIT != iApproveStatus && IPfRetCheckInfo.GOINGON != iApproveStatus) {
            	throw new BusinessException(ResHelper.getString("6001pf", "06001pf0047", stateName)
                /* @res "单据状态为{0}，不允许编辑！" */);
            }
        }
        if (!isBillCanEdit(regapplyVO))  {
        	throw new BusinessException(ResHelper.getString("6001pf", "06001pf0016")
            /* @res "您没有权限编辑此单据！" */);
        }

        //页面编辑数据
		HashMap result = new HashMap();
		ExtSpecilAggBill extagg = new ExtSpecilAggBill();
		extagg.setHead((SuperVO) regapplyVO);
		ExtBillCard card =  HRNccTemplateUtils.getExtBillCard(HRYFConst.APPCODE_REGAPPLY, HRYFConst.PAGECODE_REGAPPLY, extagg, Arrays.asList("list"));
		Form form = new Form();
		form.setPageid(card.getPageid());
		form.setTempletid(card.getTempletid());
		form.setModel(card.getHead().getModel());
		result.put("formData", form);
		
		String pk_hi_regapply = regapplyVO.getPk_hi_regapply();
		Glbdef2VO[] glbdef1VOs = (Glbdef2VO[]) ServiceLocator.find(IPersistenceRetrieve.class).retrieveByClause(null, Glbdef2VO.class, "pk_hi_regapply ='"+pk_hi_regapply+"' and nvl(dr,0) = 0");
		if(glbdef1VOs != null && glbdef1VOs.length > 0) {
			result.put("formBodyData", HRNccTemplateUtils.getGridWithArea(HRYFConst.PAGECODE_REGAPPLY, "card_body", glbdef1VOs));
		}
		
		result.put("isBillCodeEditable", isBillCodeEditable(context));
		context.setPk_org(regapplyVO.getPk_org());
		// 返回前后项目模板
		PageMeta pm = super.showAllSetItems(context, HRYFConst.APPCODE_REGAPPLY, HRYFConst.PAGECODE_REGAPPLY, true, regItemSet,TRNConst.TRNS_OLDINFO_TAB,TRNConst.TRNS_NEWINFO_TAB);
		pm = RegPageMetaUtil.getPageMetaByEditType( context, pm,HRYFConst.APPCODE_REGAPPLY, "edit",regapplyVO);
		result.put(TRNConst.TRNS_OLDINFO_TAB, pm.getAreaMeta(TRNConst.TRNS_OLDINFO_TAB));
		result.put(TRNConst.TRNS_NEWINFO_TAB, pm.getAreaMeta(TRNConst.TRNS_NEWINFO_TAB));
		result.put(TRNConst.TRNS_PSNINFO_TAB, pm.getAreaMeta(TRNConst.TRNS_PSNINFO_TAB));
		result.put("card", pm.getAreaMeta("card"));
		return result;
	}


}