<?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">

   <link href="css/sb-admin-2.min.css" rel="stylesheet">
    <link href="css/all.min.css" rel="stylesheet" type="text/css">
	<link rel="stylesheet" href="vendor/croppie/croppie.css" />



    <script src="vendor/croppie/croppie.js"></script>
    <script src="vendor/ckeditor.js"></script>
</head>
	<style>
.colgreen{
	
	color:green;
	font-size:15px;
	
	
	
}


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

</style>

</head>
<body >



    <div id="wrapper">

        <div id="content-wrapper" class="d-flex flex-column">
<?php include "includes/topbar.php"; ?>
            <div id="con tent">

                

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

                    <div class="card shadow mb-4">
                        <div class="card-body">
		
<?php
                                if ( isset($_GET["ad_id"]) ) {
									$adm = $_GET['ad_id'];
                                    $ad_run = mysqli_query($con, "SELECT * FROM users WHERE uid = '$_GET[ad_id]' AND ad_id='$adm'");
                                    
							
								}
				
                                    ?>				
						
						
 <?php
                                if ( isset($_GET["edit_id"]) ) {
									$adm = $_GET['ad_id'];
                                    $edit_run = mysqli_query($con, "SELECT * FROM ad WHERE uid = '$_GET[edit_id]' AND ad_id='$adm'");
                                    $edit_row = mysqli_fetch_assoc($edit_run);
                                    ?>
                        
						
						<?php
                                } else {
                                    ?>
									
									  <?php
                                if ( isset($_GET["ad_id"]) ) {
                                    $ad_run = mysqli_query($con, "SELECT * FROM users WHERE uid = '$_GET[ad_id]'");
                                    $ad_row = mysqli_fetch_assoc($ad_run);
                                    ?>
              <br>  <br> <br> <br>             
<h1 class="h3 mb-2 text-gray-800 text-center "><a href="admin_lvl2.php?see_id=<?php echo $ad_row["uid"]; ?>"><?php echo $ad_row["uname"]; ?> </h1>
                       </a>     <hr>
                           
                                    <form class="user" method="post" enctype="multipart/form-data">
                                        <h4 class="mb-3 badge badge-secondary"> <?php echo $ad_row["uname"]; ?></h4>
                                        

 <input type="hidden"  type="text" class="form-control form-control-sm mb-2" id="aowner" value="<?php echo $ad_row["uid"]; ?>"  name="aowner" >
                                          
	<input type="hidden"  type="text" class="form-control form-control-sm mb-2" id="aowner" value="<?php echo $ad_row["uname"]; ?>"  name="aname" >
                                          
	<input type="hidden"  type="text" class="form-control form-control-sm mb-2" id="aowner" value="<?php echo $ad_row["utype"]; ?>"  name="atype" >
      <input type="hidden"  type="text" class="form-control form-control-sm mb-2" id="aowner" value="<?php echo $ad_row["uref"]; ?>"  name="aref" >
                                          
	                                    
										
                                     
										<div class="">
											
											<div  class="form-group row" >
											<div class="col-sm-2 mb-1 mb-sm-0"></div>
											
						<div class="col-sm-8 mb-1 mb-sm-0">
						
						 <label>Header pic url : </label> 
                                                 <textarea   name="content" id="content" class="form-control ckeditor" >
                         </textarea>
												
												 <label>Ad Dis: </label>  
                          <textarea  type="text" class="form-control form-control-sm mb-2" id="adist" name="adist" >
						     </textarea>
						  
						   <label>AD Credit Point: </label>
                                                <input type="number" class="form-control form-control-sm mb-2" id="apoint" name="apoint" >

                                            </div>
								</div>	
								

									
										<div  class="form-group row" >
											<div class="col-sm-3 mb-1 mb-sm-0"></div>
											
					   <button type="submit" class="btn btn-primary btn-user m-2" name="new_admin">
                                            <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 class="row m-0">
                                
                                     <div class="col-md-1"></div>
                                <div class="col-md-10 p-0">
                                    <div class="table-responsive">
                                        <table class="table table-hover table-bordered table-sm" id="thisismytable">
                                            <thead>
                                                <tr>
                                                    <th>No.</th>
                                                    <th>Name</th>
                                                 
                                                    <th>Point</th>
                                            
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                             
													 $sql = "SELECT * FROM ad WHERE aowner='$adm' ORDER BY adpos ASC";
                                                    $run = mysqli_query($con, $sql);
                                                   
                                                    $c = 1;
                                                    while ( $rows = mysqli_fetch_assoc($run) ) {
                                                        echo "
                                                        <tr id='$rows[aid]'>
                                                            <td>  
                                                                <span class='badge badge-info badge-pill'>$c</span>
															
                                                            </td>
                                                            <td>
															
															 <a   style='padding:2px  4px 2px 4px; border-radius: 2px;   background: #007bb5; color: white;text-align: center;text-decoration: none;'  href='adx.php?xid=$rows[aid]' target='popup'
  onclick=window.open('adx.php?xid=$rows[aid]','popup','width=600,height=600,scrollbars=no,resizable=no')>
  Ad Content
</a>
                                                         
                                                            <br>
                                                               $rows[ucreated] 

            
			  
                       
                        <br>
 $rows[aid] - $rows[adist]                                                                                                      
                                                               
                                                            </td>
                                                          
                                   <td class='align-middle'>
                                       <br>  <br>    $rows[apoint] 
                                    </td>
                                                           
                                     <td class='text-center align-middle'>
                                                              
 <a href='adedit.php?edit_id=$rows[aid]'  target='_blank' class='m-2 btn btn-success btn-sm rounded-circle' title='Click to Edit'> <i class='fas fa-pencil-alt'></i> </a>

                                                                <a href='ad.php?del_id=$rows[aid]' class='delete m-2 btn btn-danger btn-sm rounded-circle' title='Click to Delete'> <i class='fas fa-trash'></i> </a>
                                                            </td>
                                                        </tr>
														
<div class='modal' id='myModal'>
  <div class='modal-dialog'>
    <div class='modal-content'>


      <div class='modal-header'>
      
        <button type='button' class='close' data-dismiss='modal'>&times;</button>
      </div>


      <div class='modal-body'>
      $rows[hpicurl] 
      </div>

 
      <div class='modal-footer'>
        <button type='button' class='btn btn-danger' data-dismiss='modal'>Close</button>
      </div>

    </div>
  </div>
</div>
                                                        ";
                                                        $c++;
                                                    }
                                                ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                         
                        </div>
                    </div>

                </div>

            </div>




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

        </div>

    </div>




<script>
     CKEDITOR.replace( 'content', {
        height: 200,
        filebrowserUploadUrl: "upload.php"
    });



   
</script>

    <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/custom.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>
		$(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>
<script>
        var $sortable = $("#thisismytable > tbody");

        $sortable.sortable({
            stop: function ( event, ui ) {
                var parameters = $sortable.sortable( "toArray" );
                $.post("category_position.php",{value:parameters},function(result){
                    console.log("Position Complete");
                });
            }
        });
	</script>


</body>
</html>


<?php
 
 
 
    if(isset($_POST['new_admin'])){
	
        $aname = $_POST['aname'];
        $aref = $_POST['aref'];
			$aowner = $_POST['aowner'];
        $atype = $_POST['atype'];
   

  


		 $hpic = $_POST['hpic'];
		 $hpicurl = $_POST['content'];
	 $adist = $_POST['adist'];
		   $apoint = $_POST['apoint'];

            $ins_sql = "INSERT INTO ad (aref,aowner, atype, aname, hpic,hpicurl,adist,apoint, ulogo,uagreement, ucreated) VALUES ( '$aref', '$aowner', '$atype', '$aname','$hpic', '$hpicurl','$adist','$apoint','$ulogo_upl_path','$uagreement_upl_path', '$currentTime')";
  if(mysqli_query($con, $ins_sql)){
                ?>
                <script>window.location = 'ad.php?ad_id=<?php echo $ad_row["uid"]; ?>';</script>
                <?php
            }
            else{
                ?>
                <script>
                    alert('OOPS !!!!');
                    setTimeout(() => {
                        window.location = 'ad.php?ad_id=<?php echo $ad_row["uid"]; ?>';
                    }, 1200);
                </script>
                <?php
            }
        } else {
            ?>
          
            <?php
        
    }




    if ( isset($_GET["del_id"]) ) {

        $del_sql = "DELETE FROM ad WHERE aid = '$_GET[del_id]'";

        if ( mysqli_query($con, $del_sql) ) {
            ?>
            <script>window.location = 'ad.php?ad_id=<?php echo $edit_id["uid"]; ?>';</script>
            <?php
        } else {
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'admin_lvl2.php';
                }, 1200);
            </script>
            <?php
        }
        
    }

  

  
  

		



      

    

?>