<?php
session_start();

include("include/dbConfig.php");




$update_alert = false;
if(isset($_POST['add'])){
    
    
    $choose_item = $_POST['product_name'];
$choose_price = $_POST['price'];

$quantity = $_POST['quantity'];
// $color = $_POST['hiddenColor'];
// $email = $_POST['email'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$road = $_POST['road'];
$building = $_POST['building'];
$note =  $_POST['note'];
$state = $_POST['state'];
$area = $_POST['area'];
$by = $_POST['agent'];
$team = $_POST['team'];
         $checkquery = $db->query("SELECT * FROM customers WHERE customer_phone ='+971$phone'");
        
        // IF CUSTOMER DOESN'T EXIST WITH THE POSTED PHONE
        // echo mysqli_num_rows($checkquery); die();
        if(!$checkquery || mysqli_num_rows($checkquery)==0 ){ 
        
            //ADD NEW CUSTOMER
            $insert_query = "INSERT INTO customers (customer_name, customer_phone, customer_road, customer_building, state, area) VALUES ('$name',  '+971$phone','$road','$building', '$state', '$area')";
            // echo $insert_query;
            $q = mysqli_query($db,$insert_query);
            
               if ( false===$q ) {
                    printf("error: %s\n", mysqli_error($db));
                } else {
                    
                }
               
                // FETCHING CUSTOMER ID
                 $customer_id_fetch_query = $db->query("SELECT * FROM customers WHERE customer_phone = '+971$phone'");
                 while($row = $customer_id_fetch_query->fetch_assoc()) {
                    $customer_id = $row['customer_id'];
                 }
        
                // CREATE ORDER ID
                   $order_insert_query = "INSERT INTO orders (customer_id, note,total, `date`,status,team ,  `placed_by` ) VALUES ($customer_id, '$note',  $quantity*$choose_price,  '".date("Y-m-d H:i:s")."'  , 'processing', $team,  '$by')";
                   
                   mysqli_query($db, $order_insert_query );
                   
                   
                   // FETCHING CREATED ORDER ID
                   $query = $db->query("SELECT * FROM orders WHERE customer_id = ".$customer_id);
                   while($row = $query->fetch_assoc()){
                       
                       $order_id = $row['order_id'];
                   }
                   
                   
                   // ADDING ITEM AGAINST ORDER ID
                   
                   $order_items_insert_query = "INSERT INTO order_items (order_id, customer_id, item_name, item_quantity, item_total ) VALUES ($order_id, $customer_id, '$choose_item', $quantity, $quantity*$choose_price )";
                    // echo $order_items_insert_query;
                 if(mysqli_query($db, $order_items_insert_query )){
                   $done = true;
                 }
                 
        
        
        } else { // IF CUSTOMER EXISTS WITH THIS EMAIL 
        
                echo '<script> alert("Order exists against this Phone Number");</script>';
        
                
        }
}
?><!doctype html>
<html lang="en">

    
<!-- Mirrored from themesbrand.com/symox/layouts/index.html by HTTrack Website Copier/3.x [XR&CO'2014], Fri, 14 Jan 2022 05:37:58 GMT -->
<head>
        
        <meta charset="utf-8" />
        <title>Submit Orders </title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta content="Premium Multipurpose Admin & Dashboard Template" name="description" />
        <meta content="Themesbrand" name="author" />
        <!-- App favicon -->
        <link rel="shortcut icon" href="assets/images/favicon.ico">
  <!-- gridjs css -->
        <link rel="stylesheet" href="assets/libs/gridjs/theme/mermaid.min.css">

        <!-- Bootstrap Css -->
        <link href="assets/css/bootstrap.min.css" id="bootstrap-style" rel="stylesheet" type="text/css" />
        <!-- Icons Css -->
        <link href="assets/css/icons.min.css" rel="stylesheet" type="text/css" />
        <!-- App Css-->
        <link href="assets/css/app.min.css" id="app-style" rel="stylesheet" type="text/css" />
        <link href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css"  rel="stylesheet" type="text/css" />

        <style type="text/css">
            
            #hide {
                display: none;
            }
        </style>
    </head>

    <body data-layout="horizontal" data-topbar="dark">

        <!-- Begin page -->
        <div id="layout-wrapper">
            
            <header id="page-topbar" class="isvertical-topbar">
                <div class="navbar-header">
                    <div class="d-flex">
                        <!-- LOGO -->
                        <div class="navbar-brand-box">
                            <a href="index.html" class="logo logo-dark">
                                <span class="logo-sm">
                                    <img src="assets/images/logo-sm.svg" alt="" height="22">
                                </span>
                                <span class="logo-lg">
                                    <img src="assets/images/logo-sm.svg" alt="" height="22"> <span class="logo-txt">Symox</span>
                                </span>
                            </a>

                            <a href="index.html" class="logo logo-light">
                                <span class="logo-sm">
                                    <img src="assets/images/logo-sm.svg" alt="" height="22">
                                </span>
                                <span class="logo-lg">
                                    <img src="assets/images/logo-sm.svg" alt="" height="22"> <span class="logo-txt">Symox</span>
                                </span>
                            </a>
                
                        </div>

                        <button type="button" class="btn btn-sm px-3 font-size-16 header-item vertical-menu-btn">
                            <i class="fa fa-fw fa-bars"></i>
                        </button>

                        <!-- Search -->
                        <form class="app-search d-none d-lg-block">
                            <div class="position-relative">
                                <input type="text" class="form-control" placeholder="Search...">
                                <span class="bx bx-search"></span>
                            </div>
                        </form>
            
                    </div>

                    <div class="d-flex">
                        <div class="dropdown d-inline-block d-lg-none">
                            <button type="button" class="btn header-item"
                                data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <i class="icon-sm" data-feather="search"></i>
                            </button>
                            <div class="dropdown-menu dropdown-menu-lg dropdown-menu-end p-0">
                                <form class="p-2">
                                    <div class="search-box">
                                        <div class="position-relative">
                                            <input type="text" class="form-control rounded bg-light border-0" placeholder="Search...">
                                            <i class="mdi mdi-magnify search-icon"></i>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>

                        
                        

                        <div class="dropdown d-inline-block">
                            <button type="button" class="btn header-item user text-start d-flex align-items-center" id="page-header-user-dropdown"
                                data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <img class="rounded-circle header-profile-user" src="assets/images/users/avatar-1.jpg"
                                alt="Header Avatar">
                            </button>
                            <div class="dropdown-menu dropdown-menu-end pt-0">
                                <a class="dropdown-item" href="contacts-profile.html"><i class='bx bx-user-circle text-muted font-size-18 align-middle me-1'></i> <span class="align-middle">My Account</span></a>
                               
                                <div class="dropdown-divider"></div>
                              
                                <a class="dropdown-item" href="auth-logout.html"><i class='bx bx-log-out text-muted font-size-18 align-middle me-1'></i> <span class="align-middle">Logout</span></a>
                            </div>
                        </div>
                    </div>
                </div>
            </header>
            <!-- ========== Left Sidebar Start ========== -->
            <div class="vertical-menu">

                <!-- LOGO -->
                <div class="navbar-brand-box">
                    <a href="index.html" class="logo logo-dark">
                        <span class="logo-sm">
                            <img src="assets/images/logo-sm.svg" alt="" height="22">  <span class="logo-txt">Create Orders</span>
                        </span>
                        <span class="logo-lg">
                            <img src="assets/images/logo-sm.svg" alt="" height="22"> <span class="logo-txt">Create Orders</span>
                        </span>
                    </a>

                    <a href="index.html" class="logo logo-light">
                        <span class="logo-lg">
                            <img src="assets/images/logo-sm.svg" alt="" height="22"> <span class="logo-txt">Create Orders</span>
                        </span>
                        <span class="logo-sm">
                            <img src="assets/images/logo-sm.svg" alt="" height="22">  <span class="logo-txt">Create Orders</span>
                        </span>
                    </a>
                </div>

                <button type="button" class="btn btn-sm px-3 font-size-16 header-item vertical-menu-btn">
                    <i class="fa fa-fw fa-bars"></i>
                </button>

                <div data-simplebar class="sidebar-menu-scroll">

                    <!--- Sidemenu -->
             
                    <!-- Sidebar -->
                </div>
            </div>
            <!-- Left Sidebar End -->
            <header id="page-topbar" class="ishorizontal-topbar">
                <div class="navbar-header">
                    <div class="d-flex">
                        <!-- LOGO -->
                        <div class="navbar-brand-box">
                            <a href="index.php" class="logo logo-dark">
                                <span class="logo-sm">
                                    <img src="assets/images/logo-sm.svg" alt="" height="22">  <span class="logo-txt">Create Orders</span>
                                </span>
                                <span class="logo-lg">
                                    <img src="assets/images/logo-sm.svg" alt="" height="22"> <span class="logo-txt">Create Orders</span>
                                </span>
                            </a>

                            <a href="index.php" class="logo logo-light">
                                <span class="logo-sm">
                                    <img src="assets/images/logo-sm.svg" alt="" height="22">  <span class="logo-txt">Create Orders</span>
                                </span>
                                <span class="logo-lg">
                                    <img src="assets/images/logo-sm.svg" alt="" height="22"> <span class="logo-txt">Create Orders</span>
                                </span>
                            </a>
                        </div>

                        <button type="button" class="btn btn-sm px-3 font-size-16 d-lg-none header-item" data-bs-toggle="collapse" data-bs-target="#topnav-menu-content">
                            <i class="fa fa-fw fa-bars"></i>
                        </button>

                        <div class="topnav">
                            <nav class="navbar navbar-light navbar-expand-lg topnav-menu">
        
                                <div class="collapse navbar-collapse" id="topnav-menu-content">
                                    <ul class="navbar-nav">
                                            
                                        
        
                                    </ul>
                                </div>
                            </nav>
                        </div>
                    </div>

                    <div class="d-flex">
                   

                        <div class="dropdown d-none d-sm-inline-block">
                            <button type="button" class="btn header-item light-dark" id="mode-setting-btn">
                                <i data-feather="moon" class="icon-sm layout-mode-dark "></i>
                                <i data-feather="sun" class="icon-sm layout-mode-light"></i>
                            </button>
                        </div>
                        <!--<div class="dropdown d-inline-block">-->
                        <!--    <button type="button" class="btn header-item user text-start d-flex align-items-center" id="page-header-user-dropdown"-->
                        <!--        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">-->
                        <!--        <img class="rounded-circle header-profile-user" src="assets/images/users/avatar-1.jpg"-->
                        <!--        alt="Header Avatar">-->
                        <!--    </button>-->
                        <!--    <div class="dropdown-menu dropdown-menu-end pt-0">-->
                        <!--        <a class="dropdown-item" href="#"><i class='bx bx-user-circle text-muted font-size-18 align-middle me-1'></i> <span class="align-middle">My Account</span></a>-->
                                
                        <!--        <a class="dropdown-item" href="sign-out.php"><i class='bx bx-log-out text-muted font-size-18 align-middle me-1'></i> <span class="align-middle">Logout</span></a>-->
                        <!--    </div>-->
                        <!--</div>-->

                        
                    </div>
                </div>
            </header>
    


            <!-- ============================================================== -->
            <!-- Start right Content here -->
            <!-- ============================================================== -->
            <div class="main-content">

                <div class="page-content">
                    <div class="container-fluid">

                        <!-- start page title -->
                        <div class="row">
                            <div class="col-12">
                                <div class="page-title-box d-flex align-items-center justify-content-between">
                                    <h4 class="mb-0">Welcome !</h4>

                                    <div class="page-title-right">
                                        <ol class="breadcrumb m-0">
                                            <li class="breadcrumb-item"><a href="javascript: void(0);">Create Order</a></li>
                                            <li class="breadcrumb-item active">Welcome !</li>
                                        </ol>
                                    </div>

                                </div>
                            </div>
                        </div>
                        <!-- end page title -->
                        <div class="row">
                            <!-- <div class="col-md-12">-->
                            <!--    <a href="manage-orders.php" class="btn btn-primary"><i class="fas fa-list"></i> &nbsp; &nbsp; Manage Orders</a>-->
                            <!--</div>-->
                            
                            <?php if($done == true){ ?>
                          <div class="col-sm-12">
                                                <div class="alert alert-success alert-dismissible fade show px-4 mb-0 text-center" role="alert">
                                                    <i class="mdi mdi-check-all d-block display-4 mt-2 mb-3 text-success"></i>
                                                    <h5 class="text-success">Order Fulfilled</h5>
                                                    <p>Thanks for filling out this form. Order has been saved</p>
                                                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                                </div>
                                            </div><!-- end col -->
                          <?php } ?>
                            <div class="col-md-3">
                            </div>
                              <div class="col-md-6 ">
                                <br>
                                    <div class="mt-5 mt-lg-4 mt-xl-0">
                                        <!--<h5 class="font-size-14 mb-4" id="hide"><i class="mdi mdi-arrow-right text-primary me-1"></i> Customers Information</h5>-->
                                        
                                        
                                        
                                        <form action="" method="post">
                                            <div class="row mb-4" >
                                                <label for="horizontal-email-input" class="col-sm-3 col-form-label">Customer's Phone</label>
                                               
                                                <div class="col-sm-6">
                                                    <div class="input-group">
                                                        <div class="input-group-text">+971</div>
                                                        <input type="tel" class="form-control" id="specificSizeInputGroupUsername phone" placeholder="e.g, 565656550" maxlength="9" minlength="9" name="phone" required>
                                                    </div>
                                                </div>
                                                <div class="col-sm-2">
                                                    <button class="btn btn-info" type="button" id="check"> Check </button>    
                                                </div>
                                                <div class="col-sm-12">
                                                     <center>
                                                         <div id="output">
                                                       
                                                            
                                                        
                                                        </div>
                                                    </center>
                                                </div>
                                            </div>
                                            <div id="hide">
                                                 <div class="row mb-4">
                                                <label for="horizontal-firstname-input" class="col-sm-3 col-form-label">Customer's Name</label>
                                                <div class="col-sm-9">
                                                  <input type="text" class="form-control" placeholder="Enter FullName" id="horizontal-firstname-input" name="name" required>
                                                </div>
                                            </div>
                                          
                                            
                                            
                                            
                                            <div class="row mb-4">
                                                <label for="horizontal-firstname-input" class="col-sm-3 col-form-label">State</label>
                                                <div class="col-sm-3">
                                                    <label for="horizontal-email-input" class="col-sm-3 col-form-label">State</label>
                                                  <select class="form-control state" name="state" required>
                                                      <option value="">Choose...</option>
                                                      <option value="Abu Dhabi">Abu Dhabi</option>
                                                     <option value="Ajman">Ajman</option>
                                                     <option value="Al Ain">Al Ain</option>
                                                     <option value="Dubai">Dubai</option>
                                                     <option value="Fujairah">Fujairah</option>
                                                     <option value="Ras Al Khaimah">Ras Al Khaimah</option>
                                                     <option value="Sharjah">Sharjah</option>
                                                     <option value="Umm Al Quwain">Umm Al Quwain</option>
                                                  </select>
                                                </div>
                                                <div class="col-sm-3">
                                                    <label for="horizontal-email-input" class="col-sm-3 col-form-label">Area</label>
                                                  <input type="text" class="form-control" id="specificSizeInputGroupUsername" placeholder="Area"  name="area" required>
                                                </div>
                                            </div>
                                            
                                            <div class="row mb-4" >
                                                <label for="horizontal-password-input" class="col-sm-3 col-form-label">Customer's Address</label>
                                                <div class="col-sm-9">
                                                    <textarea class="form-control" rows="5" placeholder="Customer's Complete Address here..." name="building" required></textarea>
                                                  
                                                </div>
                                            </div>
                                            <h5 class="font-size-14 mb-4" ><i class="mdi mdi-arrow-right text-primary me-1"></i> Order Information</h5>
                                            <div class="row mb-4">
                                                <label for="horizontal-firstname-input" class="col-sm-3 col-form-label">Product Name</label>
                                                <div class="col-sm-9">
                                                  <input type="text" class="form-control" placeholder="Product Name" id="horizontal-firstname-input" name="product_name" required>
                                                </div>
                                            </div>
                                            
                                            <div class="row mb-4">
                                                <label for="horizontal-password-input" class="col-sm-3 col-form-label">Quantity</label>
                                                <div class="col-sm-3">
                                                   <input type="number" class="form-control" value="1" id="horizontal-password-input" name="quantity" disabled>
                                                   <input type="hidden" class="form-control" value="1" id="horizontal-password-input" name="quantity">
                                                </div>
                                                
                                            </div><div class="row mb-4" >
                                                <label for="horizontal-password-input" class="col-sm-3 col-form-label">Product Price</label>
                                                <div class="col-sm-3">
                                                   <input type="number" class="form-control" value="100" min="1" id="horizontal-password-input" name="price" required>
                                                </div>
                                                
                                            </div>
                                            <h5 class="font-size-14 mb-4" ><i class="mdi mdi-arrow-right text-primary me-1"></i> Team / Agent Information</h5>
                                             <div class="row mb-4" >
                                                <label for="horizontal-firstname-input" class="col-sm-3 col-form-label">Select Team</label>
                                                <div class="col-sm-9">
                                                  <select class="form-control" name="team" required>
                                                        <option value="">Choose...</option>
                                                        
                                                        <?php
                                                        $query = $db->query("SELECT * FROM teams");
                                                        while($row =$query->fetch_assoc()){ ?>
                                                        <option value="<?php echo $row['team_id']; ?>"><?php echo $row['team_name']; ?></option>
                                                        <?php } ?>
                                                  </select>
                                                </div>
                                            </div>
                                            <div class="row mb-4">
                                                <label for="horizontal-firstname-input" class="col-sm-3 col-form-label">Agent Name</label>
                                                <div class="col-sm-9">
                                                  <input type="text" class="form-control" placeholder="Agent who's creating Order" id="horizontal-firstname-input" name="agent" required>
                                                </div>
                                            </div>
                                            
                                            <div class="row mb-4" >
                                                <label for="horizontal-email-input" class="col-sm-3 col-form-label">Order Note / Remarks</label>
                                                <div class="col-sm-9">
                                                    <textarea class="form-control" name="note" rows="5" placeholder="Order Note here..." required></textarea>
                                                </div>
                                            </div>
                                            <div class="row justify-content-end" >
                                                <div class="col-sm-9">
                                                   <!--  <div class="form-check mb-4">
                                                        <input type="checkbox" class="form-check-input" id="horizontal-customCheck">
                                                        <label class="form-check-label" for="horizontal-customCheck">Remember me</label>
                                                    </div> -->

                                                    <div>
                                                        <button type="submit" class="btn btn-primary w-md" name="add">Submit Order</button>
                                                    </div>
                                                </div>
                                            </div>
                                            </div>
                                           
                                        </form>
                                    </div>
                            </div>
                        </div>
                   
                        
                    </div> <!-- container-fluid -->
                </div>
                <!-- End Page-content -->

                
                <footer class="footer">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-sm-6">
                                <script>document.write(new Date().getFullYear())</script> &copy; Create Order.
                            </div>
                            <div class="col-sm-6">
                                <div class="text-sm-end d-none d-sm-block">
                                    Made with <i class="mdi mdi-heart text-danger"></i> by <a href="https://eclosol.com/" target="_blank" class="text-reset">Zohaib @ Eclosol</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </footer>
            </div>
            <!-- end main content-->

        </div>
        <!-- END layout-wrapper -->

     

 
        <!-- JAVASCRIPT -->
        <script src="assets/libs/bootstrap/js/bootstrap.bundle.min.js"></script>
        <script src="assets/libs/metismenujs/metismenujs.min.js"></script>
        <script src="assets/libs/simplebar/simplebar.min.js"></script>
        <script src="assets/libs/feather-icons/feather.min.js"></script> 
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-ajaxy/1.6.1/scripts/jquery.ajaxy.js" integrity="sha512-4WpSQe8XU6Djt8IPJMGD9Xx9KuYsVCEeitZfMhPi8xdYlVA5hzRitm0Nt1g2AZFS136s29Nq4E4NVvouVAVrBw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
            <script type="text/javascript">
              
            $(document).ready(function(){
                // alert("hello");
                
                
                $('#output').html("");
                
                
                
                var phone = $('input[type="tel"]').val();
                
                
                $("#check").click(function(){
                //   console.log($('input[type="tel"]').val());
                // alert($('input[type="tel"]').val());
                    if($('input[type="tel"]').val().length==9){
                        $.ajax({
    
                            url:"ajax_data.php",
    
                            method:"POST",
    
                            data:{phone: $('input[type="tel"]').val()},
    
                        
                            
                            success:function(data)
                            {
                                // alert(data);
                               if(data==1){
                                    $('#output').html('<p  class="text-success" id="greeen" >Lets Go!</p>');
                                 
                                    $("#hide").css("display", "block", "!important");
                                        
                                   
                               } else if(data ==0) {
                                   $('#output').html('<p  class="text-danger" id="redd" >This customer Already exists</p>');
                                   $('#hide').css('display', 'none');
                               }
                               
                            }
                        });
                    } else {
                        alert("Please enter a valid number");
                    }
                    
                });
                
                
                $('input[type="tel"]').on("change", function(){
                    $('#hide').css('display', 'none');
                    $('#output').html("");
                });
            });
        </script>

        <!-- gridjs js -->
        <!--<script src="assets/libs/gridjs/gridjs.umd.js"></script>-->

        <!--<script src="assets/js/pages/gridjs.init.js"></script>-->

        <script src="assets/js/app.js"></script>
       

    </body>

<!-- Mirrored from themesbrand.com/symox/layouts/index.html by HTTrack Website Copier/3.x [XR&CO'2014], Fri, 14 Jan 2022 05:51:51 GMT -->
</html>
