<?php   include_once('../config/db.php');   include_once('../config/check.php');  
 

if (filter_has_var(INPUT_POST, "submit")) 
    try{
            $date =            htmlspecialchars (strtoupper($_POST['date']));
            $date =            date("Y-m-d", strtotime($date));

            $product=                   htmlspecialchars ( $_POST['product']);
            $name =                     htmlspecialchars ( $_POST['name']);
            $phone =                    htmlspecialchars ( $_POST['phone']);
            $email =                    htmlspecialchars ( $_POST['email']);
            $website =                  htmlspecialchars ( $_POST['website']);
            $cname =                    htmlspecialchars ( $_POST['companyname']);
            $description =              htmlspecialchars ( $_POST['description']);
            
            $origin =                   htmlspecialchars ( $_POST['origin']);
            $source =                   htmlspecialchars ( $_POST['source']);
            $mode =                   htmlspecialchars ( $_POST['mode']);
            $status =                   htmlspecialchars ( $_POST['status']);

            $nextaction =               $_POST['nextaction'];
            $number =                 count($_POST["nextaction"]); 

            $sql = 'INSERT INTO crmlead(date,product,name,companyname,phone,email,description,origin,source,mode,status,uid) VALUES(:date,:product,:name,:cname,:phone,:email,:descr,:origin,:source,:mode,:status,:uid )';
            $conn->prepare($sql)->execute(['date'=>$date,'product'=>$product,'name' =>$name,'cname' =>$cname,'phone' =>$phone,'email' =>$email ,'descr' =>$description,'origin'=>$origin,'source' =>$source,'mode'=>$mode,'status'=>$status,'uid'=>$uid      ]);
            $lastid = $conn->lastInsertId(); 

            for($i=0; $i<$number; $i++)  
               {  
                        $sql = 'INSERT INTO crmleadaction   (date,lead,laction,notes,uid )
                                                     VALUES (:date,:lead,:laction,:notes,:uid )';
                        $stmt = $conn->prepare($sql)->execute(['date'=> $date,'lead' =>$lastid,
                                                               'laction' =>$nextaction[$i],'notes'=>$description,'uid'=>$uid  ]);
                 } 

            header("location:crmlead.php");

        } catch(Exception $e) { echo 'Message: ' .$e->getMessage();}

?>

