<?php
    session_start();
    include "../../db.php";
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('jS  F Y');

    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) ) {
        header("Location: logout.php");
        exit();
    }
    if ( $_SESSION['utype'] != 'master_admin' && $_SESSION['uref'] != 'fssdf4543frts' ) {
        header("Location: logout.php");
        exit();
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Admin Panel</title>

    <link href="css/all.min.css" rel="stylesheet" type="text/css">
    <link href="css/sb-admin-2.min.css" rel="stylesheet">
    <link href="css/alltable.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
	<style>
.colgreen{
	
	color:green;
	font-size:15px;
	
	
	
}


.colred{
	
	color:#286090;
	font-size:15px;
}

</style>

</head>
<body id="page-top" class="mt-55">

<div class="overlay"></div>

    <div id="wrapper">

        <div id="content-wrapper" class="d-flex flex-column">

            <div id="content">

                <?php include "includes/topbar.php"; ?>

                <div class="container-fluid px-0">

                    <div class="card shadow mb-4">
                        <div class="card-body">
 <?php
                                if ( isset($_GET["edit_id"]) ) {
                                    $edit_run = mysqli_query($con, "SELECT * FROM ad WHERE aid = '$_GET[edit_id]'");
                                    $edit_row = mysqli_fetch_assoc($edit_run);
                                    ?>
                        
						
						
                            <h1 class="h3 mb-2 text-gray-800 text-center "><a href="admin_lvl2.php?see_id=<?php echo $edit_row["aid"]; ?>"><?php echo $edit_row["aname"]; ?> </h1>
                       </a>     <hr>

                           
                                    <form class="user" method="post" enctype="multipart/form-data">
                                        <h4 class="mb-3 badge badge-secondary"> <?php echo $edit_row["aname"]; ?></h4>
                                     
										
                                     
										<div class="">
											
											<div  class="form-group row" >
											<div class="col-sm-3 mb-1 mb-sm-0"></div>
											<div class="col-sm-2 mb-1 mb-sm-0">
                                                <label>Header Pic : </label>  

<a  href="<?php echo $edit_row["hpic"]; ?>"  target="_blank"> <img src="<?php echo $edit_row['hpic'] ?>" alt="image" height="20" class=""></a>

												
                        <input type="file" accept="image/*" class="form-control" name="hpic" >  

						</div>
						<div class="col-sm-4 mb-1 mb-sm-0">
						 <label>Header pic url : </label> <a  href="<?php echo $edit_row["hpicurl"]; ?>"  target="_blank">  <?php echo $edit_row["hpicurl"]; ?></a>
                                                <input type="text" class="form-control form-control-sm mb-2" id="hpicurl" value="<?php echo $edit_row["hpicurl"]; ?>"  name="hpicurl" >
                                            </div>
								</div>	
								
								<div  class="form-group row" >
											<div class="col-sm-3 mb-1 mb-sm-0"></div>
											<div class="col-sm-2 mb-1 mb-sm-0">
                                                <label>Middle Blog Pic : </label>  <a  href="<?php echo $edit_row["mbpic"]; ?>"  target="_blank"> <img src="<?php echo $edit_row['mbpic'] ?>" alt="image" height="20" class=""></a>
                        <input type="file" accept="image/*" class="form-control" name="mbpic" >  

						</div>
						<div class="col-sm-4 mb-1 mb-sm-0">
						 <label>Middle Blog Pic url : </label><a  href="<?php echo $edit_row["mbpicurl"]; ?>"  target="_blank">  <?php echo $edit_row["mbpicurl"]; ?></a>
                        <input type="text" class="form-control form-control-sm mb-2" id="mbpicurl" value="<?php echo $edit_row["mbpicurl"]; ?>"  name="mbpicurl" >
                                            </div>
								</div>	
								
								<div  class="form-group row" >
											<div class="col-sm-3 mb-1 mb-sm-0"></div>
											<div class="col-sm-2 mb-1 mb-sm-0">
                                                <label>	End Blog Pic : </label>   <a  href="<?php echo $edit_row["ebpic"]; ?>"  target="_blank"> <img src="<?php echo $edit_row['ebpic'] ?>" alt="image" height="20" class=""></a>
                        <input type="file" accept="image/*" class="form-control" name="ebpic" >  

						</div>
						<div class="col-sm-4 mb-1 mb-sm-0">
						 <label>End Blog pic url : </label><a  href="<?php echo $edit_row["ebpicurl"]; ?>"  target="_blank">  <?php echo $edit_row["ebpicurl"]; ?></a>
                                                <input type="text" class="form-control form-control-sm mb-2" id="ebpicurl" value="<?php echo $edit_row["ebpicurl"]; ?>"  name="ebpicurl" >
                                            </div>
								</div>	
								
								<div  class="form-group row" >
											<div class="col-sm-3 mb-1 mb-sm-0"></div>
											<div class="col-sm-2 mb-1 mb-sm-0">
                                                <label>footer pic : </label>  <a  href="<?php echo $edit_row["fpic"]; ?>"  target="_blank"> <img src="<?php echo $edit_row['fpic'] ?>" alt="image" height="20" class=""></a>
                        <input type="file" accept="image/*" class="form-control" name="fpic" >  

						</div>
						<div class="col-sm-4 mb-1 mb-sm-0">
						 <label>Footer pic url : </label> <a  href="<?php echo $edit_row["fpicurl"]; ?>"  target="_blank"> <?php echo $edit_row["fpicurl"]; ?></a>
                                                <input type="text" class="form-control form-control-sm mb-2" id="fpicurl" value="<?php echo $edit_row["fpicurl"]; ?>"  name="fpicurl" >
                                            </div>
								</div>	

									
										<div  class="form-group row" >
											<div class="col-sm-3 mb-1 mb-sm-0"></div>
						<button type="submit" class="btn btn-success btn-user m-2" name="edit_users">					
					  
                                            <i class="far fa-image"></i> Update Organization Profile
                                        </button>
                                        <a href="admin_lvl2.php" class="btn btn-warning btn-user m-2"> <i class="fas fa-ban"></i> Cancel </a>
								</div>	
										
										
                                    </div> 
                                        
                                    </form>
									 	
                                   
                                    <?php
                                
								
								}
                            ?>

                            <hr>

                         
                        </div>
                    </div>

                </div>

            </div>

            <?php include "includes/footer.php"; ?>

        </div>

    </div>




   



    <script>
        function checkFileDetails() {
            var fi = document.getElementById('file');
            if (fi.files.length > 0) {
                for (var i = 0; i <= fi.files.length - 1; i++) {
                    var fileName, fileExtension, fileSize, fileType, dateModified;
                    fileName = fi.files.item(i).name;
                    fileExtension = fileName.replace(/^.*\./, '');
                    if (fileExtension == 'png' || fileExtension == 'jpg' || fileExtension == 'jpeg') {
                        readImageFile(fi.files.item(i));
                    }
                }
                function readImageFile(file) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        var img = new Image();
                        img.src = e.target.result;
                        img.onload = function () {
                            var imgWidth = this.width;
                            var imgHeight = this.height;

                            if ( imgWidth == 1200 && imgHeight == 512 || imgWidth == 1500 && imgHeight == 640 ) {
                                var imgCheck = "<i class='fas fa-check-circle text-success'></i>";
                            } else {
                                var imgCheck = "<i class='fas fa-times-circle text-danger'></i>";
                            }
                            document.getElementById('fileInfo').innerHTML =
                                document.getElementById('fileInfo').innerHTML + '<br> ' +
                                'Name: <b>' + file.name + '</b> <br>' +
                                'Size: <b>' + Math.round((file.size / 1024)) + '</b> KB <br>' +
                                'Dimension: <b>' + imgWidth + '</b> x <b>' + imgHeight + '</b> ' + imgCheck + ' <br>';
                        }
                    };
                    reader.readAsDataURL(file);
                }
            }
        }
    </script>




    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.easing.min.js"></script>
    <script src="js/sb-admin-2.min.js"></script>
    <script src="js/sweetalert2.min.js"></script>
    <script src="js/jquery.dataTables.min.js"></script>
    <script src="js/dataTables.bootstrap4.min.js"></script>
    <script src="js/datatables.mark.min.js"></script>
    <script src="js/jquery-ui-custom.min.js"></script>
    <script src="js/custom.js"></script>
 <script src="vendor/ckeditor.js"></script>
    <script>
		$(document).ready(function(){
            $('#thisismytable').DataTable();
            $('#show-img').on('show.bs.modal', function (e) {
                var src = $(e.relatedTarget).attr('src');
                $(this).find('.modal-body > img').attr('src', src);
            });
		});

        var $sortable = $("#thisismytable > tbody");

        $sortable.sortable({
            stop: function ( event, ui ) {
                var parameters = $sortable.sortable( "toArray" );
                $.post("slider_position.php",{value:parameters},function(result){
                    $(document).ready(function() {
                        const Toast = Swal.mixin({
                            toast: true,
                            position: 'bottom',
                            showConfirmButton: false,
                            timer: 2000
                        });
                        Toast.fire({
                            type: 'success',
                            title: result
                        });
                    });
                });
            }
        });
	</script>
	

