<?php
session_start();
include('conn.php');
if(isset($_POST['submit']))
{
//$i=$_POST['id'];

$category=$_POST['category'];

$image_name1=$_FILES['file1']['name'];                  /* Update a image 2*/
$image_size=$_FILES['file1']['size'];
$image_type=$_FILES['file1']['type'];
$image_tmp=$_FILES['file1']['tmp_name'];
$store="pic/".$image_name1;                               /* location to store image 2 */
$upload=move_uploaded_file($image_tmp,$store);
    
    if($image_name1=='')
    {
     $image_name1=$_POST['hidden1'];
    }
      else
      {
      $image_name1=$_FILES['file1']['name'];
    }
   


$query=mysqli_query($conn,"UPDATE add_category SET category_name='$category',category_img='$image_name1',date=now() where cate_id='".$_GET['change']."'");

if($query==true)
{
  echo"<script>alert('Your details has been updated');
  window.location.href='category-list.php';</script>";
}
else
{
  echo"<script>alert('Your details has not been updated');window.location.href='category-list.php';</script>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>AdminLTE 3 | Validation Form</title>

  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="dist/css/adminlte.min.css">
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
  <!-- Navbar -->
 <?php include "header.php";
 ?>
  <!-- /.navbar -->

  <!-- Main Sidebar Container -->
  <?php include "sidebar.php";
  ?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <!-- <h1>Add Category</h1> -->
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Home</a></li>
              <li class="breadcrumb-item active">Validation</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <!-- left column -->
          <div class="col-md-12">
            <!-- jquery validation -->
            <div class="card card-primary">
              <div class="card-header">
                <h3 class="card-title">Add<small>Category</small></h3>
              </div>
             
              <?php $query=mysqli_query($conn,"SELECT * FROM  add_category where cate_id='".$_GET['change']."'"  );
            $row=mysqli_fetch_array($query);
 
 ?>
              <form method="post" enctype="multipart/form-data">
             
                <div class="card-body">

                <div class="row">
                <!-- <input type="hidden" name="id" > -->

                <div class="form-group col-md-6">
                    <label for="exampleInputEmail1">Category Name</label>
                    <input type="text"  name="category" value="<?php echo $row['category_name'];?>" class="form-control" >
                  </div>
                  
                  <div class="form-group col-md-6">
                    <label for="exampleInputPassword1">Category Image</label>
                   <input type="file" name="file1" id="defaultForm-pass" class="form-control validate" >
      <input type="hidden" name="hidden1" class="form-control" value="<?php echo $row['category_img'];?>" >
           <center> <img src="pic/<?php echo $row['category_img']; ?>" style="width:100px;height:100px;"></center>
                  </div>

                  
                <!-- /.card-body -->
                <div class="card-footer">
                 <button type="submit" name="submit"class="btn btn-primary">Update</button>
                </div>
              
              </form>
            </div>
            <!-- /.card -->
            </div>
          <!--/.col (left) -->
          <!-- right column -->
          
          <!--/.col (right) -->
        </div>
        <!-- /.row -->
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
<?php include "footer.php";?>
  <!-- Control Sidebar -->
  <aside class="control-sidebar control-sidebar-dark">
    <!-- Control sidebar content goes here -->
  </aside>
  <!-- /.control-sidebar -->
</div>
<!-- ./wrapper -->

<!-- jQuery -->
<script src="plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- jquery-validation -->
<script src="plugins/jquery-validation/jquery.validate.min.js"></script>
<script src="plugins/jquery-validation/additional-methods.min.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/adminlte.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="dist/js/demo.js"></script>
<!-- Page specific script -->
<script>
$(function () {
  $.validator.setDefaults({
    submitHandler: function () {
      alert( "Form successful submitted!" );
    }
  });
  $('#quickForm').validate({
    rules: {
      email: {
        required: true,
        email: true,
      },
      password: {
        required: true,
        minlength: 5
      },
      terms: {
        required: true
      },
    },
    messages: {
      email: {
        required: "Please enter a email address",
        email: "Please enter a valid email address"
      },
      password: {
        required: "Please provide a password",
        minlength: "Your password must be at least 5 characters long"
      },
      terms: "Please accept our terms"
    },
    errorElement: 'span',
    errorPlacement: function (error, element) {
      error.addClass('invalid-feedback');
      element.closest('.form-group').append(error);
    },
    highlight: function (element, errorClass, validClass) {
      $(element).addClass('is-invalid');
    },
    unhighlight: function (element, errorClass, validClass) {
      $(element).removeClass('is-invalid');
    }
  });
});
</script>

 <!-- SWeet Alert Js -->
 <script src="js/sweetalert.js"></script>
   <?php 
   if(isset($_SESSION['status']) && $_SESSION['status']!='' )
   {
    
   ?>
  <script>
 swal({
  title: "<?php echo $_SESSION['status'];?>",
  //text: "You clicked the button!",
  icon: "<?php echo $_SESSION['status_code'];?>",
  button: "Ok!",
});
  </script>
  <?php 
   unset($_SESSION['status']);
   }
   ?> 
  
</body>
</html>
