<?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-6 col-sm-12">
                        <div class="title">
                            <h4>Lead Details</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 active" aria-current="page">Lead Details</li>
                            </ol>
                        </nav>
                    </div>
                    <div class="col-md-6 col-sm-12 text-right">
                        <a href="<?php echo base_url('leads/followup/' . $lead->lead_id); ?>" class="btn btn-warning">
                            <i class="fa fa-plus"></i> Add Follow-up
                        </a>
                        <a href="<?php echo base_url('leads/edit/' . $lead->lead_id); ?>" class="btn btn-primary">
                            <i class="fa fa-edit"></i> Edit 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; 
            $this->session->unset_userdata('success');
            $this->session->unset_userdata('error');
            ?>
            <!-- Lead Information -->
            <div class="row">
                <div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 mb-30">
                    <div class="card-box height-100-p overflow-hidden">
                        <div class="profile-tab height-100-p">
                            <div class="tab height-100-p">
                                <ul class="nav nav-tabs customtab" role="tablist">
                                    <li class="nav-item">
                                        <a class="nav-link active" data-toggle="tab" href="#basic_info" role="tab">Basic Info</a>
                                    </li>
                                    <li class="nav-item">
                                        <a class="nav-link" data-toggle="tab" href="#product_details" role="tab">Product Details</a>
                                    </li>
                                    <li class="nav-item">
                                        <a class="nav-link" data-toggle="tab" href="#followup_history" role="tab">Follow-up History</a>
                                    </li>
                                </ul>
                                <div class="tab-content">
                                    <!-- Basic Info Tab -->
                                    <div class="tab-pane fade show active" id="basic_info" role="tabpanel">
                                        <div class="pd-20">
                                            <div class="profile-info">
                                                <h5 class="mb-10 h5 text-blue">Lead Information</h5>
                                                <ul class="lead-details">
                                                    <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>Email:</span>
                                                        <?php echo $lead->customer_email ? $lead->customer_email : '-'; ?>
                                                    </li>
                                                    <li>
                                                        <span>Mobile:</span>
                                                        <?php echo $lead->customer_mobile; ?>
                                                    </li>
                                                    <li>
                                                        <span>Existing Customer:</span>
                                                        <?php echo $lead->is_existing_customer; ?>
                                                    </li>
                                                    <?php if($lead->is_existing_customer == 'Yes'): ?>
                                                        <!-- <li>
                                                            <span>Relationship Number:</span>
                                                            <?php echo $lead->relationship_number ? $lead->relationship_number : '-'; ?>
                                                        </li> -->
                                                        <li>
                                                            <span>Account Number:</span>
                                                            <?php echo $lead->account_number ? $lead->account_number : '-'; ?>
                                                        </li>
                                                    <?php endif; ?>
                                                    <li>
                                                        <span>Product:</span>
                                                        <strong><?php echo $lead->product_name; ?></strong>
                                                    </li>
                                                    <li>
                                                        <span>Branch:</span>
                                                        <?php echo $lead->branch_name; ?>
                                                    </li>
                                                    <li>
                                                        <span>Executive:</span>
                                                        <?php echo $lead->executive_name; ?>
                                                    </li>
                                                    <li>
                                                        <span>Lead Source Type:</span>
                                                        <span class="badge badge-lead-source"><?php echo $lead->customer_lead_source_type; ?></span>
                                                    </li>
                                                    <li>
                                                        <span>Lead Source Channel:</span>
                                                        <span class="badge badge-lead-channel"><?php echo $lead->customer_lead_source_channel; ?></span>
                                                    </li>
                                                    <li>
                                                        <span>Lead Campaign Name:</span>
                                                        <span class="badge badge-lead-campaign"><?php echo $lead->customer_lead_campaign_name; ?></span>
                                                    </li>
                                                    <li>
                                                        <span>Lead Referrer Detail:</span>
                                                        <span class="badge badge-lead-referrer"><?php echo $lead->customer_lead_referrer_detail; ?></span>
                                                    </li>
                                                    <li>
                                                        <span>Created Date:</span>
                                                        <?php echo date('d-M-Y h:i A', strtotime($lead->created_at)); ?>
                                                    </li>
                                                    <li>
                                                        <span>Last Updated:</span>
                                                        <?php echo date('d-M-Y h:i A', strtotime($lead->updated_at)); ?>
                                                    </li>
                                                </ul>
                                            </div>
                                        </div>
                                    </div>
                                    <!-- Product Details Tab -->
                                    <div class="tab-pane fade" id="product_details" role="tabpanel">
                                        <div class="pd-20">
                                            <h5 class="mb-20 h5 text-blue"><?php echo $lead->product_name; ?> Details</h5>
                                            <ul class="profile-info">
                                                <?php 
                                                    if($lead->product_id == 1) : 
                                                        // Home Loans
                                                ?>
                                                        <li><span>Property Value: </span>₹ <?php echo $lead->property_value ? number_format($lead->property_value, 2) . ' Lakhs' : '-'; ?></li>
                                                        <li><span>Loan Amount: </span>₹ <?php echo $lead->loan_amount ? number_format($lead->loan_amount, 2) . ' Lakhs' : '-'; ?></li>
                                                        <li><span>Income Source: </span><?php echo $lead->income_source ? $lead->income_source : '-'; ?></li>
                                                        <li><span>Expected Purchase: </span><?php echo $lead->expected_purchase_month && $lead->expected_purchase_year ? $lead->expected_purchase_month . ' ' . $lead->expected_purchase_year : '-'; ?></li>
                                                        <li><span>Property Identified: </span><?php echo $lead->property_identified ? $lead->property_identified : '-'; ?></li>
                                                        <li><span>Property Address: </span><?php echo $lead->property_address ? $lead->property_address : '-'; ?></li>
                                                <?php 
                                                    elseif($lead->product_id == 2) : 
                                                        // Car Loans
                                                ?>
                                                        <li><span>Car Value: </span>₹ <?php echo $lead->car_value ? number_format($lead->car_value, 2) . ' Lakhs' : '-'; ?></li>
                                                        <li><span>Car Make: </span><?php echo $lead->car_make ? $lead->car_make : '-'; ?></li>
                                                        <li><span>Car Grade/Model: </span><?php echo $lead->car_grade ? $lead->car_grade : '-'; ?></li>
                                                        <li><span>Loan Amount: </span>₹ <?php echo $lead->loan_amount ? number_format($lead->loan_amount, 2) . ' Lakhs' : '-'; ?></li>
                                                        <li><span>Income Source: </span><?php echo $lead->income_source ? $lead->income_source : '-'; ?></li>
                                                        <li><span>Expected Purchase: </span><?php echo $lead->expected_purchase_month && $lead->expected_purchase_year ? $lead->expected_purchase_month . ' ' . $lead->expected_purchase_year : '-'; ?></li>
                                                <?php 
                                                    elseif($lead->product_id == 3) : 
                                                        // Two Wheeler Loans
                                                ?>
                                                        <li><span>Two Wheeler Value: </span>₹ <?php echo $lead->two_wheeler_value ? number_format($lead->two_wheeler_value, 2) : '-'; ?></li>
                                                        <li><span>Two Wheeler Make: </span><?php echo $lead->two_wheeler_make ? $lead->two_wheeler_make : '-'; ?></li>
                                                        <li><span>Two Wheeler Grade/Model: </span><?php echo $lead->two_wheeler_grade ? $lead->two_wheeler_grade : '-'; ?></li>
                                                        <li><span>Loan Amount: </span>₹ <?php echo $lead->loan_amount ? number_format($lead->loan_amount, 2) . ' Lakhs' : '-'; ?></li>
                                                        <li><span>Income Source: </span><?php echo $lead->income_source ? $lead->income_source : '-'; ?></li>
                                                        <li><span>Expected Purchase: </span><?php echo $lead->expected_purchase_month && $lead->expected_purchase_year ? $lead->expected_purchase_month . ' ' . $lead->expected_purchase_year : '-'; ?></li>
                                                <?php 
                                                    elseif($lead->product_id == 4 || $lead->product_id == 12 || $lead->product_id == 13 || $lead->product_id == 14) : 
                                                        // Fixed Deposits
                                                ?>
                                                        <li><span>Deposit Amount: </span>₹ <?php echo $lead->deposit_amount ? number_format($lead->deposit_amount, 2) : '-'; ?></li>
                                                        <li><span>Tenure: </span><?php echo $lead->tenure_days ? $lead->tenure_days . ' Days' : '-'; ?></li>
                                                        <li><span>Interest Rate: </span><?php echo $lead->interest_rate ? $lead->interest_rate . '%' : '-'; ?></li>
                                                        <li><span>Maturity Value: </span>₹ <?php echo $lead->maturity_value ? number_format($lead->maturity_value, 2) : '-'; ?></li>
                                                        <li><span>Creation Date: </span><?php echo $lead->creation_month && $lead->creation_year ? $lead->creation_month . ' ' . $lead->creation_year : '-'; ?></li>
                                                <?php 
                                                    elseif($lead->product_id == 5 || $lead->product_id == 11) : 
                                                        // Savings Account
                                                ?>
                                                        <li><span>Account Creation Date: </span><?php echo $lead->account_creation_month && $lead->account_creation_year ? $lead->account_creation_month . ' ' . $lead->account_creation_year : '-'; ?></li>
                                                <?php 
                                                    elseif($lead->product_id == 6) : 
                                                        // MSME Loan
                                                ?>
                                                        <li><span>Business Name: </span><?php echo $lead->msme_business_name ? $lead->msme_business_name : '-'; ?></li>
                                                        <li><span>Business Type: </span><?php echo $lead->msme_business_type ? $lead->msme_business_type : '-'; ?></li>
                                                        <li><span>Business Vintage: </span><?php echo $lead->msme_business_vintage ? $lead->msme_business_vintage : '-'; ?></li>
                                                        <li><span>Annual Turnover: </span>₹ <?php echo $lead->msme_business_annual_turnover ? number_format($lead->msme_business_annual_turnover, 2) . ' Lakhs' : '-'; ?></li>
                                                        <li><span>Required Loan Amount: </span>₹ <?php echo $lead->msme_business_loan_amount ? number_format($lead->msme_business_loan_amount, 2) . ' Lakhs' : '-'; ?></li>
                                                        <li><span>Purpose of Loan: </span><?php echo $lead->msme_business_loan_purpose ? $lead->msme_business_loan_purpose : '-'; ?></li>
                                                <?php 
                                                    elseif($lead->product_id == 7) : 
                                                        // Education Loan
                                                ?>
                                                        <li><span>Course Name: </span><?php echo $lead->course_name ? $lead->course_name : '-'; ?></li>
                                                        <li><span>College / University: </span><?php echo $lead->college_name ? $lead->college_name : '-'; ?></li>
                                                        <li><span>Study Location: </span><?php echo $lead->study_location ? $lead->study_location : '-'; ?></li>
                                                        <li><span>Course Fee: </span>₹ <?php echo $lead->course_fee ? number_format($lead->course_fee, 2) . ' Lakhs' : '-'; ?></li>
                                                        <li><span>Required Loan Amount: </span>₹ <?php echo $lead->course_loan_amount ? number_format($lead->course_loan_amount, 2) . ' Lakhs' : '-'; ?></li>
                                                <?php 
                                                    elseif($lead->product_id == 8) : 
                                                        // MSME Loan
                                                ?>
                                                        <li><span>Property Type: </span><?php echo $lead->mortgage_property_type ? $lead->mortgage_property_type : '-'; ?></li>
                                                        <li><span>Property Value: </span>₹ <?php echo $lead->mortgage_property_value ? number_format($lead->mortgage_property_value, 2) . ' Lakhs' : '-'; ?></li>
                                                        <li><span>Property Location: </span><?php echo $lead->mortgage_property_location ? $lead->mortgage_property_location : '-'; ?></li>
                                                        <li><span>Required Loan Amount: </span>₹ <?php echo $lead->mortgage_loan_amount ? number_format($lead->mortgage_loan_amount, 2) . ' Lakhs' : '-'; ?></li>
                                                        <li><span>Income Source: </span><?php echo $lead->mortgage_income_source ? $lead->mortgage_income_source : '-'; ?></li>
                                                        <li><span>Purpose of Loan: </span><?php echo $lead->mortgage_loan_purpose ? $lead->mortgage_loan_purpose : '-'; ?></li>
                                                <?php 
                                                    endif;
                                                ?>
                                                <li><span>Remarks: </span><?php echo $lead->remarks ? nl2br($lead->remarks) : '-'; ?></li>
                                            </ul>
                                        </div>
                                    </div>
                                    <!-- Follow-up History Tab -->
                                    <div class="tab-pane fade" id="followup_history" role="tabpanel">
                                        <div class="pd-20">
                                            <h5 class="mb-20 h5 text-blue">Follow-up History</h5>
                                            <?php if(!empty($followups)): ?>
                                                <div class="timeline">
                                                    <?php foreach($followups as $followup): ?>
                                                        <div class="timeline-item">
                                                            <div class="timeline-badge <?php echo ($followup->updated_status == 'Converted') ? 'success' : (($followup->updated_status == 'Follow-up') ? 'warning' : 'info'); ?>">
                                                                <i class="fa fa-circle"></i>
                                                            </div>
                                                            <div class="timeline-panel">
                                                                <div class="timeline-heading">
                                                                    <h6 class="timeline-title">
                                                                        Status: <span class="badge badge-<?php echo ($followup->updated_status == 'Converted') ? 'success' : (($followup->updated_status == 'Follow-up') ? 'warning' : 'info'); ?>">
                                                                            <?php echo $followup->updated_status; ?>
                                                                        </span>
                                                                    </h6>
                                                                    <p><small class="text-muted">
                                                                        <i class="fa fa-clock-o"></i> <?php echo date('d-M-Y h:i A', strtotime($followup->followup_date)); ?>
                                                                        by <?php echo $followup->followup_by_name; ?>
                                                                    </small></p>
                                                                </div>
                                                                <div class="timeline-body">
                                                                    <p><strong>Previous Status:</strong> <?php echo $followup->previous_status; ?></p>
                                                                    <p><strong>Notes:</strong> <?php echo nl2br($followup->followup_notes); ?></p>
                                                                    <?php if($followup->next_followup_date): ?>
                                                                        <p><strong>Next Follow-up:</strong> <?php echo date('d-M-Y', strtotime($followup->next_followup_date)); ?></p>
                                                                    <?php endif; ?>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    <?php endforeach; ?>
                                                </div>
                                            <?php else: ?>
                                                <p class="text-muted">No follow-up history available.</p>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Status Card -->
                <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 mb-30">
                    <div class="card-box height-100-p overflow-hidden">
                        <div class="profile-photo">
                            <div class="text-center view-lead-icon-status">
                                <div class="avatar-photo">
                                    <?php
                                    $status_icon = '';
                                    $status_color = '';
                                    switch($lead->lead_status) {
                                        case 'New Lead':
                                            $status_icon = 'fa-plus-circle';
                                            $status_color = '#1b00ff';
                                            break;
                                        case 'Follow-up':
                                            $status_icon = 'fa-refresh';
                                            $status_color = '#f39c12';
                                            break;
                                        case 'Converted':
                                            $status_icon = 'fa-check-circle';
                                            $status_color = '#00d4bd';
                                            break;
                                        case 'Lost to Competition':
                                            $status_icon = 'fa-times-circle';
                                            $status_color = '#e74c3c';
                                            break;
                                        case 'Plan Dropped':
                                            $status_icon = 'fa-ban';
                                            $status_color = '#95a5a6';
                                            break;
                                    }
                                    ?>
                                    <i class="fa <?php echo $status_icon; ?>" style="font-size: 80px; color: <?php echo $status_color; ?>;"></i>
                                </div>
                                <h5 class="text-center h5 mb-0"><?php echo $lead->lead_status; ?></h5>
                            </div>
                        </div>
                        <div class="profile-info">
                            <h5 class="mb-10 h5 text-blue">Status Information</h5>
                            <ul class="mb-20">
                                <li>
                                    <span>Current Status:</span>
                                    <strong><?php echo $lead->lead_status; ?></strong>
                                </li>
                                <li>
                                    <span>Next Follow-up:</span>
                                    <?php if($lead->next_followup_date): ?>
                                        <strong><?php echo date('d-M-Y', strtotime($lead->next_followup_date)); ?></strong>
                                    <?php else: ?>
                                        <span class="text-muted">Not scheduled</span>
                                    <?php endif; ?>
                                </li>
                            </ul>
                            <div class="text-center">
                                <a href="<?php echo base_url('leads/followup/' . $lead->lead_id); ?>" class="btn btn-warning btn-block">
                                    <i class="fa fa-plus"></i> Add Follow-up
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php include(APPPATH . 'views/layout/footer.php'); ?>
    </div>
</div>
<style>
.timeline {
    position: relative;
    padding: 20px 0;
}
.timeline-item {
    position: relative;
    padding-left: 50px;
    margin-bottom: 30px;
}
.timeline-badge {
    position: absolute;
    left: 0;
    top: 0;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    text-align: center;
    line-height: 30px;
    color: white;
}
.timeline-badge.success {
    background-color: #00d4bd;
}
.timeline-badge.warning {
    background-color: #f39c12;
}
.timeline-badge.info {
    background-color: #1b00ff;
}
.timeline-panel {
    background: #f8f9fa;
    padding: 15px;
    border-radius: 5px;
    border-left: 3px solid #1b00ff;
}
.timeline-heading {
    margin-bottom: 10px;
}
.timeline-title {
    margin: 0 0 5px 0;
}
.timeline-body p {
    margin-bottom: 8px;
}
</style>
<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'); ?>
