<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');
################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)<0 or $start=="")?0:$start;
$pagesize = intval($pagesize)==0?$pagesize=20:$pagesize;

$heading="Manage Videos";
$record_type="Videos";
$script_name="manage_videos.php";
$table_name="video_tbl";
####################################################

if($id=="add_record") {
    
        $qry1= db_query("select * from $table_name where slink_url='$url'");
        if(NumRows($qry1)>0){
        $_SESSION['session_message']=$record_type.' Already Exists';
        }
    else{
        $sql=db_query("insert into $table_name set slink_title='$title',slink_url='$url'");
		$_SESSION['session_message']='New '.$record_type.' has been added successfully';
    }
		
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
}

if($action=="Update") {
	while (list($key,$val)=each($_POST)) {
		if (substr($key,0,6)=="order_") {
			$key1 = substr($key,6,strlen($key)-6);
			$qry1=mysqli_query($dbconn,"UPDATE `$table_name` SET `client_order`={$val} WHERE `id`={$key1}");
		}
	}
	$_SESSION['session_message']=$record_type.' order has been updated successfully';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;
}

if($id=="update_record") {

	if(!strlen($form_check_alert)>0) {
    
		mysqli_query($dbconn,"UPDATE `$table_name` SET  slink_title='$title', slink_url='$url' where id='$recordID'");
		$_SESSION['session_message']=$record_type.' has been updated successfully';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
	else {
		
		$msg=html_head();
		$msg.=$form_check_alert;
		$msg.=html_foot();
	}	
}

if($id=="delete_record") {
	mysqli_query($dbconn,"delete from $table_name where id='$recordID'");
	$_SESSION['session_message']=$record_type.' has been deleted successfully';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

admin_header("");
?>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
 <tr>
	<td align="left" width="50%" class="myTitle"><?=$heading;?></td>
	<td align="right">
	 <form name="pgimg_form" action="<?=$PHP_SELF?>" method="post" style="margin:0px;padding:0px;">
	 <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
	  <tr>
	   <td align="right">Records Per Page:
	    <select name="pagesize" onChange="this.form.submit();">
	     <option value="0">--Select--</option>
	     <?php
	     foreach($_GLOBALS['pgsize_arr'] as $val) {
		     if(trim($pagesize)==trim($val)) {
			     echo '<option value="'.$val.'" selected>'.$val.'</option>';
		     }
		     else{
			     echo '<option value="'.$val.'">'.$val.'</option>';
		     }
	     }
	     ?>
	    </select>	    
		 </td>
		</tr>

	 </table>
	 </form>
	</td>
 </tr>
</table>
<hr size="2" width="99%" color="#DA251C">
<?php
$qry1= db_query("select * from $table_name where slink_url !=''");
$reccnt = NumRows($qry1);
$qry= db_query("select * from $table_name where slink_url!='' order by id desc limit $start, $pagesize");
echo pageNavigation($reccnt,$pagesize,$start);
print_message();
?>
<table width="99%" border="0" cellpadding="2" cellspacing="2" class="lightBorder table table-bordered table-hover">
	<form action="" method="POST">
 <?php
 if($reccnt>0) {
	 ?>
        <thead>
	  <tr height="25" bgcolor="#EAEAEA">
	  <td width="10%" align="center"><strong>S.No.</strong></td>
	  <td width="30%" align="left"><strong>Video Title</strong></td>
	  <td width="40%" align="center"><strong>Video Url ID</strong></td>
	  <td width="30%" align="left"><strong>Options</strong></td>
	  </tr>
        </thead>
	 <?php
	 $sl=$start+1;
	 while($arr=mysqli_fetch_array($qry)) {
		//  $arr=ms_htmlentities_decode($arr);
		 if($sl%2!=0) {
			 $color="#f9f9f9";
		 }
		 else{
			 $color="#f0f0f0";
		 }
		 ?>	
        <tbody>
		  <tr bgcolor="<?=$color;?>">
		  <td align="center"><?=$sl;?></td>
		  <td align="left"><?=$arr['slink_title'];?></td>
		  <td align="center">
		   <?php
		   if($arr['slink_url']!="") {
echo $arr['slink_url'];
		   }
		   ?>
		   </td>
              <td align="center">&nbsp;<a href="<?=$script_name;?>?id=edit_record&start=<?=$start;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr['id'];?>#bot"><img src="<?=SITE_ADMIN_URL?>/images/edit.gif" alt="Edit This <?=$record_type;?>" title="Edit This <?=$record_type;?>" border="0"></a>&nbsp;<a href="<?=$script_name;?>?id=delete_record&start=<?=$start;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr['id'];?>" onClick="return confirm('Are you sure to delete this record...');"><img src="<?=SITE_ADMIN_URL?>/images/cancel.gif" alt="Delete This <?=$record_type;?>" border="0"></a></td>
		 </tr>

		 <?php
		 $sl++;
	 }?>
 <?php }
 else{
	 ?>
 <tr><td><b>No Record Found........</b></td></tr>
 <?php
 }
 ?>
 </tbody>
 </form>
</table><br />

<form name="bannerFrm" method=post action="" enctype="multipart/form-data" onSubmit="return validate('bannerFrm') && chk_valid_date();">
   <?php
 if($_GET['recordID']=="" || $id!="edit_record") {
	 ?>
	 <h1>Add New <?=$record_type;?></h1>
	 <?php
 }
     else {
	 ?>
	   <div class="text-left myTitle">Edit <?=$record_type;?></div>
        <div class="text-right"><a href="<?=$script_name;?>" class="btn btn-primary text-light"><b>Add New <?=$record_type;?></b></a></div>
	 <?php
 }
    $res=getResult($table_name," where id='$_GET[recordID]'");
 ?>  
       <div class="form-group">
        <label>Video Title <span>*</span></label>
        <?php
        if($res['slink_url']!="") { ?>
	       <input name="title" type="text" value="<?=$res['slink_title'];?>" class="form-control" placeholder="Video Title"/>
        <?php } else{ ?>
        <input name="title" type="text" class="form-control" placeholder="Video Title"/>
        <?php  } ?>
        
    </div>
    <div class="form-group">
        <label>Video link <span>*</span></label>
        <?php
        if($res['slink_url']!="") { ?>
	       <input name="url" type="text" value="<?=$res['slink_url'];?>" class="form-control" placeholder="please enter Youtube video link id"/>
        <?php } else{ ?>
        <input name="url" type="text" class="form-control" placeholder="please enter Youtube video link id"/>
        <?php  } ?>
        <p><b>Please enter Youtube video link id</b></p>
    </div>
    <div class="form-group"><?php
   if($recordID !="") {
	   ?>
	   <input type="submit" name="submit" value="Update <?=$record_type;?>" class="btn btn-success">
	   <input type="hidden" name="recordID" value="<?=$res['id'];?>">
	   <input type="hidden" name="id" value="update_record">
	   <?php	   
   }
   else{
	   ?>
	   <input type="submit" name="submit" value="Add New <?=$record_type;?>" class="btn btn-success">
	   <input type="hidden" name="id" value="add_record">
	   <?php
   }
   ?>
   <input type="hidden" name="start" value="<?=$start;?>">
   <input type="hidden" name="pagesize" value="<?=$pagesize;?>">
    </div>
</form>
<script src="jquery.min.js"></script>
<?php
admin_footer();
exit;
?>