<?php
session_start();
if(!isset($_SESSION['username'])) {
    header("Location: ../login.php");
    exit();
}

include('../includes/db.php');

$success_message = "";
$error_message = "";

// Add Customer
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_customer'])) {
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $phone = mysqli_real_escape_string($conn, $_POST['phone']);
    $vehicle_no = mysqli_real_escape_string($conn, $_POST['vehicle_no']);
    $opening_balance = mysqli_real_escape_string($conn, $_POST['opening_balance']);
    $date_time = date('Y-m-d H:i:s');
    
    if(empty($name)) {
        $error_message = "Customer Name is required!";
    } elseif(!is_numeric($opening_balance) || $opening_balance < 0) {
        $error_message = "Opening balance must be a valid positive number!";
    } else {
        $sql = "INSERT INTO customers (name, phone, vehicle_no, balance, date_time) 
                VALUES ('$name', '$phone', '$vehicle_no', '$opening_balance', '$date_time')";
        
        if(mysqli_query($conn, $sql)) {
            $success_message = "Customer added successfully with opening balance of Rs " . number_format($opening_balance, 2);
            
            if($opening_balance > 0) {
                $customer_id = mysqli_insert_id($conn);
                $record_sql = "INSERT INTO customer_payments (customer_id, amount, date_time, payment_type, notes) 
                               VALUES ('$customer_id', '$opening_balance', '$date_time', 'opening_balance', 'Opening balance added')";
                mysqli_query($conn, $record_sql);
            }
        } else {
            $error_message = "Error: " . mysqli_error($conn);
        }
    }
}

// Delete Customer
if(isset($_GET['delete'])) {
    $delete_id = mysqli_real_escape_string($conn, $_GET['delete']);
    
    $check_sql = "SELECT id FROM credit_sales WHERE customer_id='$delete_id'";
    $check_result = mysqli_query($conn, $check_sql);
    
    if(mysqli_num_rows($check_result) > 0) {
        $error_message = "Cannot delete! Customer has credit sales records.";
    } else {
        $sql = "DELETE FROM customers WHERE id='$delete_id'";
        if(mysqli_query($conn, $sql)) {
            $success_message = "Customer deleted successfully!";
        } else {
            $error_message = "Error deleting customer";
        }
    }
}

// Get all customers
$sql = "SELECT * FROM customers ORDER BY id DESC";
$result = mysqli_query($conn, $sql);

// Get statistics
$total_customers = mysqli_num_rows($result);
$total_balance = 0;
$customers_with_balance = 0;

while($row = mysqli_fetch_assoc($result)) {
    $total_balance += $row['balance'];
    if($row['balance'] > 0) {
        $customers_with_balance++;
    }
}
mysqli_data_seek($result, 0);
?>

<?php include('../includes/log.php'); ?>
<?php include('../includes/sd.php'); ?>

