<?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 if (isset($_POST['txtSearch']) && trim($_POST['txtSearch']) != null) {
    $pass = 1;
} else if (isset($_POST['countryId']) && trim($_POST['countryId']) != null) {
    $pass = 1;
} else if (isset($_POST['stateId']) && trim($_POST['stateId']) != null) {
    $pass = 1;
} else if (isset($_POST['cityId']) && trim($_POST['cityId']) != null) {
    $pass = 1;
} else if (isset($_POST['searchGender']) && trim($_POST['searchGender']) != null) {
    $pass = 1;
} else if (isset($_POST['searchMemberStatus']) && trim($_POST['searchMemberStatus']) != null) {
    $pass = 1;
} else if (isset($_POST['searchCaste']) && trim($_POST['searchCaste']) != null) {
    $pass = 1;
} else if (isset($_POST['searchResidencyStatus']) && trim($_POST['searchResidencyStatus']) != null) {
    $pass = 1;
} else if (isset($_POST['searchCurrency']) && trim($_POST['searchCurrency']) != null) {
    $pass = 1;
} else if (isset($_POST['searchDisposition']) && trim($_POST['searchDisposition']) != null) {
    $pass = 1;
} else if (isset($_POST['searchMinAge']) && trim($_POST['searchMinAge']) != null) {
    $pass = 1;
} else if (isset($_POST['searchMaxAge']) && trim($_POST['searchMaxAge']) != null) {
    $pass = 1;
} else if (isset($_POST['searchMinFootHeight']) && trim($_POST['searchMinFootHeight']) != null) {
    $pass = 1;
} else if (isset($_POST['searchMinInchHeight']) && trim($_POST['searchMinInchHeight']) != null) {
    $pass = 1;
} else if (isset($_POST['searchMaxFootHeight']) && trim($_POST['searchMaxFootHeight']) != null) {
    $pass = 1;
} else if (isset($_POST['searchMaxInchHeight']) && trim($_POST['searchMaxInchHeight']) != null) {
    $pass = 1;
}else if (isset($_POST['religion']) && trim($_POST['religion']) != 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 .= " date_time >= '{$fromdate}' ";
        }
    }

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

        if (trim($_POST['txtToDate']) != null) {
            $condition .= " date_time <= '{$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->extension;

$extension =$_POST['ddlUserId']; 


$query = "select count(*) from rishta_new_api where (source = '$extension' AND $condition ) OR (destination = '$extension'  AND $condition) order by id desc";
$usermaster = $mysqli->prepare($query);
$usermaster->execute();
$usermaster->bind_result($total_records);
$usermaster->fetch();
$usermaster->close();







//$query = "select *,(select count(*) from rishta_new_api where lead_id = rishta_leadmaster.lead_id) as followcount  from rishta_leadmaster " . $where . " " . $condition . " order by lead_created desc limit 10 offset " . $skip;

//$query = ("select * from rishta_new_api where (destination = '$extension') OR (source = '$extension') OR (dst_channel LIKE '%$extension%') AND  ($condition) ");

$query = ("select * from rishta_new_api where (source = '$extension' AND $condition ) OR (destination = '$extension'  AND $condition)  order by id desc limit 10 offset " . $skip);
$usermaster = $mysqli->prepare($query);
$usermaster->execute();
$userresult = $usermaster->get_result();

$total_row=mysqli_num_rows($userresult);

$querys = ("select source,(SELECT sum(duration)) as total  from rishta_new_api where (source = '$extension') AND  ($condition)");
$usermasters = $mysqli->prepare($querys);
$usermasters->execute();
$userresults = $usermasters->get_result();



$queryss = ("select source,(SELECT sum(duration)) as totals  from rishta_new_api where (source = '$extension') AND  ($condition)");
$usermasterss = $mysqli->prepare($queryss);
$usermasterss->execute();
$userresultss = $usermasterss->get_result();

$queryss = ("select destination,(SELECT sum(duration)) as stotals  from rishta_new_api where (destination = '$extension') AND  ($condition)");
$usermasterss = $mysqli->prepare($queryss);
$usermasterss->execute();
$userresultsss = $usermasterss->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();
?>

<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 Duration:<?php while ($row = mysqli_fetch_assoc($userresultsss))
{ 
    $stotals=$row['stotals'];

     
   
  //echo gmdate("H:i:s", $new);;
  
 //echo gmdate('H:i:s', $new);
  // $now= $row['total'];
   
  // echo date("Y-m-d h:i:s", strtotime($now));
   
 //   echo strtotime("$now"), "\n";
    
   //  echo date("d/M/Y H:i:s", $now), "\n";
}

?>
<?php while ($row = mysqli_fetch_assoc($userresultss))
{ 
    $totals=$row['totals'];

     $new=  $stotals +  $totals;
   
  //echo gmdate("H:i:s", $new);;
  
 echo gmdate('H:i:s', $new);
  // $now= $row['total'];
   
  // echo date("Y-m-d h:i:s", strtotime($now));
   
 //   echo strtotime("$now"), "\n";
    
   //  echo date("d/M/Y H:i:s", $now), "\n";
}

?>
</h4>
    </div>
    
                <div class="col-md-6">
        <h4 class="subheading">Average Call:<?php 

    $avg=  mysqli_num_rows($userresult);
     $news= $new/$avg;
     echo gmdate('H:i:s', $news);
// $new= $row['totals'] / $total_row;
  
  //echo gmdate("H:i:s", $new);
  
 

?>
</h4>
    </div>

</div>
<div class="row mtop10">
    <div class="col-md-12">
        <?php
        if (mysqli_num_rows($userresult) > 0) {
            ?>
            <table class="table table-responsive table-stripped table-bordered">
                <thead>
          	<tr>
														<th>Sr.No</th>
														<th>Destination</th>
														<th>Source</th>
														<th>Source Channel</th>
														<th>DST Channel</th>
														<th>Status</th>
														<th>Duration</th>
														<th>Date</th>
														<th>DB 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->destination ?></td>
																<td><?php echo $userrow->source ?></td>
																<td><?php echo $userrow->src_channel ?></td>
																<td><?php echo $userrow->dst_channel ?></td>
																<td><?php echo $userrow->status ?></td>
																<td><?php echo $userrow->duration ?> sec</td>
																<td><?php echo $userrow->date ?></td>
																	<td><?php echo $userrow->date_time ?></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 leads.
            </div>
            <?php
        }
        ?>
    </div>
</div>