<?php
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
get_key("office_info");

empty($do) && $do = 'list';
if ($do == 'list') {
	//列表信息 
	$wheresql = '';
	$page = max(1, getGP('page','G','int'));
	$pagesize = $_CONFIG->config_data('pagenum');
	$offset = ($page - 1) * $pagesize;
	$vuidtype = getGP('vuidtype','G');
	$url = 'admin.php?ac=send&fileurl=sms&userkeytype='.$userkeytype;
	$vstartdate = getGP('vstartdate','G');
	$venddate = getGP('venddate','G');
	if ($vstartdate!='' && $venddate!='') {
		$wheresql .= " AND (senddate>='".$vstartdate."' and senddate<='".$venddate."')";
		$url .= '&vstartdate='.$vstartdate.'&venddate='.$venddate;
	}
	//权限判断
	$un = getGP('un','G');
	$ui = getGP('ui','G');
	if(!is_superadmin() && $ui==''){
		$wheresql .= " and senduser='".$_USER->id."'";
	}
	if ($ui!='') {
		$wheresql .= " and senduser in(".$ui.")";
		$url .= '&ui='.$ui.'&un='.$un;
	}
	if ($content = getGP('content','G')) {
		$wheresql .= " AND content LIKE '%$content%' ";
		$url .= '&content='.rawurlencode($content);
	}
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."sms_send WHERE 1 $wheresql ");
     $sql = "SELECT * FROM ".DB_TABLEPRE."sms_send WHERE 1 $wheresql ORDER BY sendid desc LIMIT $offset, $pagesize";
	$result = $db->fetch_all($sql);

	include_once('template/send.php');

} elseif ($do == 'update') {
	
	get_key("office_info_delete");

	$idarr = getGP('id','P','array');
	foreach ($idarr as $id) {
	$db->query("DELETE FROM ".DB_TABLEPRE."sms_send WHERE sendid = '$id' ");
	//db->query("DELETE FROM ".DB_TABLEPRE.DB_JT."user_view WHERE uid = '$id'");
	
	if($id!='')
	{
   $oalog = array(
		'uid' => $_USER->id,
		'content' => '删除内部短信',
		'title' => '删除内部短信',
		'startdate' => get_date('Y-m-d H:i:s',PHP_TIME),
		'contentid' => $id,
		'type' => '4'
	);
	insert_db(DB_JT.'oalog',$oalog);
	
	}
	}
	show_msg('短消息删除成功！', 'admin.php?ac=send&fileurl=sms&userkeytype='.getGP('userkeytype','P').'');

}elseif ($do == 'excel') {
	$datename="sms_".get_date('YmdHis',PHP_TIME);
	$outputFileName = 'data/excel/'.$datename.'.xls';
		$content = array();
		$archive=array("接收人","发送人","发送时间","内容");
		$content[] = $archive;
		$wheresql = '';
		$vstartdate = getGP('vstartdate','P');
		$venddate = getGP('venddate','P');
		if ($vstartdate!='' && $venddate!='') {
			$wheresql .= " AND (senddate>='".$vstartdate."' and senddate<='".$venddate."')";
		}
		//权限判断
		$un = getGP('un','P');
		$ui = getGP('ui','P');
		if(!is_superadmin() && $ui==''){
			$wheresql .= " and senduser='".$_USER->id."'";
		}
		if ($ui!='') {
			$wheresql .= " and senduser in(".$ui.")";
		}
		if ($content = getGP('content','P')) {
			$wheresql .= " AND content LIKE '%$content%' ";
		}
		$sql = "SELECT * FROM ".DB_TABLEPRE."sms_send WHERE 1 $wheresql ORDER BY sendid desc";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$c=unserialize($row['content']);
			$archive = array(
				"".$row['receiveuser']."",
				"".get_realname($row['senduser'])."",
				"".str_replace("-",".",$row[senddate])."",
				"".sms_content($c['name'],$c['content'],$c['url'],$c['type']).""
			);
			$content[] = $archive;
		}
	$excel = new ExcelWriter($outputFileName);
	if($excel==false) 
		echo $excel->error; 
	foreach($content as $v){
		$excel->writeLine($v);
	}
	$excel->sendfile($outputFileName);
} 
?>