</body>
</html>


<?php
 
 



    if(isset($_POST['edit_ad'])){ 
       
  if( !empty($_FILES["hpic"]["name"]) && isset($_FILES) ){

            $filetmp = $_FILES["hpic"]["tmp_name"];
            $filename = $_FILES["hpic"]["name"];
            $filetype = $_FILES["hpic"]["type"];
            $fileerror = $_FILES["hpic"]["error"];

            $filesize = $_FILES["hpic"]["size"];

            $fileExt = explode('.', $filename);
            $fileActualExt = strtolower(end($fileExt));

            $fileNewName = time().".".$fileActualExt;

            $allowed = array('jpg', 'jpeg', 'pdf', 'png');

            if ( $filesize == 0 || $filesize > 1000000 ) {
                ?>
                <script>
                    alert("Max File Size 1 mb");
                    window.location = "ad.php?edit_id=<?php echo $uid; ?>";
                </script>
                <?php
            } else {
                if ( in_array($fileActualExt, $allowed) ) {

                    $hpic_upl_path = "../upload/ad/".$fileNewName;

                    move_uploaded_file($filetmp, $hpic_upl_path);

                    // $edit_sql = "UPDATE notice SET ulogo = '$ulogo_upl_path', nupdatedon = '$nupdatedon'";
                }
                else {
                    ?>
                    <script>
                        alert("Upload PDF / JPG / JPEG / PNG Image");
                        window.location = "ad.php?edit_id=<?php echo $uid; ?>";
                    </script>
                    <?php
                }
            }
        }


if( !empty($_FILES["mbpic"]["name"]) && isset($_FILES) ){

            $filetmp = $_FILES["mbpic"]["tmp_name"];
            $filename = $_FILES["mbpic"]["name"];
            $filetype = $_FILES["mbpic"]["type"];
            $fileerror = $_FILES["mbpic"]["error"];

            $filesize = $_FILES["mbpic"]["size"];

            $fileExt = explode('.', $filename);
            $fileActualExt = strtolower(end($fileExt));

            $fileNewName = time().".".$fileActualExt;

            $allowed = array('jpg', 'jpeg', 'pdf', 'png');

            if ( $filesize == 0 || $filesize > 1000000 ) {
                ?>
                <script>
                    alert("Max File Size 1 mb");
                    window.location = "ad.php?edit_id=<?php echo $uid; ?>";
                </script>
                <?php
            } else {
                if ( in_array($fileActualExt, $allowed) ) {

                    $mbpic_upl_path = "../upload/ad/".$fileNewName;

                    move_uploaded_file($filetmp, $mbpic_upl_path);

                    // $edit_sql = "UPDATE notice SET ulogo = '$ulogo_upl_path', nupdatedon = '$nupdatedon'";
                }
                else {
                    ?>
                    <script>
                        alert("Upload PDF / JPG / JPEG / PNG Image");
                        window.location = "ad.php?edit_id=<?php echo $uid; ?>";
                    </script>
                    <?php
                }
            }
        }

if( !empty($_FILES["ebpic"]["name"]) && isset($_FILES) ){

            $filetmp = $_FILES["ebpic"]["tmp_name"];
            $filename = $_FILES["ebpic"]["name"];
            $filetype = $_FILES["ebpic"]["type"];
            $fileerror = $_FILES["ebpic"]["error"];

            $filesize = $_FILES["ebpic"]["size"];

            $fileExt = explode('.', $filename);
            $fileActualExt = strtolower(end($fileExt));

            $fileNewName = time().".".$fileActualExt;

            $allowed = array('jpg', 'jpeg', 'pdf', 'png');

            if ( $filesize == 0 || $filesize > 1000000 ) {
                ?>
                <script>
                    alert("Max File Size 1 mb");
                    window.location = "ad.php?edit_id=<?php echo $uid; ?>";
                </script>
                <?php
            } else {
                if ( in_array($fileActualExt, $allowed) ) {

                    $ebpic_upl_path = "../upload/ad/".$fileNewName;

                    move_uploaded_file($filetmp, $ebpic_upl_path);

                    // $edit_sql = "UPDATE notice SET ulogo = '$ulogo_upl_path', nupdatedon = '$nupdatedon'";
                }
                else {
                    ?>
                    <script>
                        alert("Upload PDF / JPG / JPEG / PNG Image");
                        window.location = "ad.php?edit_id=<?php echo $uid; ?>";
                    </script>
                    <?php
                }
            }
        }



if( !empty($_FILES["fpic"]["name"]) && isset($_FILES) ){

            $filetmp = $_FILES["fpic"]["tmp_name"];
            $filename = $_FILES["fpic"]["name"];
            $filetype = $_FILES["fpic"]["type"];
            $fileerror = $_FILES["fpic"]["error"];

            $filesize = $_FILES["fpic"]["size"];

            $fileExt = explode('.', $filename);
            $fileActualExt = strtolower(end($fileExt));

            $fileNewName = time().".".$fileActualExt;

            $allowed = array('jpg', 'jpeg', 'pdf', 'png');

            if ( $filesize == 0 || $filesize > 1000000 ) {
                ?>
                <script>
                    alert("Max File Size 1 mb");
                    window.location = "ad.php?edit_id=<?php echo $uid; ?>";
                </script>
                <?php
            } else {
                if ( in_array($fileActualExt, $allowed) ) {

                    $fpic_upl_path = "../upload/ad/".$fileNewName;

                    move_uploaded_file($filetmp, $fpic_upl_path);

                    // $edit_sql = "UPDATE notice SET ulogo = '$ulogo_upl_path', nupdatedon = '$nupdatedon'";
                }
                else {
                    ?>
                    <script>
                        alert("Upload PDF / JPG / JPEG / PNG Image");
                        window.location = "ad.php?edit_id=<?php echo $uid; ?>";
                    </script>
                    <?php
                }
            }
        }




if( !empty($_FILES["ulogo"]["name"]) && isset($_FILES) ){

            $filetmp = $_FILES["ulogo"]["tmp_name"];
            $filename = $_FILES["ulogo"]["name"];
            $filetype = $_FILES["ulogo"]["type"];
            $fileerror = $_FILES["ulogo"]["error"];

            $filesize = $_FILES["ulogo"]["size"];

            $fileExt = explode('.', $filename);
            $fileActualExt = strtolower(end($fileExt));

            $fileNewName = time().".".$fileActualExt;

            $allowed = array('jpg', 'jpeg', 'pdf', 'png');

            if ( $filesize == 0 || $filesize > 1000000 ) {
                ?>
                <script>
                    alert("Max File Size 1 mb");
                    window.location = "ad.php?edit_id=<?php echo $uid; ?>";
                </script>
                <?php
            } else {
                if ( in_array($fileActualExt, $allowed) ) {

                    $ulogo_upl_path = "../upload/ad/".$fileNewName;

                    move_uploaded_file($filetmp, $ulogo_upl_path);

                    // $edit_sql = "UPDATE notice SET ulogo = '$ulogo_upl_path', nupdatedon = '$nupdatedon'";
                }
                else {
                    ?>
                    <script>
                        alert("Upload PDF / JPG / JPEG / PNG Image");
                        window.location = "ad.php?edit_id=<?php echo $uid; ?>";
                    </script>
                    <?php
                }
            }
        }





  if( !empty($_FILES["uagreement"]["name"]) && isset($_FILES) ){

            $filetmp = $_FILES["uagreement"]["tmp_name"];
            $filename = $_FILES["uagreement"]["name"];
            $filetype = $_FILES["uagreement"]["type"];
            $fileerror = $_FILES["uagreement"]["error"];

            $filesize = $_FILES["uagreement"]["size"];

            $fileExt = explode('.', $filename);
            $fileActualExt = strtolower(end($fileExt));

            $fileNewName = time().".".$fileActualExt;

            $allowed = array('jpg', 'jpeg', 'pdf', 'png');

            if ( $filesize == 0 || $filesize > 1000000 ) {
                ?>
                <script>
                    alert("Max File Size 1 mb");
                    window.location = "ad.php?edit_id=<?php echo $uid; ?>";
                </script>
                <?php
            } else {
                if ( in_array($fileActualExt, $allowed) ) {

                    $uagreement_upl_path = "../upload/agreemenyt/".$fileNewName;

                    move_uploaded_file($filetmp, $uagreement_upl_path);

                    // $edit_sql = "UPDATE notice SET ulogo = '$ulogo_upl_path', nupdatedon = '$nupdatedon'";
                }
                else {
                    ?>
                    <script>
                        alert("Upload PDF / JPG / JPEG / PNG Image");
                        window.location = "ad.php?edit_id=<?php echo $uid; ?>";
                    </script>
                    <?php
                }
            }
        }
		 $hpicurl = mysqli_real_escape_string($con, strip_tags($_POST['hpicurl']));
		 $mbpicurl = mysqli_real_escape_string($con, strip_tags($_POST['mbpicurl']));
		  $ebpicurl = mysqli_real_escape_string($con, strip_tags($_POST['ebpicurl']));
		 $fpicurl = mysqli_real_escape_string($con, strip_tags($_POST['fpicurl']));
        {
            $up_sql = "UPDATE ad SET hpicurl = '$hpicurl', mbpicurl = '$mbpicurl', ebpicurl = '$ebpicurl',fpicurl = '$fpicurl' ";
   
   if(!empty($_FILES["hpic"]["name"])){
            $up_sql .=", hpic='$hpic_upl_path'";
        }
		
		if(!empty($_FILES["mbpic"]["name"])){
            $up_sql .=", mbpic='$mbpic_upl_path'";
        }
		
		if(!empty($_FILES["ebpic"]["name"])){
            $up_sql .=", ebpic='$ebpic_upl_path'";
        }
		
		if(!empty($_FILES["fpic"]["name"])){
            $up_sql .=", fpic='$fpic_upl_path'";
        }
		
  if(!empty($_FILES["ulogo"]["name"])){
            $up_sql .=", ulogo='$ulogo_upl_path'";
        }
if(!empty($_FILES["uagreement"]["name"])){
            $up_sql .=", uagreement='$uagreement_upl_path'";
        }
        $up_sql .= " WHERE uid = '$_GET[edit_id]'";
		 if(mysqli_query($con, $up_sql)){

       
            ?>
            <script>
                alert("Data Updated Successfully");
                window.location = 'ad.php?edit_id=<?php echo $uid; ?>';
            </script>

        
               
                <?php
            }
            else{
                ?>
                <script>
                    alert('OOPS !!!!');
                    setTimeout(() => {
                        window.location = 'ad.php?edit_id=<?php echo $uid; ?>';
                    }, 1200);
                </script>
                <?php
            }
        
            ?>
          
			
			
            <?php
        }
    }

?>