<?php
require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../../includes/header.php';
$user = currentUser(); $db = (new Database())->getConnection();
$db->exec("CREATE TABLE IF NOT EXISTS mkt_brand_strategy (strategy_id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(200) NOT NULL, category ENUM('브랜딩','리포지셔닝','캠페인전략','콘텐츠전략','채널전략') DEFAULT '브랜딩', quarter VARCHAR(7), target_audience VARCHAR(200), core_message TEXT, kpi_target TEXT, status ENUM('초안','검토','확정','실행','완료') DEFAULT '초안', staff_id INT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)");

if ($_SERVER['REQUEST_METHOD']==='POST' && $_POST['action']==='add') {
    $db->prepare("INSERT INTO mkt_brand_strategy (title,category,quarter,target_audience,core_message,kpi_target,staff_id) VALUES(?,?,?,?,?,?,?)")
       ->execute([$_POST['title'],$_POST['category'],$_POST['quarter'],$_POST['audience'],$_POST['message'],$_POST['kpi'],$user['staff_id']]);
} elseif ($_SERVER['REQUEST_METHOD']==='POST' && $_POST['action']==='update_status') {
    $db->prepare("UPDATE mkt_brand_strategy SET status=? WHERE strategy_id=?")->execute([$_POST['status'],$_POST['sid']]);
}

$strategies = $db->query("SELECT bs.*, s.name AS staff_name FROM mkt_brand_strategy bs LEFT JOIN staff s ON bs.staff_id=s.staff_id ORDER BY bs.created_at DESC")->fetchAll();
?>
<div class="page-header"><h2>🎯 브랜드 전략</h2><div class="breadcrumb">마케팅 &gt; 브랜드 전략</div></div>

<div class="card" style="margin-bottom:1.25rem;">
    <div class="card-header"><h3>전략 등록</h3></div>
    <form method="POST"><input type="hidden" name="action" value="add">
        <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(12rem,1fr));gap:0.75rem;">
            <div class="form-group"><label>전략명</label><input type="text" name="title" required placeholder="전략명"></div>
            <div class="form-group"><label>분류</label><select name="category"><option>브랜딩</option><option>리포지셔닝</option><option>캠페인전략</option><option>콘텐츠전략</option><option>채널전략</option></select></div>
            <div class="form-group"><label>분기</label><input type="text" name="quarter" placeholder="2026-Q1"></div>
            <div class="form-group"><label>타겟 오디언스</label><input type="text" name="audience" placeholder="20~30대 직장인"></div>
        </div>
        <div class="form-group"><label>핵심 메시지</label><textarea name="message" rows="2" placeholder="브랜드 핵심 메시지"></textarea></div>
        <div class="form-group"><label>KPI 목표</label><textarea name="kpi" rows="2" placeholder="인지도 30% 향상, 리드 500건"></textarea></div>
        <button type="submit" class="btn btn-primary">등록</button>
    </form>
</div>

<div class="card">
    <div class="card-header"><h3>전략 목록</h3></div>
    <div style="overflow-x:auto;">
    <table class="data-table">
        <thead><tr><th>전략명</th><th>분류</th><th>분기</th><th>타겟</th><th>담당</th><th>상태</th><th>변경</th></tr></thead>
        <tbody>
        <?php foreach($strategies as $s): ?>
        <tr>
            <td><strong><?= htmlspecialchars($s['title']) ?></strong></td>
            <td><span class="badge badge-info"><?= $s['category'] ?></span></td>
            <td><?= $s['quarter']??'-' ?></td>
            <td style="font-size:var(--fs-xs);"><?= htmlspecialchars($s['target_audience']??'-') ?></td>
            <td><?= htmlspecialchars($s['staff_name']??'-') ?></td>
            <td><span class="badge <?= ['초안'=>'badge-gray','검토'=>'badge-warning','확정'=>'badge-info','실행'=>'badge-success','완료'=>'badge-success'][$s['status']] ?>"><?= $s['status'] ?></span></td>
            <td><form method="POST" style="display:flex;gap:0.25rem;"><input type="hidden" name="action" value="update_status"><input type="hidden" name="sid" value="<?= $s['strategy_id'] ?>"><select name="status" style="font-size:var(--fs-xs);padding:0.25rem;"><option>초안</option><option>검토</option><option>확정</option><option>실행</option><option>완료</option></select><button type="submit" class="btn btn-outline" style="font-size:var(--fs-xs);padding:0.2rem 0.5rem;">변경</button></form></td>
        </tr>
        <?php endforeach; ?>
        <?php if(!$strategies): ?><tr><td colspan="7" style="text-align:center;color:var(--text-tertiary);">등록된 전략이 없습니다</td></tr><?php endif; ?>
        </tbody>
    </table>
    </div>
</div>
<?php require_once __DIR__ . '/../../includes/footer.php'; ?>
