<?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['txtSearch']) && trim($_POST['txtSearch']) != null) {
    $pass = 1;
} 
else {
    $pass = 0;
}


if ($pass == 1) {

    $where = 'where';



    if (trim($_POST['txtSearch']) != null) {
        $search_data = trim($_POST['txtSearch']);
        $condition .= trim($condition) != null ? ' and ' : ' ';
        $condition .= " (contactnumber	 like '%{$search_data}%' or callduration like '%{$search_data}%')";
    }

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

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

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

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


$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->extension3;

$extension =$_POST['ddlUserId'];	
	

$query = "select count(*) from rishta_3cx_calls " . $where . " " . $condition . " ";
$usermaster = $mysqli->prepare($query);
$usermaster->execute();
$usermaster->bind_result($total_records);
$usermaster->fetch();
$usermaster->close();

$duration='07:00';

//$query = "select *,(select count(*) from rishta_3cx_calls where id = rishta_3cx_calls.id) as followcount  from rishta_3cx_calls " . $where . " " . $condition . " order by id desc limit 50 offset " . $skip;
//$query = "select *,(select count(*) from cdi_followups where id = rishta_3cx_calls.id) as followcount  from rishta_3cx_calls " . $where . " " . $condition . " order by id desc limit 10 offset " . $skip;

$usermaster = $mysqli->prepare("select * from  rishta_3cx_calls where (agentextension ='$extension') AND (callduration > '$duration') AND ($condition) order by id desc limit 10  offset " . $skip);
$usermaster->execute();
$userresult = $usermaster->get_result();

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

$totalDuration = 0;

$durationQuery = "
    SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(STR_TO_DATE(callduration, '%i:%s')))) AS total_duration 
    FROM rishta_3cx_calls 
    WHERE agentextension = ? 
      AND STR_TO_DATE(callduration, '%i:%s') > STR_TO_DATE(?, '%i:%s') 
      AND ($condition)
";

$durationStmt = $mysqli->prepare($durationQuery);
$durationStmt->bind_param("ss", $extension, $duration);
$durationStmt->execute();
$durationResult = $durationStmt->get_result();

if ($row = $durationResult->fetch_assoc()) {
    $formattedDuration = $row['total_duration']; // Already in HH:MM:SS
} else {
    $formattedDuration = "00:00:00";
}

?>
<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 class="col-md-6">
       <h4 class="subheading">Total Call Duration: <?php echo $formattedDuration; ?></h4>

    </div>

</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>Contact Number</th>
														<th>Call Type</th>
														<th>Duration</th>	
														<th>Date</th>
														
													</tr>
                </thead>
                <tbody>
                <?php
                $i = 0;
                while ($userrow = mysqli_fetch_object($userresult)) {
                  $i++;
                    ?>
             		<tr class="text-center">
																<td><?php echo $i; ?></td>
																<td class="text-left"><?php echo $userrow->	contactnumber ?></td>
																<td><?php echo $userrow->calltype ?></td>
																<td><?php echo $userrow->callduration ?></td>
																<td><?php $dateString = "$userrow->calldatetime";
																
																$dateTime = DateTime::createFromFormat('m/d/Y H:i', $dateString);

// Format as "Month DD, YYYY HH:MM"
$formattedDate = $dateTime->format('F d, Y H:i');

echo $formattedDate;
																
																
																?></td>
															</tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
            <!--div align="center">
                <nav>
                    <ul class="pagination">
                     <?php
                   //     $total = ceil($total_records / 100);
                //        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>
