<?php include(APPPATH . 'views/layout/template-top.php'); ?>
<?php include(APPPATH . 'views/layout/header.php'); ?>
<!-- Main Container -->
<div class="main-container">
    <!-- <div class="pd-ltr-20 xs-pd-20-10"> -->
    <div class="xs-pd-20-10">
        <div class="min-height-200px">
            <!-- Page Header -->
            <div class="page-header mb-30">
                <div class="row">
                    <div class="col-md-6 col-sm-12">
                        <div class="title">
                            <h4>Lead Management</h4>
                        </div>
                        <nav aria-label="breadcrumb" role="navigation">
                            <ol class="breadcrumb">
                                <li class="breadcrumb-item"><a href="<?php echo base_url(); ?>">Home</a></li>
                                <li class="breadcrumb-item active" aria-current="page">Leads</li>
                            </ol>
                        </nav>
                    </div>
                    <div class="col-md-6 col-sm-12 text-right">
                        <a href="<?php echo base_url('leads/add'); ?>" class="btn btn-primary">
                            <i class="fa fa-plus"></i> Add New Lead
                        </a>
                    </div>
                </div>
            </div>
            <!-- Flash Messages -->
            <?php if($this->session->flashdata('success')): ?>
                <div class="alert alert-success alert-dismissible fade show flash-message" role="alert">
                    <?php echo $this->session->flashdata('success'); ?>
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            <?php endif; ?>
            <?php if($this->session->flashdata('error')): ?>
                <div class="alert alert-danger alert-dismissible fade show flash-message" role="alert">
                    <?php echo $this->session->flashdata('error'); ?>
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            <?php endif; 
                $this->session->unset_userdata('success');
                $this->session->unset_userdata('error');
            ?>
            <!-- Filters -->
            <div class="card-box mb-30">
                <div class="pd-20">
                    <h5 class="h5 mb-20">Filters</h5>
                    <form method="get" action="<?php echo base_url('leads'); ?>">
                        <div class="row">
                            <div class="col-md-2 mb-20">
                                <label>Status</label>
                                <select name="status" class="form-control">
                                    <option value="">All Status</option>
                                    <option value="New Lead" <?php echo ($this->input->get('status') == 'New Lead') ? 'selected' : ''; ?>>New Lead</option>
                                    <option value="Contacted" <?php echo ($this->input->get('status') == 'Contacted') ? 'selected' : ''; ?>>Contacted</option>
                                    <option value="Not Reachable" <?php echo ($this->input->get('status') == 'Not Reachable') ? 'selected' : ''; ?>>Not Reachable</option>
                                    <option value="Interested" <?php echo ($this->input->get('status') == 'Interested') ? 'selected' : ''; ?>>Interested</option>
                                    <option value="Not Interested" <?php echo ($this->input->get('status') == 'Not Interested') ? 'selected' : ''; ?>>Not Interested</option>

                                    <option value="Document Pending" <?php echo ($this->input->get('status') == 'Document Pending') ? 'selected' : ''; ?>>Document Pending</option>
                                    <option value="Login Done" <?php echo ($this->input->get('status') == 'Login Done') ? 'selected' : ''; ?>>Login Done</option>
                                    <option value="Under Process" <?php echo ($this->input->get('status') == 'Under Process') ? 'selected' : ''; ?>>Under Process</option>
                                    <option value="Sanctioned / Approved" <?php echo ($this->input->get('status') == 'Sanctioned / Approved') ? 'selected' : ''; ?>>Sanctioned / Approved</option>
                                    <option value="Disbursed / Account Opened" <?php echo ($this->input->get('status') == 'Disbursed / Account Opened') ? 'selected' : ''; ?>>Disbursed / Account Opened</option>
                                    <option value="Rejected" <?php echo ($this->input->get('status') == 'Rejected') ? 'selected' : ''; ?>>Rejected</option>
                                    <option value="Closed" <?php echo ($this->input->get('status') == 'Closed') ? 'selected' : ''; ?>>Closed</option>
                                    <option value="Completed" <?php echo ($this->input->get('status') == 'Completed') ? 'selected' : ''; ?>>Completed</option>
                                </select>
                            </div>
                            <div class="col-md-2 mb-20">
                                <label>Branch</label>
                                <select name="branch" class="form-control">
                                    <option value="">All Branch</option>
                                    <?php foreach($branches as $branch): ?>
                                        <option value="<?php echo $branch->branch_id; ?>"
                                            <?php echo ($this->input->get('branch') == $branch->branch_id) ? 'selected' : ''; ?>>
                                            <?php echo $branch->branch_name; ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-3 mb-20">
                                <label>Product</label>
                                <select name="product" class="form-control">
                                    <option value="">All Products</option>
                                    <?php foreach($products as $product): ?>
                                        <option value="<?php echo $product->product_id; ?>" <?php echo ($this->input->get('product') == $product->product_id) ? 'selected' : ''; ?>>
                                            <?php echo $product->product_name; ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-3 mb-20">
                                <label>Search</label>
                                <input type="text" name="search" class="form-control" placeholder="Name, Mobile, Email, Lead #" value="<?php echo $this->input->get('search'); ?>">
                            </div>
                            <div class="col-md-2 mb-20">
                                <label>&nbsp;</label>
                                <button type="submit" class="btn btn-primary btn-block">Filter</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <!-- Leads Table -->
            <div class="card-box mb-30">
                <div class="pd-20">
                    <h4 class="text-blue h4 mb-0">All Leads</h4>
                </div>
                <div class="pb-20">
                    <div class="table-responsive">
                        <table class="table tbl-lms-all-lead table-striped hover nowrap" id="leadsDataTable" style="width:100%">
                            <thead>
                                <tr>
                                    <th>Lead #</th>
                                    <th>Customer Name</th>
                                    <th>Product</th>
                                    <th>Status</th>
                                    <th>Priority</th>
                                    <th>Lead Source</th>
                                    <th>Executive</th>
                                    <th>Branch</th>
                                    <th>Next Follow-up</th>
                                    <th>Created Date</th>
                                    <th class="datatable-nosort">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if(!empty($leads)): ?>
                                    <?php foreach($leads as $lead): ?>
                                        <tr>
                                            <td><?php echo $lead->lead_number; ?></td>
                                            <td>
                                                <strong><?php echo $lead->customer_name; ?></strong><br>
                                                <small><?php echo $lead->customer_email; ?></small><br>
                                                <small><?php echo $lead->customer_mobile; ?></small>
                                            </td>
                                            <td><?php echo $lead->product_name; ?></td>
                                            <td>
                                                <?php 
                                                    $badge_class = '';
                                                    switch ($lead->lead_status) {
                                                        case 'New Lead':
                                                            $badge_class = 'badge-primary';
                                                            break;
                                                        case 'Contacted':
                                                            $badge_class = 'badge-info';
                                                            break;
                                                        case 'Not Reachable':
                                                            $badge_class = 'badge-dark';
                                                            break;
                                                        case 'Interested':
                                                            $badge_class = 'badge-success';
                                                            break;
                                                        case 'Not Interested':
                                                            $badge_class = 'badge-danger';
                                                            break;
                                                        case 'Document Pending':
                                                            $badge_class = 'badge-warning';
                                                            break;
                                                        case 'Login Done':
                                                            $badge_class = 'badge-secondary';
                                                            break;
                                                        case 'Under Process':
                                                            $badge_class = 'badge-purple';
                                                            break;
                                                        case 'Sanctioned / Approved':
                                                            $badge_class = 'badge-teal';
                                                            break;
                                                        case 'Disbursed / Account Opened':
                                                            $badge_class = 'badge-success';
                                                            break;
                                                        case 'Rejected':
                                                            $badge_class = 'badge-danger';
                                                            break;
                                                        case 'Closed':
                                                            $badge_class = 'badge-danger';
                                                            break;
                                                        case 'Completed':
                                                            $badge_class = 'badge-success';
                                                            break;
                                                        default:
                                                            $badge_class = 'badge-light';
                                                            break;
                                                    }
                                                ?>
                                                <span class="badge <?php echo $badge_class; ?>"><?php echo $lead->lead_status; ?></span>
                                            </td>
                                            <td>
                                                <?php 
                                                    $badge_class = '';
                                                    switch ($lead->lead_priority) {
                                                        case 'Hot Lead':
                                                            $badge_class = 'badge-danger';
                                                            break;
                                                        case 'Warm Lead':
                                                            $badge_class = 'badge-warning';
                                                            break;
                                                        case 'Cold Lead':
                                                            $badge_class = 'badge-info';
                                                            break;
                                                        default:
                                                            $badge_class = 'badge-light';
                                                            break;
                                                    }
                                                ?>
                                                <span class="badge <?php echo $badge_class; ?>"><?php echo $lead->lead_priority; ?></span>
                                            </td>
                                            <td>
                                                <?php 
                                                    if ($lead->lead_type != NULL) {
                                                ?>
                                                        <span class="badge badge-lead-type"><?php echo $lead->lead_type; ?></span>
                                                <?php
                                                    } else {
                                                ?>
                                                        -
                                                <?php
                                                    }
                                                ?>
                                                
                                            </td>
                                            <td><?php echo $lead->executive_name; ?></td>
                                            <td><?php echo $lead->branch_name; ?></td>
                                            <td>
                                                <?php if (!empty($lead->next_followup_date) && $lead->next_followup_date != '0000-00-00') : ?>
                                                    <?php echo date('d-M-Y', strtotime($lead->next_followup_date)); ?>
                                                <?php else: ?>
                                                    <span class="text-muted">-</span>
                                                <?php endif; ?>
                                            </td>
                                            <td><?php echo date('d-M-Y', strtotime($lead->created_at)); ?></td>
                                            <td>
                                                <div class="dropdown">
                                                    <a class="btn btn-link font-24 p-0 line-height-1 no-arrow dropdown-toggle" href="#" role="button" data-toggle="dropdown">
                                                        <i class="dw dw-more"></i>
                                                    </a>
                                                    <div class="dropdown-menu dropdown-menu-right dropdown-menu-icon-list">
                                                        <a class="dropdown-item" href="<?php echo base_url('leads/view/' . $lead->lead_id); ?>">
                                                            <i class="dw dw-eye"></i> View
                                                        </a>
                                                        <a class="dropdown-item" href="<?php echo base_url('leads/followup/' . $lead->lead_id); ?>">
                                                            <i class="dw dw-edit2"></i> Add Follow-up
                                                        </a>
                                                        <a class="dropdown-item" href="<?php echo base_url('leads/edit/' . $lead->lead_id); ?>">
                                                            <i class="dw dw-edit-2"></i> Edit
                                                        </a>
                                                        <?php if($this->session->userdata('role') == 'Admin' || $this->session->userdata('role') == 'Super Admin'): ?>
                                                            <a class="dropdown-item text-danger" href="<?php echo base_url('leads/delete/' . $lead->lead_id); ?>" onclick="return confirm('Are you sure you want to delete this lead?');">
                                                                <i class="dw dw-delete-3"></i> Delete
                                                            </a>
                                                        <?php endif; ?>
                                                    </div>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="11" class="text-center">No leads found</td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <?php include(APPPATH . 'views/layout/footer.php'); ?>
    </div>
</div>
<script>
$(document).ready(function() {
    // Auto-dismiss flash messages after 5 seconds
    setTimeout(function() {
        $('.flash-message').fadeOut('slow', function() {
            $(this).remove();
        });
    }, 5000);
});
</script>
<?php include(APPPATH . 'views/layout/template-bottom.php'); ?>
