<?php
include("include/config.php");
include("include/protect.class.php");
include("include/jdf.php");
include('include/functions.php');
include("include/thumbnail_create.php");

$prot=new protect();
if ($prot->showPage) {
$db=new DB($globalConfig['dbase'], $globalConfig['dbhost'],$globalConfig['dbuser'], $globalConfig['dbpass']);
mysql_query("SET NAMES utf8");
extract($_GET);
extract($_POST);

$page = $_GET['page'];
$limit = $_GET['limit'];
if (!($limit)){
$limit =20;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$numresults = $db->query("select * from contact  "); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
$pages = intval($numrows/$limit); // Number of results pages.
// $pages now contains int of pages, unless there is a remainder from division.
if ($numrows%$limit) {
$pages++;} // has remainder so add one page
$current = ($page/$limit) + 1; // Current page number.
if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.
else {
$total = $pages;} // Else total pages is $pages value.
$first = $page + 1; // The first result.
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
else{
$last = $numrows;} // If last results page, last result equals total number of results.
//escape from PHP mode.
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> مدیریت پیامهای رسیده</title><link href="css/dropdown/dropdown.vertical.rtl.css" media="all" rel="stylesheet" type="text/css" />
<link href="css/dropdown/themes/default/default.css" media="all" rel="stylesheet" type="text/css" />

<?php include("maintemplate/mainheader.php");?>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="ajax_base.js"></script>
</head>
<body>
<div id="wpwrap">
<div id="wpcontent">
	<?php include("include/menutop.php");?> 
<div id="wpbody">
	<?php include("include/header.php");?>
    <div style="overflow:hidden;" id="wpbody-content">
        <div id="screen-meta">
            <div class="wrap">
                <div id="icon-index" class="icon32"><br></div>
                <h2>مدیریت پیامهای رسیده </h2>
                <div id="dashboard-widgets-wrap">
                    <div id="dashboard-widgets" class="metabox-holder">
                        <div class="postbox-container" style="width:100%;">
                            <div id="normal-sortables" class="meta-box-sortables ui-sortable"><div id="dashboard_recent_comments" class="postbox ">  
                    <div class="handlediv" title="برای جانشینی کلیک کنید"><br></div><h3 class="hndle"><span>   </span></h3>
                    <div id="page_result" style="line-height:24px"></div>
<p align="center">Page <b><?=$current?></b> of <b><?=$total?></b></p>

<?php

if(isset($_GET['act'])){
	switch ($_GET['act']){
		case 'send_message':
			if(isset($_POST['submit'])){
				$from = returnConfigPortal('title');
				$email =  'info@pishsakhteh.com';
				$to = $_POST['email'];
				$header  = "MIME-Version: 1.0\r\n";
				$header .= "Content-type: text/html; charset=utf-8\r\n";
				$header .= "To: ".$to."\r\n";
				$header .= "From: ".$from." <".$email.">\r\n";
				$header .= "Cc: \r\n";
				$subject = $_POST['title'];
				$message = $_POST['description'].'<br><span style="font-family:Tahoma, Arial; font-size:12px;">'.date("l d F Y").'</span>';
				mail($to, $subject, $message, $header);
				echo 'پیام شما با موفقيت به ایمیل '.$to.' ارسال شد.';
			}
			//$email = $_GET['email'];
		
		
			if(isset($_POST['submit'])){
				$db->execute("INSERT INTO messages_user SET s_m_user = '1',
							 								r_m_user = '".(int)$_POST['uid']."',
															title = '".$db->safe_string($_POST['title'])."',
															message = '".$db->safe_string($_POST['description'])."',
															date = now()");
				
				//echo 'پیام شما با موفقيت ارسال شد.';
			}
			//$userName = get_user_name($_GET['uid']);
			?>
			<form action="adminContact.php?act=send_message&email=<?=$_GET['email']?>" method="post">
			<table width="500" border="1">
			  <tr>
				<td> ایمیل: </td>
				<td><input type="text" name="email" value="<?=$_GET['email']?>"  readonly="readonly" /></td>
			  </tr>
			  <tr>
				<td>عنوان :‌</td>
				<td><input type="text" name="title"  /></td>
			  </tr>
			  <tr>
				<td>متن :‌ </td>
				<td><textarea name="description"></textarea></td>
			  </tr>
			  <tr>
				<td>&nbsp;</td>
				<td><input type="submit" name="submit" value="ارسال" /></td>
			  </tr>
			</table>
			</form>
			
			<?php

		
		break;
	}
	
}



function dell_mid($id){

$dell="delete from contact where id='".mysql_real_escape_string($id)."'";    
mysql_query($dell) or die ("Bad Query<br>".$dell);
		echo debug_message('حذف با موفقیت انجام شد ...','2','');
		
		echo '<form action="adminContact.php" method="GET">
				<p align="center"><input type="submit" id="button" value="OK"></p>
		</form>';	
}

if ($_GET[delid]){
$dcat = ($_GET[delid]);
dell_mid($dcat);
}
?>

<table class="listTable" dir="rtl" style="border:1px solid #069; border-collapse:collapse; margin-top:30px;width:99%" cellspacing="3" border="1">
        <tr>
          <th > </th>
          <th > نام</th>
          <th >ایمیل </th>
          <th >تاریخ </th>
          <th > ارسال پاسخ</th>
          <th > مشاهده پیام</th>
          <th >حذف</th>
          
        </tr>
<?php
$sql="select * from contact order by date desc LIMIT $page, $limit";
$query = mysql_query($sql) or die ("Bad Query<br>".$sql);
$i=1;
$j=1;
$p = $numrows-$page;
while($row = $db->fetchNextArray($query)){

echo "<input type=\"hidden\" id=\"id$j\" value=".$row['id'].">";
	     if($row['state']=='2'){$type_taead= "<input type=\"button\" id=\"status1\"  value=\"\"   onclick=\"javascript:get('page_result',this.parentNode,'status1','?act=get_send_contact','send_ajax_chitasoft.php','id$j'); loading(&quot;page_result&quot;);\" style=\"background-image: url(images/ok.gif); border:0;width:20px; height:20px\" title=\"مشاهده پیام\" >";
		 
		 
}else{$type_taead="<input type=\"button\" id=\"status1\"  value=\"\"   onclick=\"javascript:get('page_result',this.parentNode,'status1','?act=get_send_contact','send_ajax_chitasoft.php','id$j'); loading(&quot;page_result&quot;);\" style=\"background-image: url(images/no_ok.gif); border:0;width:20px; height:20px\" title=\"مشاهده پیام\" >" ;
} 
$j=$j+1;	
		if($data['visited'] == 0){
			$sytle = 'style="color:#390"';
		}else{
			$style = '';
		}
		if(($i % 2) == 0) {
			$class = 'tableRow2';
			$classOver='tableRowOver2';
		}else{
			$class = 'tableRow1';
			$classOver='tableRowOver';
		}
echo '<tr class="'.$class.'" onmouseover="this.className=\''.$classOver.'\'" onmouseout="this.className=\''.$class.'\'">';
echo "<td class=\"tablebattom1\">".$i."</td> 
<td class=\"tablebattom1\">".$row['name']."</td> 
<td class=\"tablebattom1\">".$row['mail']. " </td>
<td class=\"tablebattom1\">".tep_hejri2($row['date']). " </td>
<td class=\"tablebattom1\"> <a href=\"adminContact.php?act=send_message&email=".$row['mail']."\"> ارسال پاسخ </a></td>
<td class=\"tablebattom1\">".$type_taead."</td>
<td class=\"tablebattom1\"><a href=\"adminContact.php?delid=".$row['id']."\"><img border=\"0\" src=\"images/dell.gif  \"></a></td></tr> \n ";
$i=$i+1;
$p--;
}

echo '</table> 
<p align="center" style="margin-top:20px;">';

if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\" class=\"pages\">قبلی</a>    \n");}

for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
 $ppage = $limit*($i - 1);
 if ($ppage == $page){
 echo("<b class=\"pages_on\">".Convertnumber2farsi($i)."</b> \n");} // If current page don't give link, just text.
 else{
 echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\" class=\"pages\">".Convertnumber2farsi($i)."</a> \n");}
}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\" class=\"pages\">بعدی</a>\n");}
?>
</p>

                           </div>
                            </div>
                        </div>                        
                    </div>
                    <div class="clear"></div>
                </div><!-- dashboard-widgets-wrap -->
            </div><!-- wrap -->
			<div class="clear"></div></div><!-- wpbody-content -->
			<div class="clear"></div></div><!-- wpbody -->
			<div class="clear"></div></div><!-- wpcontent -->
        </div>
    </div><!-- wpcontent -->
</div><!-- wpwrap -->
<?php include("include/footer.php");?>
</div>
</div>
</body>
</html><?php } ?>