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

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

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

// Add General Party
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_gp'])) {
    $gp_id = mysqli_real_escape_string($conn, $_POST['gp_id']);
    $gp_name = mysqli_real_escape_string($conn, $_POST['gp_name']);
    $gp_address = mysqli_real_escape_string($conn, $_POST['gp_address']);
    $gp_phone = mysqli_real_escape_string($conn, $_POST['gp_phone']);
    $opening_balance = mysqli_real_escape_string($conn, $_POST['opening_balance']);
    $date_time = date('Y-m-d H:i:s');
    
    if(empty($gp_name) || empty($gp_phone)) {
        $error_message = "GP Name and Phone are required!";
    } elseif(!is_numeric($opening_balance) || $opening_balance < 0) {
        $error_message = "Opening balance must be a valid positive number!";
    } else {
        $check_sql = "SELECT id FROM general_parties WHERE gp_id='$gp_id'";
        $check_result = mysqli_query($conn, $check_sql);
        
        if(mysqli_num_rows($check_result) > 0) {
            $error_message = "GP ID already exists! Please use a unique ID.";
        } else {
            $sql = "INSERT INTO general_parties (gp_id, gp_name, gp_address, gp_phone, opening_balance, current_balance, date_time) 
                    VALUES ('$gp_id', '$gp_name', '$gp_address', '$gp_phone', '$opening_balance', '$opening_balance', '$date_time')";
            
            if(mysqli_query($conn, $sql)) {
                $success_message = "General Party added successfully with opening balance of Rs " . number_format($opening_balance, 2);
                
                if($opening_balance > 0) {
                    $gp_id_db = mysqli_insert_id($conn);
                    $record_sql = "INSERT INTO gp_openings (gp_id, gp_name, amount, date_time, notes, user_name, date_created) 
                                   VALUES ('$gp_id_db', '$gp_name', '$opening_balance', '$date_time', 'Opening balance added', '{$_SESSION['username']}', '$date_time')";
                    mysqli_query($conn, $record_sql);
                }
            } else {
                $error_message = "Error: " . mysqli_error($conn);
            }
        }
    }
}

// Delete GP
if(isset($_GET['delete'])) {
    $delete_id = mysqli_real_escape_string($conn, $_GET['delete']);
    
    $check_sql = "SELECT id FROM gp_payable WHERE gp_id='$delete_id' 
                  UNION SELECT id FROM gp_paid WHERE gp_id='$delete_id'
                  UNION SELECT id FROM gp_received WHERE gp_id='$delete_id'";
    $check_result = mysqli_query($conn, $check_sql);
    
    if(mysqli_num_rows($check_result) > 0) {
        $error_message = "Cannot delete! GP has transaction records.";
    } else {
        $sql = "DELETE FROM general_parties WHERE id='$delete_id'";
        if(mysqli_query($conn, $sql)) {
            $success_message = "General Party deleted successfully!";
        } else {
            $error_message = "Error deleting party";
        }
    }
}

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

// Get statistics
$total_gp = mysqli_num_rows($result);
$total_balance = 0;
$gp_with_balance = 0;

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

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

