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

// Initialize variables
$where = '';
$condition = '';
$skip = 0;

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

// Check if any filter is applied
$pass = isset($_POST['ddlUserId']) || isset($_POST['ddlLeadType']) || isset($_POST['txtFromDate']) || isset($_POST['txtToDate']) || isset($_POST['txtSearch']) || isset($_POST['countryId']) || isset($_POST['stateId']) || isset($_POST['cityId']) || isset($_POST['searchGender']) || isset($_POST['searchMemberStatus']) || isset($_POST['searchCaste']) || isset($_POST['searchResidencyStatus']) || isset($_POST['searchCurrency']) || isset($_POST['searchDisposition']) || isset($_POST['searchMinAge']) || isset($_POST['searchMaxAge']) || isset($_POST['searchMinFootHeight']) || isset($_POST['searchMinInchHeight']) || isset($_POST['searchMaxFootHeight']) || isset($_POST['searchMaxInchHeight']) || isset($_POST['religion']);

if ($pass == 1) {
    $where = 'where';
    
    $fromdate = date('Y-m-d H:i:s', strtotime($_POST['txtFromDate']));
    $todate = date('Y-m-d H:i:s', strtotime($_POST['txtToDate']));

    // Add conditions based on posted data
    if (!empty($_POST['txtFromDate'])) {
        $fromdate = $_POST['txtFromDate'];
        $condition .= !empty($condition) ? ' and ' : ' ';
        $condition .= "calldatetime >= '{$fromdate}'";
    }

    if (!empty($_POST['txtToDate'])) {
        $todate = $_POST['txtToDate'];
        $condition .= !empty($condition) ? ' and ' : ' ';
        $condition .= "calldatetime <= '{$todate}'";
    }
}

// Retrieve total records count
$query = "select count(*) from rishta_3cx_calls $where $condition order by id desc";
$usermaster = $mysqli->prepare($query);
$usermaster->execute();
$usermaster->bind_result($total_records);
$usermaster->fetch();
$usermaster->close();

// Retrieve user details
$memberdetailmaster = $mysqli->prepare("select * from cdi_usermaster where user_id=?");
$memberdetailmaster->bind_param("i", $_GET['userid']);
$memberdetailmaster->execute();
$memberdetailmaster = $memberdetailmaster->get_result();
$userrow = mysqli_fetch_object($memberdetailmaster);
$extensions = $userrow->extension;
$extension = $_POST['ddlUserId'];
$duration = '07:00';

// Retrieve user data based on conditions
$usermaster = $mysqli->prepare("select *,(select count(*) from rishta_3cx_calls where (agentextension = cdi_usermaster.extension3) AND (calltype='Inbound') AND ($condition)) as incount,(select count(*) from rishta_3cx_calls where (agentextension = cdi_usermaster.extension3) AND (calltype='Outbound') AND ($condition)) as outcount,(select count(*) from rishta_3cx_calls where (agentextension = cdi_usermaster.extension3) AND (calltype='Missed') AND ($condition)) as misscall,(select count(*) from rishta_3cx_calls where (agentextension = cdi_usermaster.extension3) AND (calltype='Notanswered') AND ($condition)) as notanswered,(select count(contactnumber) from rishta_3cx_calls where (agentextension = cdi_usermaster.extension3) AND (calltype='Outbound') OR (calltype='Inbound') AND ($condition)) as freash,(select count(*) from rishta_3cx_calls where (agentextension = cdi_usermaster.extension3) AND (callduration > '$duration') AND ($condition)) as more from cdi_usermaster order by user_id desc");
$usermaster->execute();
$userresult = $usermaster->get_result();
$total_row = mysqli_num_rows($userresult);

// Retrieve sales executives data
$salesmaster = $mysqli->prepare("select * from cdi_usermaster where user_type='sales_exec' order by user_creationdate desc");
$salesmaster->execute();
$salesresult = $salesmaster->get_result();
?>


<div class="row mtop20">
    <div class="col-md-12">
         <div class="col-md-6">
        <h4 class="subheading">No. of Calls: <?php echo mysqli_num_rows($userresult) ?></h4>
    </div>
  
    
 

