<?php
session_start();
include_once "../config/database.php";

if(!isset($_SESSION['user_id'])){
    header("Location: /beauty_pos/login.php");
    exit;
}

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if($id == 0){
    header("Location: /beauty_pos/products/index.php");
    exit;
}

/* ---------------- FETCH PRODUCT ---------------- */
$productQuery = "SELECT * FROM products WHERE id='$id'";
$productResult = mysqli_query($conn, $productQuery);
$product = mysqli_fetch_assoc($productResult);

if(!$product){
    header("Location: /beauty_pos/products/index.php");
    exit;
}

/* ---------------- CATEGORIES ---------------- */
$catResult = mysqli_query($conn, "SELECT * FROM categories ORDER BY category_name ASC");

/* ---------------- BRANDS ---------------- */
$brandResult = mysqli_query($conn, "SELECT * FROM brands ORDER BY brand_name ASC");
/* ---------------- FETCH CURRENCIES ---------------- */
$currencyResult = mysqli_query($conn,"
SELECT *
FROM currencies
WHERE status=1
ORDER BY is_default DESC,currency_name ASC
");

/* ---------------- UPDATE ---------------- */
if(isset($_POST['update'])){

    $category_id = $_POST['category_id'];
    $brand_id = $_POST['brand_id'];
    $currency_id = $_POST['currency_id'];

    $product_name = mysqli_real_escape_string($conn, $_POST['product_name']);
    $sku = mysqli_real_escape_string($conn, $_POST['sku']);
    $barcode = mysqli_real_escape_string($conn, $_POST['barcode']);
    $shade = mysqli_real_escape_string($conn, $_POST['shade']);
    $batch_no = mysqli_real_escape_string($conn, $_POST['batch_no']);
    $expiry_date = $_POST['expiry_date'];
    $cost_price = $_POST['cost_price'];
    $selling_price = $_POST['selling_price'];
    $wholesale_price = $_POST['wholesale_price'];
    $stock = $_POST['stock'];
    $status = $_POST['status'];

    /* ---------------- IMAGE HANDLING ---------------- */
    $imageName = $product['image'];

    if(!empty($_FILES['image']['name'])){
        $imageName = time().'_'.$_FILES['image']['name'];
        move_uploaded_file($_FILES['image']['tmp_name'], "../uploads/".$imageName);
    }

    /* ---------------- UPDATE QUERY ---------------- */
    $update = "UPDATE products SET
        category_id='$category_id',
        brand_id='$brand_id',
        currency_id='$currency_id',
        product_name='$product_name',
        sku='$sku',
        barcode='$barcode',
        shade='$shade',
        batch_no='$batch_no',
        expiry_date='$expiry_date',
        cost_price='$cost_price',
        selling_price='$selling_price',
        wholesale_price='$wholesale_price',
        stock='$stock',
        image='$imageName',
        status='$status'
        WHERE id='$id'
    ";

    if(mysqli_query($conn, $update)){
        $success = "Product updated successfully!";
    } else {
        $error = "Update failed!";
    }
}

include_once "../includes/header.php";
include_once "../includes/sidebar.php";
?>

<div class="overlay" id="overlay"></div>

<div class="main-content">

<?php include_once "../includes/topbar.php"; ?>

<div class="container-fluid">

<div class="card-box rounded-4 shadow p-4">

<h4 class="mb-3">Edit Product</h4>

<?php if(isset($success)){ ?>
<div class="alert alert-success"><?php echo $success; ?></div>
<?php } ?>

<?php if(isset($error)){ ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php } ?>

<form method="POST" enctype="multipart/form-data">

<div class="row g-3">

<div class="col-md-6">
<label>Product Name</label>
<input type="text" name="product_name" class="form-control"
value="<?php echo $product['product_name']; ?>" required>
</div>

<div class="col-md-3">
<label>Category</label>
<select name="category_id" class="form-control">
<?php while($c=mysqli_fetch_assoc($catResult)){ ?>
<option value="<?php echo $c['id']; ?>"
<?php if($product['category_id']==$c['id']) echo "selected"; ?>>
<?php echo $c['category_name']; ?>
</option>
<?php } ?>
</select>
</div>

<div class="col-md-3">
<label>Brand</label>
<select name="brand_id" class="form-control">
<?php while($b=mysqli_fetch_assoc($brandResult)){ ?>
<option value="<?php echo $b['id']; ?>"
<?php if($product['brand_id']==$b['id']) echo "selected"; ?>>
<?php echo $b['brand_name']; ?>
</option>
<?php } ?>
</select>
</div>

<div class="col-md-3">
<label>Currency</label>

<select name="currency_id" class="form-control" required>

<?php while($cur=mysqli_fetch_assoc($currencyResult)){ ?>

<option
value="<?php echo $cur['id']; ?>"
<?php if($product['currency_id']==$cur['id']) echo "selected"; ?>>

<?php echo $cur['currency_name']." (".$cur['currency_symbol']." - ".$cur['currency_code'].")"; ?>

</option>

<?php } ?>

</select>

</div>

<div class="col-md-3">
<label>SKU</label>
<input type="text" name="sku" class="form-control"
value="<?php echo $product['sku']; ?>">
</div>

<div class="col-md-3">
<label>Barcode</label>
<input type="text" name="barcode" class="form-control"
value="<?php echo $product['barcode']; ?>">
</div>

<div class="col-md-3">
<label>Shade</label>
<input type="text" name="shade" class="form-control"
value="<?php echo $product['shade']; ?>">
</div>

<div class="col-md-3">
<label>Batch No</label>
<input type="text" name="batch_no" class="form-control"
value="<?php echo $product['batch_no']; ?>">
</div>

<div class="col-md-4">
<label>Expiry Date</label>
<input type="date" name="expiry_date" class="form-control"
value="<?php echo $product['expiry_date']; ?>">
</div>

<div class="col-md-4">
<label>Cost Price</label>
<input type="number" step="0.01" name="cost_price" class="form-control"
value="<?php echo $product['cost_price']; ?>">
</div>

<div class="col-md-4">
<label>Selling Price</label>
<input type="number" step="0.01" name="selling_price" class="form-control"
value="<?php echo $product['selling_price']; ?>">
</div>

<div class="col-md-4">
<label>Wholesale Price</label>
<input type="number" step="0.01" name="wholesale_price" class="form-control"
value="<?php echo $product['wholesale_price']; ?>">
</div>

<div class="col-md-4">
<label>Stock</label>
<input type="number" name="stock" class="form-control"
value="<?php echo $product['stock']; ?>">
</div>

<div class="col-md-4">
<label>Status</label>
<select name="status" class="form-control">
<option value="1" <?php if($product['status']==1) echo "selected"; ?>>Active</option>
<option value="0" <?php if($product['status']==0) echo "selected"; ?>>Inactive</option>
</select>
</div>

<div class="col-md-6">
<label>Current Image</label><br>
<?php if($product['image']){ ?>
<img src="/beauty_pos/uploads/<?php echo $product['image']; ?>"
width="80" class="rounded">
<?php } ?>
</div>

<div class="col-md-6">
<label>Change Image</label>
<input type="file" name="image" class="form-control">
</div>

<div class="col-12">
<button type="submit" name="update" class="btn btn-success">
Update Product
</button>
</div>

</div>

</form>

</div>

</div>

<?php include_once "../includes/footer.php"; ?>

</div>