<?php
require_once __DIR__ . '/layout.php';
$db = db();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    if ($_POST['action'] === 'add_control') {
        $symbol = $_POST['symbol'];
        $price = (float)$_POST['price'];
        $time = $_POST['execution_time'];
        $duration = (int)$_POST['duration'];
        
        $db->prepare("INSERT INTO price_controls (symbol, target_price, execution_time, duration) VALUES (?, ?, ?, ?)")
           ->execute([$symbol, $price, $time, $duration]);
        header("Location: ai_control.php?msg=added");
        exit;
    }
    if ($_POST['action'] === 'delete') {
        $id = (int)$_POST['id'];
        $db->prepare("DELETE FROM price_controls WHERE id = ?")->execute([$id]);
        header("Location: ai_control.php?msg=deleted");
        exit;
    }
}

$title = 'AI控盘 (插针设置)';
ob_start();
$controls = $db->query("SELECT * FROM price_controls ORDER BY execution_time DESC LIMIT 50")->fetchAll();
?>
<div class="d-flex justify-content-between align-items-center mb-4">
    <div class="d-flex align-items-center gap-3">
        <a href="index.php" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-left"></i> 返回</a>
        <h4 class="mb-0">AI价格干预</h4>
    </div>
</div>
<div class="row g-4 mb-4">
    <div class="col-md-4">
        <div class="card p-4">
            <h6 class="mb-4">添加插针设置</h6>
            <form method="POST">
                <input type="hidden" name="action" value="add_control">
                <div class="mb-3">
                    <label class="form-label">币种</label>
                    <input type="text" name="symbol" class="form-control" list="coinSymbols" required placeholder="例如: BTC/USDT">
                    <datalist id="coinSymbols">
                        <option value="BTC/USDT">
                        <option value="ETH/USDT">
                        <option value="SOL/USDT">
                        <option value="BNB/USDT">
                        <option value="XRP/USDT">
                        <option value="ADA/USDT">
                        <option value="DOGE/USDT">
                    </datalist>
                </div>
                <div class="mb-3">
                    <label class="form-label">目标价格</label>
                    <input type="number" step="0.00000001" name="price" class="form-control" required placeholder="例如: 65000">
                </div>
                <div class="mb-3">
                    <label class="form-label">执行时间</label>
                    <input type="datetime-local" name="execution_time" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">持续时长 (秒)</label>
                    <input type="number" name="duration" class="form-control" value="60">
                </div>
                <button type="submit" class="btn btn-primary w-100">添加控制</button>
            </form>
        </div>
    </div>
    <div class="col-md-8">
        <div class="table-container">
            <h6 class="mb-4">执行记录 / 预设列表</h6>
            <table class="table table-hover">
                <thead>
                    <tr class="small text-muted">
                        <th>币种</th>
                        <th>目标价格</th>
                        <th>执行时间</th>
                        <th>时长</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($controls as $c): ?>
                    <tr>
                        <td><strong><?= $c['symbol'] ?></strong></td>
                        <td class="text-primary fw-bold"><?= $c['target_price'] ?></td>
                        <td><?= $c['execution_time'] ?></td>
                        <td><?= $c['duration'] ?>秒</td>
                        <td>
                            <?php if (strtotime($c['execution_time']) > time()): ?>
                                <span class="badge bg-warning">待执行</span>
                            <?php elseif (strtotime($c['execution_time']) + $c['duration'] < time()): ?>
                                <span class="badge bg-secondary">已结束</span>
                            <?php else: ?>
                                <span class="badge bg-success">执行中</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <form method="POST" class="d-inline">
                                <input type="hidden" name="action" value="delete">
                                <input type="hidden" name="id" value="<?= $c['id'] ?>">
                                <button type="submit" class="btn btn-sm btn-link text-danger" onclick="return confirm('确定删除吗？')">删除</button>
                            </form>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php
$content = ob_get_clean();
renderAdminPage($content, $title);
?>
