<?php
include("../../classes/manager.class.php");
//include("../../classes/batch_setup.class.php");

$m = new Manager();
$output = '';
$name = '';  
$batch_qty = '';
$batch_date = '';
$product_id = '';
$description = '';
$status = '';
$current_date = date('d-m-Y');  // Set the current date

if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}

if (isset($_GET['add'])) {
    $action = "add";
} elseif (isset($_GET['edit']) && isset($_GET['id'])) {
    $action = "edit";
    $batch = BatchSetup::GetBatchById($_GET['id']);
    if ($batch) {
        $name = $batch->name;  
        $batch_qty = $batch->batch_qty;
        $batch_date = date('d-m-Y', strtotime($batch->batch_date));  // Format for display
        $product_id = $batch->product_id;
        $description = $batch->description;
        $status = $batch->status; 
    }
} else {
    header("Location: batch_setup.php?view");
    exit();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    extract($_REQUEST);
    $data['name'] = $name;  
    $data['product_id'] = $product_id;
    $data['batch_qty'] = $batch_qty;
    $data['batch_date'] = date('Y-m-d', strtotime($batch_date));
    $data['description'] = $description;
    $data['status'] = $status;
    $data['datetime'] = date('Y-m-d H:i:s');

    $errors = [];
    if (empty($name)) {
        array_push($errors, "Please enter batch name");
    }
    if (empty($batch_qty)) {
        array_push($errors, "Please enter batch quantity");
    }

    if (count($errors) == 0) {
        if ($action == "add") {
            $q = BatchSetup::AddBatch($data);
        } elseif ($action == "edit") {
            $q = BatchSetup::UpdateBatch($_GET['id'], $data);
        }

        if ($q) {
            header("Location: batch_setup.php?view&success");
            exit();
        } else {
            $output = "<div class='alert alert-danger'>Error in saving batch! " . $db->last_error . "</div>";
        }
    } else {
        foreach ($errors as $val) {
            $output .= "<div class='alert alert-warning'>$val</div>";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('../includes/inc_head.php'); ?>
    <!-- Include MaterializeCSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
    <!-- Include jQuery -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <!-- Initialize Materialize Datepicker -->
    <script>
        $(document).ready(function(){
            $('#batch_date').datepicker({
                format: 'dd-mm-yyyy',  // Adjust the date format as needed
                autoClose: true        // Automatically close the picker after date selection
            });
        });
    </script>
</head>
<body>
<div id="container">
    <?php include('../includes/inc_top.php'); ?>
    <div class="row">
        <div class="col s12 m12">
            <h3><?php echo ucfirst($action); ?> Batch</h3>
            <form action="" method="post" id="batch_form" novalidate="novalidate">
                <div class="col s12 form-group">
                    <?php echo $output; ?>
                    <div class="col s8">
                        <div class="row">
                            <span>(*) Mark Fields are mandatory</span><br/><br/>
                        </div>
                        <div class="row margin">
                            <div class="input-field col s12">
                                <input class="form-control" type="text" id="name" name="name"
                                       value='<?php echo $name; ?>' required/>
                                <label for="name" class="control-label">Batch Name *</label>
                            </div>
                        </div>
                        <div class="row margin">
                            <div class="input-field col s12">
                                <input class="form-control" type="number" id="batch_qty" name="batch_qty"
                                       value='<?php echo $batch_qty; ?>' required/>
                                <label for="batch_qty" class="control-label">Batch Quantity *</label>
                            </div>
                        </div>
                        <!-- Updated Date Picker Field -->
                        <div class="col m6 s12">
                            <div class="row margin">
                                <div class="input-field col s12">
                                    <input class="form-control datepicker" required type="text" id="batch_date"
                                           name="batch_date"
                                           placeholder="<?= date('01-01-Y') ?>"
                                           value='<?= !empty($batch_date) ? $batch_date : $current_date; ?>'/><br/>
                                    <label for="batch_date" class="control-label">Batch Date *</label>
                                </div>
                            </div>
                        </div>
                        <!-- End of Date Picker Field -->
                        <div class="row margin">
                            <div class="input-field col s12">
                                <textarea class="materialize-textarea" name="description"
                                          id="description"><?php echo $description; ?></textarea>
                                <label for="description" class="control-label">Description</label>
                            </div>
                        </div>
                        <div class="row margin">
                            <div class="input-field col s12">
                                <textarea class="materialize-textarea" name="status"
                                          id="status"><?php echo $status; ?></textarea>
                                <label for="status" class="control-label">Status</label>
                            </div>
                        </div>
                        <div class="row margin">
                            <div class="input-field col s12">
                                <button type="submit" class="btn waves-effect waves-light">Submit</button>
                                <button type="button" class="btn waves-effect waves-light"
                                        onclick="location.href='batch_setup.php?view'">Cancel
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<?php include('../includes/inc_footer.php'); ?>
<?php include('../includes/inc_js.php'); ?>
</body>
</html>
