<?php  
// Konfigurasi database  
define('DB_HOST', 'localhost');          // Set database host  
define('DB_USER', 'wiselie_nanda');      // Set database user     
define('DB_PASS', 'Clemira123');         // Set database password    
define('DB_NAME', 'wiselie_inventory');  // Set database name  
  
// Fungsi untuk menghubungkan ke database  
function getDBConnection() {  
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);  
      
    if ($conn->connect_error) {  
        die("Connection failed: " . $conn->connect_error);  
    }  
      
    return $conn;  
}  
  
// Membuat tabel jika belum ada  
function createTable() {  
    $conn = getDBConnection();  
    $sql = "CREATE TABLE IF NOT EXISTS tasks (  
        id INT(11) AUTO_INCREMENT PRIMARY KEY,  
        task_name VARCHAR(255) NOT NULL,  
        description TEXT NOT NULL,  
        division ENUM('Production', 'Warehouse') NOT NULL,  
        status ENUM('Open', 'Closed') DEFAULT 'Open',  
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP  
    )";  
  
    if ($conn->query($sql) === TRUE) {  
        // Tabel berhasil dibuat  
    } else {  
        echo "Error creating table: " . $conn->error;  
    }  
  
    $conn->close();  
}  
  
// Menambahkan tugas baru  
if ($_SERVER['REQUEST_METHOD'] == 'POST') {  
    $task_name = $_POST['task_name'];  
    $description = $_POST['description'];  
    $division = $_POST['division'];  
    $status = $_POST['status'];  
  
    $conn = getDBConnection();  
    $stmt = $conn->prepare("INSERT INTO tasks (task_name, description, division, status) VALUES (?, ?, ?, ?)");  
    $stmt->bind_param("ssss", $task_name, $description, $division, $status);  
      
    if ($stmt->execute()) {  
        echo "<script>alert('New task added successfully.');</script>";  
    } else {  
        echo "<script>alert('Error: " . $stmt->error . "');</script>";  
    }  
      
    $stmt->close();  
    $conn->close();  
}  
  
// Panggil fungsi untuk membuat tabel  
createTable();  
?>  
  
<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>Operation Task Management</title>  
    <style>  
        body {  
            font-family: 'Arial', sans-serif;  
            margin: 0;  
            padding: 0;  
            background-color: #f4f4f4;  
        }  
  
        .container {  
            width: 90%;  
            max-width: 1200px;  
            margin: auto;  
            overflow: hidden;  
            padding: 20px;  
            background: #ffffff;  
            border-radius: 8px;  
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);  
        }  
  
        header {  
            background: #35424a;  
            color: #ffffff;  
            padding: 20px;  
            text-align: center;  
            border-radius: 8px 8px 0 0;  
        }  
  
        h1 {  
            margin: 0;  
            font-size: 2.5em;  
        }  
  
        main {  
            padding: 20px;  
        }  
  
        .add-task, .task-list {  
            margin-bottom: 20px;  
            padding: 20px;  
            background: #f9f9f9;  
            border-radius: 8px;  
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);  
        }  
  
        .add-task h2, .task-list h2 {  
            margin-top: 0;  
            color: #35424a;  
        }  
  
        form {  
            display: flex;  
            flex-direction: column;  
        }  
  
        label {  
            margin: 10px 0 5px;  
            font-weight: bold;  
        }  
  
        input[type="text"], textarea, select {  
            padding: 10px;  
            border: 1px solid #ccc;  
            border-radius: 5px;  
            margin-bottom: 15px;  
            font-size: 1em;  
        }  
  
        input[type="submit"] {  
            background: #35424a;  
            color: #ffffff;  
            border: none;  
            padding: 10px;  
            border-radius: 5px;  
            cursor: pointer;  
            font-size: 1em;  
            transition: background 0.3s;  
        }  
  
        input[type="submit"]:hover {  
            background: #45a049;  
        }  
  
        table {  
            width: 100%;  
            border-collapse: collapse;  
            margin-top: 10px;  
        }  
  
        table, th, td {  
            border: 1px solid #ccc;  
        }  
  
        th, td {  
            padding: 10px;  
            text-align: left;  
        }  
  
        th {  
            background: #35424a;  
            color: #ffffff;  
        }  
  
        tr:nth-child(even) {  
            background: #f2f2f2;  
        }  
  
        footer {  
            text-align: center;  
            padding: 10px 0;  
            background: #35424a;  
            color: #ffffff;  
            border-radius: 0 0 8px 8px;  
            margin-top: 20px;  
        }  
  
        @media (max-width: 768px) {  
            .container {  
                width: 95%;  
            }  
  
            h1 {  
                font-size: 2em;  
            }  
        }  
    </style>  
</head>  
<body>  
    <div class="container">  
        <header>  
            <h1>Operation Task Management</h1>  
        </header>  
          
        <main>  
            <section class="add-task">  
                <h2>Add New Task</h2>  
                <form method="POST" action="">  
                    <label for="task_name">Task Name:</label>  
                    <input type="text" id="task_name" name="task_name" required>  
                      
                    <label for="description">Description:</label>  
                    <textarea id="description" name="description" required></textarea>  
                      
                    <label for="division">Division:</label>  
                    <select id="division" name="division" required>  
                        <option value="Production">Production</option>  
                        <option value="Warehouse">Warehouse</option>  
                    </select>  
                      
                    <label for="status">Status:</label>  
                    <select id="status" name="status" required>  
                        <option value="Open">Open</option>  
                        <option value="Closed">Closed</option>  
                    </select>  
                      
                    <input type="submit" value="Add Task">  
                </form>  
            </section>  
  
            <section class="task-list">  
                <h2>Task List</h2>  
                <table>  
                    <tr>  
                        <th>ID</th>  
                        <th>Task Name</th>  
                        <th>Description</th>  
                        <th>Division</th>  
                        <th>Status</th>  
                        <th>Created At</th>  
                    </tr>  
                    <?php  
                    // Menampilkan daftar tugas  
                    $conn = getDBConnection();  
                    $result = $conn->query("SELECT * FROM tasks");  
  
                    while ($row = $result->fetch_assoc()) {  
                        echo "<tr>  
                                <td>{$row['id']}</td>  
                                <td>{$row['task_name']}</td>  
                                <td>{$row['description']}</td>  
                                <td>{$row['division']}</td>  
                                <td>{$row['status']}</td>  
                                <td>{$row['created_at']}</td>  
                              </tr>";  
                    }  
  
                    $conn->close();  
                    ?>  
                </table>  
            </section>  
        </main>  
  
        <footer>  
            <p>&copy; 2025 Operation Task Management. All rights reserved.</p>  
        </footer>  
    </div>  
</body>  
</html>  
