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

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

$employee_id = isset($_GET['employee_id']) ? intval($_GET['employee_id']) : 0;
$success_message = "";
$error_message = "";

// Get employee details
$sql = "SELECT * FROM employees WHERE id = '$employee_id'";
$result = mysqli_query($conn, $sql);
$employee = mysqli_fetch_assoc($result);

if(!$employee) {
    header("Location: employees.php");
    exit();
}

// Get recent ledger entries (last 10)
$ledger_sql = "SELECT * FROM employee_ledger WHERE employee_id = '$employee_id' ORDER BY id DESC LIMIT 10";
$ledger_result = mysqli_query($conn, $ledger_sql);

// Calculate totals
$total_payable = 0;
$total_recovery = 0;
$all_ledger = mysqli_query($conn, "SELECT * FROM employee_ledger WHERE employee_id = '$employee_id'");
while($entry = mysqli_fetch_assoc($all_ledger)) {
    if(in_array($entry['entry_type'], ['Salary', 'Advance', 'Bonus', 'Opening Payable'])) {
        $total_payable += $entry['amount'];
    } elseif(in_array($entry['entry_type'], ['Recovery', 'Opening Receivable'])) {
        $total_recovery += $entry['amount'];
    }
}
?>

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Employee Details - <?php echo htmlspecialchars($employee['employee_name']); ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800&display=swap');
        
        * {
            font-family: 'Poppins', sans-serif;
        }
        
        body {
            background: #f5f5f5;
        }
        
        .page-header {
            background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
            border-radius: 15px;
            padding: 25px 30px;
            margin-bottom: 30px;
            color: #fff;
        }
        
        .page-header h2 {
            margin: 0 0 5px 0;
            font-size: 28px;
            color: #fff;
        }
        
        .profile-card {
            background: #fff;
            border-radius: 15px;
            overflow: hidden;
            margin-bottom: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }
        
        .profile-header {
            background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
            padding: 30px;
            text-align: center;
            color: #fff;
            position: relative;
        }
        
        .profile-avatar {
            width: 100px;
            height: 100px;
            background: rgba(255,255,255,0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 15px;
        }
        
        .profile-avatar i {
            font-size: 50px;
        }
        
        .profile-name {
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 5px;
        }
        
        .profile-designation {
            font-size: 14px;
            opacity: 0.9;
        }
        
        .profile-body {
            padding: 30px;
        }
        
        .info-row {
            display: flex;
            justify-content: space-between;
            padding: 12px 0;
            border-bottom: 1px solid #e0e0e0;
        }
        
        .info-label {
            font-weight: 600;
            color: #666;
        }
        
        .info-value {
            font-weight: 700;
            color: #333;
        }
        
        .balance-card {
            background: linear-gradient(135deg, #A04657 0%, #6b2a36 100%);
            border-radius: 12px;
            padding: 20px;
            text-align: center;
            color: #fff;
            margin-bottom: 20px;
        }
        
        .balance-amount {
            font-size: 36px;
            font-weight: 800;
        }
        
        .action-buttons {
            display: flex;
            gap: 15px;
            margin-top: 20px;
        }
        
        .btn-payable {
            background: #ffc107;
            color: #856404;
            flex: 1;
            padding: 12px;
            border-radius: 10px;
            text-decoration: none;
            font-weight: 700;
            text-align: center;
            transition: all 0.3s;
        }
        
        .btn-payable:hover {
            background: #e0a800;
            color: #856404;
        }
        
        .btn-paid {
            background: #28a745;
            color: #fff;
            flex: 1;
            padding: 12px;
            border-radius: 10px;
            text-decoration: none;
            font-weight: 700;
            text-align: center;
            transition: all 0.3s;
        }
        
        .btn-paid:hover {
            background: #1e7e34;
            color: #fff;
        }
        
        .btn-back {
            background: #6c757d;
            color: #fff;
            padding: 10px 20px;
            border-radius: 8px;
            text-decoration: none;
            font-weight: 600;
        }
        
        .btn-back:hover {
            background: #5a6268;
            color: #fff;
        }
        
        .table-card {
            background: #fff;
            border-radius: 15px;
            overflow: hidden;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            margin-bottom: 30px;
        }
        
        .card-header-custom {
            background: #f8f9fa;
            padding: 15px 20px;
            border-bottom: 1px solid #e0e0e0;
        }
        
        .card-header-custom h4 {
            margin: 0;
            font-size: 18px;
            font-weight: 700;
            color: #333;
        }
        
        .table {
            margin-bottom: 0;
        }
        
        .table th {
            background: #f8f9fa;
            font-weight: 700;
        }
        
        .badge-salary { background: #17a2b8; color: #fff; padding: 4px 10px; border-radius: 20px; font-size: 11px; }
        .badge-advance { background: #ffc107; color: #856404; padding: 4px 10px; border-radius: 20px; font-size: 11px; }
        .badge-bonus { background: #28a745; color: #fff; padding: 4px 10px; border-radius: 20px; font-size: 11px; }
        .badge-recovery { background: #dc3545; color: #fff; padding: 4px 10px; border-radius: 20px; font-size: 11px; }
        .badge-opening { background: #6c757d; color: #fff; padding: 4px 10px; border-radius: 20px; font-size: 11px; }
        
        .amount-positive { color: #28a745; font-weight: 700; }
        .amount-negative { color: #dc3545; font-weight: 700; }
        
        @media (max-width: 768px) {
            .action-buttons {
                flex-direction: column;
            }
            .balance-amount {
                font-size: 28px;
            }
        }
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="page-header">
        <div>
            <h2><i class="fas fa-user-tie"></i> Employee Details</h2>
            <p>View employee information and manage transactions</p>
        </div>
        <div>
            <a href="employees.php" class="btn-back">
                <i class="fas fa-arrow-left"></i> Back to Employees
            </a>
        </div>
    </div>
    
    <?php if(isset($_GET['msg'])): ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="fas fa-check-circle"></i> 
            <?php 
                if($_GET['msg'] == 'added') echo "Employee added successfully!";
                if($_GET['msg'] == 'payable_added') echo "Payable entry added successfully!";
                if($_GET['msg'] == 'paid_added') echo "Paid entry added successfully!";
            ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>
    
    <div class="row">
        <div class="col-lg-4">
            <div class="profile-card">
                <div class="profile-header">
                    <div class="profile-avatar">
                        <i class="fas fa-user-tie"></i>
                    </div>
                    <div class="profile-name"><?php echo htmlspecialchars($employee['employee_name']); ?></div>
                    <div class="profile-designation"><?php echo htmlspecialchars($employee['designation']); ?></div>
                </div>
                <div class="profile-body">
                    <div class="info-row">
                        <span class="info-label"><i class="fas fa-phone"></i> Phone</span>
                        <span class="info-value"><?php echo htmlspecialchars($employee['phone']); ?></span>
                    </div>
                    <div class="info-row">
                        <span class="info-label"><i class="fas fa-money-bill-wave"></i> Basic Salary</span>
                        <span class="info-value">Rs <?php echo number_format($employee['basic_salary'], 2); ?></span>
                    </div>
                    <div class="info-row">
                        <span class="info-label"><i class="fas fa-chart-line"></i> Opening Balance</span>
                        <span class="info-value">
                            Rs <?php echo number_format($employee['opening_balance'], 2); ?>
                            (<?php echo ucfirst($employee['opening_balance_type']); ?>)
                        </span>
                    </div>
                    <div class="info-row">
                        <span class="info-label"><i class="fas fa-calendar-alt"></i> Joined Date</span>
                        <span class="info-value"><?php echo date('d-m-Y', strtotime($employee['date_time'])); ?></span>
                    </div>
                    
                    <div class="balance-card">
                        <div class="balance-amount">Rs <?php echo number_format($employee['balance'], 2); ?></div>
                        <div>Current Outstanding Balance</div>
                        <small><?php echo ($employee['balance'] > 0) ? 'Company has to pay employee' : 'Employee has taken advance'; ?></small>
                    </div>
                    
                    <div class="action-buttons">
                        <a href="employee_add_payable.php?employee_id=<?php echo $employee_id; ?>" class="btn-payable">
                            <i class="fas fa-plus-circle"></i> Add Payable<br>
                            <small>(Salary/Advance/Bonus)</small>
                        </a>
                        <a href="employee_add_paid.php?employee_id=<?php echo $employee_id; ?>" class="btn-paid">
                            <i class="fas fa-money-bill-wave"></i> Add Paid<br>
                            <small>(Recovery)</small>
                        </a>
                    </div>
                    
                    <div class="mt-3 text-center">
                        <small class="text-muted">
                            <i class="fas fa-info-circle"></i> 
                            <strong>Payable:</strong> Company pays employee | 
                            <strong>Paid:</strong> Employee pays back
                        </small>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-lg-8">
            <div class="table-card">
                <div class="card-header-custom">
                    <h4><i class="fas fa-history"></i> Recent Transactions (Last 10)</h4>
                </div>
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Entry Type</th>
                                <th>Amount</th>
                                <th>Payment Method</th>
                                <th>Description</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if(mysqli_num_rows($ledger_result) > 0): ?>
                                <?php while($entry = mysqli_fetch_assoc($ledger_result)): ?>
                                    <tr>
                                        <td><?php echo date('d-m-Y h:i A', strtotime($entry['date_time'])); ?></td>
                                        <td>
                                            <?php
                                            $badge_class = '';
                                            if($entry['entry_type'] == 'Salary') $badge_class = 'badge-salary';
                                            elseif($entry['entry_type'] == 'Advance') $badge_class = 'badge-advance';
                                            elseif($entry['entry_type'] == 'Bonus') $badge_class = 'badge-bonus';
                                            elseif($entry['entry_type'] == 'Recovery') $badge_class = 'badge-recovery';
                                            else $badge_class = 'badge-opening';
                                            ?>
                                            <span class="<?php echo $badge_class; ?>">
                                                <i class="fas <?php echo ($entry['entry_type'] == 'Recovery') ? 'fa-undo-alt' : 'fa-plus-circle'; ?>"></i>
                                                <?php echo $entry['entry_type']; ?>
                                            </span>
                                        </td>
                                        <td class="<?php echo ($entry['entry_type'] != 'Recovery' && $entry['entry_type'] != 'Opening Receivable') ? 'amount-positive' : 'amount-negative'; ?>">
                                            <?php echo ($entry['entry_type'] != 'Recovery' && $entry['entry_type'] != 'Opening Receivable') ? '+' : '-'; ?> 
                                            Rs <?php echo number_format($entry['amount'], 2); ?>
                                        </td>
                                        <td>
                                            <?php if($entry['payment_method'] == 'bank'): ?>
                                                <i class="fas fa-university"></i> Bank
                                            <?php else: ?>
                                                <i class="fas fa-money-bill-wave"></i> Cash
                                            <?php endif; ?>
                                        </td>
                                        <td><?php echo htmlspecialchars($entry['description'] ?: '—'); ?></td>
                                    </tr>
                                <?php endwhile; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="5" class="text-center py-4">
                                        <i class="fas fa-book" style="font-size: 48px; color: #ccc;"></i>
                                        <p class="mt-2">No transactions yet</p>
                                    </td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
                <div class="card-header-custom text-end">
                    <a href="employee_ledger.php?employee_id=<?php echo $employee_id; ?>" class="btn-sm-custom">
                        <i class="fas fa-eye"></i> View Full Ledger
                    </a>
                </div>
            </div>
            
            <!-- Summary Stats -->
            <div class="row">
                <div class="col-md-6">
                    <div class="table-card">
                        <div class="card-header-custom">
                            <h4><i class="fas fa-arrow-up"></i> Total Payable</h4>
                        </div>
                        <div class="profile-body text-center">
                            <div class="amount-positive" style="font-size: 32px; font-weight: 800;">
                                Rs <?php echo number_format($total_payable, 2); ?>
                            </div>
                            <small class="text-muted">Salary + Advance + Bonus + Opening Payable</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="table-card">
                        <div class="card-header-custom">
                            <h4><i class="fas fa-arrow-down"></i> Total Recovery</h4>
                        </div>
                        <div class="profile-body text-center">
                            <div class="amount-negative" style="font-size: 32px; font-weight: 800;">
                                Rs <?php echo number_format($total_recovery, 2); ?>
                            </div>
                            <small class="text-muted">Recovery + Opening Receivable</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<?php include('../includes/ft.php'); ?>
</body>
</html>