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

$id=$_GET['userid'];

function getCount($tablename, $status, $where_condition = null)
{
    global $mysqli;
    if ($where_condition != null) {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where " . $status . " = 1 and " . $where_condition);
    } else {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where " . $status . " = 1");
    }

    $countmaster->execute();
    $countmaster->bind_result($count);
    $countmaster->fetch();
    $countmaster->close();
    return $count;
}

function getCountLeadsByLeadStatus($tablename, $type, $status, $e_id = 0)
{
    global $mysqli;

    if ($e_id == 0) {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and disposition = '" . $status . "'");
    } else {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and disposition = '" . $status . "' and lead_allocated_to=" . $e_id);
    }
    $countmaster->execute();
    $countmaster->bind_result($count);
    $countmaster->fetch();
    $countmaster->close();
    return $count;
}

function getCountLeads($tablename, $type, $e_id = 0)
{
    global $mysqli;

    if ($e_id == 0) {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "'");
    } else {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and lead_allocated_to=" . $e_id);
    }
    $countmaster->execute();
    $countmaster->bind_result($count);
    $countmaster->fetch();
    $countmaster->close();
    return $count;
}

function getCountMonthlyLeads($tablename, $type, $e_id = 0)
{
    global $mysqli;

    if ($e_id == 0) {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and MONTH(lead_created_on)=MONTH(now()) and YEAR(lead_created_on)=YEAR(now())");
    } else {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and MONTH(lead_created_on)=MONTH(now()) and YEAR(lead_created_on)=YEAR(now()) and lead_allocated_to=" . $e_id);
    }
    $countmaster->execute();
    $countmaster->bind_result($count);
    $countmaster->fetch();
    $countmaster->close();
    return $count;
}

function getCountMonthlyLeadsStatus($tablename, $type, $status, $e_id = 0)
{
    global $mysqli;

    if ($e_id == 0) {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and disposition = '" . $status . "' and MONTH(lead_created_on)=MONTH(now()) and YEAR(lead_created_on)=YEAR(now())");
    } else {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and disposition = '" . $status . "' and MONTH(lead_created_on)=MONTH(now()) and YEAR(lead_created_on)=YEAR(now()) and lead_allocated_to=" . $e_id);
    }
    $countmaster->execute();
    $countmaster->bind_result($count);
    $countmaster->fetch();
    $countmaster->close();
    return $count;
}

function getCountTodaysLeads($tablename, $type, $e_id = 0)
{
    global $mysqli;

    if ($e_id == 0) {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and DATE(lead_created_on) = CURDATE() ");
    } else {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and DATE(lead_created_on) = CURDATE() and lead_allocated_to=" . $e_id);
    }
    $countmaster->execute();
    $countmaster->bind_result($count);
    $countmaster->fetch();
    $countmaster->close();
    return $count;
}

function getCountTodaysLeadsStatus($tablename, $type, $status, $e_id = 0)
{
    global $mysqli;

    if ($e_id == 0) {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and disposition = '" . $status . "' and DATE(lead_created_on) = CURDATE() ");
    } else {
        $countmaster = $mysqli->prepare("select count(*) from " . $tablename . " where lead_type = '" . $type . "' and disposition = '" . $status . "' and DATE(lead_created_on) = CURDATE() and lead_allocated_to=" . $e_id);
    }
    $countmaster->execute();
    $countmaster->bind_result($count);
    $countmaster->fetch();
    $countmaster->close();
    return $count;
}

$usermaster = $mysqli->prepare("select *,(select count(*) from cdi_followups where lead_id = cdi_leadmaster.lead_id) as followcount from cdi_leadmaster order by lead_created desc limit 10");
$usermaster->execute();
$userresult = $usermaster->get_result();


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

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

function get_percentage($total, $leads)
{
    if ($total == 0) {
        return 0;
    }
    return number_format(($leads / $total) * 100, 2);
}


$ar_leads_data = array();
$ar_leads_data['new call'] = 'New Call';
$ar_leads_data['no match shared yet'] = 'No Match Shared yet';
$ar_leads_data['match shared'] = 'Match Shared';
$ar_leads_data['follow up'] = 'Follow up';
$ar_leads_data['close to conversion'] = 'Close to conversion';
$ar_leads_data['fully paid'] = 'Fully Paid';
$ar_leads_data['lead rejected'] = 'Lead Rejected';
$ar_leads_data['refund'] = 'Refund';
$ar_leads_data['fresh lead'] = 'Fresh Lead';
$ar_leads_data['renewal lead'] = 'Renewal Lead';
$ar_leads_data['ptential crm lead'] = 'Ptential CRM Lead';
$ar_leads_data['old leads'] = 'Old Leads';

$ar_alert_data = array('alert-info', 'alert-warning', 'alert-success', 'alert-danger');
?>
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta name="description" content="">
	<meta name="author" content="">
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
	
	<title>Pipeline - Rishta Depot</title>
	<?php include('./include/meta.php'); ?>
	<style>
		.table {
			display: block;
			overflow-x: auto;
			width: 100%;
		}
		.highlight {
            background-color:red ; /* Change this to the desired highlight color */
            color:#000;
            font-weight:1000;
        }
	</style>
	
