<?php
// Include the database connection
include 'db_connection.php';

// Initialize variables
$message = '';
$message_type = '';

// Process form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $description = $_POST['description'];
    $amount = $_POST['amount'];
    $date = $_POST['date'];

    $query = "INSERT INTO assets (description, amount, date) VALUES (?, ?, ?)";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("sds", $description, $amount, $date);
    
    if ($stmt->execute()) {
        $message = "Asset added successfully.";
        $message_type = "success";
    } else {
        $message = "Error adding asset: " . $conn->error;
        $message_type = "error";
    }
    
    $stmt->close();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Asset - Asset Management System</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>
        :root {
            --primary: #3a57e8;
            --secondary: #6c757d;
            --success: #1cbb8c;
            --info: #17a2b8;
            --warning: #f5b759;
            --danger: #fc5b69;
            --light: #f8f9fa;
            --dark: #343a40;
        }
        
        body {
            font-family: 'Poppins', sans-serif;
            background-color: #f5f7ff;
            color: #333;
        }
        
        .dashboard-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .header {
            background: linear-gradient(135deg, var(--primary) 0%, #2039c5 100%);
            color: white;
            padding: 20px;
            border-radius: 10px;
            margin-bottom: 25px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        .card {
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            margin-bottom: 20px;
            border: none;
        }
        
        .card-header {
            background: linear-gradient(135deg, var(--primary) 0%, #2039c5 100%);
            color: white;
            border-radius: 10px 10px 0 0 !important;
            font-weight: 600;
            padding: 15px 20px;
        }
        
        .form-container {
            padding: 25px;
        }
        
        .form-label {
            font-weight: 600;
            color: var(--dark);
            margin-bottom: 8px;
        }
        
        .form-control {
            border-radius: 8px;
            padding: 12px 15px;
            border: 1px solid #e1e5eb;
            transition: all 0.3s;
        }
        
        .form-control:focus {
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(58, 87, 232, 0.15);
        }
        
        .btn-primary {
            background: linear-gradient(135deg, var(--primary) 0%, #2039c5 100%);
            border: none;
            padding: 12px 25px;
            font-weight: 600;
            border-radius: 8px;
            transition: all 0.3s;
        }
        
        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(58, 87, 232, 0.3);
        }
        
        .alert {
            border-radius: 8px;
            padding: 15px 20px;
            margin-bottom: 20px;
            border: none;
        }
        
        .alert-success {
            background-color: rgba(28, 187, 140, 0.15);
            color: #0f6848;
            border-left: 4px solid var(--success);
        }
        
        .alert-error {
            background-color: rgba(252, 91, 105, 0.15);
            color: #d62536;
            border-left: 4px solid var(--danger);
        }
        
        .stats-card {
            text-align: center;
            padding: 20px;
            border-radius: 10px;
            background: white;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            margin-bottom: 20px;
        }
        
        .stats-number {
            font-size: 24px;
            font-weight: 700;
            color: var(--primary);
        }
        
        .stats-label {
            color: var(--secondary);
            font-size: 14px;
        }
        
        .recent-assets {
            margin-top: 30px;
        }
        
        .table th {
            background-color: var(--primary);
            color: white;
        }
        
        .highlight-row {
            background-color: rgba(58, 87, 232, 0.05);
        }
        
        .action-buttons .btn {
            padding: 0.25rem 0.5rem;
            font-size: 0.875rem;
        }
        
        @media (max-width: 768px) {
            .dashboard-container {
                padding: 15px;
            }
            
            .header {
                padding: 15px;
            }
            
            .form-container {
                padding: 20px 15px;
            }
        }
    </style>
</head>
<body>
    <div class="dashboard-container">
        <!-- Header -->
        <div class="header">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <h2><i class="fas fa-cube me-2"></i>Asset Management</h2>
                    <p class="mb-0">Add and manage company assets</p>
                </div>
                <div class="col-md-4 text-md-end">
                    <a href="dashboard.php" class="btn btn-light"><i class="fas fa-arrow-left me-1"></i> Back to Dashboard</a>
                </div>
            </div>
        </div>
        
        <!-- Display messages -->
        <?php if (!empty($message)): ?>
            <div class="alert alert-<?php echo $message_type == 'success' ? 'success' : 'error'; ?>">
                <i class="fas fa-<?php echo $message_type == 'success' ? 'check-circle' : 'exclamation-circle'; ?> me-2"></i>
                <?php echo $message; ?>
            </div>
        <?php endif; ?>
        
        <div class="row">
            <div class="col-md-8">
                <!-- Add Asset Form -->
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0"><i class="fas fa-plus-circle me-2"></i>Add New Asset</h5>
                    </div>
                    <div class="form-container">
                        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                            <div class="mb-3">
                                <label for="description" class="form-label">Description</label>
                                <input type="text" class="form-control" id="description" name="description" required>
                            </div>
                            
                            <div class="mb-3">
                                <label for="amount" class="form-label">Amount ($)</label>
                                <input type="number" step="0.01" class="form-control" id="amount" name="amount" required>
                            </div>
                            
                            <div class="mb-4">
                                <label for="date" class="form-label">Date</label>
                                <input type="date" class="form-control" id="date" name="date" required>
                            </div>
                            
                            <button type="submit" class="btn btn-primary w-100">
                                <i class="fas fa-save me-2"></i>Add Asset
                            </button>
                        </form>
                    </div>
                </div>
                
                <!-- Recent Assets Table -->
                <div class="card recent-assets">
                    <div class="card-header">
                        <h5 class="mb-0"><i class="fas fa-history me-2"></i>Recently Added Assets</h5>
                    </div>
                    <div class="form-container">
                        <?php
                        // Fetch recent assets
                        $query = "SELECT * FROM assets ORDER BY date DESC LIMIT 5";
                        $result = $conn->query($query);
                        
                        if ($result && $result->num_rows > 0): ?>
                            <div class="table-responsive">
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <th>Description</th>
                                            <th>Amount</th>
                                            <th>Date</th>
                                            <th>Actions</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php while ($row = $result->fetch_assoc()): ?>
                                        <tr class="highlight-row">
                                            <td><?php echo htmlspecialchars($row['description']); ?></td>
                                            <td>$<?php echo number_format($row['amount'], 2); ?></td>
                                            <td><?php echo date('M j, Y', strtotime($row['date'])); ?></td>
                                            <td class="action-buttons">
                                                <button class="btn btn-sm btn-outline-primary">
                                                    <i class="fas fa-edit"></i>
                                                </button>
                                                <button class="btn btn-sm btn-outline-danger">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </td>
                                        </tr>
                                        <?php endwhile; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php else: ?>
                            <div class="text-center py-4">
                                <i class="fas fa-cube fa-3x text-muted mb-3"></i>
                                <p class="text-muted">No assets found. Add your first asset above.</p>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            
            <div class="col-md-4">
                <!-- Statistics Cards -->
                <div class="stats-card">
                    <i class="fas fa-dollar-sign fa-2x text-primary mb-3"></i>
                    <div class="stats-number">
                        <?php
                        $query = "SELECT SUM(amount) as total FROM assets";
                        $result = $conn->query($query);
                        $total = $result->fetch_assoc()['total'];
                        echo '$' . number_format($total, 2);
                        ?>
                    </div>
                    <div class="stats-label">Total Asset Value</div>
                </div>
                
                <div class="stats-card">
                    <i class="fas fa-cubes fa-2x text-primary mb-3"></i>
                    <div class="stats-number">
                        <?php
                        $query = "SELECT COUNT(*) as count FROM assets";
                        $result = $conn->query($query);
                        $count = $result->fetch_assoc()['count'];
                        echo number_format($count);
                        ?>
                    </div>
                    <div class="stats-label">Total Assets</div>
                </div>
                
                <div class="stats-card">
                    <i class="fas fa-calendar-alt fa-2x text-primary mb-3"></i>
                    <div class="stats-number">
                        <?php
                        $query = "SELECT COUNT(*) as count FROM assets WHERE date >= DATE_SUB(NOW(), INTERVAL 30 DAY)";
                        $result = $conn->query($query);
                        $recent = $result->fetch_assoc()['count'];
                        echo number_format($recent);
                        ?>
                    </div>
                    <div class="stats-label">Assets Added (Last 30 Days)</div>
                </div>
                
                <!-- Quick Actions -->
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0"><i class="fas fa-bolt me-2"></i>Quick Actions</h5>
                    </div>
                    <div class="form-container">
                        <!-- Replace the placeholder buttons with these -->
<div class="d-grid gap-2">
    <a href="export_assets.php" class="btn btn-outline-primary">
        <i class="fas fa-file-export me-2"></i>Export Assets
    </a>
    <a href="generate_report1.php" target="_blank" class="btn btn-outline-success">
        <i class="fas fa-print me-2"></i>Generate Report
    </a>
    <a href="view_analytics.php" class="btn btn-outline-info">
        <i class="fas fa-chart-line me-2"></i>View Analytics
    </a>
</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Set default date to today
        document.getElementById('date').valueAsDate = new Date();
        
        // Add animation to form submission
        const form = document.querySelector('form');
        form.addEventListener('submit', function() {
            const button = this.querySelector('button[type="submit"]');
            button.innerHTML = '<i class="fas fa-spinner fa-spin me-2"></i>Adding...';
            button.disabled = true;
        });
    </script>
</body>
</html>

<?php
// Close database connection
$conn->close();
?>