<div class="gp-container">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-left">
            <h2><i class="fas fa-address-book"></i> General Party Ledger</h2>
            <p>Manage general parties, 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_gp; ?> Total Parties</span>
            </div>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="stats-row">
        <div class="stat-card">
            <div class="stat-icon"><i class="fas fa-address-book"></i></div>
            <div class="stat-info">
                <h3><?php echo $total_gp; ?></h3>
                <p>Total Parties</p>
                <span class="stat-trend">Registered General Parties</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 $gp_with_balance; ?></h3>
                <p>Active Credit Accounts</p>
                <span class="stat-trend">Parties 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 GP Form -->
        <div class="form-card">
            <div class="form-card-header">
                <i class="fas fa-plus-circle"></i>
                <h4>Add New General Party</h4>
            </div>
            <div class="form-card-body">
                <form method="POST" action="">
                    <div class="input-group-custom">
                        <label><i class="fas fa-id-card"></i> Party ID <span class="required">*</span></label>
                        <input type="text" name="gp_id" class="form-control-modern" required placeholder="Enter unique ID (e.g., GP-001)">
                        <small class="input-help">Unique identifier for this party</small>
                    </div>
                    
                    <div class="input-group-custom">
                        <label><i class="fas fa-user"></i> Party Name <span class="required">*</span></label>
                        <input type="text" name="gp_name" class="form-control-modern" required placeholder="Enter full name">
                        <small class="input-help">Full name of the general party</small>
                    </div>
                    
                    <div class="input-group-custom">
                        <label><i class="fas fa-map-marker-alt"></i> Address</label>
                        <textarea name="gp_address" class="form-control-modern" rows="2" placeholder="Complete address"></textarea>
                        <small class="input-help">Complete address of the party</small>
                    </div>
                    
                    <div class="input-group-custom">
                        <label><i class="fas fa-phone"></i> Phone Number <span class="required">*</span></label>
                        <input type="tel" name="gp_phone" class="form-control-modern" required placeholder="03XXXXXXXXX">
                        <small class="input-help">Active phone number for contact</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_gp" class="btn-submit">
                        <i class="fas fa-save"></i> Add General Party
                    </button>
                </form>
            </div>
        </div>
        
        <!-- General Parties List -->
        <div class="table-card">
            <div class="table-card-header">
                <div class="header-info">
                    <i class="fas fa-list"></i>
                    <h4>General Parties List</h4>
                </div>
                <div class="table-search">
                    <i class="fas fa-search"></i>
                    <input type="text" id="tableSearch" placeholder="Search party..." class="search-input">
                </div>
            </div>
            <div class="table-card-body">
                <div class="table-responsive">
                    <table class="modern-table" id="gpTable">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Party ID</th>
                                <th>Party Name</th>
                                <th>Phone</th>
                                <th>Opening Bal.</th>
                                <th>Current Bal.</th>
                                <th>Status</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php if(mysqli_num_rows($result) > 0): ?>
                            <?php 
                            $sr_no = 1;
                            while($row = mysqli_fetch_assoc($result)): 
                                $balance_status = $row['current_balance'] > 0 ? 'due' : ($row['current_balance'] < 0 ? 'advance' : 'clear');
                                $balance_badge_class = $row['current_balance'] > 0 ? 'badge-due' : ($row['current_balance'] < 0 ? 'badge-advance' : 'badge-clear');
                            ?>
                                <tr>
                                    <td data-label="#"><?php echo $sr_no++; ?></td>
                                    <td data-label="Party ID">
                                        <span class="party-id-badge"><?php echo htmlspecialchars($row['gp_id']); ?></span>
                                    </div>
                                    <td data-label="Party Name">
                                        <div class="party-name-cell">
                                            <div class="party-avatar"><i class="fas fa-building"></i></div>
                                            <strong><?php echo htmlspecialchars($row['gp_name']); ?></strong>
                                        </div>
                                    </div>
                                    <td data-label="Phone">
                                        <div class="phone-cell"><i class="fas fa-phone-alt"></i> <?php echo htmlspecialchars($row['gp_phone']); ?></div>
                                    </div>
                                    <td data-label="Opening Bal.">Rs <?php echo number_format($row['opening_balance'], 2); ?></div>
                                    <td data-label="Current Bal.">
                                        <div class="current-balance <?php echo $balance_status; ?>">
                                            Rs <?php echo number_format(abs($row['current_balance']), 2); ?>
                                            <?php if($row['current_balance'] > 0): ?>
                                                <span class="dr-cr">Dr</span>
                                            <?php elseif($row['current_balance'] < 0): ?>
                                                <span class="dr-cr">Cr</span>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                    <td data-label="Status">
                                        <span class="status-badge <?php echo $balance_badge_class; ?>">
                                            <?php if($row['current_balance'] > 0): ?>
                                                <i class="fas fa-hourglass-half"></i> Due
                                            <?php elseif($row['current_balance'] < 0): ?>
                                                <i class="fas fa-money-bill-wave"></i> Advance
                                            <?php else: ?>
                                                <i class="fas fa-check-circle"></i> Clear
                                            <?php endif; ?>
                                        </span>
                                    </div>
                                    <td data-label="Actions">
                                        <div class="action-buttons">
                                            <a href="gp_ledger.php?id=<?php echo $row['id']; ?>" class="btn-action btn-ledger" title="View Full Ledger">
                                                <i class="fas fa-book-open"></i>
                                            </a>
                                            <a href="gp_payable.php?gp_id=<?php echo $row['id']; ?>" class="btn-action btn-payable" title="Add Payable">
                                                <i class="fas fa-plus-circle"></i>
                                            </a>
                                            <a href="gp_paid.php?gp_id=<?php echo $row['id']; ?>" class="btn-action btn-paid" title="Add Paid">
                                                <i class="fas fa-check-circle"></i>
                                            </a>
                                            <a href="gp_received.php?gp_id=<?php echo $row['id']; ?>" class="btn-action btn-received" title="Add Received">
                                                <i class="fas fa-hand-holding-usd"></i>
                                            </a>
                                            <a href="?delete=<?php echo $row['id']; ?>" class="btn-action btn-delete" onclick="return confirm('Delete this party?')" title="Delete Party">
                                                <i class="fas fa-trash-alt"></i>
                                            </a>
                                        </div>
                                    </div>
                                </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="8" class="empty-state">
                                    <i class="fas fa-address-book"></i>
                                    <p>No general parties found</p>
                                </div>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
                </div>
            </div>
        </div>
    </div>
</div>

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

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

