<?php 
//error_reporting(E_ALL);
require_once('../config/config.php');
require_once(CLASS_DIR.'dbClass.php');
$cupage = 'order';
$page = $_GET['page'];
$obj = new DBClass();


if(isset($_GET['msg'])){
	if($_GET['msg'] == add){
		$info[] = 'Product Added Successfully';
	}
	if($_GET['msg'] == edit){
		$info[] = 'Product Updated Successfully';
	}
}
if(isset($_GET['action']) && $_GET['action'] == 'statchng'){
	$sql = "SELECT * FROM coal_listing WHERE id = ".$_GET['id'];
	$rs = $obj->executeQry($sql,$link);
	$rows = $obj->fetchRow();
	$rowNum = $obj->getRow();
	if ($rowNum > 0){
		
        if($rows['status']=='0'){
		  $updt = 1;
		}
        else{
            $updt = 0;
            
        // *********************** start  mail to admin *****************
		$to        	= 	$rows['email1'];
		$subject	=	"Coalbazaar.com product listing approved";
		$message	= 	"Dear ".$rows['person_name']."  <br>".
		"Your request for Product Listing with coalbazaar.com is aproved.<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($updt==0) {
		$sql2 = "UPDATE coal_listing SET status = '".$updt."', actdate = '".date("Y-m-d")."' WHERE id = ".$_GET['id'];
		}
		else {
		$sql2 = "UPDATE coal_listing SET status = '".$updt."' WHERE id = ".$_GET['id'];
		}
		$rs2 = $obj->executeQry($sql2,$link);
		if($rs2){
			$info[] = "Status Updated Successfully...!";
		}
	}
}

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


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


if(isset($_POST['submit']) && $_POST['submit'] == 'Find CRN'){
        
        $sql = "SELECT * FROM coal_listing  WHERE crn = ".$_POST['crn'];
        $rs = $obj->executeQry($sql,$link);
        $totalrows = $obj->getRow();
        
        $limit = 20;
        if(empty($page)) {
        $page = 1;
        }
        $limitvalue = ($page - 1) * $limit;
        
        $sql = "SELECT * FROM coal_listing WHERE crn = ".$_POST['crn']." 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);
        for($i=1; $i<=$numofpages; $i++){
        	if($page == $i)
        	$pagination .= $i."&nbsp;";
        	
        	else 
        	$pagination .= '<a href=\''.ADMCRN.'?page='.$i.'\'>'.$i.'</a>&nbsp;';
        	
        }
        
        if($page < $numofpages){    
        	$nextpage = $page+1;
        	//$pagination .= '<a href=\''.ADMPAGES.'?page='.$nextpage.'\'>'.$nextpage.'</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"><a href="orders.php">Manage Listings</a></td>
    <td align="center" class="submenu"><a href="search_by_date.php">Search By Date</a></td>
    <td align="center" class="submenu">Search By CRN</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>
            <form action="" method="post">
            <td width="250" height="35">Search By CRN</td>
            <td width="250" height="35">Enter CRN : <input type="text" name="crn" id="crn"/></td>
            <td width="250" height="35"><input type="submit" name="submit" value="Find CRN"/></td>
            <td width="160" height="35">&nbsp;</td>
            </form>
          </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="61" height="30" align="center" valign="middle" class="bdr2">S.No.</td>
                    <td width="300" align="center" valign="middle" class="bdr2">Name(Organsatoin Name)</td>
                    <td width="81" align="center" valign="middle" class="bdr2">Activate Date</td>
                    <td width="85" align="center" valign="middle" class="bdr2">CRN</td>
                    <td width="61" align="center" valign="middle" class="bdr2">Status</td>
                    <td width="69" align="center" valign="middle" class="bdr2">Popular</td>
                    <td width="144" align="center" valign="middle" class="bdr3">Actions</td>
                  </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" />';
			$pop = ($row['popular'] == '1')?'<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" />';
			$obj1 = new DBClass();
            
			$odate = date("d-m-Y",strtotime($row['actdate']));
?>

                  <tr class="<?php echo (($i%2) == 0) ? "bg_td1" : "bg_td1";?>">
                    <td align="center"><?php echo $i;?></td>
                    <td><?php echo $row['person_name'].' ('.$row['organisation_name'].')';?></td>
                    <td><?php echo $odate;?></td>
                    <td><?php echo $row['crn'];?></td>
                    <td align="center"><a href="orders.php?action=statchng&id=<?=$row['id'];?>&page=<?php echo $page;?>" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image62','','../images/icon_status2.png',1)"><?=$stat;?></a></td> 
                    <td align="center"><a href="orders.php?action=popchng&id=<?=$row['id'];?>&page=<?php echo $page;?>" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image62','','../images/icon_status2.png',1)"><?=$pop;?></a></td> 
                    <td align="center">&nbsp; &nbsp;&nbsp;<a href="view_order.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" /></a>&nbsp;&nbsp;
			<a href="add_listing.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="orders.php?action=delete&id=<?=$row['id'];?>&page=<?php echo $page;?>" onClick="if(confirm('Are you sure, You want delete this record?')){location.replace('orders.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>
                  </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); ?>