<?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'];

$r=06;


$duration='07:00';

$from_date=$_POST['txtFromDate'];
$to_date=$_POST['txtToDate'];

//$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) ");

$usermaster = $mysqli->prepare("select *,(select count(*) from rishta_leadmaster where 	lead_allocated_to = rishta_target.user_id AND (lead_created >= '".$from_date."' AND lead_created <= '".$to_date."')) as leadcount

from rishta_target  where MONTH=MONTH(now()) and YEAR=YEAR(now())  ");
$usermaster->execute();
$userresult = $usermaster->get_result();

//$total_row=mysqli_num_rows($userresult);






$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 class="table table-responsive table-stripped table-bordered">
                <thead>
          	<tr>
											    <th>Sr.No</th>
                                          <th>Name</th>
                                        <th>Target</th>
                                        <th>Month & Year</th>
                                        <th>Total Leads</th>
                                        <th>Achievements</th>
                                        <th>Pending</th>
                                        <th>Percentage</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="view_report.php?userid=<?php echo $userrow->user_id ?>"><?php
																	    
																	    $user= $userrow->user_id;
																	    
																	    $salesmaster = $mysqli->prepare("select * from cdi_usermaster where user_id=' $user'");
                                                                        $salesmaster->execute();
                                                                        $salesresult = $salesmaster->get_result();
                                                                        
                                                                        $salerow=mysqli_fetch_object($salesresult);
																	    
																	   
																	  echo  $salerow->	user_full_name; 
																	    
																	    ?></a></td>
												<td class="text-left"><?php echo $userrow-> target ?></td>
																<td class="text-left"><?php echo date('M Y'); ?></td>
															
																	<td class="text-left"><?php echo  $userrow-> leadcount ?></td>
																		<td class="text-left"><?php echo  $userrow-> paidcount ?></td>
																			<td class="text-left"><?php echo  $userrow-> pencount ?></td>
																				<td class="text-left"><?php 
																				$total_leads=  $userrow-> leadcount;
																				$total_achievements=  $userrow-> paidcount;
																			  $percentage = 0;
                                        if(!empty($total_leads)) {

                                            if(!empty($total_achievements)) {

                                                $percentage = (($total_achievements * 100)/$total_leads);
                                            }
                                        }
                                        $percentage = number_format($percentage, 2, '.', '');
                                        echo $percentage ; 
                                        ?>%
																				
																				
																				</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>