<?php
//error_reporting(E_ALL);
require_once('../config/config.php');
require_once(CLASS_DIR.'dbClass.php');
$cupage = 'user';
$page = $_GET['page'];
$obj = new DBClass();
$stat = $_GET['stat'];
if(isset($_GET['msg'])){
	if($_GET['msg'] == 'add'){
		$info[] = 'User Added Successfully';
	}
	if($_GET['msg'] == 'edit'){
		$info[] = 'User Updated Successfully';
	}
}

if(isset($_GET['action']) && $_GET['action'] == 'statchng'){
	$sql = "SELECT * FROM ".TBL_USER." WHERE id = ".$_GET['user'];
	$rs = $obj->executeQry($sql,$link);
	$rows = $obj->fetchRow();
	$rowNum = $obj->getRow();
	if ($rowNum > 0){
		$updt = $rows['status']=='0'?'1':'0';
		$sql2 = "UPDATE ".TBL_USER." SET status = '".$updt."' WHERE id = ".$_GET['user'];
		$rs2 = $obj->executeQry($sql2,$link);
		if($rs2){
			$info[] = "Status Updated Successfully...!";
		}
	}
}

if(isset($_GET['action']) && $_GET['action'] == 'delete'){
	$sql1 = "DELETE FROM ".TBL_USER." WHERE id = '".$_GET['id']."' ";
	$rs1 = $obj->executeQry($sql1,$link);
	$info[] = 'Record deleted successfully';
}

if(isset($_GET['action']) && $_GET['action'] == 'aprove'){
	$sql5 = "UPDATE ".TBL_USER." SET aprove = 0 WHERE id = '".$_GET['id']."' ";
	$rs5 = $obj->executeQry($sql5,$link);
	if($rs5){
		$sqluser = "SELECT * FROM ".TBL_USER." WHERE id = ".$_GET['id'];
		$rsuser = $obj->executeQry($sqluser,$link);
		$rowuser = $obj->fetchRow();
		// *********************** start  mail to admin *****************
		$to        	= 	$rowuser['email'];
		$subject	=	"Coalbazaar.com registration approved";
		$message	= 	"Dear ".$rowuser['fname']."  <br>".
		"Your request for account with coalbazaar.com is aproved.<br>".
		"your account is activated now. you can login and enter.<br>".
        "your directory entry and product listing.<br><br>".
		"Regards<br>".
		"Administrator<br>".
		"coalbazaar.com<br>";
		
						
		// To send HTML mail, the Content-type header must be set
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		
		// Additional headers
		//$headers .= 'To: ' "\r\n";
		$headers .= 'From: <admin@coalbazaar.com>' . "\r\n";
		
		@$sended=mail($to, $subject, $message, $headers);	
		
		if($sended!='')	
		{
			$info[] = 'User aproved successfully...!';
		}

	}
}

if(isset($_GET['stat']) && $_GET['stat'] == 'unaprove'){
$sql = "SELECT * FROM ".TBL_USER." WHERE aprove = 1 and id !=53";
} elseif(isset($_GET['stat']) && $_GET['stat'] == 'fb') {
$sql = "SELECT * FROM ".TBL_USER." WHERE account_type_id = 1 and id !=53 and oauth_uid!=0";
} elseif(isset($_GET['stat']) && $_GET['stat'] == 'free') {
$sql = "SELECT * FROM ".TBL_USER." WHERE account_type_id = 1 and id !=53";
} elseif(isset($_GET['stat']) && $_GET['stat'] == 'paid') {
$sql = "SELECT * FROM ".TBL_USER." WHERE account_type_id != 1 and id !=53";
}elseif(isset($_GET['stat']) && $_GET['stat'] == 'temp') {
$sql = "SELECT * FROM ".TBL_TMPUSER;
} else{
$sql = "SELECT * FROM ".TBL_USER." WHERE aprove = 0 and id !=53";
}
$rs = $obj->executeQry($sql,$link);
$totalrows = $obj->getrow();

