<?php
// initializing variables
$pshopshopname = "";
$pownername = "";
$pregdate = "";
$pprimarynum = "";
$psecondarynum = "";
$pemail = "";
$image = "";
$errors = array(); 
// connect to the database
include_once 'db_connection.php';

// UPLOAD IMAGE
if(isset($_POST['submit_button'])) {
  // receive all input values from the form
  $pshopname = mysqli_real_escape_string($connect, $_POST['pshopname']);
  $pownername = mysqli_real_escape_string($connect, $_POST['pownername']);
  $pregdate = mysqli_real_escape_string($connect, $_POST['pregdate']);
  $pprimarynum = mysqli_real_escape_string($connect, $_POST['pprimarynum']);
  $psecondarynum = mysqli_real_escape_string($connect, $_POST['psecondarynum']);
  $pemail = mysqli_real_escape_string($connect, $_POST['pemail']);

  $image   = isset($_POST['image'])?$_POST['image']:'';
  $uploads_dir = 'uploads/shops';
  $img_path = 'http://localhost/folder/uploads/shops/';
  $tmp_name = $_FILES["image"]["tmp_name"];
  $name = basename($_FILES["image"]["name"]);
  move_uploaded_file($tmp_name, "$uploads_dir/$name");
  $p_img_url =  $img_path.$name;    
  if(!empty($name))
   {
    $p_img_url = $img_path.$name;
   }
   else
    {
      $p_img_url = '';
    }

  if (empty($pshopname)) { array_push($errors, "Shop Name is required"); }
  if (empty($pownername)) { array_push($errors, "Owner Name is required"); }
  if (empty($pregdate)) { array_push($errors, "Registration Date is required"); }
  if (empty($pprimarynum)) { array_push($errors, "Primary Number is required"); }
  if (empty($psecondarynum)) { array_push($errors, "Secondary Number is required"); }
  if (empty($pemail)) { array_push($errors, "Email is required"); }

  
  // first check the database to make sure 
  // a user does not already exist with the same username and/or email
  $product_check_query = "SELECT * FROM clients WHERE shop_name='$pshopname' LIMIT 1";
  $result = mysqli_query($connect, $product_check_query);
  $product = mysqli_fetch_assoc($result);

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
    
    $query = "INSERT INTO clients(shop_name,owner_name,registration_date,primary_number,secondary_number,email,image_dir) VALUES ('$pshopname','$pownername','$pregdate','$pprimarynum','$psecondarynum','$pemail','$p_img_url')";

  if (mysqli_query($connect,$query)) 
  {
    $_SESSION['pshopname'] = $pshopname;
    echo '<script>alert("$pshopname Client Added successfully");</script>'; 
  }
 }
}
?>