<?php
/*
	[Office 515158] (C) 2009-2015 天生创想 Inc.
	$Id: mod_apps mobile $
*/
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
empty($do) && $do = 'list';
if(getGP('tpltype','G')!=''){
	$tpltype=getGP('tpltype','G');
}else{
	$tpltype=getGP('tpltype','P');
}
if(getGP('tplid','G')!=''){
	$tplid=getGP('tplid','G');
}else{
	$tplid=getGP('tplid','P');
}
if ($do == 'list') {
	if($tplid==''){
		include_once('template/appslist.php');
	}else{
		$appid=getGP('appid','G','int');
		if($appid!=''){
			$tpl = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."app_type  WHERE tplid = '".$tplid."'  ");
			$work = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."apps  WHERE id = '".$appid."'  ");
			//获取当前流程
			if(getGP('perid','G','int')!=''){
				$flow['perid']=getGP('perid','G','int');
				$flow['flowid']=getGP('flowid','G','int');
			}else{
				$sql = "SELECT perid,flowid FROM ".DB_TABLEPRE."app_personnel  WHERE appid = '".$appid."' and (pertype=0 or pertype=4) order by perid desc";
				$flow = $db->fetch_one_array($sql);
			}
			if($tpl['tplkey']==1){
				include_once('template/appsadd.php');
			}else{
				echo '无效的类型！';
			}
		}else{
			$apps = array();
			$apps_db = array();
			$tpl = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."app_type  WHERE tplid = '".$tplid."'  ");
			//写入主表信息
			$apps['number']=get_date('YmdHis',PHP_TIME);
			$apps['tpltype']=$tpltype;
			$apps['tplid']=$tplid;
			$apps['title']=trim($tpl["title"]).'(No:'.get_date('YmdHis',PHP_TIME).')'.get_realname($_USER->id);
			$apps['uid']=$_USER->id;
			$apps['type']=0;
			$apps['date']=get_date('Y-m-d H:i:s',PHP_TIME);
			insert_db('apps',$apps);
			$appid=$db->insert_id();
			if($tpl['tplkey']!=4 && $tpl['tplkey']!=5){
				//写入数据表
				$apps_db = array(
						'appid' => $appid,
						'tplid' => $tplid,
						'tpltype' => $tpltype,
						'date'=>get_date('Y-m-d H:i:s',PHP_TIME)
					);
				insert_db('apps_'.$tplid,$apps_db);
			}
			//处理流程
			$sql = "SELECT * FROM ".DB_TABLEPRE."app_flow  WHERE tplid = '".$tplid."' and flownum=1 order by fid asc";
			$flow = $db->fetch_one_array($sql);
			$personnel1 = array(
				'name' => get_realname($_USER->id),
				'uid' =>$_USER->id,
				'designationdate' =>get_date('Y-m-d H:i:s',PHP_TIME),
				'pertype' =>0,
				'appid' => $appid,
				'tplid' =>$tplid,
				'flowid' => $flow['fid'],
				'appkey' => $flow['flowkey2'],
				'appkey1' => $flow['flowkey3'],
				'flowdatetype' => 0,
				'flowdate' => get_date('Y-m-d H:i:s',PHP_TIME)
				);
			insert_db('app_personnel',$personnel1);
			//更新LOG
			$content=serialize($apps);
			$title='新建公文';
			get_logadd($id,$content,$title,14,$_USER->id);
			header('Location: index.php?ac=appsadd&fileurl=plugin&tplid='.$tplid.'&tpltype='.$tpltype.'&appid='.$appid.'');
		}
		
	}

}elseif ($do == 'add') {
	mod_apps_update();
	mod_appsform_update();
	//workform_view_update();
	//更新LOG
	$title='保存公文数据';
	get_logadd($id,'',$title,14,$_USER->id);
	header('Location: index.php?ac=appsadd&fileurl=plugin&tplid='.$tplid.'&tpltype='.$tpltype.'&flowid='.getGP('flowid','P','int').'&perid='.getGP('perid','P','int').'&appid='.getGP('appid','P','int'));
}elseif ($do == 'fileadd') {
	$appid = getGP('appid','G','int');
	if($appid!=''){
		$fileoffice=array();
		$fileoffice['filename'] = unescape(getGP('title','G'));
		$fileoffice['date'] = get_date('Y-m-d H:i:s',PHP_TIME);
		$fileoffice['fileaddr'] = str_replace('../','',getGP('annex','G'));
		$fileoffice['uid'] = $_USER->id;
		$fileoffice['officetype'] = 1;
		$fileoffice['filetype'] = 2;
		$fileoffice['fileid'] = 0;
		$fileoffice['number'] = $appid;
		insert_db('fileoffice',$fileoffice);
		$id=$db->insert_id();
		if($id!=''){
			echo 'true';
			exit;
		}
	}
	echo 'false';
	exit;
}
?>