$limit = 10;
if(empty($page)) {
$page = 1;
}
$limitvalue = ($page - 1) * $limit;
$adjacents = 3;

if(isset($_GET['stat']) && $_GET['stat'] == 'unaprove'){
$sql = "SELECT * FROM ".TBL_USER." WHERE aprove = 1 and id !=53 ORDER BY id DESC LIMIT $limitvalue, $limit";
}elseif(isset($_GET['stat']) && $_GET['stat'] == 'fb') {
$sql = "SELECT * FROM ".TBL_USER." WHERE account_type_id = 1 and id !=53 and oauth_uid!=0 ORDER BY id DESC LIMIT $limitvalue, $limit";
} elseif(isset($_GET['stat']) && $_GET['stat'] == 'free') {
$sql = "SELECT * FROM ".TBL_USER." WHERE account_type_id = 1 and id !=53 ORDER BY id DESC LIMIT $limitvalue, $limit";
} elseif(isset($_GET['stat']) && $_GET['stat'] == 'paid') {
$sql = "SELECT * FROM ".TBL_USER." WHERE account_type_id != 1 and id !=53 ORDER BY id DESC LIMIT $limitvalue, $limit";
}  elseif(isset($_GET['stat']) && $_GET['stat'] == 'temp') {
$sql = "SELECT * FROM ".TBL_TMPUSER." ORDER BY id DESC LIMIT $limitvalue, $limit";
}   else {
$sql = "SELECT * FROM ".TBL_USER." WHERE aprove = 0 and id !=53 ORDER BY id DESC LIMIT $limitvalue, $limit";
}
$rs = $obj->executeQry($sql,$link);
$row = $obj->fetchrow();
$rownum = $obj->getrow();

if($rownum < 1){
	$error[] = 'No records forund........';
}

if($page > 1){     
	$prevpage = $page-1;
	//$pagination = '<a href=\''.ADMPAGES.'?page='.$prevpage.'\'>'.$prevpage.'</a>&nbsp;';
}

$numofpages = ceil($totalrows / $limit);
$lpm1 = $numofpages - 1;
if ($numofpages < 7 + ($adjacents * 2))
{    
	for ($counter = 1; $counter <= $numofpages; $counter++){
		if($page == $counter)
		$pagination .= $counter."&nbsp;";
		
		else 
		$pagination .= '<a href=\''.ADMUSERS.'?stat='.$stat.'&page='.$counter.'\'>'.$counter.'</a>&nbsp;';
		
	}
}

