<?php
$page_title = 'List Standard Times';
require_once('includes/load.php');
page_require_level(2);

// Fungsi untuk mengambil data dari tabel line_process
function get_line_processes() {
    global $db;
    $sql = "SELECT * FROM line_process";
    $result = $db->query($sql);
    $processes = [];

    if ($result) {
        while ($row = $db->fetch_assoc($result)) {
            $processes[] = $row;
        }
    }

    return $processes;
}

// Hapus entri berdasarkan ID
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $sql = "DELETE FROM line_process WHERE id = '{$id}'";
    
    if ($db->query($sql)) {
        $session->msg('s', 'Record deleted successfully.');
    } else {
        $session->msg('d', 'Failed to delete the record.');
    }
    
    redirect('list_std_time.php', false);
}

$line_processes = get_line_processes();
include_once('layouts/header.php');
?>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <strong>
                    <span class="glyphicon glyphicon-th"></span>
                    <span>Standard Times List</span>
                </strong>
            </div>
            <div class="panel-body">
                <table id="std-times-table" class="table table-bordered">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Part Number</th>
                            <th>Process No</th>
                            <th>Process Name</th>
                            <th>Man Power</th>
                            <th>Standard Time (minutes)</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (!empty($line_processes)) : ?>
                            <?php foreach ($line_processes as $process) : ?>
                                <tr>
                                    <td><?php echo remove_junk($process['id']); ?></td>
                                    <td><?php echo remove_junk($process['part_number']); ?></td>
                                    <td><?php echo remove_junk($process['process_no']); ?></td>
                                    <td><?php echo remove_junk($process['name_process']); ?></td>
                                    <td><?php echo remove_junk($process['man_power']); ?></td>
                                    <td><?php echo remove_junk($process['std_time']); ?></td>
                                    <td>
                                        <a href="list_std_time.php?delete=<?php echo (int)$process['id']; ?>" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete this record?')">Delete</a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else : ?>
                            <tr>
                                <td colspan="7">No data available</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>