body {
    background: #f5f7fb;
    overflow-x: hidden;
}

h1, h2, h3, h4, h5, h6,
.page-header h2,
.stat-info h3,
.form-card-header h4,
.header-info h4,
.modern-table th {
    font-family: 'Poppins', sans-serif;
    font-weight: 600;
}

/* Main Content */
.main-content {
    margin-left: 280px;
    padding: 15px 20px;
    transition: all 0.3s;
    background: #f5f7fb;
}

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

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

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

.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 18px;
    border-radius: 40px;
    font-weight: 600;
    font-size: 12px;
}

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

.stat-card {
    background: #fff;
    border-radius: 16px;
    padding: 16px;
    display: flex;
    align-items: center;
    gap: 14px;
    border: 1px solid #f0f0f0;
    transition: all 0.3s;
}

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

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

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

.stat-info h3 {
    font-size: 22px;
    margin: 0;
    font-weight: 700;
    color: #1a1a2e;
}

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

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

/* Alerts */
.alert-custom {
    padding: 12px 20px;
    border-radius: 14px;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 13px;
    width: 100%;
}

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

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

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

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

/* Form Card */
.form-card {
    background: #fff;
    border-radius: 16px;
    overflow: hidden;
    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;
}

.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;
}

.required {
    color: #dc3545;
}

.form-control-modern {
    width: 100%;
    padding: 9px 12px;
    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;
}

.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;
    padding: 4px 0;
    font-size: 11px;
}

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

.btn-submit {
    width: 100%;
    padding: 10px;
    background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
    color: #fff;
    border: none;
    border-radius: 10px;
    font-weight: 600;
    font-size: 13px;
    cursor: pointer;
}

.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;
    border: 1px solid #f0f0f0;
    width: 100%;
}

.table-card-header {
    padding: 12px 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: 15px;
}

.table-search {
    position: relative;
}

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

.search-input {
    padding: 7px 12px 7px 32px;
    border: 2px solid #e8e8e8;
    border-radius: 30px;
    width: 200px;
    font-size: 12px;
}

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

.table-responsive {
    overflow-x: auto;
    width: 100%;
}

.modern-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
    min-width: 800px;
}

.modern-table th {
    padding: 10px 10px;
    background: #f8f9fa;
    font-weight: 700;
    color: #555;
    font-size: 11px;
    border-bottom: 1px solid #e8e8e8;
}

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

.party-id-badge {
    background: #e3f2fd;
    color: #1976d2;
    padding: 3px 8px;
    border-radius: 15px;
    font-size: 10px;
    font-weight: 600;
    display: inline-block;
}

.party-name-cell {
    display: flex;
    align-items: center;
    gap: 8px;
}

.party-avatar {
    width: 28px;
    height: 28px;
    background: rgba(160,70,87,0.1);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.party-avatar i {
    font-size: 14px;
    color: #A04657;
}

.phone-cell {
    display: flex;
    align-items: center;
    gap: 5px;
}

.phone-cell i {
    color: #A04657;
    font-size: 11px;
}

.current-balance {
    font-weight: 700;
    font-size: 13px;
}

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

.current-balance.advance {
    color: #17a2b8;
}

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

.dr-cr {
    font-size: 8px;
    font-weight: 600;
    margin-left: 2px;
}

.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px;
    border-radius: 20px;
    font-size: 9px;
    font-weight: 600;
}

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

.badge-advance {
    background: #e3f2fd;
    color: #1565c0;
}

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

.action-buttons {
    display: flex;
    gap: 5px;
    flex-wrap: wrap;
}

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

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

.btn-payable {
    background: #fff3e0;
    color: #ff9800;
}
.btn-payable:hover {
    background: #ff9800;
    color: #fff;
}

.btn-paid {
    background: #e8f5e9;
    color: #4caf50;
}
.btn-paid:hover {
    background: #4caf50;
    color: #fff;
}

.btn-received {
    background: #e3f2fd;
    color: #2196f3;
}
.btn-received:hover {
    background: #2196f3;
    color: #fff;
}

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

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

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

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

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

@media (max-width: 992px) {
    .main-content {
        margin-left: 0;
        padding: 12px 15px;
    }
}

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

/* 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
const searchInput = document.getElementById('tableSearch');
if(searchInput) {
    searchInput.addEventListener('keyup', function() {
        let value = this.value.toLowerCase();
        let rows = document.querySelectorAll('#gpTable tbody tr');
        rows.forEach(row => {
            row.style.display = row.textContent.toLowerCase().includes(value) ? '' : '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);
}

// Auto hide alerts
document.querySelectorAll('.alert-custom').forEach(alert => {
    setTimeout(() => {
        alert.style.opacity = '0';
        setTimeout(() => {
            if(alert && alert.parentElement) alert.remove();
        }, 300);
    }, 5000);
});
</script>

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