<?php
session_start();
$key = md5(date("Y-m-d").":".$_SERVER['REMOTE_ADDR']);
if (isset($_SESSION['aW1iZXN0cmEuY29tLm15']) <> $key) {
    header("location:".$_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME']); // cara baru !
} else {
    include "_header.php";
    include "_navtop.php";
    include "_navside.php";
    include "_functions.php";
    require "conn.php"; 
    
    date_default_timezone_set("Asia/Kuala_Lumpur");
    $tarikh = date("Y-m-d H:i:s");
    

// Check kalau ada sumbit package ADD
        if (isset($_POST['pkg-add'])) {
        
            $pkgName     = mysqli_real_escape_string($con, $_POST['pkg_name']);
            $pkgPrice    = mysqli_real_escape_string($con, $_POST['pkg_price']);  
            $pkgLevel    = mysqli_real_escape_string($con, $_POST['pkg_level']);
            $pkgDesc     = mysqli_real_escape_string($con, $_POST['pkg_desc']);
            $pkgImgs     = basename($_FILES['pkgimg']['name']);
            $pkgImges    = str_replace(" ","_",$pkgImgs);
            
    	    //Check if inputs are empty
        	if (empty($pkgName) || empty($pkgPrice) || empty($pkgLevel) || empty($pkgDesc)) {
        		$notis = "Your imput is empty !";
        		
        	} else {

                if(empty($pkgImgs)) { 
                    $pkgIm = $pkgImgs;
                    mysqli_query($con, "INSERT INTO setting_trip_package (trip_level,trip_name,trip_price,trip_desc)
                    VALUES ('".$pkgLevel."', '".$pkgName."', '".$pkgPrice."', '".$pkgDesc."')");
                        
                    $notis = "</br><div class='alert alert-info' role='alert'> * ".$pkgName." trip package was successfully added !</div>";  
                
                }   else    {  
                  
                    $pkgIm = $pkgImges;  
                    // Get file info 
                    $fileName = basename($_FILES['pkgimg']['name']); 
                    $fileType = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); 
                    
                    // Allow certain file formats 
                    $allowTypes = array('jpg','png','jpeg','gif'); 
                    
                    if(in_array($fileType, $allowTypes)){ 
                        
                        $image      = $_FILES['pkgimg']['tmp_name']; 
                        $imgContent = addslashes(file_get_contents($image));
                        $image      = file_get_contents($image);
                        $image_name = $pkgImges;
                        
                        file_put_contents('images/'.$image_name, $image);
                        $img_path   = 'images/'.$image_name;
                            
                        // Insert image content into database 
                        $sqlImg = "INSERT INTO setting_trip_package (trip_level,trip_name,trip_price,trip_desc,trip_img)
                        VALUES ('".$pkgLevel."', '".$pkgName."', '".$pkgPrice."', '".$pkgDesc."', '".$img_path."')";
                       
                        if (mysqli_query($con, $sqlImg)) {
                            $notis = "<div class='alert alert-info' role='alert'> * ".$pkgName." trip package was successfully added !</div>";
                        }else{ 
                            $notis = "<div class='alert alert-info' role='alert'> * ".$pkgName." trip package was unsuccessful add, please try again !</div>";
                        }  
                    }else{ 
                        $notis = "<div class='alert alert-info' role='alert'> * Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload !</div>";
                    } 
                    
                }  
         	}
        }


// Check kalau ada sumbit package UPDATE
        if (!empty(isset($_POST['pkg-update'])) ){
           
            $pkgID          = mysqli_real_escape_string($con, $_POST['pkg_id']);
        	$pkgName        = mysqli_real_escape_string($con, $_POST['pkg_name']);
            $pkgPrice       = mysqli_real_escape_string($con, $_POST['pkg_price']);  
            $pkgLevel       = mysqli_real_escape_string($con, $_POST['pkg_level']);
            $pkgDesc        = mysqli_real_escape_string($con, $_POST['pkg_desc']);
            $pkgImgs        = basename($_FILES['pkgimg']['name']);
            $pkgImges       = str_replace(" ","_",$pkgImgs);
        		
                if(empty($pkgImgs)) { 
                    $pkgIm = $pkgImgs;
                    
                    mysqli_query($con, "UPDATE setting_trip_package 
                                        SET     trip_level  ='".$pkgLevel."', 
                                                trip_name   ='".$pkgName."', 
                                                trip_price  ='".$pkgPrice."', 
                                                trip_desc   ='".$pkgDesc."' 
                                        WHERE   trip_id     ='".$pkgID."' ");
                        
                    $notis = "</br><div class='alert alert-info' role='alert'> * ".$pkgName." trip package was successfully added !</div>";  
                
                }   else    {  
                  
                    $pkgIm = $pkgImges;  
                    // Get file info 
                    $fileName = basename($_FILES['pkgimg']['name']); 
                    $fileType = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); 
                    
                    // Allow certain file formats 
                    $allowTypes = array('jpg','png','jpeg','gif'); 
                    
                    if(in_array($fileType, $allowTypes)){ 
                        
                        $image      = $_FILES['pkgimg']['tmp_name']; 
                        $imgContent = addslashes(file_get_contents($image));
                        $image      = file_get_contents($image);
                        $image_name = $pkgImges;
                        
                        file_put_contents('images/'.$image_name, $image);
                        $img_path   = 'images/'.$image_name;
                        
                        // Insert image content into database 
                        $sqlImg = " UPDATE setting_trip_package 
                                    SET     trip_level  ='".$pkgLevel."',
                                            trip_name   ='".$pkgName."',
                                            trip_price  ='".$pkgPrice."',
                                            trip_desc   ='".$pkgDesc."',
                                            trip_img    ='".$img_path."' 
                                    WHERE   trip_id     ='".$pkgID."' ";
                       
                        if (mysqli_query($con, $sqlImg)) {
                            $notis = "<div class='alert alert-info' role='alert'> * ".$pkgName." trip package was successfully added !</div>";
                        }else{ 
                            $notis = "<div class='alert alert-info' role='alert'> * ".$pkgName." trip package was unsuccessful add, please try again !</div>";
                        }  
                    }else{ 
                        $notis = "<div class='alert alert-info' role='alert'> * Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload !</div>";
                    } 
                    
                } 
         	
        }








// Check kalau ada sumbit package DEL
        if (!empty(isset($_POST['pkg-del'])) ){
           
            $tripID                = mysqli_real_escape_string($con, $_POST['pkg_id']);
        	$sql_package           = "SELECT * FROM setting_trip_package WHERE trip_id='".$tripID."' ";
        	$result_package        = mysqli_query($con, $sql_package);
        	$row_package           = mysqli_fetch_array($result_package);
        	$resultCheck_package   = mysqli_num_rows($result_package);
        		
        	if ($resultCheck_package == 1) {
        		   
        	    $pkgName            = $row_package['trip_name'];
        	    $filename           = $row_package['trip_img'];
            
                mysqli_query($con, "DELETE FROM setting_trip_package WHERE trip_id='".$tripID."' ");
                
               if (file_exists($filename)) {
                    unlink($filename);
                    $notis = "</br><div class='alert alert-info' role='alert'> * ".$pkgName." trip package was successfully remove !</div>";
                } else {
                    $notis = "</br><div class='alert alert-info' role='alert'> * ".$pkgName." trip package image was unsuccessful remove !</div>";
                }

        	} else {

                $pkgName            = "";
                $pkgPrice           = "";  
                $pkgLevel           = "";
                $pkgDesc            = "";
                $pkgImg             = "";
                $pkgButton          = '<button type="submit" name="pkg-add" class="btn btn-primary" form="package">Add</button>';
                $pkgTitle           = '<i class="fa fa-plus-square-o fa-1x "></i>';
         	}
         	
        }






        
// Check kalau ada sumbit package EDIT
        if (!empty(isset($_POST['pkg-edit'])) ){
           
            $tripID                = mysqli_real_escape_string($con, $_POST['pkg_id']);
        	$sql_package           = "SELECT * FROM setting_trip_package WHERE trip_id='".$tripID."'";
        	$result_package        = mysqli_query($con, $sql_package);
        	$row_package           = mysqli_fetch_array($result_package);
        	$resultCheck_package   = mysqli_num_rows($result_package);
        		
        	if ($resultCheck_package == 1) {
        		
        		$pkgID              = $row_package['trip_id'    ];   
        	    $pkgName            = $row_package['trip_name'  ];
                $pkgPrice           = $row_package['trip_price' ];  
                $pkgLevel           = $row_package['trip_level' ];
                $pkgDesc            = $row_package['trip_desc'  ];
                $pkgImg             = $row_package['trip_img'   ];
                $pkgButton          = '<button type="submit" name="pkg-update" class="btn btn-primary" form="package">Save</button>';
                $pkgTitle           = '<i class="fa fa-edit fa-1x "></i>';
                
        	} else {

                $pkgName            = "";
                $pkgPrice           = "";  
                $pkgLevel           = "";
                $pkgDesc            = "";
                $pkgImg             = "";
                $pkgButton          = '<button type="submit" name="pkg-add" class="btn btn-primary" form="package">Add</button>';
                $pkgTitle           = '<i class="fa fa-plus-square-o fa-1x "></i>';
         	}
         	
        } else {
                $pkgName            = "";
                $pkgPrice           = "";  
                $pkgLevel           = "";
                $pkgDesc            = "";
                $pkgImg             = "";
                $pkgButton          = '<button type="submit" name="pkg-add" class="btn btn-primary" form="package">Add</button>';
                $pkgTitle           = '<i class="fa fa-plus-square-o fa-1x "></i>';
        }
        
    
?>
<script>
    window.setTimeout(function() {
        $(".alert").fadeTo(500, 0).slideUp(500, function(){
            $(this).remove();
        });
    }, 2000);
</script>

        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2><i class="fa fa-cogs"></i> Setting</h2>                        
                    </div>
                </div>
            <hr />
            <?= $notis ?>
			<div class="row">
                <div class="col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <?= $pkgTitle; ?> Trip Package <span class="pull-right"><a href="trip_packages.php" style="text-decoration: none;">+ Create New</a></span>
                        </div>
                        <div class="panel-body">
                            <div class="row">
                            <form id="package" role="form" method="post" enctype="multipart/form-data">   
                            <input class="form-control" type="hidden" name="pkg_id" value="<?= $pkgID; ?>" />
                                <div class="col-md-12">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Package Name</label>
                                            <input class="form-control" type="text" name="pkg_name" value="<?= ucwords($pkgName); ?>" required />
                                        </div>
                                        <div class="form-group">
                                            <label>Package Price</label>
                                            <input class="form-control" type="number" name="pkg_price" value="<?= $pkgPrice; ?>" required />
                                        </div>
                                        <div class="form-group">
                                            <label>Package Level</label>
                                            <select class="form-control" name="pkg_level" required >
                                                <option value="<?= $pkgLevel; ?>">  <?= ucwords($pkgLevel); ?>    </option>
                                                <option value="level 1">            Level 1             </option>
                                                <option value="level 2">            Level 2             </option>
                                                <option value="level 3">            Level 3             </option>
                                                <option value="level 4">            Level 4             </option>
                                                <option value="level 5">            Level 5             </option>
                                            </select>
                                            
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Package Image</label>
                                            <input class="form-control" type="file" name="pkgimg"  required />
                                        </div>
                                        <div class="form-group">
                                            <label>Short Description</label>
                                            <textarea class="form-control" name="pkg_desc" rows="5">
                                            <?= $pkgDesc; ?>
                                            </textarea>
                                        </div>
                                    </div>
                                    <div class="col-md-12">
                                        <?= $pkgButton; ?>
                                    </div>
                                </div>
                                </form>
                            </div>
                                
                        </div>
                    </div>
                </div>

			</div>

            <div class="row">
                <div class="col-md-12">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                             Trip Package Listing  
                        </div>
                        <div class="panel-body">
                         

<?
$c = 1;    
    $sql = "SELECT * FROM setting_trip_package ORDER BY trip_level, trip_price ASC";
    $result = mysqli_query($con, $sql);

    while ($row = mysqli_fetch_array($result)) {
?>
                                                
                                                <div class="container col-md-12 border border-info" style="margin-bottom:20px;">
                                                  <form id="pkg<?= $row['trip_id']; ?>" method="post" action="">  
                                                  <img src="<?= $row['trip_img']; ?>" class="responsive" style="width:100%; height:200px;">
                                                  <input class="form-control" type="hidden" name="pkg_id" value="<?= $row['trip_id']; ?>" />
                                                    <div class="level ">
                                                        <div class="col-md-12 border-1" style="margin-left:5px;">
                                                            <h5 style="margin-top:5px;"><i class="fa fa-flag fa-flip-horizontal"></i> <?= ucwords($row['trip_level']); ?> </h5>
                                                            <h1 style="margin-top:-5px;">RM <?= $row['trip_price']; ?> <span class="small" style="color:#FFF;">/ pax</span></h1> 
                                                        </div>
                                                    </div>
                                                    <div class="content " >
                                                        <div class="col-md-9 border-1" style="margin-left:5px; margin-top:-10px; display:inline-block;">
                                                            <div >
                                                                <h3><?= ucwords($row['trip_name']); ?></h3>  
                                                                <h6  style="margin-top:-5px;"><span class="small"><?= ucwords($row['trip_desc']); ?> </span></h6> 
                                                            </div>
                                                        </div>
                                                        <div class="col-md-3" style="position: absolute; top: 50%; left: 95%; transform: translate(-50%, -50%);">
                                                            <button name="pkg-edit" type="submit" class="btn btn-circle btn-xl btn-outline-light" style="background:#000;" form="pkg<?= $row['trip_id']; ?>">  <i class="fa fa-edit fa-2x "       title="Edit Package"      ></i></button>
                                                            <button name="pkg-del"  type="submit" class="btn btn-circle btn-xl btn-outline-light" style="background:#000;" form="pkg<?= $row['trip_id']; ?>">   <i class="fa fa-trash-o fa-2x btn" title="Delete Package"    ></i></button>
                                                        </div>                                                    
                                                    </div>
                                                  </form>
                                                </div> 
                                                
                                         

 <? } ?>
 

 

                           
                            
                        </div>
                    </div>
                    <!--End Advanced Tables -->
                </div>
            </div>

            </div>
        </div>
<?
    include "_footer.php";
}
?>
