<?php
  session_start();
  include('assets/includes/config.php');
  
    date_default_timezone_set('Asia/Kolkata');// change according timezone
    $currentTime = date( 'd-m-Y h:i:s A', time () );


    $id = intval($_GET['id']);
    $result = mysqli_query($con, "SELECT * FROM products WHERE id  = '$id'");
    $row = mysqli_fetch_array($result);
    //print_r($row);die;
    $image1 = $row['productImage1'];
    $image2 = $row['productImage2'];
    $image3 = $row['productImage3'];
    $image4 = $row['productImage4'];

    if(isset($_POST['submit'])) {
      //print_r($_FILES);die;
      $category = $_POST['category'];
      $name = $_POST['name'];
      $price = $_POST['price'];
      $quantity = $_POST['quantity'];
      $country_of_origin = $_POST['country-of-origin'];
      $application = $_POST['application'];
      $brand = $_POST['brand'];
      $capacity = $_POST['capacity'];
      $battery_type = $_POST['battery-type'];
      $voltage = $_POST['voltage'];
      $delivery_time = $_POST['delivery_time'];
      $description = $_POST['description'];
      $id = intval($_GET['id']);

      if ($_FILES["productimage1"]["name"]=="") {
        $productimage1 = $image1;
      } else {
        move_uploaded_file($_FILES["productimage1"]["tmp_name"],"assets/productimages/$id/".$_FILES["productimage1"]["name"]);
        $productimage1 = $_FILES["productimage1"]["name"];
      }

      if ($_FILES["productimage2"]["name"]=="") {
        $productimage2 = $image2;
      } else {
        move_uploaded_file($_FILES["productimage2"]["tmp_name"],"assets/productimages/$id/".$_FILES["productimage2"]["name"]);
        $productimage2 = $_FILES["productimage2"]["name"];
      }

      if ($_FILES["productimage3"]["name"]=="") {
        $productimage3 = $image3;
      } else {
        move_uploaded_file($_FILES["productimage3"]["tmp_name"],"assets/productimages/$id/".$_FILES["productimage3"]["name"]);
        $productimage3 = $_FILES["productimage3"]["name"];
      }

      if ($_FILES["productimage4"]["name"]=="") {
        $productimage4 = $image4;
      } else {
        move_uploaded_file($_FILES["productimage4"]["tmp_name"],"assets/productimages/$id/".$_FILES["productimage4"]["name"]);
        $productimage4 = $_FILES["productimage4"]["name"];
      }

      $sql = mysqli_query($con, "update products set category = '$category', productName = '$name', productPrice = '$price', quantity = '$quantity', country_of_origin = '$country_of_origin', application = '$application', brand = '$brand', capacity = '$capacity', battery_type = '$battery_type', voltage = '$voltage', delivery_time = '$delivery_time', description = '$description', productImage1 = '$productimage1', productImage2 = '$productimage2', productImage3 = '$productimage3', productImage4 = '$productimage4', updationDate = '$currentTime' where id = '$id'");
      $_SESSION['msg'] = "Product Updated !!"; 
      header('location:product-view.php');
    }
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Admin | Edit Category</title>
  <!-- plugins:css -->
  <link rel="stylesheet" href="assets/vendors/iconfonts/mdi/css/materialdesignicons.min.css">
  <link rel="stylesheet" href="assets/vendors/iconfonts/ionicons/css/ionicons.css">
  <link rel="stylesheet" href="assets/vendors/iconfonts/typicons/src/font/typicons.css">
  <link rel="stylesheet" href="assets/vendors/iconfonts/flag-icon-css/css/flag-icon.min.css">
  <link rel="stylesheet" href="assets/vendors/css/vendor.bundle.base.css">
  <link rel="stylesheet" href="assets/vendors/css/vendor.bundle.addons.css">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  <!-- inject:css -->
  <link rel="stylesheet" href="assets/css/shared/style.css">
  <!-- Layout styles -->
  <link rel="stylesheet" href="assets/css/demo_1/style.css">
  <link rel="shortcut icon" href="assets/images/favicon.png" />
</head>