<!DOCTYPE html>
<html>
<head>
<?php include_once('../inc/slink.php');   ?>  
</head>
<body <?php include_once('../inc/snav.php');  ?>>
<div class="wrapper">

  <!-- Main Header -->
    <header class="main-header">

    <!-- Logo -->
       <?php include_once('../inc/sbrand.php');   ?>

    <!-- Header Navbar -->
    <?php include_once('../inc/snavbar.php');   ?>

  </header>
 
  <!-- Left side column. contains the logo and sidebar -->
    <aside class="main-sidebar">

    <!-- sidebar: style can be found in sidebar.less -->
     <?php include_once('../inc/smenu.php');   ?>
    <!-- /.sidebar -->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">

  <section class="content-header" style="padding-bottom:0px;">
            <p style="color:green; font-size:20px; font-weight:bold; " > <i class="fa fa-building" ></i> LEADS    </p>
                        
  </section>

  <section class="content container-fluid" style="padding-top:0px;"> 
        <div class="box box-default box-xs">
            <div class="box-body">
                <div class="row">
                <form class="form-horizontal add_form "  action="" method="post"  >
                                               
                                <div class="col-sm-3"> 
                                    <label > Date  </label>  
                                    <input type="text"   autocomplete="off" class="form-control datepicker" name="date"   required=""   > 
                                </div>  
                                <div class="col-md-3">
                                    <label > Interested In  </label>  
                                    <select class="form-control js-example-basic-single    "  required="" name="product" style="width:100%" >
                                                        <?php $stmt = $conn->prepare("SELECT * FROM crmproduct  ");
                                                        $stmt->execute(); foreach ($stmt as $row) {?>
                                                                  <option> </option>
                                                                <option value="<?php echo $row['id']; ?>"><?php
                                                                    echo $row['name']; ?></option> <?php } ?>
                                    </select> 
                                </div>                                            
                                <div class="col-sm-3"> 
                                    <label > Lead Name   </label>  
                                        <input type="text"  name="name" value="" autocomplete="off" class="form-control"   required=""   > 
                                </div> 
                                <div class="col-sm-3"> 
                                    <label > Company Name    </label>  
                                        <input type="text"  name="companyname" value="" autocomplete="off" class="form-control" > 
                                </div> 
                                <div class="col-sm-3"> 
                                    <label > Phone  </label>  
                                        <input type="phone"  name="phone" value="" autocomplete="off" class="form-control"   required=""   > 
                                </div> 
                                <div class="col-sm-3"> 
                                    <label > Email  </label>  
                                        <input type="email"  name="email" value="" autocomplete="off" class="form-control" > 
                                </div> 
                                <div class="col-sm-3"> 
                                    <label> Website  </label>  
                                        <input type="text"  name="website" value="" autocomplete="off" class="form-control" > 
                                </div> 
                                                                                        
                                <div class="col-md-3">
                                    <label> Notes </label>           
                                    <input type="text" class="form-control" name="description" required="" autocomplete="off" >
                                </div> 
                               
                                <div class="col-md-3">
                                    <label> Lead Origin  </label>  
                                    <select class="form-control js-example-basic-single    "  required="" name="origin" style="width:100%" >
                                                        <?php $stmt = $conn->prepare("SELECT * FROM crmleadorigin  ");
                                                        $stmt->execute(); foreach ($stmt as $row) {?>
                                                                  <option> </option>
                                                                <option value="<?php echo $row['id']; ?>"><?php
                                                                    echo $row['name']; ?></option> <?php } ?>
                                    </select> 
                                </div> 
                                <div class="col-md-2">
                                    <label> Lead Source </label>  
                                    <select class="form-control js-example-basic-single    "  required="" name="source" style="width:100%" >
                                                        <?php $stmt = $conn->prepare("SELECT * FROM crmleadsource  ");
                                                        $stmt->execute(); foreach ($stmt as $row) {?>
                                                                  <option> </option>
                                                                <option value="<?php echo $row['id']; ?>"><?php
                                                                    echo $row['name']; ?></option> <?php } ?>
                                    </select> 
                                </div> 
                                <div class="col-md-2">
                                    <label> Lead Mode </label>  
                                    <select class="form-control js-example-basic-single    "  required="" name="mode" style="width:100%" >
                                                        <?php $stmt = $conn->prepare("SELECT * FROM crmleadmode ");
                                                        $stmt->execute(); foreach ($stmt as $row) {?>
                                                                  <option> </option>
                                                                <option value="<?php echo $row['id']; ?>"><?php
                                                                    echo $row['name']; ?></option> <?php } ?>
                                    </select> 
                                </div> 
                                <div class="col-md-3">
                                    <label> Lead action  </label>  
                                    <select class="form-control js-example-basic-multiple"   name="nextaction[]" multiple="multiple"   >
                                                        <?php $stmt = $conn->prepare(" SELECT * FROM crmleadnextaction  ");
                                                        $stmt->execute(); foreach ($stmt as $row) {?>
                                                                 
                                                                <option value="<?php echo $row['id']; ?>"><?php
                                                                    echo $row['name']; ?></option> <?php } ?>
                                    </select> 
                                </div> 
                                <div class="col-md-2">
                                    <label> Lead status   </label>  
                                    <select class="form-control js-example-basic-single   "  required="" name="status" style="width:100%" >
                                                        <?php $stmt = $conn->prepare("SELECT * FROM crmleadstatus  ");
                                                        $stmt->execute(); foreach ($stmt as $row) {?>
                                                                  <option> </option>
                                                                <option value="<?php echo $row['id']; ?>"><?php
                                                                    echo $row['name']; ?></option> <?php } ?>
                                    </select> 
                                </div> 
                                                 
                                  
                            <!-- submit button -->
                            <div class="form-group">                              
                                <div class="col-sm-offset-4 col-sm-10" style="padding:0.5rem;">
                                        <button type="submit"  name="submit" class="btn btn-success col-md-3 subbtn ">  <i class="fa fa-plus"></i>  Add lead type  </button> </div>
                            </div> 
                    </form>               

                   
                   
                </div>
            </div>
        </div>
                                
    </section>

</div>
  <!-- /.content-wrapper -->
  
  <!-- Main Footer -->
  <footer class="main-footer">
    <?php  include_once('../inc/footer.php'); ?>
  </footer>
  
</div>
                         
<!-- ./wrapper -->

<!-- REQUIRED JS SCRIPTS -->
<?php include_once('../inc/slinkjs.php') ?>

<script>

    $(document).ready(function() {
    $('.js-example-basic-multiple').select2();
});

</script>

</body>
</html>