<?php

if($_SESSION["type"] != 1){
	echo "No permission to access"; die();
	}
$ads_id= $_GET['ads_id'];
?>
<div class="row">
    <div class="col-lg-12" style="margin-top:-41px;margin-bottom:-13px;">
        <h1 class="page-header" style="font-size:19px; color:#09F;"><i class="fa fa-user fa-fw"></i>&nbsp;Advertising</h1>
    </div>
    <!-- /.col-lg-12 -->
</div>
<!-- Row -->
<?php  
	function getExtension($str) {
        $i = strrpos($str, ".");
        if (!$i) {
            return "";
        }
        $l = strlen($str) - $i;
        $ext = substr($str, $i + 1, $l);
        return $ext;
    }
    $upload_dir = "../images/ads/";      
    if (isset($_POST['btsave'])) {
        $ads_name = $_POST['ads_name'];
        $ads_url = $_POST['ads_url'];
		$ads_type = $_POST['ads_type'];
		if (file_exists($_FILES['file']['tmp_name'])){
			$check=mysqli_query($conn,"SELECT * FROM tbl_ads WHERE ads_id=$ads_id");
					$url_row=mysqli_fetch_array($check);
						{unlink("../images/ads/".$url_row['ads_file']);}
				//===function random name
				$image = $_FILES['file']['name'];
				$tempext = getExtension($image);
				$extfile = mt_rand() . "." . $tempext;
				//===function random name
				move_uploaded_file($_FILES["file"]["tmp_name"], $upload_dir . $extfile);
				//echo "Uploaded File :" . $_FILES["myfile"]["name"];
				$url=$extfile;		
		}
//======end upload file
		if($_FILES['file']['tmp_name'] !=NULL){
        mysqli_query($conn,"SET NAMES utf8");
        mysqli_query($conn,"update tbl_ads set ads_name='$ads_name',ads_file='$url',ads_url='$ads_url',ads_type=$ads_type where ads_id = $ads_id") or die(mysql_error());
		}else{
			mysqli_query($conn,"SET NAMES utf8");
        mysqli_query($conn,"update tbl_ads set ads_name='$ads_name',ads_url='$ads_url',ads_type=$ads_type where ads_id = $ads_id") or die(mysql_error());
		}
        ?>
        	<script type="text/javascript">
              window.location.href="?page=addads";
            </script>
       <?php 
    }   
?>
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default" style="width:100%;">
            <div class="panel-body">
                <div class="row">
                    <form method="post" enctype="multipart/form-data">
                        <?php
							mysqli_query($conn,'SET NAMES utf8');
							$sql_p=mysqli_query($conn,"SELECT * FROM tbl_ads where ads_id = $ads_id"); 
							while($row5=mysqli_fetch_array($sql_p)){
						?>
                        <div class="col-lg-6">
                            <div class="form-group">
                                <label>Image File</label>
                                <img src="../images/ads/<?php echo $row5['ads_file'];?>" width="80" style="border:1px solid #F00; float:right"/><input type="file" name="file" /> 
                            </div>
                            <div class="form-group">
                                <label style="color:#3a3a3a;"v>Ads Name</label>
                                <input class="form-control" placeholder="Ads Name" name="ads_name" value="<?php echo $row5['ads_name'];?>"/>
                            </div>
                            <div class="form-group">
                                <label>Ads Url</label>
                                <input class="form-control" placeholder="Ads Url" name="ads_url" value="<?php echo $row5['ads_url'];?>" />
                            </div>
                            <div class="form-group">
                                <label>Ads Type <b style="color:#00F;">(1 = Left &nbsp;2 = Right)</b></label>
                                <input class="form-control" placeholder="Ads Type" name="ads_type" value="<?php echo $row5['ads_type'];?>" />
                            </div>
                            <div class="form-group" style="margin-top:-25px;">
                                <label></label><br/>
                                <input type="submit" name="btsave" class="btn btn-success" value="&nbsp;Update&nbsp;" title="Update"/>
                                 <a href="?page=addads">
                            <input type="button" name="addnew" class="btn btn-primary" value="&nbsp;Back&nbsp;" title="Back"/>
                            </a>
                            </div> 
                        </div>
                       <?php } ?>    
                    </form>
                </div>
            </div>
       </div>
    </div>
</div>
<?php
	include('ads_data.php');
?>