<?php

     session_start();

     require_once('database.php');

     //user create account

   if(isset($_POST['user_createaccount'])){

    $name=$_POST['name'];

    $email=$_POST['email'];

    $phone=$_POST['phone'];

    $password=$_POST['pwd1'];

    //serch email already exist or not 

     $q1="SELECT email from customers where email='$email'";

         $rec=db::getCell($q1);

    if($rec){

         echo "<script>location='sign_up.php?status=1'</script>"; 

    }

    else{
        $hashed_password = password_hash($password, PASSWORD_DEFAULT);
        $query="INSERT INTO customers(name,email,phone,password)

    VALUES('$name','$email','$phone','$hashed_password')";

    $insert=db::query($query);

    if($insert){

        $_SESSION['user_email']=$email;

        echo "<script>location='checkout.php'</script>";

    }

    else{

        echo "<script>location='sign_up.php?status=2'</script>"; 

    } 

    }

   }

     //user login

     if(isset($_POST['login']))

     {

    $email=$_POST['email'];

    $password=$_POST['password'];

    $query="SELECT * from customers where email='$email'";

    $rec=db::getRecord($query);

    

    if($rec)

    { 
        if (password_verify($password, $rec['password']))
            {
                /* The password is correct. */
                $_SESSION['user_email']=$email;

                echo "<script>location='index.php'</script>";
            }
        

    }

       echo "<script>location='login.php?status=1'</script>";

}

//order information

if(isset($_POST['ids'])){

    $address=$_POST['address'];

    $floor=$_POST['floor'];

    $busines_name=$_POST['busines_name'];

    $special_note=$_POST['message'];

    $email=$_SESSION['user_email'];

    $order_id=uniqid();

    $c=$_POST['ids'];

    $q=$_POST['quantities'];

    $order_type=$_POST['order_type'];

    //implode function

//    $implode_code[] = implode(",",$c);

//    $implode_quantity[] = implode(",",$q);

    //json encode

     $query_c=json_encode($c);

      $query_q=json_encode($q);

      //custmer id from db in $data

    $query="SELECT id FROM customers WHERE email='$email'";

    $data=db::getCell($query);

    if($data){


        $query="INSERT INTO order_information(customer_id,order_id,address,	floor,business_name,special_note,order_type)

         VALUES('$data','$order_id','$address','$floor','$busines_name','$special_note','$order_type')";

        
        $order_information=db::query($query);

       // insert order item data

       $que="INSERT INTO order_items(order_id,customer_id,product_code,product_qty)

        VALUES('$order_id','$data','$query_c','$query_q')";

         $order_items=db::query($que);

                

    }

     echo 'success';
    }



     //reservation

     if(isset($_POST['reservaton_data'])){

         $name=$_POST['name'];

         $phone=$_POST['number'];

         $email=$_POST['email'];

         $date=$_POST['date'];

         $time=$_POST['time'];

          $person=$_POST['person'];

         $message=$_POST['message'];

         $query="INSERT INTO reservation(name,phone_number,email,date,time,person,message)

                VALUES('$name','$phone','$email','$date','$time','$person','$message')";

                 $reservaation=db::query($query);

        if($reservaation){

           echo "<script>location='reservation.php?status=1'</script>"; 

        }

        

    }

    

    //reservation

     if(isset($_POST['contact_us'])){

        $name = $_POST['name'];

        $visitor_email = $_POST['email'];

        $visitor_phone = $_POST['phone'];

        $message = $_POST['message'];

        $email_from =$visitor_email;   //<== update the email address

       $email_subject ="New Form submission";

       $email_body = "You have received a new message from the user $name.\n".

       "Here is the message:\n $message.\n".

       "Phone Number: $visitor_phone";

       $to = "info@skyliteconsulting.com";     //<== update the email address

       $headers = "From: $email_from \r\n";

       //Send the email!

       mail($to,$email_subject,$email_body,$headers);

      //done. redirect to index page.

      echo "<script>location='contact_us.php?status=1'</script>";

    }







?>