<?php
session_start();
require 'db.php'; // must define $conn (MySQLi)

$user_id = $_SESSION['user_id'] ?? null;
$opening_cash = floatval($_POST['opening_cash'] ?? 0);

if (!$user_id) {
    echo json_encode(['status' => 'error', 'message' => 'User not logged in']);
    exit;
}

$stmt = $conn->prepare("INSERT INTO shifts (user_id, start_time, opening_cash, status) VALUES (?, NOW(), ?, 'open')");
if ($stmt) {
    $stmt->bind_param("id", $user_id, $opening_cash);
    if ($stmt->execute()) {
        echo json_encode(['status' => 'success']);
    } else {
        echo json_encode(['status' => 'error', 'message' => 'Execution failed']);
    }
    $stmt->close();
} else {
    echo json_encode(['status' => 'error', 'message' => 'Preparation failed']);
}
