<?php include('menu.php');

?>

  <body>
      <?php
        if (isset($_SESSION['add'])) {
          echo $_SESSION['add'];
          unset($_SESSION['add']);
        }

       ?>

    <!-- navigation -->
    <nav>
      <!--social-link-and-phone-number  -->
      
      <!-- menu bar -->
      <div class="navigation">
        <!-- .logo -->
        <a href="<?php echo SITEURL; ?>shopping/index.php" class="logo">
          <img src="images/logo.png">
        </a>
        <div class="toggle"></div>
        <!-- munu -->
        <ul class="menu">
            <li><a href="<?php echo SITEURL;?>shopping/index.php">Home</a></li>
          <li><a href="<?php echo SITEURL;?>shopping/general.php">General</a></li>
          <li><a href="<?php echo SITEURL;?>shopping/men.php">Men</a>
            <!-- label -->
            <!-- <label class="sale-label">Sale</label> -->
          </li>
          <li><a href="<?php echo SITEURL;?>shopping/women.php">Women</a></li>
          <li><a href="<?php echo SITEURL;?>shopping/kids.php">Kids</a></li>


        </ul>
         <!-- right-menu -->
         <div class="right-menu">
           <!-- search -->
           <a href="javascript:void(0);">
             <img class="search" src="images/search.svg" width="25px">
           </a>
           <!-- user -->
           <a href="javascript:void(0);">
             <img class="user"src="images/user.svg" width="25px">
           </a>
           <!-- cart-icon-->
           <?php
               $count = 0;
               if (isset($_SESSION['cart'])){
                 $count = count($_SESSION['cart']);
               }
           ?>
           <a href="viewcart.php">

              <i class="fas fa-shopping-cart">
                 <img class="fa-shopping-cart"src="images/cart-plus.svg"width="25px">
                 <span class="num-cart-product"><?php echo $count?></span>
              </i>
             <!--img class="cart"src="images/cart-plus.svg" width="20px" number of products in a cart -->

           </a>
         </div>

      </div>
    </nav>
    <!-- search bar -->
    <div class="search-bar">
      <!-- search-input -->
      <form action="#" method="post">
        <div class="search-input">

            <input type="search" name="search"  placeholder="Search For Product" required />
          <input type="submit"  name="submitsearch" class="btn"/>
          <!-- cansel btn -->
          <a href="javascript:void(0);" class="search-cansel"> &times;</a>
          <!-- input -->

        </div>
      </form>

    </div>
    <!-- login and sign up form-->
    <div class="form">

      <!-- login -->
      <div class="login-form">
        <!-- cansel btn -->
        <a href="javascript:void(0);" class="login-cansel"> &times;</a>
        <!-- heading-->

        <strong>Log In</strong>
        <!-- inputs-->
        <form action="" method="post">
           <input type="text" name="username" placeholder="enter username"required>

          <input type="password" name="password" placeholder="enter password"required>
          <!-- submit -->
          <input type="submit" name="login" value="Log in">
        </form>
        <div class="form-btns">
          <!-- forget password and sign up btn -->
          <a href="javascript:void(0);" class="forget">Forget Your Password?</a>
          <a href="javascript:void(0);" class="sign-up">Create Account</a>
        </div>

      </div>



      <!-- sign up-->
      <div class="sign-up-form">
        <!-- cansel btn -->
        <a href="javascript:void(0);" class="sign-up-cansel"> &times;</a>
        <!-- heading-->
        <strong>Sign Up</strong>
        <!-- inputs-->
        <form action="" method="post">
          <input type="text" name="fullname" placeholder="enter Your Fullname" required>
          <input type="text" name="username" placeholder="enter username" required>
          <input type="email" name="email"placeholder="enter your email" required>
          <input type="number" name="phone"placeholder="enter your phoneNo" required>
          <input type="password" name="password" placeholder="enter password" required>
          <input type="password" name="confirm-password" placeholder="confirm  password" required>
          <!-- submit -->
          <input type="submit" name="SignUp"value="SignUp">
        </form>
        <div class="form-btns">
          <!-- forget password and sign up btn -->
          <a href="javascript:void(0);" class="already-account">Already Have Account?</a>

        </div>

      </div>
    </div>
    <?php

    ?>
    <!-- insert data into db -->
    <?php
       //process the value from form and save in the SQLiteDatabase
       //check whether the submit btn is clicked or not
       if(isset($_POST['SignUp'])){
         // Button clicked
         //get the data from form
         $fullname = mysqli_real_escape_string($conn,$_POST['fullname']);
         $username = mysqli_real_escape_string($conn,$_POST['username']);
         $email = mysqli_real_escape_string($conn,$_POST['email']);
         $phone = mysqli_real_escape_string($conn,$_POST['phone']);
         $password = md5($_POST['password']); //pasword encryption with md5
         $confirmpassword = md5($_POST['confirm-password']); //pasword encryption with md5
         if($password==$confirmpassword){
           // sql to insert the data into the database
           $sql = "INSERT INTO tbl_user SET
              full_name = '$fullname',
              username = '$username',
              password = '$password',
              email = '$email',
              phone_number = '$phone'
           ";
           //execute query and save data in the database
           $res = mysqli_query($conn, $sql);
           //check whether the data is inserted or not and display appropriate message
            if($res==TRUE){
              // create session var to display message
             $_SESSION['add'] = "Account Created Successfully";
              //Redirect Page
             header('location:'.SITEURL.'shopping/');

            }else{
              // create session var to display message
              $_SESSION['failed'] = "Failed To Create Account";
              //Redirect Page
              header('location:'.SITEURL.'shopping/index.php');

            }
         }else{
           // create session var to display message
           $_SESSION['pwdmatch'] = "Password did not match";
           //Redirect Page
           header('location:'.SITEURL.'shopping/lgin&sign-up.php');

         }
       }
       ?>
       <?php
       //check whether the submit btn is clicked or not
        if (isset($_POST['login'])) {
          //process for login
          //1.get the data frm login form
          //$username = $_POST['username'];

          $raw_password = md5($_POST['password']);

          $username = mysqli_real_escape_string($conn, $_POST['username']);
          $password = mysqli_real_escape_string($conn, $raw_password);
          //2.sql to check whether the user with the username and password exists or not
          $sql1 = "SELECT * FROM tbl_user WHERE username='$username' AND password='$password'";
          //3.execute the querry
          $res1 = mysqli_query($conn, $sql1);
          //4.count rows to check whether the user exists or not
          $count1 = mysqli_num_rows($res1);
          if ($count1>0) {
            // user available and login success
            //$_SESSION['login'] = "<div class='success'>Login Successful.</div>";
          //  $_SESSION['user'] = $username;//to check whether the user is logged in or not and logout will unset it
            //redirect to the dashboard
          //  echo "yeap";
            //header('location:'.SITEURL.'shopping/');
          }else{
            //user not available and login failed
          //  $_SESSION['login'] = "<div class='error'>Login Failed.</div>";
            //redirect to the dashboard
            //header('location:'.SITEURL.'shopping/index.php');
          //  echo "nope";
          }
        }else{

          //error occurred during data proccessing
        }
        ?>

    <?php include('full-slider.php'); ?>
    
    
    <section class="new-arrival">
      <!-- heading -->
      <?php 

          if (isset($_SESSION['id'])) {
          $sub_id = $_SESSION['id'];
           
        }
         if (isset($_SESSION['title'])) {
          $title = $_SESSION['title'];
           
        } 
           
      ?>
      <?php
           
            $per_page = 2;
            if (isset($_GET['page'])) {
              //echo "lets go";
              $page = $_GET['page'];
            }else {
              //echo "wait for a minute";
              $page = "";
            }if ($page=='' || $page==1) {
              $page1 = 0;
            }else {
              $page1 = ($page * $per_page) - $per_page;
            }



            $sql5 = "SELECT * FROM products  WHERE sub_category_id = $sub_id";
            $res5 = mysqli_query($conn, $sql5);
            $count5= mysqli_num_rows($res5);
            $count5 = ceil($count5 / $per_page);
          
            //echo $count3;
             ?>
         
      <div class="arrival-heading">
        <strong>Products on Category "<?php echo $title;?>"</strong>
        <p>Enjoy Your Shopping👌🙏</p>
      </div>
      <div class="product-container">
        <!-- pproduct box-->
        
       <?php
          
          $sql = "SELECT * FROM products WHERE sub_category_id = $sub_id limit $page1, $per_page";
          $res = mysqli_query($conn, $sql);
          $count= mysqli_num_rows($res);
          if ($count>0) {
            while ($row = mysqli_fetch_assoc($res)) {
              // code...
              $id = $row['id'];
              $title = $row['title'];
              $image_name = $row['image_name'];
              $description = $row['description'];
              $featured = $row['featured'];
              $active = $row['active'];
              $size = $row['size'];
              $price = $row['price'];
              $qty = $row['qty'];
              ?>
        <form action="item-description1.php" method="post" enctype="multipart/form-data">
          <div class="product-box">

            <div class="product-img">
                <!-- cart-->
                <!-- cart -->
            
              <!-- <input type="submit" name="submit" value="cart" class="add-cart"> -->

              <?php
              if ($image_name) {
                //img is available
                ?>
                <img src="<?php echo SITEURL; ?>images/products/<?php echo $image_name; ?>" width="100px" >
                <?php
              }else {
                //img is not available
                echo  "<div class='error'>Img is not Added</div>";
              }

               ?>
            </div>
            <div class="product-details">
              <a href="javascript:void(0);" class="p-name"><?php echo $title?></a>
              <span class="p-price">$<?php echo $price?></span>
              <button type="submit" name="submit" class="add-cart">
                   VIEW PRODUCT
                </button>
            </div>
             <input type="hidden" name="qty" value="<?php echo $qty?>">
            <input type="hidden" name="id" value="<?php echo $id?>">
            <input type="hidden" name="title" value="<?php echo $title?>">
            <input type="hidden" name="image_name" value="<?php echo $image_name?>">
            <input type="hidden" name="price" value="<?php echo $price?>">
            <input type="hidden" name="size" value="<?php echo $size?>">
            <input type="hidden" name="description" value="<?php echo $description?>">
            <input type="hidden" name="active" value="<?php echo $active?>">
            <input type="hidden" name="featured" value="<?php echo $featured?>">

          </div>
          <!-- pproduct box -->
        </form>

        <?php
      }
    }else {
      echo 'There Was No Match For Your Search';
    }
  
    ?>

      </div>
       <ul class="pager">
            <?php
            for ($i=1; $i <=$count5 ; $i++) {
              if ($i == $page) {
                  echo "<li><a class='active_link' href='catproducts.php?page=$i'>$i</a></li>";
              }else {
                echo "<li><a href='catproducts.php?page=$i'>$i</a></li>";
              }

            }

             ?>

           </ul>
    </section>



    

    <?php include('products-related.php'); ?>
    <?php include('most-viewed.php'); ?>
    <?php include('banner.php'); ?>
    <?php include('footer.php'); ?>
    <?php include('js.php'); ?>

  </body>
</html>