if ($numofpages >= 7 + ($adjacents * 2))
{
	if($page < 1 + ($adjacents * 3)){
		for ($counter = 1; $counter <= 4 + ($adjacents * 2); $counter++){
			if($page == $counter)
			$pagination .= $counter."&nbsp;";
			
			else 
			$pagination .= '<a href=\''.ADMUSERS.'?stat='.$stat.'&page='.$counter.'\'>'.$counter.'</a>&nbsp;';
			
		}
		$pagination .= "...";
		$pagination .= "<a href=\"" . ADMUSERS."?stat='".$stat."'&page=" . $lpm1 . "\">$lpm1</a>&nbsp;";
		$pagination .= "<a href=\"" . ADMUSERS."?stat='".$stat."'&page=" . $numofpages . "\">$numofpages</a>&nbsp;";
	}
	
	elseif($numofpages - ($adjacents * 2) > $page && $page > ($adjacents * 2))
	{
		$pagination .= "<a href=\"" . ADMUSERS."?stat='".$stat."'&page=" . "1\">1</a>&nbsp;";
		$pagination .= "<a href=\"" . ADMUSERS."?stat='".$stat."'&page=" . "2\">2</a>&nbsp;";
		$pagination .= "...";
		for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
		{
			if ($counter == $page)
				$pagination .= $counter;
			else
				$pagination .= "<a href=\"" . ADMUSERS."?stat='".$stat."'&page=" . $counter . "\">$counter</a>&nbsp;";					
		}
		$pagination .= "...";
		$pagination .= "<a href=\"" . ADMUSERS."?stat='".$stat."'&page=" . $lpm1 . "\">$lpm1</a>&nbsp;";
		$pagination .= "<a href=\"" . ADMUSERS."?stat='".$stat."'&page=" . $numofpages . "\">$numofpages</a>&nbsp;";		
	}
	else
	{
		$pagination .= "<a href=\"" . ADMUSERS."?stat='".$stat."'&page=" . "1\">1</a>&nbsp;";
		$pagination .= "<a href=\"" . ADMUSERS."?stat='".$stat."'&page=" . "2\">2</a>&nbsp;";
		$pagination .= "...";
		for ($counter = $numofpages - (1 + ($adjacents * 3)); $counter <= $numofpages; $counter++)
		{
			if ($counter == $page)
				$pagination .= $counter;
			else
				$pagination .= "<a href=\"" . ADMUSERS."?stat='".$stat."'&page=" . $counter . "\">$counter</a>&nbsp;";					
		}
	}

}
?>
<script type="text/javascript" src="ibox.js"></script>
<?php require_once(INCLUDES_DIR.ADMHEADER); ?>
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="29" align="center" class="submenu"><?php if($_GET['stat'] != 'free'){?><a href="users.php?stat=free"  class="style1">Free Members</a><?php } else { echo "Free Members"; } ?></td>
	<td height="29" align="center" class="submenu"><?php if($_GET['stat'] != 'fb'){?><a href="users.php?stat=fb"  class="style1">Facebook/twitter Member</a><?php } else { echo "Free Members"; } ?></td>
    <td align="center" class="submenu"><?php if($_GET['stat'] != 'paid'){?><a href="users.php?stat=paid"  class="style1">Paid Members</a><?php } else { echo "Paid Members"; } ?></td>
    <td align="center" class="submenu"><?php if($_GET['stat'] != 'unaprove'){?><a href="users.php?stat=unaprove">Users Waiting Aproval</a><?php } else { echo "Users Waiting Aproval"; } ?></td>
	 <td align="center" class="submenu"><?php if($_GET['stat'] != 'temp'){?><a href="users.php?stat=temp">Temp Users</a><?php } else { echo "Temp Users"; } ?></td>
  </tr>
