<?php
$page_title = 'Edit Product';
require_once('includes/load.php');
// Checkin What level user has permission to view this page
page_require_level(1);

$all_files = find_all('files');
$product = find_by_id('products', (int)$_GET['id']);
$all_categories = find_all('categories');
$all_photo = find_all('media');
if (!$product) {
    $session->msg("d", "Missing product id.");
    redirect('product.php');
}

if (isset($_POST['product'])) {
    $req_fields = array('product-title', 'product-categorie', 'product-quantity', 'buying-price', 'part-number', 'product-uom', 'new-code');
    validate_fields($req_fields);

    if (empty($errors)) {
        $p_name  = remove_junk($db->escape($_POST['product-title']));
        $p_cat   = (int)$_POST['product-categorie'];
        $p_qty   = remove_junk($db->escape($_POST['product-quantity']));
        $p_buy   = remove_junk($db->escape($_POST['buying-price']));
        $p_part_number = remove_junk($db->escape($_POST['part-number']));
        $p_uom   = remove_junk($db->escape($_POST['product-uom']));
        $p_new_code = remove_junk($db->escape($_POST['new-code']));

        if (is_null($_POST['product-photo']) || $_POST['product-photo'] === "") {
            $media_id = '0';
        } else {
            $media_id = remove_junk($db->escape($_POST['product-photo']));
        }

        $query   = "UPDATE products SET";
        $query  .= " name ='{$p_name}', quantity ='{$p_qty}',";
        $query  .= " buy_price ='{$p_buy}', categorie_id ='{$p_cat}', media_id='{$media_id}', part_number='{$p_part_number}', UoM='{$p_uom}', new_code='{$p_new_code}'";
        $query  .= " WHERE id ='{$product['id']}'";
        $result = $db->query($query);

        if ($result && $db->affected_rows() === 1) {
            $session->msg('s', "Product updated ");
            redirect('product.php', false);
        } else {
            $session->msg('d', ' Sorry failed to update!');
            redirect('edit_product.php?id=' . $product['id'], false);
        }
    } else {
        $session->msg("d", $errors);
        redirect('edit_product.php?id=' . $product['id'], false);
    }
}
?>

<?php 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="panel panel-default">
        <div class="panel-heading">
            <strong>
                <span class="glyphicon glyphicon-th"></span>
                <span>Edit Product</span>
            </strong>
        </div>
        <div class="panel-body">
            <div class="col-md-7">
                <form method="post" action="edit_product.php?id=<?php echo (int)$product['id'] ?>">
                    <div class="form-group">
                        <label for="product-title">Product Name</label>
                        <input type="text" class="form-control" name="product-title" value="<?php echo htmlspecialchars($product['name']); ?>">
                    </div>
                    <div class="form-group">
                        <label for="product-categorie">Category</label>
                        <select class="form-control" name="product-categorie">
                            <option value="">Select a category</option>
                            <?php foreach ($all_categories as $cat) : ?>
                                <option value="<?php echo (int)$cat['id']; ?>" <?php if ($product['categorie_id'] == $cat['id']) echo "selected"; ?>>
                                    <?php echo htmlspecialchars($cat['name']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="product-quantity">Quantity</label>
                        <input type="number" step="0.01" class="form-control" name="product-quantity" value="<?php echo htmlspecialchars($product['quantity']); ?>">
                    </div>
                    <div class="form-group">
                        <label for="buying-price">Buying Price</label>
                        <input type="number" step="0.01" class="form-control" name="buying-price" value="<?php echo htmlspecialchars($product['buy_price']); ?>">
                    </div>
                    <div class="form-group">
                        <label for="part-number">Part Number</label>
                        <input type="text" class="form-control" name="part-number" value="<?php echo isset($product['part_number']) ? htmlspecialchars($product['part_number']) : ''; ?>">
                    </div>
                    <div class="form-group">
                        <label for="product-uom">Unit of Measure (UoM)</label>
                        <input type="text" class="form-control" name="product-uom" value="<?php echo htmlspecialchars($product['UoM']); ?>">
                    </div>
                    <div class="form-group">
                        <label for="new-code">New Code</label>
                        <input type="text" class="form-control" name="new-code" value="<?php echo isset($product['new_code']) ? htmlspecialchars($product['new_code']) : ''; ?>">
                    </div>
                    <div class="form-group">
                        <label for="product-photo">Product Photo</label>
                        <select class="form-control" name="product-photo">
                            <option value="">No file</option>
                            <?php foreach ($all_files as $file) : ?>
                                <option value="<?php echo (int)$file['id']; ?>" <?php if ($product['media_id'] == $file['id']) echo "selected"; ?>>
                                    <?php echo htmlspecialchars($file['name']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <button type="submit" name="product" class="btn btn-primary">Update</button>
                </form>
            </div>
        </div>
    </div>
</div>

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