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

$prot=new protect();
if ($prot->showPage) {
$db=new DB($globalConfig['dbase'], $globalConfig['dbhost'],$globalConfig['dbuser'], $globalConfig['dbpass']);
include('include/returnConfig.php');

			if($_GET['act'] == 'acceptdel'){
				if(isset($_GET['confirm'])){
					$db->execute("DELETE FROM u_users WHERE ID = '".(int)$_GET['uid']."'");
					$db->execute("DELETE FROM transfers WHERE user_id = '".(int)$_GET['uid']."'");
					$db->execute("DELETE FROM settle WHERE payer_id = '".(int)$_GET['uid']."'");
					$db->execute("DELETE FROM credit WHERE user_id = '".(int)$_GET['uid']."'");
					header("location: user_list.php");
				}
			}
			if($_GET['act'] == 'lockUser'){
					$db->execute("UPDATE u_users SET userGroup = ".(int)$_GET['lock']." WHERE ID = '".(int)$_GET['uid']."'");
					header("location: user_list.php");
			}
//---------------------------------------------------------------			
			if($_GET['act'] == 'updateStatusCompany'){
					$db->execute("UPDATE u_users SET statusCompany = ".$_GET['status']." WHERE ID = '".(int)$_GET['uid']."'");
					header("location: user_list.php");
			}
//---------------------------------------------------------------			
			if($_GET['act'] == 'updateStatusNaja'){
					$db->execute("UPDATE u_users SET statusNaja = ".$_GET['status']." WHERE ID = '".(int)$_GET['uid']."'");
					
					if($_GET['status']==1){
						send_mail($_GET['uid'], "تایید کاربر",returnConfig('email_note_badaz_taide_naja_karbar') );
						if(returnConfig('sms_state_register_karbar_taide_naja')==1 and mobile_user($_GET['uid'])){
						sendSms(returnConfig('linkSms'),returnConfig('userNameSms'),returnConfig('passSms'),returnConfig('sms_note_badaz_taide_naja_karbar'),mobile_user($_GET['uid']));	
						smsInsert($_GET['uid'],returnConfig('smsNumber'),mobile_user($_GET['uid']),returnConfig('sms_note_badaz_taide_naja_karbar'));
						}
					}					
					header("location: user_list.php");
			}
//-----------------------------------------------------------------			
			if($_GET['act'] == 'createXLS'){
				getNumbersXLS($_GET['uid']);
				//header("location: user_list.php");
			}


$page = $_GET['page'];
$limit = $_GET['limit'];
if (!($limit)){
$limit = 20;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
	$whereclaus = '';
if(isset($_POST['userID'])){
	if($_POST['userID'] != ''){
		$whereclaus = " WHERE userName like  '%".$db->safe_string($_POST['userID'])."%' or 	name like  '%".$db->safe_string($_POST['userID'])."%' or  lastName like  '%".$db->safe_string($_POST['userID'])."%' ";
	}
}
if(isset($_GET['uid'])){
	if($_GET['uid'] != ''){
		$whereclaus = " WHERE ID = '".$db->safe_string($_GET['uid'])."'";
	}
}


if(isset($_GET['parent_id'])){
	if($_GET['parent_id'] != ''){
		$whereclaus = " WHERE parent_id = '".$db->safe_string($_GET['parent_id'])."'";
	}
}


if(isset($_GET['activeUsers'])){
	$numresults = $db->query("SELECT * FROM u_users ORDER BY ID DESC"); // the query.
	$query = '&activeUsers';
}else{
	$numresults = $db->query("SELECT * FROM u_users ".$whereclaus." ORDER BY ID DESC"); // 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.
?>
<!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>

<?php include("maintemplate/mainheader.php");?>
</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>
                      <?php if($_GET['act']=='delete'){?>
<div style="margin: 20px auto ; padding: 0 .7em; width:98%; text-align:center;" class="error_box"> 
<p>
</p><p><span class="ui-icon ui-icon-alert" style="float: left;"></span>آیا مطمئن هستید که میخواهید این کاربر را حذف کنید<br/><a href="user_list.php?act=acceptdel&uid=<?php echo $_GET['uid']?>&confirm"><div class="mybutton" style="float:right; margin-right:39%;">حذف</div></a><a href="user_list.php"><div class="mybutton" style="float:right;">بازگشت</div></a></p>
<p></p></div>
<?php }?>     
                            
                            
<?php
//	switch($action){
		
			
			if($_GET['act'] == 'show'){
				
				$sql = $db->query("SELECT * FROM u_users WHERE ID = '".(int)$_GET['uid']."'");
				$res = $db->fetchNextArray($sql);
?>
<table  class="table1" width="500" border="" cellpadding="8" cellspacing="8" bordercolor="">
  <tr>
    <td class="td_header">نام و نام خانوادگی</td>
    <td h class="td_header"><?=$res['name'] . $res['lastName']?></td>
  </tr>

  <tr>
    <td>نام كاربری</td>
    <td><?=$res['userName']?></td>
  </tr>
  
  <tr>
    <td>ايميل</td>
    <td><?=$res['email']?></td>
  </tr>
  <tr>
    <td>موبایل</td>
    <td><?=$res['mobile']?></td>
  </tr> 
  <tr>
    <td>آدرس</td>
    <td><?=$res['address']?></td>
  </tr> 
  <tr>
    <td>تاربخ عضويت</td>
    <td><?=tep_hejri11($res['date'])?></td>
  </tr>
  
  <tr>
    <td>موجودی حساب كاربری</td>
    <td><?php echo get_credit_amount($res['ID']); ?> تومان</td>
  </tr>
 

</table>

</div>
<?php
}

// Now we can display results.
if(isset($_GET['activeUsers'])){
	
}else{
	$sql = $db->query("SELECT * FROM u_users ".$whereclaus." ORDER BY ID DESC LIMIT $page, $limit"); // the query.
}
if ($numrows == 0){
echo("موردی يافت نشد."); // bah, modify the "Not Found" error for your needs.
}
?>
<form action="user_list.php" method="post">
جستجو بر اساس نام كاربری:<input type="text" name="userID"  /><input type="submit" name="submit" value="جستجو" />
</form>

<table class="listTable" dir="rtl" style="border:1px solid #069; border-collapse:collapse; margin-top:30px;width:99%" cellspacing="3" border="1">
	<tr>
    	<th width="">  کد کاربری</th>
        <th width=""> نام كاريری /نام كامل / ايميل </th>
        <th width=""> گزارشات </th>
        <th width=""> ويرايش </th>
    </tr>
<?php
	$i = $numrows-$page;
	while($data=$db->fetchNextArray($sql)){
		if($data['visited'] == 0){
			$sytle = 'style="color:#390;line-height:22px"';
		}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.'\'">';
?>
    	<td width=""><b> <?=$data['ID']?> </b></td>
        <td width=""<?=$sytle?> dir="ltr" s> <?=$data['userName']?> <br /><?php echo $data['name'] .' '. $data['lastName']; ?> <br /><?php echo $data['email']; ?></td>
       

        <td width="" style="line-height:22px">
        نماینده : <?=getReturnNemayandeh($data['ID'])?><br />
		موجودی : <?=Convertnumber2farsi(number_format(get_credit_amount($data['ID'])))?><br />
        تاریخ عضویت : <?=tep_hejri2($data['date'])?><br />
        <?php if(!($data['profile_num']=='0')){echo 'دارای اطلاعات تکمیلی';}else{echo 'بدون اطلاعات تکمیلی';}?>
        </td>
        <td width="">
		<?='<a href="user_list.php?act=show&uid='.$data['ID'].'" title="نمايش جزئيات"><img src="images/q.gif" border="0"></a> |'?> 
        <?php
		if(return_available($_SESSION['adminID'],'A_delete')==1){
		echo '<a href="user_list.php?act=delete&uid='.$data['ID'].'"><img src="images/delete_icon.png" border="0" title="حذف"></a>| ' ;
		}?> 
        <a href="sendMessage.php?uid=<?=$data['ID']?>" target="_blank" title="تماس با كاربر"><img src="images/contactIcon.png" border="0" title="تماس با كاربر" /></a>|
     
		<?php
		if(return_available($_SESSION['adminID'],'addCredit')==1){
        echo '<a href="transfer.php?userName='.$data['userName'].'" target="_blank" title="افزايش یا کاهش موجودی كاربر"><img src="images/semn-plus-mare.png" border="0" title="افزايش یا کاهش موجودی كاربر" /></a> | ';
		}
		if(return_available($_SESSION['adminID'],'A_edite')==1){
        echo '<a href="newUsers.php?userID='.$data['ID'],'&do=edit_user" target="_blank" title="ویرایش کاربر"><img src="images/edit.gif" border="0" title="ویرایش کاربر" /></a> |'; 
		}?>        
        </td>
<?php
	echo '</tr>';
	$i--;
	}
?>
</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>
<?php
//		break;
//	}
?>
                            </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 } ?>