<?php

if($_SESSION["type"] != 1){
	echo "No permission to access"; die();
	}

?>
<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 (isset($_FILES["file"])) {
            if ($_FILES["file"]["error"] > 0) {
                echo "Error: " . $_FILES["file"]["error"] . "<br>";
            } else {
                //===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
		
        mysqli_query($conn,"SET NAMES utf8");
        mysqli_query($conn,"insert into tbl_ads values('','$ads_name','$url','$ads_url',$ads_type)") 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">
                        <div class="col-lg-6">
                            <div class="form-group">
                                <label>Image File</label>
                                <input type="file" name="file" required />
                            </div>
                            <div class="form-group">
                                <label style="color:#3a3a3a;"v>Ads Name</label>
                                <input class="form-control" placeholder="Ads Name" name="ads_name"/>
                            </div>
                            <div class="form-group">
                                <label>Ads Url</label>
                                <input class="form-control" placeholder="Ads Url" name="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" />
                            </div>
                            <div class="form-group" style="margin-top:-25px;">
                                <label></label><br/>
                                <input type="submit" name="btsave" class="btn btn-success" value="&nbsp;Save&nbsp;" title="Save"/>
                              
                            </div> 
                        </div>
                           
                    </form>
                </div>
            </div>
       </div>
    </div>
</div>
<?php
	include('ads_data.php');
?>