<?php
    
    include("../include/header.php");
    
    	$admin_base_url="https://afsarebrahim.com/admin/news/newsgallery/";
         $id=$_GET['id'];
    
        $srno = 0;
        $sqlAssign1 = "SELECT * FROM `news` where `id`='$id'";	
        $sqlAssign1_execute=mysqli_query($conn,$sqlAssign1);
        if($sqlAssign1_execute){
            
            $data=mysqli_fetch_assoc($sqlAssign1_execute);
            $title=$data['title'];
            $Images=$data['images'];
            //echo $title;
        }
   
    // ===update query start here==
     if ( isset( $_POST['newsSubmitBtn'] ) ) {
         
        $akdate=date("d-M-Y");
   		$images_name ="";
   		$uploads_dir="newsgallery";
   		 $txtname=$_POST['txtname'];
   		 $slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($_POST["txtname"])));
   		 $slug =  trim($slug,"-");
   		 $upid=$_POST['id'];
   		 $successUrl="https://afsarebrahim.com/admin/news/add-news.php";
         $failedUrl="https://afsarebrahim.com/admin/news/edit-new.php?id='.$upid.'";
   		 $sqlAssign1up = "SELECT * FROM `news` where `id`='$id'";	
        $sqlAssign1_executeup=mysqli_query($conn,$sqlAssign1up);
        $data=mysqli_fetch_assoc($sqlAssign1_executeup);
            
            $oldImages=$data['images'];
   		 
    foreach ($_FILES["files"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $tmp_name = $_FILES["files"]["tmp_name"][$key];
            $name = $_FILES["files"]["name"][$key];
            move_uploaded_file($tmp_name, "$uploads_dir/$name");
           
            $images_name =$images_name.",".$name;

    }

    }
//   $images_name= ltrim($images_name,',');
        if($images_name){
            
            $upimg=$oldImages.$images_name;
        }else{
            
            $upimg=$oldImages;
        }
 
  $updatequery="update `news` set `title`='$txtname',`url`='$slug', `images`='$upimg' where `id`='$upid' ";
  $query_execute=mysqli_query($conn,$updatequery);
  if($query_execute){

echo "<script>alert('news updated successfully')
window.location.href='$successUrl';</script>";

  
  }else{

  echo "<script>alert('oops something is wrong');
window.location.href='$failedUrl';</script>";
  }
    
    
    // ===update query end here==
    
     }
?>

<style>
    #testimonials ul li:nth-child(3) {
        background-color: #eb6a4e;
    }

    #testimonials ul li:nth-child(3) a {
        color: #fff;
    }

    #testimonials ul li:nth-child(3) a:hover {
        background-color: #eb6a4e;
    }

    #testimonials .course-detailsa img {
        width: 100% !important;
        max-height: 250px !important;
    }

    .deleteAction {
        position: relative;
    }

    .showDeleteBtn {
        display: none;
    }

    .deleteAction:hover .showDeleteBtn {
        display: block;
        position: absolute;
        top: 30%;
        left: 32%;
    }

    .deleteAction img {
        height: 150px;
        width: 100%;
        margin-bottom: 15px;
    }

</style>
<script>
    window.onload = function() {
        document.getElementById('testimonials').classList.add("active");
        document.getElementById('testimonials1').classList.add("in");
    };

</script>

<div class="small-header">
    <div class="hpanel">
        <div class="panel-body">
            <div id="hbreadcrumb" class="pull-right">
                <ol class="hbreadcrumb breadcrumb">
                    <li><a href="<?php echo $admin_baseurl . 'dashboard.php';?>">Dashboard</a></li>
                    <li class="active">
                        <span>news</span>
                    </li>
                </ol>
            </div>
            <h2 class="font-light m-b-xs">
                news Details
            </h2>

        </div>
    </div>
</div>


<div class="content">
    <div class="hpanel">
        <div class="hpanel">
            <div class="panel-heading hbuilt">
                <div class="panel-tools">
                    <a class="showhide"><i class="fa fa-chevron-up"></i></a>
                    <a class="fullscreen"><i class="fa fa-expand"></i></a>
                </div>
                update News Details
            </div>
            <div class="panel-body">
                <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
                    <div class="form-group">
                        <div class="col-md-12">
                            <label class="control-label">News Title</label>
                            <input type="text" name="txtname" class="form-control" placeholder="News Title" value="<?php echo $title; ?>" required>
                        </div>
                        <div class="col-md-12">
                            <!--<label class="control-label">News id</label>-->
                            <input type="hidden" name="id" class="form-control" placeholder="News Title" value="<?php echo $data['id']; ?>" required>
                        </div>
                        <div class="col-md-12">
                            <div class="row" id="gallerySection">
                                <?php
                        
                        $ImagesArray=explode(",",$Images);
                        foreach($ImagesArray as $singleImage){
                            ?>
                                <div class="col-md-2 deleteAction" style="padding:20px 10px">
                                    <img src="<?php echo$admin_base_url.$singleImage  ?>" style="width:100%">
                                    <button class="showDeleteBtn btn btn-warning" type="button" data-row-id="<?php echo $data['id']; ?>" data-img="<?php echo $singleImage; ?>">Delete</button>
                                </div>
                                <?php
                            
                            
                        }
                        
                        ?>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <label class="control-label">News Image</label>
                            <input type="file" id="file" class="form-control" name="files[]" multiple="multiple" accept="image/*">
                        </div>






                        <div class="col-md-12">
                            <div class="form-group" style="padding:0 20px;">
                                <br>
                                <div class="text-right">
                                    <button type="reset" class="btn btn-default">Cancel</button>
                                    <button type="submit" name="newsSubmitBtn" class="btn btn-primary">Update</button>
                                </div>
                            </div>
                        </div>
                    </div>


                </form>
            </div>

        </div>
    </div>
</div>




<br>
<!--show news-->

<!--show news end -->
<?php 
    include('../include/footer.php');
?>
<script>
    $('#gallerySection').on('click', '.showDeleteBtn', function(e) {
        e.preventDefault();
        var data_row_id = $(this).attr("data-row-id");
        var data_img = $(this).attr("data-img");
        var divval = $(this).closest('.deleteAction');
       // alert(data_row_id + " " + data_img);
        if ((data_row_id != "") && (data_img != "")) {
            if (confirm("Are you sure you want to delete this?")) {
                //alert("ok");

                $.ajax({
                    url: 'delete-gallery-photo.php',
                    type: "post",
                    data: {
                        data_row_id: data_row_id,
                        data_img: data_img
                    },
                    success: function(response) {
                        var data = JSON.parse(response);
                        if (data.status == "true") {
                            //  divval.toggle(false);
                            alert(data.msg);
                            location.href='';
                        } else {
                            alert(data.msg);
                        }
                        //console.log(response);
                    }
                });
            } else {
                return false;
            }
        }
    });

</script>
