<?php
ob_start();
session_start();
include 'connect.php';
if(isset($_SESSION['adminemail']))
{
    $user=$_SESSION['adminemail'];

    $gtp=mysqli_query($conn,"select * from admin where username='$user'");
    $gtp1=mysqli_fetch_assoc($gtp);

    include 'top.php';
?>

<div id="page-wrapper" >
    <div id="page-inner">
        <div class="main-content">
            <div class="main-content-inner">
                <div class="breadcrumbs ace-save-state" id="breadcrumbs">
                    <ul class="breadcrumb">
                        <li>
                            <i class="ace-icon fa fa-home home-icon"></i>
                            <a href="#">Home</a>
                        </li>
                        <li class="active">Follow Up</li>
                    </ul><!-- /.breadcrumb -->
                </div>

                <div class="page-content">
                    <div class="page-header">
                        <?php

                        $aca=mysqli_query($conn,"select * from financial where user='$user' order by ID desc");
                        $aca1=mysqli_fetch_assoc($aca);

                        $financial=$aca1['financial'];

                        ?>
                        <table style="width:100%;">
                            <tr><td style="width:50%;">

                                <h1>
                                    Follow Up
                                </h1>

                            </td>

                            <td style="width:50%; text-align:right; padding-right:15px; font-size:15px;">

                                <i class="fa fa-calendar"></i>&nbsp; <?php echo $financial; ?>

                            </td></tr></table>

                    </div><!-- /.page-header -->

                    <div class="row">
                        <div class="col-xs-12">

                            <!-- PAGE CONTENT BEGINS -->

                            <script src="assets/js/jquery.js"></script>
							 
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
  <script>
    $(function() {
      // Initialize the datepicker
      $("#datepicker").datepicker({
        format: "mm/dd/yyyy",
        autoclose: true,
        todayHighlight: true
      });

      // Adjust the positioning of the datepicker dropdown
      $("#datepicker").on("show", function(e) {
        var inputOffset = $(this).offset();
        var inputHeight = $(this).outerHeight();
        var datepickerDropdown = $(".datepicker-dropdown");

        datepickerDropdown.css({
          top: inputOffset.top + inputHeight + 10,
          left: inputOffset.left
        });
      });
    });
  </script>
                            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.min.css">
 <style>
    /* Custom CSS for Chosen select */
    .chosen-container {
      width: 25ch;
    }

    .chosen-container-single .chosen-single {
      border-radius: 4px;
      border: 1px solid #f0f0f0; /* Change the border color here */
      height:34px;
	  margin-bottom:10px;
      background-color: #fff;
      background-image: none;
      box-shadow: 0 0 0 1px #c0c0c0;
    }

    .chosen-container-single .chosen-single span {
      padding: 5px;
      text-transform: uppercase;
    }

    .chosen-container-single .chosen-search input[type="text"] {
      border-radius: 4px;
      border: 1px solid #c0c0c0;
      height: 34px;
    }

    .chosen-container-active .chosen-single {
      border-color: #7f7f7f;
      box-shadow: 0 0 0 1px #7f7f7f;
    }

    .chosen-container-active .chosen-single:hover {
      border-color: #7f7f7f;
      box-shadow: 0 0 0 1px #7f7f7f;
    }

    .chosen-container-active .chosen-search input[type="text"] {
      border-color: #7f7f7f;
    }
  </style>

                            <script>
                                $(document).ready(function() {
                                    $(".executive").chosen({
                                        placeholder_text_single: "-- Select --",
                                        disable_search_threshold: 5,
                                        no_results_text: "No results found",
                                        width: "30ch;",
										
										
                                    });

                                    $(".executive").change(function() {
                                        var executive = $(this).val();
                                        var dataString = 'executive=' + executive;
                                        $.ajax({
                                            type: "GET",
                                            url: "getcust1.php",
                                            data: dataString,
                                            cache: false,
                                            success: function(resp) {
                                                $('.customer').html(resp);
                                                $('.customer').trigger('chosen:updated');
                                            }
                                        });
                                    });

                                    $(".customer").chosen({
                                        placeholder_text_single: "-- Select --",
                                        disable_search_threshold: 5,
                                        no_results_text: "No results found",
                                        width: "30ch;",
                                    });

                                    $(".customer").change(function() {
                                        $('.contactrow').show();
                                        var customer = $(this).val();
                                        var dataString = 'customer=' + customer;
                                        $.ajax({
                                            type: "GET",
                                            url: "getcontact.php",
                                            data: dataString,
                                            cache: false,
                                            success: function(html) {
                                                $('.contact').html(html);
                                            }
                                        });
                                    });
                                });
                            </script>
							<script>
							$(document).ready(function() {
                            $(".chosen-select").chosen({
                            allow_single_deselect: true,
                            disable_search_threshold: 5,
                            width: "30ch"
    });
});
							</script>

                            <?php        

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

                                $customer = $_POST['customer'];
                                $date = $_POST['date'];
                                $time=$_POST['time'];
                                $remarks = $_POST['remarks'];
                                $executive=$_POST['executive'];

                                $date=date('Y-m-d',strtotime($date));

                                if($customer)
                                { 
                                    $in= "INSERT INTO followup(date,customer,remarks,executive,time)
                                            VALUES('$date','$customer','$remarks','$executive','$time')";

                                    $in1 = mysqli_query($conn,$in) or die(mysqli_error($conn));

                                    if($in1)
                                    {
                                        header("location:followup.php?s=1");
                                    }
                                }
                                else
                                {
                                    header("location:followup.php?s=2");
                                }
                            }
                            else 
                            {
                                if(isset($_GET['s']))
                                {
                                    $status=$_GET['s'];
                                    if($status=='1')
                                    {
                                        echo'<div class="alert alert-block alert-success">
                                                             <button type="button" class="close" data-dismiss="alert">
                                                                 <i class="ace-icon fa fa-times"></i>
                                                             </button>
                                                 Follow Up Added Successfully.
                                           </div>';                  
                                    }
                                    if($status=='2')
                                    {
                                        echo'<div class="alert alert-block alert-danger">
                                                             <button type="button" class="close" data-dismiss="alert">
                                                                 <i class="ace-icon fa fa-times"></i>
                                                             </button>
                                                 Values Not Inserted. Please try again.
                                           </div>';                  
                                    }
                                }
                                $sel=mysqli_query($conn,"select * from admin where username='$user'");
                                $nmrw=mysqli_num_rows($sel);
                                $sel1=mysqli_fetch_assoc($sel);
                            ?>

                           <form id="course" action="followup.php" method="post">
                                <table width="70%" style="margin:auto; line-height:35px;">
                                    <tr>
                                        <td>Date <span class="red">*</span></td>
                                        <td></td>
                                        <td><input class="form-control" style="width: 28ch; margin-bottom:10px;" type="text" name="date" class="datepicker" id="datepicker" title="*" autocomplete="off" /required></td>
                                    </tr>

                                    <?php
                                    //echo $nmrw;
                                    if($nmrw > 0) 
                                        {
                                        echo '<tr>
                                                <td>Executive <span class="red">*</span></td>
                                                <td></td>
                                                <td>
                                                    <select style="width: 30ch; margin-bottom:10px;border-radius: 4px;border: 1px solid #c0c0c0;height:34px;" name="executive" class="executive ">
                                                        <option value="">-- Select --</option>';

                                        $exec = mysqli_query($conn,"select employee.ID as ID, employee.name as name from employee left join department on employee.department=department.ID where department.crm='1' order by employee.ID asc");
                                        while($exec1 = mysqli_fetch_assoc($exec)) 
                                            {
                                            echo "<option value='".$exec1['ID']."'>".$exec1['name']."</option>";
                                            }

                                        echo '</select>
                                                </td>
                                            </tr>';
                                        } 
                                    else 
                                        {
                                        $empd = mysqli_query($conn,"select * from employee where username='$user'");
                                        $empd1 = mysqli_fetch_assoc($empd);
                                        $usr = $empd1['ID'];
                                        echo '<tr>
                                                <td><span class="red"></span></td>
                                                <td></td>
                                                <td>';    
                                        echo '<input type="Hidden" name="executive" value="'.$usr.'" />';
                                        echo '</td>
                                            </tr>';
                                        }
                                    ?>

                                    <tr>
                                        <td>Customer <span class="red">*</span></td>
                                        <td></td>
                                        <td>
                                            <select style="width: 30ch; margin-bottom:10px;border-radius: 4px;border: 1px solid #c0c0c0;height:34px;" name="customer" class="customer " style="width:220px;" id="required" title="*">
                                                <option value="" selected="selected">-- Select --</option>
                                                <?php
                                                if($nmrw == 0) {
                                                    $eem = mysqli_query($conn,"select * from employee where username='$user'");
                                                    $eem1 = mysqli_fetch_assoc($eem);

                                                    $cust = mysqli_query($conn,"select * from crm_customer where executive='$eem1[ID]' order by ID asc");
                                                    while($cust1 = mysqli_fetch_assoc($cust)) {
                                                        echo "<option value='".$cust1['ID']."'>".$cust1['name']." ".$cust1['mobile']."</option>";
                                                    }
                                                }
                                                ?>
                                            </select>
                                        </td>
                                    </tr>

                                    <tr style="display:none;" class="contactrow">
                                        <td>Contact No.</td>
                                        <td></td>
                                        <td>
                                            <div class="contact"></div>
                                        </td>
                                    </tr>

                                    <tr>
                                        <td>Time</td>
                                        <td></td>
                                        <td>
                                            <select name="time" class="chosen-select">
                                                <option value="">-- Select --</option>
                                                <?php
                                                $st = mysqli_query($conn,"select * from time_vl order by ID asc");
                                                while ($st1 = mysqli_fetch_assoc($st)) {
                                                    echo "<option value='".$st1['time']."'>".$st1['time']."</option>";
                                                }
                                                ?>
                                            </select>
                                        </td>
                                    </tr>

                                    <tr>
                                        <td>Remarks <span class="red">*</span></td>
                                        <td></td>
                                        <td style="padding-top:5px;">
                                            <textarea  class="form-control" style="width: 28ch; margin-bottom:10px;height:15ch;" name="remarks" rows="2" cols="22" style="width:220px; height:70px;" id="required" title="*"></textarea>
                                        </td>
                                    </tr>

                                    <tr>
                                        <td colspan="3" style=" padding-top:7px;margin-left: 300px;">
                                            <button type="submit" name="submit" class="btn btn btn-primary"  style="margin-left: 230px;">Submit</button>
                                        </td>
                                    </tr>
                                </table>
                            </form>

                            <?php
                            }
                            ?>

                            <!-- PAGE CONTENT ENDS -->
                        </div><!-- /.col -->
                    </div><!-- /.row -->
                </div><!-- /.page-content -->
            </div>
        </div><!-- /.main-content -->
    </div><!-- /.main-content -->
</div><!-- /.main-content -->

<?php
include 'bottom.php';
}
else
{
    header("location:login.php");
}

ob_flush();
?>
