<?php
require_once __DIR__.'/../../../includes/auth.php';
requireLogin();

require_once __DIR__.'/../../../config/database.php';

/* AJAX DETECT */
$isAjax = isset($_GET['ajax']);

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$tariffId = isset($_GET['tariff_id']) ? (int)$_GET['tariff_id'] : 0;
$profileId = $_GET['profile_id'] ?? 0;
$returnUrl = $_GET['return'] ?? "blocks.php?profile_id=" . $profileId;

if(!$id){
    die("Missing block ID");
}

/* FETCH BLOCK */

$stmt = $pdo->prepare("
SELECT *
FROM tariff_blocks
WHERE id=?
");

$stmt->execute([$id]);
$block = $stmt->fetch(PDO::FETCH_ASSOC);

if(!$tariffId){
    $tariffId = $block['tariff_id'];
}

if(!$block){
    echo "Block not found";
    exit;
}

/* UPDATE BLOCK (UNCHANGED LOGIC) */

if($_SERVER['REQUEST_METHOD']=='POST'){

    $stmt = $pdo->prepare("
    UPDATE tariff_blocks
    SET block_from=?, block_to=?, price_per_unit=?
    WHERE id=?
    ");

    $stmt->execute([
        $_POST['block_from'],
        $_POST['block_to'] ?: null,
        $_POST['price_per_unit'],
        $id
    ]);

    // ✅ ONLY CHANGE: prevent redirect if AJAX
    if($isAjax){
        echo "success";
        exit;
    }

    header("Location: " . ($_POST['return'] ?? "blocks.php?profile_id=" . $_POST['profile_id']));
    exit;
}

/* LOAD LAYOUT ONLY IF NOT AJAX */
if(!$isAjax){
    require_once __DIR__.'/../../../includes/header.php';
    require_once __DIR__.'/../../../includes/sidebar.php';
}
?>

<div class="<?= $isAjax ? '' : 'main-content' ?>">

    <div class="<?= $isAjax ? 'panel' : 'page-wrapper' ?>">

        <!-- HEADER -->
        <div class="<?= $isAjax ? 'panel-header' : 'page-header' ?>">
            <h2 style="margin:0; font-size:16px;">Edit Tariff Block</h2>

            <?php if(!$isAjax): ?>
                <a href="<?= $_GET['return'] ?? 'blocks.php?profile_id=' . ($_GET['profile_id'] ?? 0) ?>" class="btn btn-secondary">
                    Back
                </a>
            <?php endif; ?>
        </div>

        <!-- BODY -->
        <div style="padding:15px;">

        <form method="POST" action="edit_block.php?id=<?= $id ?>&profile_id=<?= $profileId ?>&ajax=1" id="edit-block-form">

                <input type="hidden" name="profile_id" value="<?= $profileId ?>">
                <input type="hidden" name="return" value="<?= htmlspecialchars($returnUrl) ?>">

                <div class="form-group">
                    <label>From</label>
                    <input 
                        name="block_from" 
                        value="<?= $block['block_from'] ?>" 
                        class="form-control"
                        required
                    >
                </div>

                <div class="form-group">
                    <label>To</label>
                    <input 
                        name="block_to" 
                        value="<?= $block['block_to'] ?>" 
                        class="form-control"
                    >
                </div>

                <div class="form-group">
                    <label>Rate</label>
                    <input 
                        name="price_per_unit" 
                        value="<?= $block['price_per_unit'] ?>" 
                        class="form-control"
                        required
                    >
                </div>

                <div style="margin-top:15px;">
                    <button type="submit" class="btn btn-primary">
                        Update Block
                    </button>
                </div>

            </form>

        </div>

    </div>

</div>

<?php
if(!$isAjax){
    require_once __DIR__.'/../../../includes/footer.php';
}
?>