<?php include('layout/template-top.php'); ?>
<?php include('layout/header.php'); ?>
<!-- Main Container -->
<div class="main-container">
    <div class="md-pd-ltr-20">
        <!-- Welcome Banner -->
        <div class="card-box pd-20 height-100-p mb-30">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <h4 class="font-20 weight-500 mb-10 text-capitalize md-wlc-title">
                        Welcome back, <span class="weight-600 font-30 text-blue md-wlc-user-title"><?php echo $this->session->userdata('name'); ?>!</span>
                    </h4>
                    <p class="font-18 max-width-600 mb-0 md-user-details">
                        <strong>Branch:</strong> <?php echo $this->session->userdata('branch_name'); ?> 
                        <br>
                        <?php 
                            if ($this->session->userdata('name') == 'Digital Banking Dept - PPB') {
                        ?>
                                <strong>Role:</strong> Digital Banking Dept - PPB
                        <?php
                            } elseif ($this->session->userdata('name') == 'Arundhatti Shingate') {
                        ?>
                                <strong>Role:</strong> Digital Banking Dept.
                        <?php
                            } else {
                        ?>
                                <strong>Role:</strong> <?php echo $this->session->userdata('role'); ?>
                        <?php
                            }
                        ?>
                    </p>
                </div>
                <div class="col-md-4">
                    <img src="<?php echo base_url('assets/images/banner-img.png'); ?>" class="dashboard-image" alt="">
                </div>
            </div>
        </div>
        <!-- Lead Statistics -->
        <div class="row mb-30">
            <div class="col-md-3 md-mb-30">
                <div class="card-box pd-30 height-100-p" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 12px;">
                    <div class="d-flex justify-content-between align-items-center h-100">
                        <div class="text-white">
                            <div class="weight-700 font-30 mb-2">
                                <?php echo $lead_stats['Total']; ?>
                            </div>
                            <div class="font-16 weight-500">Total Leads</div>
                        </div>
                        <div class="text-white" style="font-size: 50px; opacity: 0.25;">
                            <i class="fa fa-users"></i>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3 md-mb-30">
                <div class="card-box pd-30 height-100-p" style="background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%); border-radius: 12px;">
                    <div class="d-flex justify-content-between align-items-center h-100">
                        <div class="text-white">
                            <div class="weight-700 font-30 mb-2">
                                <?php echo $lead_stats['New Lead']; ?>
                            </div>
                            <div class="font-16 weight-500">New Leads</div>
                        </div>
                        <div class="text-white" style="font-size: 50px; opacity: 0.25;">
                            <i class="fa fa-user-plus"></i>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3 md-mb-30">
                <div class="card-box pd-30 height-100-p" style="background: linear-gradient(135deg, #56ab2f 0%, #a8e063 100%); border-radius: 12px;">
                    <div class="d-flex justify-content-between align-items-center h-100">
                        <div class="text-white">
                            <div class="weight-700 font-30 mb-2">
                                <?php echo $lead_stats['Completed']; ?>
                            </div>
                            <div class="font-16 weight-500">Completed</div>
                        </div>
                        <div class="text-white" style="font-size: 50px; opacity: 0.25;">
                            <i class="fa fa-check"></i>
                        </div>
                    </div>
                </div>
            </div>
            <!-- <div class="col-md-3 md-mb-30">
                <div class="card-box pd-30 height-100-p" style="background: linear-gradient(135deg, #36d1dc 0%, #5b86e5 100%); border-radius: 12px;">
                    <div class="d-flex justify-content-between align-items-center h-100">
                        <div class="text-white">
                            <div class="weight-700 font-30 mb-2">
                                <?php echo $lead_stats['Closed']; ?>
                            </div>
                            <div class="font-16 weight-500">Closed</div>
                        </div>
                        <div class="text-white" style="font-size: 50px; opacity: 0.25;">
                            <i class="fa fa-lock"></i>
                        </div>
                    </div>
                </div>
            </div> -->
            <div class="col-md-3">
                <div class="card-box pd-30 height-100-p" style="background: linear-gradient(135deg, #ff9966 0%, #ff5e62 100%); border-radius: 12px;">
                    <div class="d-flex justify-content-between align-items-center h-100">
                        <div class="text-white">
                            <div class="weight-700 font-30 mb-2">
                                <?php echo $lead_stats['Rejected']; ?>
                            </div>
                            <div class="font-16 weight-500">Rejected</div>
                        </div>
                        <div class="text-white" style="font-size: 50px; opacity: 0.25;">
                            <i class="fa fa-times-circle"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <?php 
                if ($this->session->userdata('role') == 'Admin') {
            ?>
                    <!-- Product-wise Leads -->
                    <div class="col-xl-6 mb-30">
                        <div class="card-box height-100-p pd-20">
                            <h2 class="h4 leads-tbl-title">Product-wise Leads</h2>
                            <div class="table-responsive">
                                <table class="table table-hover table-striped tbl-dashboard-product-count" id="productStatsTable">
                                    <thead>
                                        <tr>
                                            <th>Sr.No</th>
                                            <th>Product Name</th>
                                            <th class="text-right">Count</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if (!empty($product_stats)):
                                                $i = 1;
                                        ?>
                                            <?php foreach ($product_stats as $stat): ?>
                                                <tr>
                                                    <td><?php echo $i++; ?></td>
                                                    <td><?php echo $stat->product_name; ?></td>
                                                    <td class="text-right"><span class="badge badge-primary"><?php echo $stat->count; ?></span></td>
                                                </tr>
                                            <?php endforeach; ?>
                                        <?php else: ?>
                                            <tr>
                                                <td colspan="3" class="text-center">No data available</td>
                                            </tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!-- Branch-wise Leads -->
                    <div class="col-xl-6 mb-30">
                        <div class="card-box height-100-p pd-20">
                            <h2 class="h4 leads-tbl-title">Branch-wise Leads</h2>
                            <div class="table-responsive">
                                <table id="branchLeadsDataTable" class="table table-hover table-striped tbl-dashboard-branch-count">
                                    <thead>
                                        <tr>
                                            <th>Sr.No</th>
                                            <th>Branch Name</th>
                                            <th class="text-right">Count</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if (!empty($branch_stats)):
                                                $i = 1;
                                        ?>
                                            <?php foreach ($branch_stats as $bstat): ?>
                                                <tr>
                                                    <td><?php echo $i++; ?></td>
                                                    <td><?php echo $bstat->branch_name; ?></td>
                                                    <td class="text-right"><span class="badge badge-primary"><?php echo $bstat->count; ?></span></td>
                                                </tr>
                                            <?php endforeach; ?>
                                        <?php else: ?>
                                            <tr>
                                                <td colspan="3" class="text-center">No data available</td>
                                            </tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!-- Upcoming Follow-ups (Next 7 Days) -->
                    <div class="col-xl-6 mb-30">
                        <div class="card-box height-100-p pd-20">
                            <h2 class="h4 leads-tbl-title">Upcoming Follow-ups (Next 7 Days)</h2>
                            <div class="table-responsive">
                                <table class="table table-striped" id="upcomingFollowupsTable">
                                    <thead>
                                        <tr>
                                            <th>Lead #</th>
                                            <th>Customer</th>
                                            <th>Date</th>
                                            <th class="datatable-nosort">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if (!empty($upcoming_followups)): ?>
                                            <?php foreach ($upcoming_followups as $followup): ?>
                                                <tr>
                                                    <td><?php echo $followup->lead_number; ?></td>
                                                    <td><?php echo $followup->customer_name; ?></td>
                                                    <td><?php echo date('d-M-Y', strtotime($followup->next_followup_date)); ?></td>
                                                    <td>
                                                        <a href="<?php echo base_url('leads/view/' . $followup->lead_id); ?>" class="btn btn-sm btn-primary">
                                                            <i class="fa fa-eye"></i>
                                                        </a>
                                                    </td>
                                                </tr>
                                            <?php endforeach; ?>
                                        <?php else: ?>
                                            <tr>
                                                <td colspan="4" class="text-center">No upcoming follow-ups</td>
                                            </tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!-- Lead Sources -->
                    <div class="col-xl-6 mb-30">
                        <div class="card-box height-100-p pd-20">
                            <h2 class="h4 leads-tbl-title">Lead Sources</h2>
                            <div class="table-responsive">
                                <table class="table table-striped" id="productStatsTable">
                                    <thead>
                                        <tr>
                                            <th>Sources Name</th>
                                            <th class="text-right">Count</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td><i class="fa fa-building mr-1"></i>Branches</td>
                                            <td class="text-right"><span class="badge badge-primary">12</span></td>
                                        </tr>
                                        <tr>
                                            <td><i class="fa fa-phone mr-1"></i>Tele Sales</td>
                                            <td class="text-right"><span class="badge badge-primary">6</span></td>
                                        </tr>
                                        <tr>
                                            <td><i class="fa fa-globe mr-1"></i>Website</td>
                                            <td class="text-right"><span class="badge badge-primary">24</span></td>
                                        </tr>
                                        <tr>
                                            <td><i class="fa fa-file-text mr-1"></i>Landing Page</td>
                                            <td class="text-right"><span class="badge badge-primary">14</span></td>
                                        </tr>
                                        <tr>
                                            <td><i class="fa fa-comments mr-1"></i>AI Chatbot (Mitram)</td>
                                            <td class="text-right"><span class="badge badge-primary">10</span></td>
                                        </tr>
                                        <tr>
                                            <td><i class="fa fa-whatsapp mr-1"></i>WAC (WhatsApp Connect)</td>
                                            <td class="text-right"><span class="badge badge-primary">41</span></td>
                                        </tr>
                                        <tr>
                                            <td><i class="fa fa-search mr-1"></i>Search Campaigns</td>
                                            <td class="text-right"><span class="badge badge-primary">43</span></td>
                                        </tr>
                                        <tr>
                                            <td><i class="fa fa-map-marker mr-1"></i>GMB (Google My Business)</td>
                                            <td class="text-right"><span class="badge badge-primary">3</span></td>
                                        </tr>
                                        <tr>
                                            <td><i class="fa fa-instagram mr-1"></i>Instagram Campaigns</td>
                                            <td class="text-right"><span class="badge badge-primary">10</span></td>
                                        </tr>
                                        <tr>
                                            <td><i class="fa fa-youtube-play mr-1"></i>YouTube Campaigns</td>
                                            <td class="text-right"><span class="badge badge-primary">1</span></td>
                                        </tr>
                                        <tr>
                                            <td><i class="fa fa-linkedin mr-1"></i>LinkedIn Campaigns</td>
                                            <td class="text-right"><span class="badge badge-primary">22</span></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!-- Branch & Product-wise MIS -->
                    <div class="col-xl-12 mb-30">
                        <div class="card-box height-100-p pd-20">
                            <div class="d-flex justify-content-between align-items-center mb-20">
                                <h2 class="h4 leads-tbl-title">Branch & Product-wise MIS</h2>
                                <a href="<?php echo base_url('download-branch-product-mis'); ?>" class="btn btn-success btn-sm">Download CSV</a>
                            </div>
                            <div class="table-responsive">
                                <table id="misLeadsDataTable" class="table tbl-branch-mis-lead table-striped hover" style="width:100%">
                                    <thead>
                                        <tr>
                                            <th>Sr. No</th>
                                            <th>Branch Name</th>
                                            <th>Product Name</th>
                                            <th>Total Leads</th>
                                            <th>New Leads</th>
                                            <th>Closed</th>
                                            <th>Completed</th>
                                            <th>Rejected</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if (!empty($branch_product_mis_stats)) : ?>
                                            <?php $i = 1; ?>
                                            <?php foreach ($branch_product_mis_stats as $row) : ?>
                                                <tr>
                                                    <td><?php echo $i++; ?></td>
                                                    <td><?php echo $row->branch_name; ?></td>
                                                    <td><?php echo $row->product_name; ?></td>
                                                    <td>
                                                        <span class="badge badge-primary">
                                                            <?php echo $row->total_leads; ?>
                                                        </span>
                                                    </td>
                                                    <td>
                                                        <span class="badge badge-info">
                                                            <?php echo $row->new_leads; ?>
                                                        </span>
                                                    </td>
                                                    <td>
                                                        <span class="badge badge-warning">
                                                            <?php echo $row->closed_leads; ?>
                                                        </span>
                                                    </td>
                                                    <td>
                                                        <span class="badge badge-success">
                                                            <?php echo $row->completed_leads; ?>
                                                        </span>
                                                    </td>
                                                    <td>
                                                        <span class="badge badge-danger">
                                                            <?php echo $row->rejected_leads; ?>
                                                        </span>
                                                    </td>
                                                </tr>
                                            <?php endforeach; ?>
                                        <?php else : ?>
                                            <tr>
                                                <td colspan="8" class="text-center">
                                                    No data available
                                                </td>
                                            </tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
            <?php
                } else {
            ?>
                    <!-- Product-wise Leads -->
                    <div class="col-xl-6 mb-30">
                        <div class="card-box height-100-p pd-20">
                            <h2 class="h4 leads-tbl-title">Product-wise Leads</h2>
                            <div class="table-responsive">
                                <table class="table table-hover table-striped tbl-dashboard-product-count" id="productStatsTable">
                                    <thead>
                                        <tr>
                                            <th>Sr.No</th>
                                            <th>Product Name</th>
                                            <th class="text-right">Count</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if (!empty($product_stats)):
                                                $i = 1;
                                        ?>
                                            <?php foreach ($product_stats as $stat): ?>
                                                <tr>
                                                    <td><?php echo $i++; ?></td>
                                                    <td><?php echo $stat->product_name; ?></td>
                                                    <td class="text-right"><span class="badge badge-primary"><?php echo $stat->count; ?></span></td>
                                                </tr>
                                            <?php endforeach; ?>
                                        <?php else: ?>
                                            <tr>
                                                <td colspan="3" class="text-center">No data available</td>
                                            </tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!-- Upcoming Follow-ups (Next 7 Days) -->
                    <div class="col-xl-6 mb-30">
                        <div class="card-box height-100-p pd-20">
                            <h2 class="h4 leads-tbl-title">Upcoming Follow-ups (Next 7 Days)</h2>
                            <div class="table-responsive">
                                <table class="table table-striped" id="upcomingFollowupsTable">
                                    <thead>
                                        <tr>
                                            <th>Lead #</th>
                                            <th>Customer</th>
                                            <th>Date</th>
                                            <th class="datatable-nosort">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if (!empty($upcoming_followups)): ?>
                                            <?php foreach ($upcoming_followups as $followup): ?>
                                                <tr>
                                                    <td><?php echo $followup->lead_number; ?></td>
                                                    <td><?php echo $followup->customer_name; ?></td>
                                                    <td><?php echo date('d-M-Y', strtotime($followup->next_followup_date)); ?></td>
                                                    <td>
                                                        <a href="<?php echo base_url('leads/view/' . $followup->lead_id); ?>" class="btn btn-sm btn-primary">
                                                            <i class="fa fa-eye"></i>
                                                        </a>
                                                    </td>
                                                </tr>
                                            <?php endforeach; ?>
                                        <?php else: ?>
                                            <tr>
                                                <td colspan="4" class="text-center">No upcoming follow-ups</td>
                                            </tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!-- Branch & Product-wise MIS -->
                    <div class="col-xl-12 mb-30">
                        <div class="card-box height-100-p pd-20">
                            <div class="d-flex justify-content-between align-items-center mb-20">
                                <h2 class="h4 leads-tbl-title">Branch & Product-wise MIS</h2>
                                <a href="<?php echo base_url('download-branch-product-mis'); ?>" class="btn btn-success btn-sm">Download CSV</a>
                            </div>
                            <div class="table-responsive">
                                <table id="misLeadsDataTable" class="table tbl-branch-mis-lead table-striped hover" style="width:100%">
                                    <thead>
                                        <tr>
                                            <th>Sr. No</th>
                                            <th>Branch Name</th>
                                            <th>Product Name</th>
                                            <th>Total Leads</th>
                                            <th>New Leads</th>
                                            <th>Closed</th>
                                            <th>Completed</th>
                                            <th>Rejected</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if (!empty($branch_product_mis_stats)) : ?>
                                            <?php $i = 1; ?>
                                            <?php foreach ($branch_product_mis_stats as $row) : ?>
                                                <tr>
                                                    <td><?php echo $i++; ?></td>
                                                    <td><?php echo $row->branch_name; ?></td>
                                                    <td><?php echo $row->product_name; ?></td>
                                                    <td>
                                                        <span class="badge badge-primary">
                                                            <?php echo $row->total_leads; ?>
                                                        </span>
                                                    </td>
                                                    <td>
                                                        <span class="badge badge-info">
                                                            <?php echo $row->new_leads; ?>
                                                        </span>
                                                    </td>
                                                    <td>
                                                        <span class="badge badge-warning">
                                                            <?php echo $row->closed_leads; ?>
                                                        </span>
                                                    </td>
                                                    <td>
                                                        <span class="badge badge-success">
                                                            <?php echo $row->completed_leads; ?>
                                                        </span>
                                                    </td>
                                                    <td>
                                                        <span class="badge badge-danger">
                                                            <?php echo $row->rejected_leads; ?>
                                                        </span>
                                                    </td>
                                                </tr>
                                            <?php endforeach; ?>
                                        <?php else : ?>
                                            <tr>
                                                <td colspan="8" class="text-center">
                                                    No data available
                                                </td>
                                            </tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
            <?php
                }
            ?>
        </div>
        <!-- Quick Actions -->
        <div class="card-box mb-30">
            <div class="pd-20">
                <h2 class="h4 mb-20">Quick Actions</h2>
                <div class="row">
                    <div class="col-md-3 mb-20">
                        <a href="<?php echo base_url('leads/add'); ?>" class="btn btn-primary btn-block">
                            <i class="fa fa-plus"></i> Add New Lead
                        </a>
                    </div>
                    <div class="col-md-3 mb-20">
                        <a href="<?php echo base_url('leads'); ?>" class="btn btn-info btn-block">
                            <i class="fa fa-list"></i> View All Leads
                        </a>
                    </div>
                    <div class="col-md-3 mb-20">
                        <a href="<?php echo base_url('leads?status=Follow-up'); ?>" class="btn btn-warning btn-block">
                            <i class="fa fa-refresh"></i> Follow-up Leads
                        </a>
                    </div>
                    <div class="col-md-3 mb-20">
                        <a href="<?php echo base_url('leads?status=Converted'); ?>" class="btn btn-success btn-block">
                            <i class="fa fa-check"></i> Converted Leads
                        </a>
                    </div>
                </div>
            </div>
        </div>
        <?php include('layout/footer.php'); ?>
    </div>
</div>
<script>
$(document).ready(function() {
    // Product Stats Table
    $('#productStatsTable').DataTable({
        "paging": false,
        "searching": false,
        "info": false,
        "ordering": true,
        "order": [[1, "desc"]]
    });
    // Upcoming Follow-ups Table
    $('#upcomingFollowupsTable').DataTable({
        "paging": false,
        "searching": false,
        "info": false,
        "ordering": true,
        "order": [[2, "asc"]],
        columnDefs: [{
            targets: "datatable-nosort",
            orderable: false,
        }]
    });
});
</script>
<?php include('layout/template-bottom.php'); ?>