<div class="customers-container">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-left">
            <h2><i class="fas fa-users"></i> Customer Management</h2>
            <p>Manage customer accounts, track balances and transactions</p>
        </div>
        <div class="header-right">
            <div class="stats-badge">
                <i class="fas fa-chart-line"></i>
                <span><?php echo $total_customers; ?> Total Customers</span>
            </div>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="stats-row">
        <div class="stat-card">
            <div class="stat-icon"><i class="fas fa-users"></i></div>
            <div class="stat-info">
                <h3><?php echo $total_customers; ?></h3>
                <p>Total Customers</p>
                <span class="stat-trend">Registered Accounts</span>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon"><i class="fas fa-rupee-sign"></i></div>
            <div class="stat-info">
                <h3>Rs <?php echo number_format($total_balance, 2); ?></h3>
                <p>Total Outstanding</p>
                <span class="stat-trend">Total Credit Balance</span>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon"><i class="fas fa-hand-holding-usd"></i></div>
            <div class="stat-info">
                <h3><?php echo $customers_with_balance; ?></h3>
                <p>Active Credit Accounts</p>
                <span class="stat-trend">Customers with Balance</span>
            </div>
        </div>
    </div>

    <!-- Alerts -->
    <?php if($success_message): ?>
        <div class="alert-custom alert-success">
            <i class="fas fa-check-circle"></i> <?php echo $success_message; ?>
            <button class="close-alert" onclick="this.parentElement.remove();">&times;</button>
        </div>
    <?php endif; ?>
    
    <?php if($error_message): ?>
        <div class="alert-custom alert-danger">
            <i class="fas fa-exclamation-triangle"></i> <?php echo $error_message; ?>
            <button class="close-alert" onclick="this.parentElement.remove();">&times;</button>
        </div>
    <?php endif; ?>
    
    <div class="content-grid">
        <!-- Add Customer Form -->
        <div class="form-card">
            <div class="form-card-header">
                <i class="fas fa-plus-circle"></i>
                <h4>Add New Customer</h4>
            </div>
            <div class="form-card-body">
                <form method="POST" action="">
                    <div class="input-group-custom">
                        <label><i class="fas fa-user"></i> Customer Name <span class="required">*</span></label>
                        <input type="text" name="name" class="form-control-modern" required placeholder="Enter full name">
                        <small class="input-help">Full name of the customer</small>
                    </div>
                    
                    <div class="input-group-custom">
                        <label><i class="fas fa-phone"></i> Phone Number <span class="optional-label">(Optional)</span></label>
                        <input type="tel" name="phone" class="form-control-modern" placeholder="03XXXXXXXXX">
                        <small class="input-help">Active phone number for contact (optional)</small>
                    </div>
                    
                    <div class="input-group-custom">
                        <label><i class="fas fa-truck"></i> Vehicle Number <span class="optional-label">(Optional)</span></label>
                        <input type="text" name="vehicle_no" class="form-control-modern" placeholder="ABC-1234">
                        <small class="input-help">Vehicle registration number (optional)</small>
                    </div>
                    
                    <div class="input-group-custom">
                        <label><i class="fas fa-rupee-sign"></i> Opening Balance (Rs)</label>
                        <input type="number" step="0.01" name="opening_balance" class="form-control-modern" value="0" placeholder="0.00">
                        <small class="input-help">Previous outstanding balance (if any)</small>
                    </div>
                    
                    <div class="balance-preview" id="balancePreview" style="display: none;">
                        <div class="preview-row">
                            <span><i class="fas fa-info-circle"></i> Opening Balance:</span>
                            <strong id="previewAmount">Rs 0.00</strong>
                        </div>
                        <div class="preview-row">
                            <span><i class="fas fa-chart-line"></i> Status:</span>
                            <strong id="previewStatus">Clear</strong>
                        </div>
                    </div>
                    
                    <button type="submit" name="add_customer" class="btn-submit">
                        <i class="fas fa-save"></i> Add Customer
                    </button>
                </form>
            </div>
        </div>
        
        <!-- Customers List -->
        <div class="table-card">
            <div class="table-card-header">
                <div class="header-info">
                    <i class="fas fa-list"></i>
                    <h4>Customers List</h4>
                </div>
                <div class="table-search">
                    <i class="fas fa-search"></i>
                    <input type="text" id="tableSearch" placeholder="Search customer..." class="search-input">
                </div>
            </div>
            <div class="table-card-body">
                <div class="table-responsive">
                    <table class="modern-table" id="customersTable">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Customer Name</th>
                                <th>Phone</th>
                                <th>Vehicle No</th>
                                <th>Opening Balance</th>
                                <th>Current Balance</th>
                                <th>Status</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if(mysqli_num_rows($result) > 0): ?>
                                <?php while($row = mysqli_fetch_assoc($result)): 
                                    $balance_status = $row['balance'] > 0 ? 'due' : 'clear';
                                    $balance_text = $row['balance'] > 0 ? 'Due' : 'Clear';
                                    $balance_icon = $row['balance'] > 0 ? 'fa-hourglass-half' : 'fa-check-circle';
                                    
                                    // Get opening balance from customer_payments where payment_type = 'opening_balance'
                                    $opening_sql = "SELECT amount FROM customer_payments WHERE customer_id = '{$row['id']}' AND payment_type = 'opening_balance' ORDER BY id ASC LIMIT 1";
                                    $opening_res = mysqli_query($conn, $opening_sql);
                                    $opening_data = mysqli_fetch_assoc($opening_res);
                                    $opening_balance_display = $opening_data ? $opening_data['amount'] : 0;
                                ?>
                                    <tr>
                                        <td data-label="ID">#<?php echo $row['id']; ?></td>
                                        <td data-label="Customer Name">
                                            <div class="customer-name-cell">
                                                <div class="avatar">
                                                    <i class="fas fa-user-circle"></i>
                                                </div>
                                                <strong><?php echo htmlspecialchars($row['name']); ?></strong>
                                            </div>
                                        </div>
                                        </td>
                                        <td data-label="Phone">
                                            <div class="phone-cell">
                                                <i class="fas fa-phone-alt"></i>
                                                <?php echo htmlspecialchars($row['phone'] ?: '—'); ?>
                                            </div>
                                         </div>
                                        </td>
                                        <td data-label="Vehicle No">
                                            <div class="vehicle-cell">
                                                <i class="fas fa-truck"></i>
                                                <?php echo htmlspecialchars($row['vehicle_no'] ?: '—'); ?>
                                            </div>
                                         </div>
                                        </td>
                                        <td data-label="Opening Balance">
                                            <div class="opening-balance-cell">
                                                Rs <?php echo number_format($opening_balance_display, 2); ?>
                                            </div>
                                         </div>
                                        </td>
                                        <td data-label="Current Balance">
                                            <div class="balance-cell <?php echo $balance_status; ?>">
                                                Rs <?php echo number_format($row['balance'], 2); ?>
                                            </div>
                                         </div>
                                        </td>
                                        <td data-label="Status">
                                            <span class="status-badge <?php echo $balance_status; ?>">
                                                <i class="fas <?php echo $balance_icon; ?>"></i>
                                                <?php echo $balance_text; ?>
                                            </span>
                                         </div>
                                        <td data-label="Actions">
                                            <div class="action-buttons">
                                                <a href="customer_ledger.php?id=<?php echo $row['id']; ?>" 
                                                   class="btn-action btn-ledger" title="View Ledger">
                                                    <i class="fas fa-book"></i>
                                                </a>
                                                <a href="?delete=<?php echo $row['id']; ?>" 
                                                   class="btn-action btn-delete" 
                                                   onclick="return confirm('Delete this customer? This will remove all customer data!')"
                                                   title="Delete Customer">
                                                    <i class="fas fa-trash"></i>
                                                </a>
                                            </div>
                                        </div>
                                    </tr>
                                <?php endwhile; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="8" class="empty-state">
                                        <i class="fas fa-users"></i>
                                        <p>No customers found</p>
                                        <small>Click "Add New Customer" to get started</small>
                                    </div>
                                    </tr>
                            <?php endif; ?>
                        </tbody>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