<script>
    function selectUser() {
        // Get the selected option value
        var selectedUserId = $("#ddlUserId").val();

        // Remove highlight from all panels
        $(".panel-title").removeClass("highlight");

        // Highlight the selected user panel
        var userPanelId = "#userPanel" + selectedUserId;
        $(userPanelId).addClass("highlight");

        // Scroll to the corresponding user panel
        $('html, body').animate({
            scrollTop: $(userPanelId).offset().top
        }, 1000);
    }
</script>


   <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>



</head>
<?php include('./include/header.php'); ?>


<!-- Main content -->
   
    <div class="content-wrapper ">

        <div class="content-panel">
            	
            <div class='container-fluid'>
                   <div class="col-md-4 padd">
          <select id="ddlUserId" name="ddlUserId" class="form-select" onchange="selectUser()">
                            <option value="0">All Executives</option>
                            <?php
                            while ($e_userrow = mysqli_fetch_object($salesresults)) {
                                ?>
                                <option value="<?php echo $e_userrow->user_id ?>"><?php echo $e_userrow->user_full_name ?></option>
                                <?php
                            }
                            ?>
                        </select>
                </div>


                <div class='row'>
                    <div class='col-md-12'>
                        <h3>
                           Managers Pipeline Report
                        </h3>
                    </div>
                </div>
                <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">

                    <?php
                    $i = 0;
                    while ($salesrow = mysqli_fetch_object($salesresult)) {
                        ?>

                        <div class="panel panel-default" id="userPanels<?php echo $salesrow->user_id ?>">
                            <div class="panel-heading" role="tab" id="heading<?php echo $i ?>">
                                <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse<?php echo $i ?>" class="<?php echo $i == 0 ? '' : 'collapsed' ?>" aria-expanded="<?php echo $i == 0 ? 'true' : 'false' ?>"
                                   aria-controls="collapse<?php echo $i ?>">
                                    <h4 class="panel-title" id="userPanel<?php echo $salesrow->user_id ?>">
                                        <?php echo $salesrow->user_full_name . ' (' . $salesrow->user_email_id . ')' ?>
                                    </h4>
                                </a>
                            </div>
                            <div id="collapse<?php echo $i ?>" class="panel-collapse collapse <?php echo $i == 0 ? 'in' : '' ?>" role="tabpanel" aria-labelledby="heading<?php echo $i ?>">
                                <div class="panel-body">
                                    <div class='row'>
                                     
                                            <div class='col-md-4'>
                                                <table class='table table-responsive'>
                                                    <tr>
                                                        <td colspan='2' class='alert alert-myheading'>Total Leads</td>
                                                    </tr>
                                                    
                                                    
                                                    <tr class='alert alert-dark'>
                                <td style="color:white"><strong>Total Leads!</strong></td>
                                <?php
                                $total_leads = getCountLeads('rishta_leadmaster', 'rishta_lead', $salesrow->user_id);
                                // $total_leads = getCountLeads('rishta_leadmaster', 'rishta_lead');
                                $fullypaid= getCountLeadsByLeadStatus('rishta_leadmaster', 'rishta_lead', 'fully paid', $salesrow->user_id);
                                ?>
                           <td style="color:white"><?php echo $total_leads ?> Leads (Converted : <?php 
                           
                           $percentage = (($fullypaid * 100)/$total_leads);
                           
                           echo $percentage.'%';
                           ?>)
                           
                      </td>
                           
                            </tr>
                                                    
                                                    
                                                    <?php
                                                    if (isset($ar_leads_data) && !empty($ar_leads_data)) {

                                                        foreach ($ar_leads_data as $key => $value) {

                                                            $alert_class = $ar_alert_data[rand(0, 3)];

                                                            ?>
                                                            <tr class='alert <?php echo $alert_class; ?>'>
                                                                <td><strong><?php echo $value; ?></strong></td>
                                                                <td>
                                                                    <a href="filter-manage-rishta-leads.php?eid=<?php echo $salesrow->user_id ?>&type=<?php echo $key; ?>" class="alert-link">
                                                                        <div><?php echo getCountLeadsByLeadStatus('rishta_leadmaster', 'rishta_lead', $key, $salesrow->user_id) ?> Leads</div>
                                                                    </a>
                                                                </td>
                                                            </tr>
                                                        <?php }
                                                    }
                                                    ?>
                                                </table>
                                            </div>

                                            <div class='col-md-4'>
                                                <table class='table table-responsive'>
                                                    <tr>
                                                        <td colspan='2' class='alert alert-myheading'>Monthly Leads</td>
                                                    </tr>
                                                    <!--tr class='alert alert-dark'>
                                                        <td><strong>This Month Leads!</strong></td>
                                                        <!--?php
                                                        $total_leads = getCountMonthlyLeads('rishta_leadmaster', 'rishta_lead', $salesrow->user_id);
                                                        ?>
                                                        <td><!--?php echo $total_leads ?> Leads (Converted : <!--?php echo get_percentage($total_leads, (getCountMonthlyLeadsStatus('rishta_leadmaster', 'rishta_lead', 'deal'))) ?>%)</td>
                                                    </tr-->
                                                    
                                                    <tr class='alert alert-dark'>
                                <td style="color:white"><strong>This Month Leads!</strong></td>
                                <?php
                                $total_leads = getCountMonthlyLeads('rishta_leadmaster', 'rishta_lead', $salesrow->user_id);
                                // $total_leads = getCountLeads('rishta_leadmaster', 'rishta_lead');
                                $fullypaid= getCountMonthlyLeadsStatus('rishta_leadmaster', 'rishta_lead', 'fully paid', $salesrow->user_id);
                                ?>
                           <td style="color:white"><?php echo $total_leads ?> Leads (Converted : <?php 
                           
                           $percentage = (($fullypaid * 100)/$total_leads);
                           
                           echo $percentage.'%';
                           ?>)
                           
                      </td>
                           
                            </tr>
                                                    
                                                    <?php
                                                    if (isset($ar_leads_data) && !empty($ar_leads_data)) {

                                                        foreach ($ar_leads_data as $key => $value) {

                                                            $alert_class = $ar_alert_data[rand(0, 3)];

                                                            ?>
                                                            <tr class='alert <?php echo $alert_class; ?>'>
                                                                <td><strong><?php echo $value; ?></strong></td>
                                                                <td>
                                                                    <a href="1month.php?eid=<?php echo $salesrow->user_id ?>&type=<?php echo $key; ?>" class="alert-link">
                                                                        <div><?php echo getCountMonthlyLeadsStatus('rishta_leadmaster', 'rishta_lead', $key, $salesrow->user_id) ?> Leads</div>
                                                                    </a>
                                                                </td>
                                                            </tr>
                                                        <?php }
                                                    }
                                                    ?>
                                                </table>
                                            </div>

                                            <div class='col-md-4'>
                                                <table class='table table-responsive table table-striped table-hover'>
                                                    <tr>
                                                        <td colspan='2' class='alert alert-myheading'>Today Leads</td>
                                                    </tr>
                                                    <!--tr class='alert alert-dark'>
                                                        <td><strong>Today Leads!</strong></td>
                                                        <!--?php
                                                        $total_leads = getCountTodaysLeads('rishta_leadmaster', 'rishta_lead', $salesrow->user_id);
                                                        ?>
                                                        <td><!--?php echo $total_leads ?> Leads (Converted : <!--?php echo get_percentage($total_leads, (getCountTodaysLeadsStatus('rishta_leadmaster', 'rishta_lead', 'deal'))) ?>%)</td>
                                                    </tr-->
                                                    
                                                    <tr class='alert alert-dark'>
                                <td style="color:white"><strong>Today's Leads</strong></td>
                                <?php
                                $total_leads = getCountTodaysLeads('rishta_leadmaster', 'rishta_lead', $salesrow->user_id);
                                // $total_leads = getCountLeads('rishta_leadmaster', 'rishta_lead');
                                $fullypaid= getCountTodaysLeadsStatus('rishta_leadmaster', 'rishta_lead', 'fully paid', $salesrow->user_id);
                                ?>
                           <td style="color:white"><?php echo $total_leads ?> Leads (Converted : <?php 
                           
                           $percentage = (($fullypaid * 100)/$total_leads);
                           
                           echo $percentage.'%';
                           ?>)
                           
                      </td>
                           
                            </tr>
                                                    
                                                    
                                                    <?php
                                                    if (isset($ar_leads_data) && !empty($ar_leads_data)) {

                                                        foreach ($ar_leads_data as $key => $value) {

                                                            $alert_class = $ar_alert_data[rand(0, 3)];

                                                            ?>
                                                            <tr class='alert <?php echo $alert_class; ?>'>
                                                                <td><strong><?php echo $value; ?></strong></td>
                                                                <td>
                                                                    <a href="1total.php?eid=<?php echo $salesrow->user_id ?>&type=<?php echo $key; ?>" class="alert-link">
                                                                        <div><?php echo getCountTodaysLeadsStatus('rishta_leadmaster', 'rishta_lead', $key, $salesrow->user_id) ?> Leads</div>
                                                                    </a>
                                                                </td>
                                                            </tr>
                                                        <?php }
                                                    }
                                                    ?>
                                                </table>
                                            </div>

                                    </div>
                                </div>
                            </div>
                        </div>

                        <?php
                        $i++;
                    }
                    ?>

                </div>
            </div>
        </div>
    </div>

<style>
    .padd{
        padding-top:40px;
        padding-left:20px;
    }
</style>


						<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
						<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>



						<!-- /.content-wrapper -->

						<?php include('./include/footer.php'); ?>