<?php

if($_SESSION["type"] != 1){
	echo "No permission to access"; die();
	}
$ban_id= $_GET['ban_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;Banner</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/banner/";      
    if (isset($_POST['btsave'])) {
        $ban_name = $_POST['ban_name'];
        $ban_url = $_POST['ban_url'];
		$ban_order = $_POST['ban_order'];
		if (file_exists($_FILES['file']['tmp_name'])){
			$check=mysqli_query($conn,"SELECT * FROM tbl_banner WHERE ban_id=$ban_id");
					$url_row=mysqli_fetch_array($check);
						{unlink("../images/banner/".$url_row['ban_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_banner set ban_name='$ban_name',ban_file='$url',ban_url='$ban_url',ban_order=$ban_order where ban_id = $ban_id") or die(mysql_error());
		}else{
			mysqli_query($conn,"SET NAMES utf8");
        mysqli_query($conn,"update tbl_banner set ban_name='$ban_name',ban_url='$ban_url',ban_order=$ban_order where ban_id = $ban_id") or die(mysql_error());
		}
        ?>
        	<script type="text/javascript">
              window.location.href="?page=addbanner";
            </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_banner where ban_id = $ban_id"); 
							while($row5=mysqli_fetch_array($sql_p)){
						?>
                        <div class="col-lg-6">
                            <div class="form-group">
                                <label>Image File</label>
                                <img src="../images/banner/<?php echo $row5['ban_file'];?>" width="200" style="border:1px solid #F00; float:right"/><input type="file" name="file" /> 
                            </div>
                            <div class="form-group">
                                <label style="color:#3a3a3a;"v>Slider Name</label>
                                <input class="form-control" placeholder="Slider Name" name="ban_name" value="<?php echo $row5['ban_name'];?>"/>
                            </div>
                            <div class="form-group">
                                <label>Slider Url</label>
                                <input class="form-control" placeholder="Slider Url" name="ban_url" value="<?php echo $row5['ban_url'];?>" />
                            </div>
                            <div class="form-group">
                                <label>Order No</label>
                                <input class="form-control" placeholder="Order No" name="ban_order" value="<?php echo $row5['ban_order'];?>" />
                            </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=addbanner">
                            <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('banner_data.php');
?>