/* Customers Page Styles */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap');

* {
    font-family: 'Inter', sans-serif;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background: #f5f7fb;
}

/* Headings use Times New Roman */
h1, h2, h3, h4, h5, h6,
.page-header h2,
.stat-info h3,
.form-card-header h4,
.header-info h4,
.modern-table th {
    font-family: 'Times New Roman', serif;
}

.customers-container {
    width: 100%;
    margin: 0;
    padding: 0;
}

/* Page Header */
.page-header {
    background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
    border-radius: 16px;
    padding: 18px 24px;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    box-shadow: 0 4px 15px rgba(160,70,87,0.2);
    width: 100%;
}

.page-header h2 {
    color: #fff;
    margin: 0 0 5px 0;
    font-size: 22px;
    font-weight: 700;
}

.page-header p {
    color: rgba(255,255,255,0.9);
    margin: 0;
    font-size: 12px;
}

.stats-badge {
    background: rgba(255,255,255,0.2);
    padding: 6px 16px;
    border-radius: 30px;
    font-weight: 600;
    font-size: 12px;
    backdrop-filter: blur(10px);
}

/* Statistics Row */
.stats-row {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;
    margin-bottom: 20px;
    width: 100%;
}

.stat-card {
    background: #fff;
    border-radius: 16px;
    padding: 16px 18px;
    display: flex;
    align-items: center;
    gap: 15px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    transition: all 0.3s ease;
    border: 1px solid #f0f0f0;
}

.stat-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 20px rgba(160,70,87,0.12);
    border-color: #A04657;
}

