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

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

// Get current store ID from session
$store_id = $_SESSION['current_store_id'] ?? 1;

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

$stmt = $conn->prepare("INSERT INTO shifts (store_id, user_id, start_time, opening_cash, status) VALUES (?, ?, NOW(), ?, 'open')");
if ($stmt) {
    $stmt->bind_param("iid", $store_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']);
}