<body>
  <div class="container-scroller">
    <?php include('assets/includes/header.php'); ?>
    <!-- partial -->
    <div class="container-fluid page-body-wrapper">
      <?php include('assets/includes/sidebar.php'); ?>
      <!-- partial -->
      <div class="main-panel">
        <div class="content-wrapper">
          <!-- Page Title Header Starts-->
          <div class="row page-title-header">
            <div class="col-12">
              <div class="page-header">
                <h4 class="page-title">Edit Category</h4>
              </div>
            </div>
          </div>
          <!-- Page Title Header Ends-->
          <div class="row justify-content-center">
            <div class="col-md-6 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
                  <form class="forms-sample" name="edit_product" method="post" enctype="multipart/form-data">
                    <?php
                      $id = intval($_GET['id']);
                      $query = mysqli_query($con, "select p.*, c.id as cid, c.categoryName from products p inner join category c on p.category = c.id where p.id = '$id'");
                      while($row = mysqli_fetch_array($query)) {
                    ?>
                    <div class="form-group">
                      <label>Category Name</label>
                      <select name="category" class="form-control" required>
                        <option value="<?php echo htmlentities($row['cid']);?>"><?php echo htmlentities($catname = $row['categoryName']);?></option>
                        <?php 
                          $ret = mysqli_query($con, "select * from category");
                          while($result = mysqli_fetch_array($ret)) {
                            echo $cat = $result['categoryName'];
                            if($catname == $cat) {
                              continue;
                            } else {
                        ?>
                        <option value="<?php echo $result['cid'];?>"><?php echo $result['categoryName'];?></option>
                        <?php } } ?>
                      </select>
                    </div>
                    <div class="form-group">
                      <label>Product Name</label>
                      <input type="text" class="form-control" name="name" value="<?php echo htmlentities($row['productName']);?>" required>
                    </div>
                    <div class="form-group">
                      <label>Product Price</label>
                      <input type="text" class="form-control" name="price" value="<?php echo htmlentities($row['productPrice']);?>" required>
                    </div>
                    <div class="form-group">
                      <label>Quantity</label>
                      <input type="text" class="form-control" name="quantity" value="<?php echo htmlentities($row['quantity']);?>" required>
                    </div>
                    <div class="form-group">
                      <label>Country of Origin</label>
                      <input type="text" class="form-control" name="country-of-origin" value="<?php echo htmlentities($row['country_of_origin']);?>" required>
                    </div>
                    <div class="form-group">
                      <label>Application</label>
                      <input type="text" class="form-control" name="application" value="<?php echo htmlentities($row['application']);?>" required>
                    </div>
                    <div class="form-group">
                      <label>Brand</label>
                      <input type="text" class="form-control" name="brand" value="<?php echo htmlentities($row['brand']);?>" required>
                    </div>
                    <div class="form-group">
                      <label>Capacity</label>
                      <input type="text" class="form-control" name="capacity" value="<?php echo htmlentities($row['capacity']);?>" required>
                    </div>
                    <div class="form-group">
                      <label>Battery Type</label>
                      <input type="text" class="form-control" name="battery-type" value="<?php echo htmlentities($row['battery_type']);?>" required>
                    </div>
                    <div class="form-group">
                      <label>Voltage</label>
                      <input type="text" class="form-control" name="voltage" value="<?php echo htmlentities($row['voltage']);?>" required>
                    </div>
                    <div class="form-group">
                      <label>Delivery Time</label>
                      <input type="text" class="form-control" name="delivery_time" value="<?php echo htmlentities($row['delivery_time']);?>" required>
                    </div>
                    <div class="form-group">
                      <label>Description</label>
                      <textarea class="form-control" name="description" rows="2"><?php echo htmlentities($row['description']);?></textarea>
                    </div>
                    <div class="form-group">
                      <label>Product Image1</label>
                      <img src="assets/productimages/<?php echo htmlentities($row['id']);?>/<?php echo htmlentities($row['productImage1']);?>" width="100" height="100"> 
                      <input type="file" class="form-control" name="productimage1" style="border: none;">
                    </div>
                    <div class="form-group">
                      <label>Product Image2</label>
                      <img src="assets/productimages/<?php echo htmlentities($row['id']);?>/<?php echo htmlentities($row['productImage2']);?>" width="100" height="100"> 
                      <input type="file" class="form-control" name="productimage2" style="border: none;">
                    </div>
                    <div class="form-group">
                      <label>Product Image3</label>
                      <img src="assets/productimages/<?php echo htmlentities($row['id']);?>/<?php echo htmlentities($row['productImage3']);?>" width="100" height="100"> 
                      <input type="file" class="form-control" name="productimage3" style="border: none;">
                    </div>
                    <div class="form-group">
                      <label>Product Image4</label>
                      <img src="assets/productimages/<?php echo htmlentities($row['id']);?>/<?php echo htmlentities($row['productImage4']);?>" width="100" height="100"> 
                      <input type="file" class="form-control" name="productimage4" style="border: none;">
                    </div>
                    <button type="submit" name="submit" class="btn btn-success mr-2">Update</button>
                    <?php } ?>
                  </form>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- content-wrapper ends -->

<?php include('assets/includes/footer.php'); ?>

