<?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>Edit 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"><a href="<?php echo base_url('leads/view/' . $lead->lead_id); ?>">Lead Details</a></li>
                                <li class="breadcrumb-item active" aria-current="page">Edit Lead</li>
                            </ol>
                        </nav>
                    </div>
                </div>
            </div>
            <!-- Lead Form -->
            <div class="pd-20 card-box mb-30">
                <?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/edit/' . $lead->lead_id); ?>" id="leadForm">
                    <!-- Basic Information -->
                    <div class="form-group row">
                        <div class="col-md-12">
                            <h5 class="text-blue h5">Basic Information</h5>
                            <hr>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label class="form-label">Lead Number</label>
                                <input type="text" class="form-control" value="<?php echo $lead->lead_number; ?>" readonly>
                            </div>
                        </div>
                        <?php $role = $this->session->userdata('role'); ?>
                        <?php if ($role == 'Admin') : ?>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="form-label">Branch</label>
                                    <select class="form-control" name="branch_id" id="branch_id" required>
                                        <option value="">Select Executive</option>
                                        <?php foreach($branches as $branche): ?>
                                            <option value="<?php echo $branche->branch_id; ?>" <?php echo set_select('branch_id', $branche->branch_id, ($lead->executive_id == $branche->branch_id)); ?>>
                                                <?php echo $branche->branch_name; ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <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, ($lead->executive_id == $executive->admin_id)); ?>>
                                                <?php echo $executive->name; ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            </div>
                        <?php elseif ($role == 'Executive') : ?>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="form-label">Branch</label>
                                    <input type="text" class="form-control" value="<?php echo $lead->branch_name; ?>" readonly>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="form-label">Executive</label>
                                    <input type="text" class="form-control" value="<?php echo $lead->executive_name; ?>" readonly>
                                </div>
                            </div>
                        <?php elseif ($role == 'Manager') : ?>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="form-label">Branch</label>
                                    <input type="text" class="form-control" value="<?php echo $lead->branch_name; ?>" readonly>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="form-label">Manager</label>
                                    <input type="text" class="form-control" value="<?php echo $lead->executive_name; ?>" readonly>
                                </div>
                            </div>
                        <?php endif; ?>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label class="form-label">Product</label>
                                <input type="text" class="form-control" value="<?php echo $lead->product_name; ?>" readonly>
                                <small class="form-text text-muted">Product cannot be changed after lead creation</small>
                            </div>
                        </div>
                    </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', $lead->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', $lead->customer_email); ?>">
                            </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', $lead->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', $lead->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', $lead->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" <?php echo set_select('customer_gender', 'Male', ($lead->customer_gender == 'Male')); ?>>Male</option>
                                    <option value="Female" <?php echo set_select('customer_gender', 'Female', ($lead->customer_gender == 'Female')); ?>>Female</option>
                                    <option value="Other" <?php echo set_select('customer_gender', 'Other', ($lead->is_existing_customer == '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', $lead->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', $lead->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', $lead->customer_state); ?>"> -->
                                <select class="form-control" name="customer_state" required>
                                    <option value="">Select State</option>
                                    <option value="Maharashtra" <?php echo set_select('customer_state', 'Maharashtra', ($lead->customer_state == 'Maharashtra')); ?>>Maharashtra</option>
                                    <option value="Karnataka" <?php echo set_select('customer_state', 'Karnataka', ($lead->customer_state == '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', $lead->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" <?php echo set_select('customer_type', 'Individual', ($lead->customer_type == 'Individual')); ?>>Individual</option>
                                    <option value="Proprietor" <?php echo set_select('customer_type', 'Proprietor', ($lead->customer_type == 'Proprietor')); ?>>Proprietor</option>
                                    <option value="Partnership" <?php echo set_select('customer_type', 'Partnership', ($lead->customer_type == 'Partnership')); ?>>Partnership</option>
                                    <option value="Company" <?php echo set_select('customer_type', 'Company', ($lead->customer_type == '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" <?php echo set_select('customer_occupation_type', 'Salaried', ($lead->customer_occupation_type == 'Salaried')); ?>>Salaried</option>
                                    <option value="Self-Employed" <?php echo set_select('customer_occupation_type', 'Self-Employed', ($lead->customer_occupation_type == 'Self-Employed')); ?>>Self-Employed</option>
                                    <option value="Professional" <?php echo set_select('customer_occupation_type', 'Professional', ($lead->customer_occupation_type == 'Professional')); ?>>Professional</option>
                                    <option value="Student" <?php echo set_select('customer_occupation_type', 'Student', ($lead->customer_occupation_type == 'Student')); ?>>Student</option>
                                    <option value="Others" <?php echo set_select('customer_occupation_type', 'Others', ($lead->customer_occupation_type == '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', $lead->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', $lead->customer_monthly_annual_income); ?>">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <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', ($lead->is_existing_customer == 'Yes')); ?>>Yes</option>
                                <option value="No" <?php echo set_select('is_existing_customer', 'No', ($lead->is_existing_customer == 'No')); ?>>No</option>
                            </select>
                        </div>
                        <!-- <div class="col-md-4" id="relationship_number_div" style="display: <?php echo ($lead->is_existing_customer == 'Yes') ? 'block' : 'none'; ?>;">
                            <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', $lead->relationship_number); ?>">
                        </div> -->
                        <div class="col-md-4" id="account_number_div" style="display: <?php echo ($lead->is_existing_customer == 'Yes') ? 'block' : 'none'; ?>;">
                            <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', $lead->account_number); ?>">
                        </div>
                    </div>
                    <!-- Note about product details -->
                    <div class="form-group row">
                        <div class="col-md-12">
                            <div class="alert alert-info">
                                <i class="fa fa-info-circle"></i> <strong>Note:</strong> To update product-specific details or lead status, please use the "Add Follow-up" feature.
                            </div>
                        </div>
                    </div>
                    <div class="form-group row">
                        <div class="col-sm-12 col-md-10 offset-md-2">
                            <button type="submit" class="btn btn-primary">
                                <i class="fa fa-save"></i> Update Lead
                            </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>
                    </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 = '';
        }
    });
});
</script>
<?php include(APPPATH . 'views/layout/template-bottom.php'); ?>