</div>
<div class="row mtop10">
    <div class="col-md-12">
        <?php
        if (mysqli_num_rows($userresult) > 0) {
            ?>
            <table id="example1"  class="table table-bordered table-striped">
                <thead>
          	<tr>
													<th>Sr.No</th>
														<th>Agent Name</th>
															<th>Inbound</th>
														<th>Outbound</th>
															<th>Not Answered</th>
															<th>Missed Calls</th>
														<!--th>Freash</th-->
															<th>7+</th>
													</tr>
                </thead>
                <tbody>
                <?php
                $i = 0;
                while ($userrow = mysqli_fetch_object($userresult)) {
                  $i++;
                    ?>
             		<tr class="text-center">
																	<td><?php echo $i; ?></td>
																
														 <td><a href="dialers_report.php?userid=<?php echo $userrow->user_id ?>"><?php echo $userrow->	user_full_name ?></a></td>			
												
																<td class="text-left"><?php echo $userrow-> incount ?></td>
																<td class="text-left"><?php echo  $userrow-> outcount ?></td>
																	<td class="text-left"><?php echo  $userrow-> notanswered ?></td>
																		<td class="text-left"><?php echo  $userrow-> misscall ?></td>
																	<!--td class="text-left"><?//php echo  $userrow-> freash ?></td-->
																		<td class="text-left"><?php echo  $userrow-> more ?></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 onclick='getLeadsPagination(<?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 calls.
            </div>
            <?php
        }
        ?>
    </div>
</div>

<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  
   	<script src="../../../lead-crm/src/new/js/vendors.min.js"></script>
	<script src="../../../lead-crm/src/js/pages/chat-popup.js"></script>
    <script src="../../../lead-crm/assets/icons/feather-icons/feather.min.js"></script>
	<script src="../../../lead-crm/assets/vendor_components/echarts/dist/echarts-en.min.js"></script>
	<script src="../../../lead-crm/assets/vendor_components/apexcharts-bundle/dist/apexcharts.js"></script>

		
	<script src="../../../lead-crm/assets/vendor_components/datatable/datatables.min.js"></script>
	

    <script src="../../../lead-crm/assets/vendor_components/Flot/jquery.flot.js"></script>
	<script src="../../../lead-crm/assets/vendor_components/Flot/jquery.flot.resize.js"></script>
	<script src="../../../lead-crm/assets/vendor_components/Flot/jquery.flot.pie.js"></script>
	<script src="../../../lead-crm/assets/vendor_components/Flot/jquery.flot.categories.js"></script>

	<script src="../../../lead-crm/assets/vendor_components/Flot/jquery.flot.js"></script> 
	<script src="../../../lead-crm/src/js/pages/sampledata1.js"></script>
	<script src="../../../lead-crm/src/js/pages/sampledata2.js"></script>    
	<script src="../../../lead-crm/assets/vendor_components/apexcharts-bundle/irregular-data-series.js"></script>
	<script src="../../../lead-crm/assets/vendor_components/jquery.peity/jquery.peity.js"></script>
	
	<!-- CRMi App -->
	<script src="../../../lead-crm/src/js/pages/reports.js"></script>
	<script src="../../../lead-crm/src/js/template.js"></script> 
 
	<!-- CRMi App -->
	<script src="../../../lead-crm/src/js/pages/analysis.js"></script>
	
	<!-- CRMi App -->
	<!--script src="../../../lead-crm/src/js/pages/dashboard.js"></script-->

	<script src="../../../lead-crm/src/js/pages/data-table.js"></script> 

	
	<!-- CRMi App -->
	
	<script src="../../../lead-crm/src/js/pages/widget1-flot-charts.js"></script>
	<script src="../../../lead-crm/src/js/pages/flot-chart.js"></script>
	<script src="../../../lead-crm/src/js/pages/flot-chart1.js"></script>

	
		<!-- Vendor JS -->
    <script src="../../../lead-crm/assets/vendor_components/raphael/raphael.min.js"></script>
	<script src="../../../lead-crm/assets/vendor_components/morris.js/morris.min.js"></script>

	<!-- CRMi App -->
	
	<script src="../../../lead-crm/src/js/pages/widget-morris-charts.js"></script>
	<script src="../../../lead-crm/assets/vendor_components/chartist-js-develop/chartist.js"></script>
	
	<!-- CRMi App -->

	<script src="../../../lead-crm/src/js/pages/widget-chartist1.js"></script>
	<script src="../../../lead-crm/assets/vendor_components/echarts/dist/echarts-en.min.js"></script>
	
	<!-- CRMi App -->
	<script src="../../../lead-crm/src/js/pages/echart-bar.js"></script>  
	<script src="../../../lead-crm/assets/vendor_components/Flot/jquery.flot.pie.js"></script>