<?php
ob_start();
/**
 * 2026企业CMS - 后台团队管理
 */
require_once __DIR__ . '/header.php';
$pageTitle = '团队管理';
$action = $_GET['action'] ?? 'list';
$id = intval($_GET['id'] ?? 0);

if ($action === 'delete' && $id) {
    check_permission('editor');
    db_exec("DELETE FROM `team` WHERE `id`=?", [$id]);
    add_log('delete', 'team', '删除团队成员');
    header('Location: /admin/team.php?msg=' . urlencode('删除成功！'));
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save'])) {
	// CSRF 保护
	if (!csrf_validate($_POST["csrf_token"] ?? "")) {
	    die("CSRF 验证失败");
	}

    check_permission('editor');
    $name = trim($_POST['name'] ?? '');
    $position = trim($_POST['position'] ?? '');
    $bio = trim($_POST['bio'] ?? '');
    $content = $_POST['content'] ?? '';
    $email = trim($_POST['email'] ?? '');
    $wechat = trim($_POST['wechat'] ?? '');
    
    if ($action === 'add') {
        db_exec("INSERT INTO `team` (`name`,`position`,`bio`,`content`,`email`,`wechat`) VALUES (?,?,?,?,?,?)",
            [$name, $position, $bio, $content, $email, $wechat]);
        add_log('add', 'team', '添加成员: ' . $name);
    } elseif ($action === 'edit' && $id) {
        db_exec("UPDATE `team` SET `name`=?,`position`=?,`bio`=?,`content`=?,`email`=?,`wechat`=? WHERE `id`=?",
            [$name, $position, $bio, $content, $email, $wechat, $id]);
        add_log('edit', 'team', '编辑成员: ' . $name);
    }
    header('Location: /admin/team.php?msg=' . urlencode('保存成功！'));
    exit;
}

$member = [];
if ($action === 'add' || ($action === 'edit' && $id)) {
    if ($id) $member = db_query_one("SELECT * FROM `team` WHERE `id`=?", [$id]);
?>
<div class="page-header">
    <h2><?php echo htmlspecialchars($action, ENT_QUOTES, "UTF-8")==='add'?'添加成员':'编辑成员'; ?></h2>
    <a href="/admin/team.php" class="btn">← 返回列表</a>
</div>
<div class="form-card">
    <form method="post" action="">
        <?php echo csrf_input(); ?>
        <div class="form-row">
            <div class="form-group">
                <label>姓名 <span style="color:red;">*</span></label>
                <input type="text" name="name" value="<?php echo xss_clean($member['name'] ?? ''); ?>" required>
            </div>
            <div class="form-group">
                <label>职位</label>
                <input type="text" name="position" value="<?php echo xss_clean($member['position'] ?? ''); ?>">
            </div>
        </div>
        <div class="form-row">
            <div class="form-group">
                <label>邮箱</label>
                <input type="email" name="email" value="<?php echo xss_clean($member['email'] ?? ''); ?>">
            </div>
            <div class="form-group">
                <label>微信号</label>
                <input type="text" name="wechat" value="<?php echo xss_clean($member['wechat'] ?? ''); ?>">
            </div>
        </div>
        <div class="form-group">
            <label>简介</label>
            <textarea name="bio" rows="3"><?php echo xss_clean($member['bio'] ?? ''); ?></textarea>
        </div>
        <div class="form-group">
            <label>详细介绍</label>
            <textarea name="content" id="content-editor"><?php echo $member['content'] ?? ''; ?></textarea>
        </div>
        <div style="margin-top:30px;">
            <button type="submit" name="save" class="btn btn-primary" style="margin-right:10px;">保存</button>
            <a href="/admin/team.php" class="btn">取消</a>
        </div>
    </form>
</div>
<script src="https://cdn.ckeditor.com/4.22.1/full/ckeditor.js"></script>
<script>if(typeof CKEDITOR!=='undefined'){CKEDITOR.replace('content-editor');}</script>
<?php
require_once __DIR__ . '/footer.php';
exit;
}

$members = db_query("SELECT * FROM `team` WHERE `status`=1 ORDER BY `sort_order` ASC, `id` ASC");
?>
<div class="page-header">
    <h2>团队管理</h2>
    <a href="/admin/team.php?action=add" class="btn btn-primary"><i class="fas fa-plus"></i> 添加成员</a>
</div>

<div class="table-wrapper">
    <table>
        <thead><tr><th>ID</th><th>姓名</th><th>职位</th><th>邮箱</th><th>操作</th></tr></thead>
        <tbody>
            <?php foreach ($members as $m): ?>
            <tr>
                <td><?php echo $m['id']; ?></td>
                <td><?php echo xss_clean($m['name']); ?></td>
                <td><?php echo xss_clean($m['position']); ?></td>
                <td><?php echo xss_clean($m['email']); ?></td>
                <td class="actions">
                    <a href="/admin/team.php?action=edit&id=<?php echo $m['id']; ?>" class="btn btn-sm btn-primary">编辑</a>
                    <a href="javascript:void(0)" onclick="if(confirmDelete(<?php echo $m['id']; ?>,'<?php echo xss_clean($m['name']); ?>')){location.href='/admin/team.php?action=delete&id=<?php echo $m['id']; ?>'}" class="btn btn-sm btn-danger">删除</a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?php require_once __DIR__ . '/footer.php'; ?>
