<?php
$msg = '';
include("include/settings.php");
include("checksession.php");

$where = '';
$condition = '';

$skip = 0;
if (isset($_POST['page_skip'])) {
    $skip = $_POST['page_skip'] * 10 - 10;
} else {
    $_POST['page_skip'] = 0;
}

$pass = 0;

if (isset($_POST['ddlUserId']) && trim($_POST['ddlUserId']) != 0) {
    $pass = 1;
} else if (isset($_POST['ddlLeadType']) && trim($_POST['ddlLeadType']) != 0) {
    $pass = 1;
} else if (isset($_POST['txtFromDate']) && trim($_POST['txtFromDate']) != null) {
    $pass = 1;
} else if (isset($_POST['txtToDate']) && trim($_POST['txtToDate']) != null) {
    $pass = 1;
} else {
    $pass = 0;
}


if ($pass == 1) {

    $where = 'where';

    if (trim($_POST['txtFromDate']) != null) {

        $fromdate = $_POST['txtFromDate'];
        $condition .= trim($condition) != null ? ' and ' : ' ';

        if (trim($_POST['txtFromDate']) != null) {
            $condition .= " cbi_call_schedule.call_scheduling >= '{$fromdate}' ";
        }
    }

    if (trim($_POST['txtToDate']) != null) {

        $todate = $_POST['txtToDate'];
        $condition .= trim($condition) != null ? ' and ' : ' ';

        if (trim($_POST['txtToDate']) != null) {
            $condition .= " cbi_call_schedule.call_scheduling <= '{$todate}' ";
        }
    }

    if (trim($_POST['ddlUserId']) != 0) {
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " rishta_leadmaster.lead_allocated_to = " . $_POST['ddlUserId'];
    }
}

$select_query = "";
$select_query .= " select count(*)";
$select_query .= " from cbi_call_schedule inner join cdi_usermaster on cbi_call_schedule.call_added_by=cdi_usermaster.user_id ";
$select_query .= " inner join rishta_leadmaster on cbi_call_schedule.lead_id=rishta_leadmaster.lead_id ";
$select_query .= $where . " " . $condition;
$select_query .= " order by cbi_call_schedule.call_scheduling desc ";


$usermaster = $mysqli->prepare($select_query);
$usermaster->execute();
$usermaster->bind_result($total_records);
$usermaster->fetch();
$usermaster->close();

$select_query = "";
$select_query .= " select cbi_call_schedule.*, cdi_usermaster.user_full_name,cdi_usermaster.user_email_id, rishta_leadmaster.lead_allocated_to , rishta_leadmaster.contact_name, rishta_leadmaster.disposition ";
$select_query .= " from cbi_call_schedule inner join cdi_usermaster on cbi_call_schedule.call_added_by=cdi_usermaster.user_id ";
$select_query .= " inner join rishta_leadmaster on cbi_call_schedule.lead_id=rishta_leadmaster.lead_id ";
$select_query .= $where . " " . $condition;
$select_query .= " order by cbi_call_schedule.call_scheduling desc ";
$select_query .= " limit 100 OFFSET " . $skip;
$resultCallSchedule = $mysqli->prepare($select_query);
$resultCallSchedule->execute();
$resultCallSchedule = $resultCallSchedule->get_result();

$ar_salesMaster = array();
$salesMaster = $mysqli->prepare("select * from cdi_usermaster where user_type='sales_exec' order by user_creationdate desc");
$salesMaster->execute();
$salesResult = $salesMaster->get_result();
if (mysqli_num_rows($salesResult) > 0) {

    while ($rowData = mysqli_fetch_object($salesResult)) {

        $user_id = $rowData->user_id;
        $ar_salesMaster[$user_id] = $rowData->user_full_name;
    }
}
?>

<div class="row mtop20">
    <div class="col-md-6">
        <h4 class="subheading">Total Calls: <?php echo mysqli_num_rows($resultCallSchedule) ?></h4>
    </div>
    <div class="col-md-6 text-right" style="padding-bottom:10px;">
       <a href="call-scheduling.php" style="background-color:#7047ee;color:white; border-color:#7047ee;" id="" name="" class="btn btn primary ">Reset Search</a>
    </div>
</div>
<div class="row mtop10">
    <div class="col-md-12">
        <?php
        if (mysqli_num_rows($resultCallSchedule) > 0) {
            ?>
            <table class="table table-bordered table-striped">
                <thead>
                <tr>
                    <th>Sr.No</th>
                    <th>Lead Allocated To</th>
                    <th>Client Name</th>
                    <!--th>Email</th-->
                    <th>Lead Status</th>
                    <th>Schedule Date</th>
                    <!--th>Lead Created On</th-->
                    <th>View/Edit</th>
                </tr>
                </thead>
                <tbody>
                <?php
                $i = 0;
                while ($rowData = mysqli_fetch_object($resultCallSchedule)) {
                    $i++;
                    ?>
                    <tr class="text-center alert <?php echo $the_alert_class ?>">
                        <td><?php echo $i; ?></td>
                        <td class="text-center">
                            <?php
                                if(isset($ar_salesMaster) && !empty($ar_salesMaster)) {

                                    if(isset($rowData->lead_allocated_to) && !empty($rowData->lead_allocated_to)) {

                                        if(isset($ar_salesMaster[$rowData->lead_allocated_to]) && !empty($ar_salesMaster[$rowData->lead_allocated_to])) {

                                            echo $ar_salesMaster[$rowData->lead_allocated_to];
                                        }
                                    }
                                }
                            ?>
                        </td>
                        <td class="text-center"><?php echo $rowData->contact_name ?></td>
                        <!--td class="text-center"><//?php echo $rowData->user_email_id ?></td-->
                        <td class="text-center">
                            <?php 
                            echo $rowData->disposition ;
                           // $html = "&nbsp;";
                            //if (isset($rowData->disposition) && !empty($rowData->disposition)) {

                            //    if ($rowData->disposition == "call back") {
                                 //   $html = "Call Back";
                           //     }

                          //      if ($rowData->disposition == "prospect") {
                          //          $html = "Prospect";
                          //      }
                         //   }
                         //   echo $html;
                            ?>
                        </td>
                        <td class="text-center">
                            <?php
                            $html = "";
                            $html = date("d/m/Y", strtotime($rowData->call_scheduling));

                            if (isset($rowData->call_time_scheduling) && !empty($rowData->call_time_scheduling)) {

                                $html .= " " . date("h:i A", strtotime($rowData->call_time_scheduling));
                            }
                            echo $html;
                            ?>
                        </td>
                        <!--td><//?php echo $rowData->created_on ?></td-->
                        <td>
                            <a href="edit-leads.php?leadid=<?php echo $rowData->lead_id ?>" style="color:blue;">Edit/View</a>
                        </td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
            <div align="center">
                <nav>
                    <ul class="pagination">
                        <?php
                        $total = ceil($total_records / 10);
                        for ($i = 1; $i <= $total; $i++) {
                            ?>
                            <li><a  style="background-color:yellow;" onclick='getLeads(<?php echo $i ?>)' href="javascript:void(0)" class="pagelink <?php if ($i == $_POST['page_skip']) {
                                echo "active";
                                } ?>"><?php echo $i ?></a></li>
                            <?php
                        }
                        ?>
                    </ul>
                </nav>
            </div>

            <?php
        } else {
            ?>
            <div class="alert alert-danger">
                <strong>Warning!</strong> You have no leads.
            </div>
            <?php
        }
        ?>
    </div>
</div>