<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../config/database.php';

$action = $_GET['action'] ?? $_POST['action'] ?? 'list';
$type = $_GET['type'] ?? $_POST['type'] ?? '';

if ($type === 'stock_adjustment') {
    if ($action === 'list') {
        $sql = "SELECT id AS __backendId, adjustment_ref, item_name, type, old_quantity, new_quantity, reason, user, status FROM stock_adjustments ORDER BY id DESC";
        $result = $mysqli->query($sql);
        $data = [];
        if ($result) {
            while ($row = $result->fetch_assoc()) { $data[] = $row; }
        }
        echo json_encode($data);
    } elseif ($action === 'create') {
        $item_name = $_POST['item_name'] ?? '';
        $type_adj = $_POST['type_adj'] ?? '';
        $old_qty = $_POST['old_qty'] ?? 0;
        $new_qty = $_POST['new_qty'] ?? 0;
        $reason = $_POST['reason'] ?? '';
        $user = $_POST['user'] ?? 'Admin';
        $status = $_POST['status'] ?? 'pending';
        
        $stmt = $mysqli->prepare("INSERT INTO stock_adjustments (item_name, type, old_quantity, new_quantity, reason, user, status) VALUES (?, ?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("ssiisss", $item_name, $type_adj, $old_qty, $new_qty, $reason, $user, $status);
        $ok = $stmt->execute();
        echo json_encode(['success' => $ok]);
    }
} elseif ($type === 'warehouse') {
    if ($action === 'list') {
        $sql = "SELECT id AS __backendId, name, location, capacity, status FROM warehouses ORDER BY id DESC";
        $result = $mysqli->query($sql);
        $data = [];
        if ($result) {
            while ($row = $result->fetch_assoc()) { $data[] = $row; }
        }
        echo json_encode($data);
    } elseif ($action === 'create') {
        $name = $_POST['name'] ?? '';
        $location = $_POST['location'] ?? '';
        $capacity = $_POST['capacity'] ?? 0;
        
        $stmt = $mysqli->prepare("INSERT INTO warehouses (name, location, capacity, status) VALUES (?, ?, ?, 'active')");
        $stmt->bind_param("ssi", $name, $location, $capacity);
        $ok = $stmt->execute();
        echo json_encode(['success' => $ok]);
    }
} elseif ($type === 'goods_received') {
    if ($action === 'list') {
        $sql = "SELECT id AS __backendId, supplier, item_name, quantity, warehouse, status FROM goods_received ORDER BY id DESC";
        $result = $mysqli->query($sql);
        $data = [];
        if ($result) {
            while ($row = $result->fetch_assoc()) { $data[] = $row; }
        }
        echo json_encode($data);
    } elseif ($action === 'create') {
        $supplier = $_POST['supplier'] ?? '';
        $item_name = $_POST['item_name'] ?? '';
        $quantity = $_POST['quantity'] ?? 0;
        $warehouse = $_POST['warehouse'] ?? '';
        
        $stmt = $mysqli->prepare("INSERT INTO goods_received (supplier, item_name, quantity, warehouse, status) VALUES (?, ?, ?, ?, 'received')");
        $stmt->bind_param("ssss", $supplier, $item_name, $quantity, $warehouse);
        $ok = $stmt->execute();
        echo json_encode(['success' => $ok]);
    }
}
?>