</table>
<table width="1000" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="17"></td>
  </tr>
  <tr>
    <td><table width="910" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td class="pagehead"><table width="900" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="315" height="35">Manage Users</td>
            <td width="63"><!--Sort By:--></td>
            <td width="131"><!--<select name="jumpMenu" id="jumpMenu" onchange="MM_jumpMenu('parent',this,0)">
              <option>item1</option>
            </select>--></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td valign="top" bgcolor="#FFFFFF"><table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="25" class="error"><?php ShowError($error); ShowInfo($info);?></td>
          </tr>
          <!--<tr>
            <td class="bg_message"><table width="800" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="39"><img src="images/icon_mess.jpg" width="39" height="26" /></td>
                <td><?php ShowInfo($info);?></td>
              </tr>
            </table></td>
          </tr>-->
          <tr>
            <td height="9"></td>
          </tr>
          <tr>
            <td></td>
          </tr>
	<?php if($rownum > 0) { ?>
          <tr>
            <td class="bdr1"><table width="801" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td height="8" class="bg1" ><img src="../images/bg1.PNG" width="7" height="8" /></td>
              </tr>
              <tr>
                <td><table width="801" border="0" cellpadding="0" cellspacing="0" class="main">
                  <tr>
                    <td width="66" height="30" align="center" valign="middle" class="bdr2">S. No.</td>
                    <td width="178" align="center" valign="middle" class="bdr2">Name</td>
                    <td width="175" align="center" valign="middle" class="bdr2">Organisation</td>
                    <td width="122" align="center" valign="middle" class="bdr2"><?php  if($_GET['stat']=='temp'){ echo "Register Date"; } else { echo "Membership Type"; } ?></td>
                    <td width="85" align="center" valign="middle" class="bdr2"><?php  if($_GET['stat']=='temp'){ echo "Email ID"; } else { echo "Status"; } ?></td>
                    <?php  if($_GET['stat']!='temp'){ ?><td width="174" align="center" valign="middle" class="bdr3">Actions</td><?php } ?>
                  </tr>
<?php           
		$i = 1;
		do {
			$stat = ($row['status'] == '0')?'<img src="'.IMAGE_DIR.'icon_status1.png" name="Image62" width="28" height="24" border="0" id="Image62" title="Active" />':'<img src="'.IMAGE_DIR.'deactive_icon.png" border="0" title="Inactive" />';
		$membership_typ = mysql_fetch_array(mysql_query("SELECT package_type FROM ".TBL_PACKAGE." WHERE id = '".$row['account_type_id']."'"));
?>

                  <tr class="<?php echo (($i%2) == 0) ? "bg_td1" : "bg_td2";?>">
                    <td align="center"><?php echo $i;?></td>
                    <td><?php echo $row['fname'].' '.$row['lname'];?></td>
                    <td><?php echo $row['company_name'];?></td>
                    <td><?php if($_GET['stat']=='temp'){ echo date("d-m-Y" , strtotime($row['register_date'])); } else { echo $membership_typ['package_type']; }?></td>
<?php if(isset($_GET['stat']) && $_GET['stat'] == 'unaprove'){ ?>
			<td align="center"><a href="users.php?action=aprove&id=<?php echo $row['id'];?>&page=<?php echo $page;?>" ><img src="../images/wait_icon.png" name="Image65" width="28" height="24" border="0" id="Image64" /></a></td>
<?php } elseif(isset($_GET['stat']) && $_GET['stat'] == 'temp'){ ?>
<td align="center"><?php echo $row['email'];?></td> <?php
} else { ?>
                    <td align="center"><a href="users.php?action=statchng&user=<?=$row['id'];?>&status=<?=$row['status'];?>&page=<?php echo $page;?>" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image62','','../images/icon_status2.png',1)"><?=$stat;?></a></td>
<?php } ?>
                <?php  if($_GET['stat']!='temp'){  ?>    <td align="center"><a href="view_user.php?id=<?=$row['id'];?>" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image64','','../images/icon_view1.png',1)" rel="ibox"><img src="../images/icon_view.png" name="Image64" width="28" height="24" border="0" id="Image64" style="border:none" /></a>&nbsp;&nbsp;
			<!--<a href="add_user.php?action=edit&id=<?php echo $row['id'];?>" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image65','','../images/icon_edit2.png',1)"><img src="../images/icon_edit1.png" name="Image65" width="28" height="24" border="0" id="Image65" /></a>-->
			<a href="users.php?action=delete&id=<?=$row['id'];?>&page=<?php echo $page;?>" onClick="if(confirm('Are you sure, You want delete this record?')){location.replace('users.php?action=delete&id=<?=$row['id']?>&page=<?php echo $page;?>');}return false;" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image66','','../images/icon_del2.png',1)"><img src="../images/icon_del1.png" name="Image66" width="28" height="24" border="0" id="Image66" /></a>
			</td> <?php } ?>
                  </tr>
		<?php $i++; } while($row = $obj->fetchRow());
		
		?>
                  </table></td>
              </tr>
              <tr>
                <td height="2" class="bg2"><img src="../images/bg2.PNG" width="12" height="2" /></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          </table></td>
      </tr>
      <tr>
        <td valign="top" class="bg_innerfoot"><table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td align="right" class="footer_num"><?php if ($rownum > 0) echo $pagination;?></td>
            </tr>
          </table></td>
      </tr><?php } ?>
      </table></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>

<?php require_once(INCLUDES_DIR.ADMFOOTER); ?>