<?php
include "../../pidbco/pidbjoin.php";
include "../ses_write.php";

if(!isset($_SESSION))
{
    session_start();
}

//fetching Client IP address
function get_client_ip()
{
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
        $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}
$ip=get_client_ip();

if(isset($_POST['btn_edt_exec_data']))
{
    $tid = strip_tags(trim($_POST['tid']));

    $stmt = $con->prepare("SELECT id, comp_name, person_name, phone, phone2, status, follow_up FROM tele_calls_det WHERE id = ?");
    $stmt -> bind_param("i",$tid);
    $stmt -> execute();
    $stmt -> store_result();
    $stmt -> bind_result($id, $comp_name, $person_name, $phone, $phone2, $status, $follow_up);
    $all_posts = Array();
    while($stmt->fetch())
    {
        $individual_post = Array(
            'id' => $id,
            'comp_name' => $comp_name,
            'person_name' => $person_name,
            'phone' => $phone,
            'phone2' => $phone2,
            'status' => $status,
            'follow_up' => $follow_up
        );
        array_push($all_posts, $individual_post);
    }
    $stmt->close();
    ?>
    <script>
        $(document).ready(function () {

            $(".bdo-exec").select2({
                placeholder: "Select a Telecaller (BDO)",
                allowClear: true
            });
        });
    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#next_follow_up').daterangepicker({
                singleDatePicker: true,
                calender_style: "picker_4"
            }, function (start, end, label) {
                console.log(start.toISOString(), end.toISOString(), label);
            });
        });
    </script>
    <div class="x_panel">
        <div class="x_title">
            <h2>Executive Report Data<small>registration</small></h2>
            <ul class="nav navbar-right panel_toolbox">
                <li>
                    <a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                </li>
                <li>
                    <a class="close-link"><i class="fa fa-close"></i></a>
                </li>
            </ul>
            <div class="clearfix"></div>
        </div>
        <div class="x_content" id="edt_data">

            <form id="frm_1" name="frm_1" data-parsley-validate class="form-horizontal form-label-left">
                <div class="col-md-8">
                    <div class="col-md-4 col-xs-12 col-sm-12">
                        <div class="form-group">
                            <select class="bdo-exec form-control col-md-7 col-xs-12" id="bdo_exec" required="required" name="bdo_exec">
                                <option value=""></option>
                                <?php
                                $emp = mysqli_query($con,"SELECT id,emp_name FROM employee_registration");
                                while($emp_list = mysqli_fetch_object($emp))
                                {
                                    ?>
                                    <option value="<?php echo $emp_list->id; ?>"><?php echo $emp_list->emp_name; ?></option>
                                <?php
                                }
                                ?>
                            </select>
                            <input type="hidden" id="idex" value="<?php echo base64_encode($individual_post['id']); ?>">
                        </div>
                    </div>
                    <div class="col-md-4 col-xs-12 col-sm-12">
                        <input id="next_follow_up" class="date-picker form-control col-md-7 col-xs-12" placeholder="Enter Followup Date"  type="text">
                    </div>
                    <div class="col-md-4 col-xs-12 col-sm-12" style="margin-top: 2px;">
                        <select id="status" class="form-control  col-md-7 col-xs-12" name="status"  required="required">
                            <option value="">Status*</option>
                            <option value="1">Interested</option>
                            <option value="2">Order Lost</option>
                            <option value="6">Quotation Placed</option>
                            <option value="5">Order Confirmed</option>
                        </select>
                    </div>
                    <div>
                        <div class="col-md-12 col-sm-12 col-xs-12 " style="margin-top: 3px;">
                            <textarea id="report" class="form-control col-md-12 col-xs-12"  placeholder="Enter Report*" required="required" name="report" ><?php ?></textarea>
                        </div>
                    </div>

                    <div class="col-md-12" >
                        <br>
                        <?php
                        $qry = mysqli_query($con,"SELECT id, executive_bdo FROM tele_calls_det WHERE id = '$tid'");
                        $result = mysqli_fetch_object($qry);
                        //if($result->executive_bdo == '')
                        //{
                         ?>
                            <input type="button" class="btn btn-primary col-md-1 btn-sm" onclick="add_exec_dt();" value="Save" style="float: right;">
                        <?php
                       // }
                        ?>

                    </div>
                </div>
                <div class="panel panel-default col-md-4 hidden-xs hidden-sm small">
                    <div class="panel-heading">Company Details</div>
                    <div class="panel-body">
                        <div class="list-group ">
                            <a href="#" class="list-group-item"><?php echo $individual_post['comp_name']; ?></a>
                            <a href="#" class="list-group-item"><?php echo $individual_post['person_name']; ?></a>
                            <a href="#" class="list-group-item"><?php if(!empty($individual_post['phone'])){ echo $individual_post['phone']; }else{ echo $individual_post['phone2']; } ?></a>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <?php
    exit();
}
if(isset($_POST['btn_upd_exec_data']))
{
    $bdo_exec = strip_tags(trim($_POST['bdo_exec']));
    $next_follow_up = strip_tags(trim($_POST['next_follow_up']));
    $status = strip_tags(trim($_POST['status']));
    $report = strip_tags(trim($_POST['report']));
    $idex = strip_tags(trim(base64_decode($_POST['idex'])));
    $today = date('Y-m-d');

    if(!empty($bdo_exec) && !empty($status) && !empty($report))
    {
        $stmt = $con->prepare("UPDATE tele_calls_det SET executive_bdo=?, executive_status=? ,executive_follow_up=? WHERE id=?");
        $stmt->bind_param('sssi',$bdo_exec,$status,$next_follow_up,$idex);
        $stmt->execute();

        $qry6 = mysqli_query($con,"SELECT tele_id, executive_bdo_id, report FROM executive_report WHERE  tele_id='$idex' AND executive_bdo_id='$bdo_exec' AND report = '$report'");
        $num1 = mysqli_num_rows($qry6);
        if($num1 == 0)
        {
            $stmt11 = $con->prepare("INSERT INTO executive_report(tele_id, executive_bdo_id, entered_date, report) VALUES (?,?,?,?)");
            $stmt11->bind_param("iiss",$idex,$bdo_exec,$today,$report);
            $stmt11->execute();
            $stmt11->close();

        }

        ?>
        <script>
            $('#next_follow_up').val('');
            $('#bdo_exec').val('');
            $('#report').val('');
            $('#edt_frm').html('');
            $('#status').html('');
            show_exe_data();
        </script>
        <?php
    }
    else
    {
        ?><script>alert("Error!..")</script><?php
    }

    exit();
}
if(isset($_POST['btn_shw_exec_data']))
{
 ?>
    <div class="dataTables_wrapper">
        <table class="table table-striped table-bordered table-hover table-responsive" id="dataTables-example">
            <thead>
            <tr>
                <!--<th>#</th>-->
                <th>ID</th>
                <th>Company</th>
                <th class="hidden-xs">Person</th>
                <th>Exe. BDO</th>
                <th class="hidden-xs">Phone</th>
                <th class="hidden-xs">Follow Up</th>
                <th>Exe. Status</th>
            </tr>
            </thead>
            <tbody>
            <?php
            $stmt = mysqli_query($con,"SELECT * FROM tele_calls_det WHERE status='4' ORDER BY id DESC ");
            //$i=1;
            while($res = mysqli_fetch_object($stmt))
            {
                $ex_bdo = $res->executive_bdo;
                $qry15 = mysqli_query($con,"SELECT id,emp_name FROM employee_registration WHERE id='$ex_bdo'");
                $qry15_fet = mysqli_fetch_object($qry15);

                list($year,$month,$day) = explode('-', $res->executive_follow_up);
                ?>
                <tr class="odd gradeX" onclick="edit_exe_data(<?php echo $res->id; ?>)" title="Add Reort" style="cursor: pointer;">
                    <!--<td></td>-->
                    <td><?php echo $res->id; ?></td>
                    <td><?php echo $res->comp_name; ?></td>
                    <td class="hidden-xs"><?php echo $res->person_name; ?></td>
                    <td><?php echo $qry15_fet->emp_name; ?></td>
                    <td class="hidden-xs"><?php echo $res->phone; ?></td>
                    <td class="hidden-xs"><?php echo $day.' - '.$month.' - '.$year; ?></td>
                    <td class="center"><?php if($res->executive_status == 1){ echo 'Interested'; }elseif($res->executive_status == 2){ echo 'Order Lost'; }elseif($res->executive_status == 5){ echo 'Order Confirmed'; }elseif($res->executive_status == 6){ echo 'Quotation Placed'; }  ?></td>
                </tr>
                <?php
                //$i++;
            }
            $stmt->close();
            ?>
            </tbody>
        </table>
    </div>
    <!-- /.table-responsive -->
<?php
    exit();
}

?>