.stat-icon {
    width: 50px;
    height: 50px;
    background: rgba(160,70,87,0.1);
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.stat-icon i {
    font-size: 24px;
    color: #A04657;
}

.stat-info h3 {
    font-size: 24px;
    margin: 0 0 3px 0;
    font-weight: 800;
    color: #1a1a2e;
}

.stat-info p {
    margin: 0;
    color: #666;
    font-size: 11px;
    font-weight: 500;
}

.stat-trend {
    font-size: 10px;
    color: #999;
    font-weight: 500;
    display: block;
    margin-top: 2px;
}

/* Alert Styles */
.alert-custom {
    padding: 12px 18px;
    border-radius: 12px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    animation: slideIn 0.3s ease;
    font-weight: 500;
    font-size: 12px;
    width: 100%;
}

@keyframes slideIn {
    from { transform: translateY(-20px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.alert-success {
    background: #e8f5e9;
    color: #2e7d32;
    border-left: 4px solid #2e7d32;
}

.alert-danger {
    background: #ffebee;
    color: #c62828;
    border-left: 4px solid #c62828;
}

.close-alert {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    opacity: 0.5;
    transition: opacity 0.3s;
}

.close-alert:hover {
    opacity: 1;
}

/* Content Grid */
.content-grid {
    display: grid;
    grid-template-columns: 360px 1fr;
    gap: 20px;
    width: 100%;
}

/* Form Card */
.form-card {
    background: #fff;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    border: 1px solid #f0f0f0;
    height: fit-content;
}

.form-card-header {
    background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
    padding: 14px 20px;
    display: flex;
    align-items: center;
    gap: 8px;
    color: #fff;
}

.form-card-header i {
    font-size: 18px;
}

.form-card-header h4 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: #fff;
}

.form-card-body {
    padding: 20px;
}

.input-group-custom {
    margin-bottom: 16px;
}

.input-group-custom label {
    display: block;
    margin-bottom: 6px;
    font-weight: 600;
    color: #333;
    font-size: 12px;
}

.input-group-custom label i {
    color: #A04657;
    margin-right: 5px;
    width: 16px;
}

.required {
    color: #dc3545;
}

.optional-label {
    color: #999;
    font-size: 10px;
    font-weight: normal;
    margin-left: 5px;
}

.form-control-modern {
    width: 100%;
    padding: 10px 14px;
    border: 2px solid #e8e8e8;
    border-radius: 10px;
    font-size: 13px;
    transition: all 0.2s;
    background: #fafafa;
}

.form-control-modern:focus {
    border-color: #A04657;
    outline: none;
    background: #fff;
    box-shadow: 0 0 0 3px rgba(160,70,87,0.1);
}

.input-help {
    display: block;
    font-size: 10px;
    color: #999;
    margin-top: 4px;
}

.balance-preview {
    background: #f8f9ff;
    border-radius: 10px;
    padding: 10px;
    margin-bottom: 16px;
    border: 1px solid #e8e8ff;
}

.preview-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 4px 0;
    font-size: 12px;
}

.preview-row strong {
    color: #A04657;
    font-weight: 700;
}

.btn-submit {
    width: 100%;
    padding: 10px;
    background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
    color: #fff;
    border: none;
    border-radius: 10px;
    font-weight: 700;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.btn-submit:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(160,70,87,0.3);
}

/* Table Card */
.table-card {
    background: #fff;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    border: 1px solid #f0f0f0;
    width: 100%;
}

.table-card-header {
    padding: 14px 20px;
    border-bottom: 1px solid #f0f0f0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
}

.header-info {
    display: flex;
    align-items: center;
    gap: 8px;
}

.header-info i {
    font-size: 18px;
    color: #A04657;
}

.header-info h4 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
}

.table-search {
    position: relative;
}

.table-search i {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: #999;
    font-size: 12px;
}

.search-input {
    padding: 8px 12px 8px 35px;
    border: 2px solid #e8e8e8;
    border-radius: 30px;
    width: 200px;
    font-size: 12px;
    transition: all 0.2s;
}

.search-input:focus {
    border-color: #A04657;
    outline: none;
}

.table-card-body {
    padding: 0;
}

/* Modern Table */
.modern-table {
    width: 100%;
    border-collapse: collapse;
}

.modern-table th {
    padding: 12px 15px;
    background: #fafbfc;
    font-weight: 700;
    color: #555;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 1px solid #e8e8e8;
}

.modern-table td {
    padding: 12px 15px;
    border-bottom: 1px solid #f0f0f0;
    vertical-align: middle;
    font-size: 13px;
    color: #444;
}

.modern-table tbody tr:hover {
    background: #fef8f9;
}

.customer-name-cell {
    display: flex;
    align-items: center;
    gap: 10px;
}

.customer-name-cell .avatar {
    width: 32px;
    height: 32px;
    background: rgba(160,70,87,0.1);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.customer-name-cell .avatar i {
    font-size: 16px;
    color: #A04657;
}

.customer-name-cell strong {
    font-weight: 600;
    color: #1a1a2e;
}

.phone-cell, .vehicle-cell {
    display: flex;
    align-items: center;
    gap: 8px;
}

.phone-cell i, .vehicle-cell i {
    color: #A04657;
    font-size: 13px;
    width: 16px;
}

.opening-balance-cell {
    font-weight: 500;
    color: #6c5ce7;
}

.balance-cell {
    font-weight: 700;
    font-size: 14px;
}

.balance-cell.due {
    color: #dc3545;
}

.balance-cell.clear {
    color: #28a745;
}

/* Status Badges */
.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 4px 12px;
    border-radius: 30px;
    font-size: 11px;
    font-weight: 600;
}

.status-badge.due {
    background: #ffebee;
    color: #c62828;
}

.status-badge.clear {
    background: #e8f5e9;
    color: #2e7d32;
}

/* Action Buttons */
.action-buttons {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

.btn-action {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    transition: all 0.2s;
}

.btn-ledger {
    background: #e8eaf6;
    color: #3f51b5;
}

.btn-ledger:hover {
    background: #3f51b5;
    color: #fff;
    transform: scale(1.05);
}

.btn-credit {
    background: #e3f2fd;
    color: #1976d2;
}

.btn-credit:hover {
    background: #1976d2;
    color: #fff;
    transform: scale(1.05);
}

.btn-receive {
    background: #e8f5e9;
    color: #388e3c;
}

.btn-receive:hover {
    background: #388e3c;
    color: #fff;
    transform: scale(1.05);
}

.btn-delete {
    background: #ffebee;
    color: #d32f2f;
}

.btn-delete:hover {
    background: #d32f2f;
    color: #fff;
    transform: scale(1.05);
}

.btn-advance {
    background: #e3f2fd;
    color: #17a2b8;
}

.btn-advance:hover {
    background: #17a2b8;
    color: #fff;
    transform: scale(1.05);
}

.empty-state {
    text-align: center;
    padding: 40px !important;
}

.empty-state i {
    font-size: 48px;
    color: #A04657;
    margin-bottom: 12px;
    opacity: 0.5;
    display: block;
}

.empty-state p {
    margin: 0;
    color: #666;
    font-weight: 500;
    font-size: 13px;
}

.empty-state small {
    color: #999;
    font-size: 11px;
}

/* Responsive */
@media (max-width: 1100px) {
    .content-grid {
        grid-template-columns: 1fr;
    }
    .stats-row {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 768px) {
    .page-header {
        flex-direction: column;
        text-align: center;
        gap: 12px;
    }
    .stats-row {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    .table-card-header {
        flex-direction: column;
        align-items: stretch;
    }
    .table-search {
        width: 100%;
    }
    .search-input {
        width: 100%;
    }
    .modern-table th {
        display: none;
    }
    .modern-table td {
        display: block;
        text-align: right;
        padding-left: 50%;
        position: relative;
        border-bottom: none;
    }
    .modern-table td::before {
        content: attr(data-label);
        position: absolute;
        left: 15px;
        width: 45%;
        text-align: left;
        font-weight: 700;
        color: #A04657;
        font-size: 11px;
    }
    .modern-table tr {
        display: block;
        margin-bottom: 12px;
        border: 1px solid #e8e8e8;
        border-radius: 12px;
        overflow: hidden;
    }
    .modern-table td:last-child {
        border-bottom: none;
    }
    .action-buttons {
        justify-content: flex-end;
    }
    .form-card-body {
        padding: 16px;
    }
    .stat-card {
        padding: 12px 16px;
    }
    .stat-icon {
        width: 44px;
        height: 44px;
    }
    .stat-icon i {
        font-size: 22px;
    }
    .stat-info h3 {
        font-size: 22px;
    }
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 10px;
}

::-webkit-scrollbar-thumb {
    background: #A04657;
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: #6b2a36;
}
</style>

<script>
// Table search functionality
const searchInput = document.getElementById('tableSearch');
if(searchInput) {
    searchInput.addEventListener('keyup', function() {
        let searchValue = this.value.toLowerCase();
        let table = document.getElementById('customersTable');
        let rows = table.getElementsByTagName('tr');
        
        for (let i = 1; i < rows.length; i++) {
            let row = rows[i];
            let text = row.textContent.toLowerCase();
            if (text.includes(searchValue)) {
                row.style.display = '';
            } else {
                row.style.display = 'none';
            }
        }
    });
}

// Opening balance preview
const balanceInput = document.querySelector('input[name="opening_balance"]');
const balancePreview = document.getElementById('balancePreview');
const previewAmount = document.getElementById('previewAmount');
const previewStatus = document.getElementById('previewStatus');

function updateBalancePreview() {
    let balance = parseFloat(balanceInput?.value) || 0;
    
    if(balance > 0 && balancePreview) {
        balancePreview.style.display = 'block';
        previewAmount.innerHTML = 'Rs ' + balance.toFixed(2);
        previewStatus.innerHTML = 'Due (Previous outstanding)';
        previewStatus.style.color = '#dc3545';
    } else if(balancePreview) {
        balancePreview.style.display = 'none';
    }
}

if(balanceInput) {
    balanceInput.addEventListener('input', updateBalancePreview);
}
</script>

<?php include('../includes/ft.php'); ?>