<?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 New Lead</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">Add New Lead</li>
                            </ol>
                        </nav>
                    </div>
                </div>
            </div>
            <!-- Flash Messages -->
            <?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');
            ?>
            <!-- Lead Form -->
            <div class="pd-20 card-box mb-30">
                <form method="post" action="<?php echo base_url('leads/add'); ?>" id="leadForm">
                    <!-- Basic Information -->
                    <div class="form-group row">
                        <div class="col-md-12">
                            <h5 class="text-blue h5">Basic Information</h5>
                            <hr>
                        </div>
                        <?php $role = $this->session->userdata('role'); ?>
                        <?php if ($role == 'Admin') : ?>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label">Branch <span class="text-danger">*</span></label>
                                    <select class="form-control" name="branch_id" id="branch_id" required>
                                        <option value="">Select Branch</option>
                                        <?php foreach($branches as $branche): ?>
                                            <option value="<?php echo $branche->branch_id; ?>" <?php echo set_select('branch_id', $branche->branch_id); ?>>
                                                <?php echo $branche->branch_name; ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label">Assign To <span class="text-danger">*</span></label>
                                    <select class="form-control" name="admin_id" id="admin_id" required>
                                        <option value="">Select Executive</option>
                                        <?php foreach($executives as $executive): ?>
                                            <option value="<?php echo $executive->admin_id; ?>" <?php echo set_select('admin_id', $executive->admin_id); ?>>
                                                <?php echo $executive->name; ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            </div>
                        <?php elseif ($role == 'Executive') : ?>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label">Branch <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" value="<?php echo $this->session->userdata('branch_name'); ?>" readonly>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label">Executive <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" value="<?php echo $this->session->userdata('name'); ?>" readonly>
                                </div>
                            </div>
                        <?php elseif ($role == 'Manager') : ?>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label">Branch <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" value="<?php echo $this->session->userdata('branch_name'); ?>" readonly>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label">Executive <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" value="<?php echo $this->session->userdata('name'); ?>" readonly>
                                </div>
                            </div>
                        <?php endif; ?>
                    </div>
                    
                    <!-- Customer Information -->
                    <div class="form-group row">
                        <div class="col-md-12">
                            <h5 class="text-blue h5 mt-20">Customer Information</h5>
                            <hr>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="form-label">Customer Name <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" name="customer_name" required value="<?php echo set_value('customer_name'); ?>">
                                <?php echo form_error('customer_name', '<small class="text-danger">', '</small>'); ?>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="form-label">Email <span class="text-danger">*</span></label>
                                <input type="email" class="form-control" name="customer_email" value="<?php echo set_value('customer_email'); ?>">
                                <?php echo form_error('customer_email', '<small class="text-danger">', '</small>'); ?>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="form-label">Mobile Number <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" name="customer_mobile" required maxlength="10" pattern="[0-9]{10}" value="<?php echo set_value('customer_mobile'); ?>">
                                <?php echo form_error('customer_mobile', '<small class="text-danger">', '</small>'); ?>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="form-label">Alternate Mobile Number</label>
                                <input type="text" class="form-control" name="customer_alter_mobile" maxlength="10" pattern="[0-9]{10}" value="<?php echo set_value('customer_alter_mobile'); ?>">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="form-label">Date of Birth</label>
                                <input type="date" class="form-control" name="customer_dob" required value="<?php echo set_value('customer_dob'); ?>">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="form-label d-block">Gender</label>
                                <select class="form-control" name="customer_gender" required>
                                    <option value="">Select Gender</option>
                                    <option value="Male">Male</option>
                                    <option value="Female">Female</option>
                                    <option value="Other">Other</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label class="form-label">City</label>
                                <input type="text" class="form-control" name="customer_city" required value="<?php echo set_value('customer_city'); ?>">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label class="form-label">Area / Locality</label>
                                <input type="text" class="form-control" name="customer_area" required value="<?php echo set_value('customer_area'); ?>">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label class="form-label">State</label>
                                <!-- <input type="text" class="form-control" name="customer_state" required value="<?php echo set_value('customer_state'); ?>"> -->
                                <select class="form-control" name="customer_state" required>
                                    <option value="">Select State</option>
                                    <option value="Maharashtra">Maharashtra</option>
                                    <option value="Karnataka">Karnataka</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label class="form-label">PIN Code</label>
                                <input type="text" class="form-control" name="customer_pincode" required maxlength="6" pattern="[0-9]{6}" value="<?php echo set_value('customer_pincode'); ?>">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="form-label d-block">Customer Type</label>
                                <select class="form-control" name="customer_type" required>
                                    <option value="">Select Type</option>
                                    <option value="Individual">Individual</option>
                                    <option value="Proprietor">Proprietor</option>
                                    <option value="Partnership">Partnership</option>
                                    <option value="Company">Company</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="form-label d-block">Occupation Type</label>
                                <select class="form-control" name="customer_occupation_type" required>
                                    <option value="">Select Type</option>
                                    <option value="Salaried">Salaried</option>
                                    <option value="Self-Employed">Self-Employed</option>
                                    <option value="Professional">Professional</option>
                                    <option value="Student">Student</option>
                                    <option value="Others">Others</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="form-label">Employer / Business Name</label>
                                <input type="text" class="form-control" name="customer_employer_business_name" required value="<?php echo set_value('customer_employer_business_name'); ?>">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="form-label">Monthly Income / Annual Turnover</label>
                                <input type="text" class="form-control" name="customer_monthly_annual_income" required value="<?php echo set_value('customer_monthly_annual_income'); ?>">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="form-label">Existing Customer <span class="text-danger">*</span></label>
                                <select class="form-control" name="is_existing_customer" id="is_existing_customer" required>
                                    <option value="">Select</option>
                                    <option value="Yes" <?php echo set_select('is_existing_customer', 'Yes'); ?>>Yes</option>
                                    <option value="No" <?php echo set_select('is_existing_customer', 'No'); ?>>No</option>
                                </select>
                            </div>
                        </div>
                        <!-- <div class="col-md-4" id="relationship_number_div" style="display: none;">
                            <div class="form-group">
                                <label class="form-label">Relationship Number</label>
                                <input type="text" class="form-control" name="relationship_number" id="relationship_number" value="<?php echo set_value('relationship_number'); ?>">
                            </div>
                        </div> -->
                        <div class="col-md-4" id="account_number_div" style="display: none;">
                            <div class="form-group">
                                <label class="form-label">Account Number</label>
                                <input type="text" class="form-control" name="account_number" id="account_number" value="<?php echo set_value('account_number'); ?>">
                            </div>
                        </div>
                    </div>
                    
                    <!-- Product Selection -->
                    <div class="form-group row">
                        <div class="col-md-12">
                            <h5 class="text-blue h5 mt-20">Product Selection</h5>
                            <hr>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-sm-12 col-md-2 col-form-label">Product Category <span class="text-danger">*</span></label>
                        <div class="col-sm-12 col-md-10">
                            <select class="form-control" name="category_id" id="category_id" required>
                                <option value="">Select Category</option>
                                <?php foreach($categories as $category): ?>
                                    <option value="<?php echo $category->category_id; ?>" <?php echo set_select('category_id', $category->category_id); ?>>
                                        <?php echo $category->category_name; ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                            <?php echo form_error('category_id', '<small class="text-danger">', '</small>'); ?>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-sm-12 col-md-2 col-form-label">Product <span class="text-danger">*</span></label>
                        <div class="col-sm-12 col-md-10">
                            <select class="form-control" name="product_id" id="product_id" required>
                                <option value="">Select Product</option>
                                <!-- <?php foreach($products as $product): ?>
                                    <option value="<?php echo $product->product_id; ?>" <?php echo set_select('product_id', $product->product_id); ?>>
                                        <?php echo $product->product_name; ?>
                                    </option>
                                <?php endforeach; ?> -->
                            </select>
                            <?php echo form_error('product_id', '<small class="text-danger">', '</small>'); ?>
                        </div>
                    </div>
                    <!-- Product Specific Fields -->
                    <div id="product_fields"></div>
                    <!-- Common Fields -->
                    <div class="form-group row">
                        <label class="col-sm-12 col-md-2 col-form-label">Next Follow-up Date</label>
                        <div class="col-sm-12 col-md-10">
                            <input type="date" class="form-control" name="next_followup_date" min="<?php echo date('Y-m-d'); ?>" value="<?php echo set_value('next_followup_date'); ?>">
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-sm-12 col-md-2 col-form-label">Remarks</label>
                        <div class="col-sm-12 col-md-10">
                            <textarea class="form-control" name="remarks" rows="4"><?php echo set_value('remarks'); ?></textarea>
                        </div>
                    </div>
                    <!-- Customer Information -->
                    <div class="form-group row">
                        <div class="col-md-12">
                            <h5 class="text-blue h5 mt-20">Lead Source</h5>
                            <hr>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label class="form-label d-block">Source Type</label>
                                <select class="form-control" name="customer_lead_source_type" required>
                                    <option value="">Select Type</option>
                                    <option value="Digital">Digital</option>
                                    <option value="Walk-in">Walk-in</option>
                                    <option value="Referral">Referral</option>
                                    <option value="Tele-calling">Tele-calling</option>
                                    <option value="Print">Print</option>
                                    <option value="TV">TV</option>
                                    <option value="Radio">Radio</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label class="form-label d-block">Source Channel</label>
                                <select class="form-control" name="customer_lead_source_channel" required>
                                    <option value="">Select Type</option>
                                    <option value="Meta">Meta</option>
                                    <option value="Google">Google</option>
                                    <option value="Website">Website</option>
                                    <option value="Branch">Branch</option>
                                    <option value="Field">Field</option>
                                    <option value="Call Center">Call Center</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label class="form-label">Campaign Name / ID</label>
                                <input type="text" class="form-control" name="customer_lead_campaign_name" value="<?php echo set_value('customer_lead_campaign_name'); ?>">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label class="form-label">Referrer Details (if applicable)</label>
                                <input type="text" class="form-control" name="customer_lead_referrer_detail" value="<?php echo set_value('customer_lead_referrer_detail'); ?>">
                            </div>
                        </div>
                    </div>
                    <div class="form-group row">
                        <div class="col-md-10">
                            <button type="submit" class="btn btn-primary">Submit Lead</button>
                            <a href="<?php echo base_url('leads'); ?>" class="btn btn-secondary">Cancel</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <?php include(APPPATH . 'views/layout/footer.php'); ?>
    </div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Show/hide existing customer fields
    document.getElementById('is_existing_customer').addEventListener('change', function() {
        if (this.value === 'Yes') {
            // document.getElementById('relationship_number_div').style.display = 'block';
            document.getElementById('account_number_div').style.display = 'block';
        } else {
            // document.getElementById('relationship_number_div').style.display = 'none';
            document.getElementById('account_number_div').style.display = 'none';
            document.getElementById('relationship_number').value = '';
            document.getElementById('account_number').value = '';
        }
    });
    $(document).ready(function () {
        // Load products when category changes
        $('#category_id').change(function () {
            var category_id = $(this).val();
            $('#product_id').html('<option value="">Loading...</option>');
            if (category_id != '') {
                $.ajax({
                    url: '<?php echo base_url("Leadcontroller/getProductsByCategory"); ?>',
                    type: 'POST',
                    data: {
                        category_id: category_id
                    },
                    dataType: 'json',
                    success: function (response) {
                        var html = '<option value="">Select Product</option>';

                        if (response.length > 0) {
                            $.each(response, function (index, product) {
                                html += '<option value="' + product.product_id + '">' +
                                            product.product_name +
                                        '</option>';
                            });
                        } else {
                            html = '<option value="">No Products Found</option>';
                        }
                        $('#product_id').html(html);
                    },
                    error: function () {
                        $('#product_id').html('<option value="">Error Loading Products</option>');
                    }
                });
            } else {
                $('#product_id').html('<option value="">Select Product</option>');
            }
        });
    });
    // Product selection change
    document.getElementById('product_id').addEventListener('change', function() {
        const productId = this.value;
        const productFields = document.getElementById('product_fields');
        if (!productId) {
            productFields.innerHTML = '';
            return;
        }
        let fieldsHTML = '<div class="form-group row"><div class="col-md-12"><h5 class="text-blue h5 mt-20">Product Details</h5><hr></div></div>';
        // Home Loans (product_id = 1)
        if (productId == '1') {
            fieldsHTML += `
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Property Value (₹ Lakhs)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" step="0.01" class="form-control" name="property_value" placeholder="Enter property value in lakhs">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Loan Amount (₹ Lakhs)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" step="0.01" class="form-control" name="loan_amount" placeholder="Enter loan amount in lakhs">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Income Source</label>
                    <div class="col-sm-12 col-md-10">
                        <select class="form-control" name="income_source">
                            <option value="">Select</option>
                            <option value="Salaried">Salaried</option>
                            <option value="Business">Business</option>
                            <option value="Self Employed">Self Employed</option>
                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Expected Date of Purchase</label>
                    <div class="col-sm-12 col-md-5">
                        <select class="form-control" name="expected_purchase_month">
                            <option value="">Select Month</option>
                            <option value="January">January</option>
                            <option value="February">February</option>
                            <option value="March">March</option>
                            <option value="April">April</option>
                            <option value="May">May</option>
                            <option value="June">June</option>
                            <option value="July">July</option>
                            <option value="August">August</option>
                            <option value="September">September</option>
                            <option value="October">October</option>
                            <option value="November">November</option>
                            <option value="December">December</option>
                        </select>
                    </div>
                    <div class="col-sm-12 col-md-5">
                        <select class="form-control" name="expected_purchase_year">
                            <option value="">Select Year</option>
                            ${generateYearOptions()}
                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Property Identified</label>
                    <div class="col-sm-12 col-md-10">
                        <select class="form-control" name="property_identified">
                            <option value="">Select</option>
                            <option value="Yes">Yes</option>
                            <option value="No">No</option>
                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Property Address</label>
                    <div class="col-sm-12 col-md-10">
                        <textarea class="form-control" name="property_address" rows="3"></textarea>
                    </div>
                </div>
            `;
        }
        // Car Loans (product_id = 2)
        else if (productId == '2') {
            fieldsHTML += `
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Car Value (₹ Lakhs)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" step="0.01" class="form-control" name="car_value" placeholder="Enter car value in lakhs">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Car Make</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="text" class="form-control" name="car_make" placeholder="e.g., Maruti, Hyundai, Honda">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Car Grade/Model</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="text" class="form-control" name="car_grade" placeholder="e.g., Swift VXI, Creta SX">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Loan Amount (₹ Lakhs)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" step="0.01" class="form-control" name="loan_amount" placeholder="Enter loan amount in lakhs">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Income Source</label>
                    <div class="col-sm-12 col-md-10">
                        <select class="form-control" name="income_source">
                            <option value="">Select</option>
                            <option value="Salaried">Salaried</option>
                            <option value="Business">Business</option>
                            <option value="Self Employed">Self Employed</option>
                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Expected Date of Purchase</label>
                    <div class="col-sm-12 col-md-5">
                        <select class="form-control" name="expected_purchase_month">
                            <option value="">Select Month</option>
                            <option value="January">January</option>
                            <option value="February">February</option>
                            <option value="March">March</option>
                            <option value="April">April</option>
                            <option value="May">May</option>
                            <option value="June">June</option>
                            <option value="July">July</option>
                            <option value="August">August</option>
                            <option value="September">September</option>
                            <option value="October">October</option>
                            <option value="November">November</option>
                            <option value="December">December</option>
                        </select>
                    </div>
                    <div class="col-sm-12 col-md-5">
                        <select class="form-control" name="expected_purchase_year">
                            <option value="">Select Year</option>
                            ${generateYearOptions()}
                        </select>
                    </div>
                </div>
            `;
        }
        // Two Wheeler Loans (product_id = 3)
        else if (productId == '3') {
            fieldsHTML += `
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Two Wheeler Value (₹ Thousands/Lakhs)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" step="0.01" class="form-control" name="two_wheeler_value" placeholder="Enter value">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Two Wheeler Make</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="text" class="form-control" name="two_wheeler_make" placeholder="e.g., Honda, Hero, Bajaj">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Two Wheeler Grade/Model</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="text" class="form-control" name="two_wheeler_grade" placeholder="e.g., Activa 6G, Splendor Plus">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Loan Amount (₹ Lakhs)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" step="0.01" class="form-control" name="loan_amount" placeholder="Enter loan amount">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Income Source</label>
                    <div class="col-sm-12 col-md-10">
                        <select class="form-control" name="income_source">
                            <option value="">Select</option>
                            <option value="Salaried">Salaried</option>
                            <option value="Business">Business</option>
                            <option value="Self Employed">Self Employed</option>
                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Expected Date of Purchase</label>
                    <div class="col-sm-12 col-md-5">
                        <select class="form-control" name="expected_purchase_month">
                            <option value="">Select Month</option>
                            <option value="January">January</option>
                            <option value="February">February</option>
                            <option value="March">March</option>
                            <option value="April">April</option>
                            <option value="May">May</option>
                            <option value="June">June</option>
                            <option value="July">July</option>
                            <option value="August">August</option>
                            <option value="September">September</option>
                            <option value="October">October</option>
                            <option value="November">November</option>
                            <option value="December">December</option>
                        </select>
                    </div>
                    <div class="col-sm-12 col-md-5">
                        <select class="form-control" name="expected_purchase_year">
                            <option value="">Select Year</option>
                            ${generateYearOptions()}
                        </select>
                    </div>
                </div>
            `;
        }
        // Fixed Deposits (product_id = 4)
        else if (productId == '4' || productId == '12' || productId == '13' || productId == '14') {
            fieldsHTML += `
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Deposit Amount (₹ Thousands/Lakhs)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" step="0.01" class="form-control" name="deposit_amount" id="deposit_amount" placeholder="Enter deposit amount">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Tenure (Days)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" class="form-control" name="tenure_days" id="tenure_days" placeholder="Enter tenure in days">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Interest Rate (%)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" step="0.01" class="form-control" name="interest_rate" id="interest_rate" placeholder="Enter interest rate">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Maturity Value (₹)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" step="0.01" class="form-control" name="maturity_value" id="maturity_value" placeholder="Auto-calculated" readonly>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Creation Date</label>
                    <div class="col-sm-12 col-md-5">
                        <select class="form-control" name="creation_month">
                            <option value="">Select Month</option>
                            <option value="January">January</option>
                            <option value="February">February</option>
                            <option value="March">March</option>
                            <option value="April">April</option>
                            <option value="May">May</option>
                            <option value="June">June</option>
                            <option value="July">July</option>
                            <option value="August">August</option>
                            <option value="September">September</option>
                            <option value="October">October</option>
                            <option value="November">November</option>
                            <option value="December">December</option>
                        </select>
                    </div>
                    <div class="col-sm-12 col-md-5">
                        <select class="form-control" name="creation_year">
                            <option value="">Select Year</option>
                            ${generateYearOptions()}
                        </select>
                    </div>
                </div>
            `;
        }
        // Savings Account (product_id = 5)
        else if (productId == '5' || productId == '11') {
            fieldsHTML += `
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Account Creation Date</label>
                    <div class="col-sm-12 col-md-5">
                        <select class="form-control" name="account_creation_month">
                            <option value="">Select Month</option>
                            <option value="January">January</option>
                            <option value="February">February</option>
                            <option value="March">March</option>
                            <option value="April">April</option>
                            <option value="May">May</option>
                            <option value="June">June</option>
                            <option value="July">July</option>
                            <option value="August">August</option>
                            <option value="September">September</option>
                            <option value="October">October</option>
                            <option value="November">November</option>
                            <option value="December">December</option>
                        </select>
                    </div>
                    <div class="col-sm-12 col-md-5">
                        <select class="form-control" name="account_creation_year">
                            <option value="">Select Year</option>
                            ${generateYearOptions()}
                        </select>
                    </div>
                </div>
            `;
        }
        else if (productId == '6') {
            fieldsHTML += `
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Business Name</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="text" class="form-control" name="msme_business_name" placeholder="Enter business name">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Business Type</label>
                    <div class="col-sm-12 col-md-10">
                        <select class="form-control" name="msme_business_type">
                            <option value="">Select</option>
                            <option value="Proprietorship">Proprietorship</option>
                            <option value="Partnership">Partnership</option>
                            <option value="LLP">LLP</option>
                            <option value="Private Limited">Private Limited</option>
                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Business Vintage</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" class="form-control" name="msme_business_vintage" placeholder="Years in business">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Annual Turnover (₹)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" class="form-control" name="msme_business_annual_turnover" placeholder="Annual turnover">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Required Loan Amount (₹)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" class="form-control" name="msme_business_loan_amount" placeholder="Required loan amount">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Purpose of Loan</label>
                    <div class="col-sm-12 col-md-10">
                        <select class="form-control" name="msme_business_loan_purpose">
                            <option value="">Select</option>
                            <option value="Working Capital">Working Capital</option>
                            <option value="Business Expansion">Business Expansion</option>
                            <option value="Machinery Purchase">Machinery Purchase</option>
                            <option value="Inventory Purchase">Inventory Purchase</option>
                            <option value="Others">Others</option>
                        </select>
                    </div>
                </div>
            `;
        }
        else if (productId == '7') {
            fieldsHTML += `
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Course Name</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="text" class="form-control" name="course_name" placeholder="e.g., B.Tech, MBA, MBBS">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">College / University</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="text" class="form-control" name="college_name" placeholder="Enter college/university name">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Study Location</label>
                    <div class="col-sm-12 col-md-10">
                        <select class="form-control" name="study_location">
                            <option value="">Select</option>
                            <option value="India">India</option>
                            <option value="Abroad">Abroad</option>
                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Course Fee (₹)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" class="form-control" name="course_fee" placeholder="Enter total course fee">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Required Loan Amount (₹)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" class="form-control" name="course_loan_amount" placeholder="Required loan amount">
                    </div>
                </div>
            `;
        }
        else if (productId == '8') {
            fieldsHTML += `
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Property Type</label>
                    <div class="col-sm-12 col-md-10">
                        <select class="form-control" name="mortgage_property_type">
                            <option value="">Select</option>
                            <option value="Residential">Residential</option>
                            <option value="Commercial">Commercial</option>
                            <option value="Industrial">Industrial</option>
                            <option value="Plot">Plot</option>
                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Property Value (₹ Lakhs)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" step="0.01" class="form-control" name="mortgage_property_value" placeholder="Property market value">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Property Location</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="text" class="form-control" name="mortgage_property_location" placeholder="City / Area">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Required Loan Amount (₹ Lakhs)</label>
                    <div class="col-sm-12 col-md-10">
                        <input type="number" step="0.01" class="form-control" name="mortgage_loan_amount" placeholder="Required loan amount">
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Income Source</label>
                    <div class="col-sm-12 col-md-10">
                        <select class="form-control" name="mortgage_income_source">
                            <option value="">Select</option>
                            <option value="Salaried">Salaried</option>
                            <option value="Business">Business</option>
                            <option value="Self Employed">Self Employed</option>
                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-12 col-md-2 col-form-label">Purpose of Loan</label>
                    <div class="col-sm-12 col-md-10">
                        <select class="form-control" name="mortgage_loan_purpose">
                            <option value="">Select</option>
                            <option value="Business Expansion">Business Expansion</option>
                            <option value="Personal Need">Personal Need</option>
                            <option value="Debt Consolidation">Debt Consolidation</option>
                            <option value="Education">Education</option>
                            <option value="Medical">Medical</option>
                        </select>
                    </div>
                </div>
            `;
        }
        productFields.innerHTML = fieldsHTML;
        // Add FD calculation listener
        if (productId == '4' || productId == '12' || productId == '13' || productId == '14') {
            ['deposit_amount', 'tenure_days', 'interest_rate'].forEach(id => {
                const element = document.getElementById(id);
                if (element) {
                    element.addEventListener('input', calculateMaturityValue);
                }
            });
        }
    });
    // Generate year options
    function generateYearOptions() {
        const currentYear = new Date().getFullYear();
        let options = '';
        for (let i = 0; i <= 5; i++) {
            const year = currentYear + i;
            options += `<option value="${year}">${year}</option>`;
        }
        return options;
    }
    // Calculate FD maturity value
    function calculateMaturityValue() {
        const principal = parseFloat(document.getElementById('deposit_amount').value) || 0;
        const days = parseInt(document.getElementById('tenure_days').value) || 0;
        const rate = parseFloat(document.getElementById('interest_rate').value) || 0;
        if (principal > 0 && days > 0 && rate > 0) {
            // Simple interest calculation: P + (P * R * T / 100)
            // T in years = days / 365
            const timeInYears = days / 365;
            const interest = (principal * rate * timeInYears) / 100;
            const maturityValue = principal + interest;
            document.getElementById('maturity_value').value = maturityValue.toFixed(2);
        } else {
            document.getElementById('maturity_value').value = '';
        }
    }
});
</script>
<?php include(APPPATH . 'views/layout/template-bottom.php'); ?>
