<?php
$page_title = 'Input Line Process';
require_once('includes/load.php');
page_require_level(2);

if (isset($_POST['add_processes'])) {
    $part_number = remove_junk($db->escape($_POST['part_number']));
    $process_nos = $_POST['process_no'];
    $name_processes = $_POST['name_process'];
    $man_powers = $_POST['man_power'];
    $std_times = $_POST['std_time'];

    $errors = [];
    
    if (empty($part_number)) {
        $errors[] = 'Part number cannot be empty.';
    }

    // Insert data into the database
    foreach ($process_nos as $index => $process_no) {
        $process_no = remove_junk($db->escape($process_no));
        $name_process = remove_junk($db->escape($name_processes[$index]));
        $man_power = remove_junk($db->escape($man_powers[$index]));
        $std_time = remove_junk($db->escape($std_times[$index]));

        if (!empty($process_no) && !empty($name_process) && !empty($man_power) && !empty($std_time)) {
            $sql = "INSERT INTO line_process (part_number, process_no, name_process, man_power, std_time)";
            $sql .= " VALUES ('{$part_number}', '{$process_no}', '{$name_process}', '{$man_power}', '{$std_time}')";
            
            if (!$db->query($sql)) {
                $errors[] = 'Failed to add some line processes.';
            }
        }
    }

    if (empty($errors)) {
        $session->msg('s', 'Line processes added successfully!');
        redirect('input_line_process.php', false);
    } else {
        $session->msg('d', implode('<br>', $errors));
        redirect('input_line_process.php', false);
    }
}

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>Add Line Processes</span>
                </strong>
            </div>
            <div class="panel-body">
                <form method="post" action="input_line_process.php">
                    <div class="form-group">
                        <label for="part_number">Part Number</label>
                        <input type="text" class="form-control" name="part_number" placeholder="Enter Part Number" required>
                    </div>
                    <div id="line-processes">
                        <div class="line-process-row">
                            <div class="form-group">
                                <label for="process_no">Process No</label>
                                <input type="number" class="form-control" name="process_no[]" placeholder="Enter Process Number" required>
                            </div>
                            <div class="form-group">
                                <label for="name_process">Process Name</label>
                                <input type="text" class="form-control" name="name_process[]" placeholder="Enter Process Name" required>
                            </div>
                            <div class="form-group">
                                <label for="man_power">Man Power</label>
                                <input type="number" class="form-control" name="man_power[]" placeholder="Enter Man Power" required>
                            </div>
                            <div class="form-group">
                                <label for="std_time">Standard Time (in minutes)</label>
                                <input type="text" class="form-control" name="std_time[]" placeholder="Enter Standard Time" required>
                            </div>
                            <hr>
                        </div>
                    </div>
                    <button type="button" id="add-row" class="btn btn-info">Add Another Process</button>
                    <button type="submit" name="add_processes" class="btn btn-primary">Add Processes</button>
                    <a href="list_std_time.php" class="btn btn-secondary">View Standard Times</a>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- Script to dynamically add rows -->
<script>
document.getElementById('add-row').addEventListener('click', function () {
    var container = document.getElementById('line-processes');
    var newRow = document.createElement('div');
    newRow.className = 'line-process-row';
    newRow.innerHTML = `
        <div class="form-group">
            <label for="process_no">Process No</label>
            <input type="number" class="form-control" name="process_no[]" placeholder="Enter Process Number" required>
        </div>
        <div class="form-group">
            <label for="name_process">Process Name</label>
            <input type="text" class="form-control" name="name_process[]" placeholder="Enter Process Name" required>
        </div>
        <div class="form-group">
            <label for="man_power">Man Power</label>
            <input type="number" class="form-control" name="man_power[]" placeholder="Enter Man Power" required>
        </div>
        <div class="form-group">
            <label for="std_time">Standard Time (in minutes)</label>
            <input type="text" class="form-control" name="std_time[]" placeholder="Enter Standard Time" required>
        </div>
        <hr>
    `;
    container.appendChild(newRow);
});
</script>

<style>
.line-process-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 15px;
}

.line-process-row .form-group {
    flex: 1;
    margin-right: 10px;
}

.line-process-row .form-group:last-child {
    margin-right: 0;
}
</style>

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