<?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="min-height-200px">
            <!-- Page Header -->
            <div class="page-header mb-30">
                <div class="row">
                    <div class="col-md-12 col-sm-12">
                        <div class="title">
                            <h4>Add Follow-up</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"><a href="<?php echo base_url('leads'); ?>">Leads</a></li>
                                <li class="breadcrumb-item"><a href="<?php echo base_url('leads/view/' . $lead->lead_id); ?>">Lead Details</a></li>
                                <li class="breadcrumb-item active" aria-current="page">Add Follow-up</li>
                            </ol>
                        </nav>
                    </div>
                </div>
            </div>
            <div class="row">
                <!-- Lead Summary -->
                <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 mb-30">
                    <div class="card-box pd-20">
                        <h5 class="text-blue h5 mb-10">Lead Summary</h5>
                        <div class="profile-info pd-0">
                            <ul>
                                <li>
                                    <span>Lead Number:</span>
                                    <strong><?php echo $lead->lead_number; ?></strong>
                                </li>
                                <li>
                                    <span>Customer Name:</span>
                                    <?php echo $lead->customer_name; ?>
                                </li>
                                <li>
                                    <span>Mobile:</span>
                                    <?php echo $lead->customer_mobile; ?>
                                </li>
                                <li>
                                    <span>Product:</span>
                                    <?php echo $lead->product_name; ?>
                                </li>
                                <li>
                                    <span>Current Status:</span>
                                    <span class="badge badge-primary lms-lead-followup-badge"><?php echo $lead->lead_status; ?></span>
                                </li>
                                <li>
                                    <span>Created Date:</span>
                                    <?php echo date('d-M-Y', strtotime($lead->created_at)); ?>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
                <!-- Follow-up Form -->
                <div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 mb-30">
                    <div class="card-box height-100-p pd-20">
                        <h5 class="text-blue h5 mb-20">Follow-up Details</h5>
                        <?php if($this->session->flashdata('error')): ?>
                            <div class="alert alert-danger alert-dismissible fade show" 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');
                        ?>
                        <form method="post" action="<?php echo base_url('leads/followup/' . $lead->lead_id); ?>">
                            <div class="form-group">
                                <label>Current Status</label>
                                <input type="text" class="form-control" value="<?php echo $lead->lead_status; ?>" readonly>
                            </div>
                            <div class="form-group row">
                                <div class="col-md-6">
                                   <label>Update Status <span class="text-danger">*</span></label>
                                    <select class="form-control" name="updated_status" id="updated_status" required>
                                        <option value="">Select Status</option>
                                        <option value="New Lead" <?php echo set_select('updated_status', 'New Lead', ($lead->updated_status == 'New Lead')); ?>>New Lead</option>
                                        <option value="Contacted" <?php echo set_select('updated_status', 'Contacted', ($lead->updated_status == 'Contacted')); ?>>Contacted</option>
                                        <option value="Not Reachable" <?php echo set_select('updated_status', 'Not Reachable', ($lead->updated_status == 'Not Reachable')); ?>>Not Reachable</option>
                                        <option value="Interested" <?php echo set_select('updated_status', 'Interested', ($lead->updated_status == 'Interested')); ?>>Interested</option>
                                        <option value="Not Interested" <?php echo set_select('updated_status', 'Not Interested', ($lead->updated_status == 'Not Interested')); ?>>Not Interested</option>
                                        <option value="Document Pending" <?php echo set_select('updated_status', 'Document Pending', ($lead->updated_status == 'Document Pending')); ?>>Document Pending</option>
                                        <option value="Login Done" <?php echo set_select('updated_status', 'Login Done', ($lead->updated_status == 'Login Done')); ?>>Login Done</option>
                                        <option value="Under Process" <?php echo set_select('updated_status', 'Under Process', ($lead->updated_status == 'Under Process')); ?>>Under Process</option>
                                        <option value="Sanctioned / Approved" <?php echo set_select('updated_status', 'Sanctioned / Approved', ($lead->updated_status == 'Sanctioned / Approved')); ?>>Sanctioned / Approved</option>
                                        <option value="Disbursed / Account Opened" <?php echo set_select('updated_status', 'Disbursed / Account Opened', ($lead->updated_status == 'Disbursed / Account Opened')); ?>>Disbursed / Account Opened</option>
                                        <option value="Rejected" <?php echo set_select('updated_status', 'Rejected', ($lead->updated_status == 'Rejected')); ?>>Rejected</option>
                                        <option value="Closed" <?php echo set_select('updated_status', 'Closed', ($lead->updated_status == 'Closed')); ?>>Closed</option>
                                        <option value="Completed" <?php echo set_select('updated_status', 'Completed', ($lead->updated_status == 'Completed')); ?>>Completed</option>
                                    </select>
                                    <?php echo form_error('updated_status', '<small class="text-danger">', '</small>'); ?>
                                </div>
                                <div class="col-md-6">
                                   <label>Update Lead Priority <span class="text-danger">*</span></label>
                                    <select class="form-control" name="lead_priority" id="lead_priority" required>
                                        <option value="">Select Priority</option>
                                        <option value="Hot Lead" <?php echo set_select('lead_priority', 'Hot Lead', ($lead->lead_priority == 'Hot Lead')); ?>>Hot Lead</option>
                                        <option value="Warm Lead" <?php echo set_select('lead_priority', 'Warm Lead', ($lead->lead_priority == 'Warm Lead')); ?>>Warm Lead</option>
                                        <option value="Cold Lead" <?php echo set_select('lead_priority', 'Cold Lead', ($lead->lead_priority == 'Cold Lead')); ?>>Cold Lead</option>
                                    </select>
                                    <?php echo form_error('updated_status', '<small class="text-danger">', '</small>'); ?>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>Follow-up Notes <span class="text-danger">*</span></label>
                                <textarea class="form-control" name="followup_notes" rows="6" required placeholder="Enter detailed notes about this follow-up..."><?php echo set_value('followup_notes', $lead->followup_notes); ?></textarea>
                                <?php echo form_error('followup_notes', '<small class="text-danger">', '</small>'); ?>
                                <small class="form-text text-muted">
                                    Include details such as:
                                    <ul class="mb-0">
                                        <li>Customer response and feedback</li>
                                        <li>Discussion points</li>
                                        <li>Concerns or objections raised</li>
                                        <li>Documents requested or submitted</li>
                                        <li>Next action items</li>
                                    </ul>
                                </small>
                            </div>
                            <div class="form-group" id="">
                                <label>Next Follow-up Date</label>
                                <input type="date" class="form-control" name="next_followup_date" id="next_followup_date" min="<?php echo date('Y-m-d'); ?>" value="<?php echo set_value('next_followup_date'); ?>">
                                <small class="form-text text-muted">Leave blank if no further follow-up is needed</small>
                            </div>
                            <div class="form-group">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fa fa-save mr-1"></i> Save Follow-up
                                </button>
                                <a href="<?php echo base_url('leads/view/' . $lead->lead_id); ?>" class="btn btn-secondary">
                                    <i class="fa fa-times"></i> Cancel
                                </a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <!-- Follow-up History -->
            <div class="row">
                <div class="col-12 mb-30">
                    <div class="card-box pd-20">
                        <h5 class="text-blue h5 mb-20">Previous Follow-ups</h5>
                        <?php 
                        $followups = $this->Leadmodel->getLeadFollowups($lead->lead_id);
                        if(!empty($followups)): 
                        ?>
                            <div class="table-responsive">
                                <table class="table table-striped">
                                    <thead>
                                        <tr>
                                            <th>Date</th>
                                            <th>By</th>
                                            <th>Previous Status</th>
                                            <th>Updated Status</th>
                                            <th>Notes</th>
                                            <th>Next Follow-up</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach($followups as $followup): ?>
                                            <tr>
                                                <td><?php echo date('d-M-Y h:i A', strtotime($followup->followup_date)); ?></td>
                                                <td><?php echo $followup->followup_by_name; ?></td>
                                                <td><span class="badge badge-secondary"><?php echo $followup->previous_status; ?></span></td>
                                                <td>
                                                    <?php 
                                                    $badge_class = '';
                                                    switch($followup->updated_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 $followup->updated_status; ?></span>
                                                </td>
                                                <td><?php echo nl2br(substr($followup->followup_notes, 0, 100)) . (strlen($followup->followup_notes) > 100 ? '...' : ''); ?></td>
                                                <td>
                                                    <?php if($followup->next_followup_date): ?>
                                                        <?php echo date('d-M-Y', strtotime($followup->next_followup_date)); ?>
                                                    <?php else: ?>
                                                        <span class="text-muted">-</span>
                                                    <?php endif; ?>
                                                </td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php else: ?>
                            <p class="text-muted">No previous follow-ups recorded.</p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
        <?php include(APPPATH . 'views/layout/footer.php'); ?>
    </div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
    const statusSelect = document.getElementById('updated_status');
    const nextFollowupDiv = document.getElementById('next_followup_div');
    const nextFollowupInput = document.getElementById('next_followup_date');
    statusSelect.addEventListener('change', function() {
        const status = this.value;
        // Show next follow-up date only for "Follow-up" status
        if (status === 'Follow-up') {
            nextFollowupDiv.style.display = 'block';
            nextFollowupInput.required = true;
        } else {
            nextFollowupDiv.style.display = 'none';
            nextFollowupInput.required = false;
            nextFollowupInput.value = '';
        }
        // Add helpful messages based on status
        const notesField = document.querySelector('textarea[name="followup_notes"]');
        let placeholder = 'Enter detailed notes about this follow-up...';
        switch(status) {
            case 'Converted':
                placeholder = 'Congratulations! Please provide details about the conversion, such as loan amount sanctioned, account opened, etc.';
                break;
            case 'Lost to Competition':
                placeholder = 'Please provide details about why the lead was lost to competition, which competitor, and any feedback from the customer.';
                break;
            case 'Plan Dropped':
                placeholder = 'Please provide reasons why the customer dropped their plan and any feedback received.';
                break;
            case 'Follow-up':
                placeholder = 'Provide details about the discussion, customer feedback, concerns raised, and next steps planned.';
                break;
        }
        notesField.placeholder = placeholder;
    });
    // Trigger change event on page load if status is already selected
    if (statusSelect.value) {
        statusSelect.dispatchEvent(new Event('change'));
    }
});
</script>
<?php include(APPPATH . 'views/layout/template-bottom.php'); ?>
