<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-5-20
 */
require_once ROOT_PATH . 'core/lib/content_workflow.php';
cmsEnsureContentWorkflowSchema($db, 'recruit');
$state = cmsNormalizeContentState(trim((string) ($_GET['state'] ?? 'all')));
$action = isset($_GET['action']) ? $_GET['action'] : 'list';
cmsHandleContentWorkflowRequest($db, 'recruit', $action);



    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax_action'])) {
        while (ob_get_level() > 0) { ob_end_clean(); }
        header('Content-Type: application/json; charset=utf-8');
        $ajaxAct = $_POST['ajax_action'];

        if ($ajaxAct === 'delete') {
            $idsRaw = isset($_POST['ids']) ? $_POST['ids'] : '[]';
            $ids = json_decode($idsRaw, true);
            if (!is_array($ids)) { $ids = []; }
            $ids = array_map('intval', $ids);
            if (empty($ids)) { echo json_encode(['code'=>400,'msg'=>'请选择要删除的项']); exit; }
            $placeholders = implode(',', array_fill(0, count($ids), '?'));
            $db->query("DELETE FROM {$db->table('recruit')} WHERE id IN ($placeholders) AND deleted_at>0", $ids);
            echo json_encode(['code'=>200,'msg'=>'已删除 '.count($ids).' 项']); exit;
        }

        if ($ajaxAct === 'show') {
            $idsRaw = isset($_POST['ids']) ? $_POST['ids'] : '[]';
            $ids = json_decode($idsRaw, true);
            if (!is_array($ids)) { $ids = []; }
            $ids = array_map('intval', $ids);
            if (empty($ids)) { echo json_encode(['code'=>400,'msg'=>'请选择']); exit; }
            $placeholders = implode(',', array_fill(0, count($ids), '?'));
            $db->query("UPDATE {$db->table('recruit')} SET status=1, update_time=? WHERE id IN ($placeholders)", array_merge([time()], $ids));
            echo json_encode(['code'=>200,'msg'=>'已显示']); exit;
        }

        if ($ajaxAct === 'hide') {
            $idsRaw = isset($_POST['ids']) ? $_POST['ids'] : '[]';
            $ids = json_decode($idsRaw, true);
            if (!is_array($ids)) { $ids = []; }
            $ids = array_map('intval', $ids);
            if (empty($ids)) { echo json_encode(['code'=>400,'msg'=>'请选择']); exit; }
            $placeholders = implode(',', array_fill(0, count($ids), '?'));
            $db->query("UPDATE {$db->table('recruit')} SET status=0, update_time=? WHERE id IN ($placeholders)", array_merge([time()], $ids));
            echo json_encode(['code'=>200,'msg'=>'已隐藏']); exit;
        }
    }


$action = isset($_GET['action']) ? $_GET['action'] : 'list';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($action === 'save') {
        $id = intval($_POST['id']);
        $data = [
            'channel_id' => intval($_POST['channel_id']),
            'title' => trim($_POST['title']),
            'department' => trim($_POST['department']),
            'region' => trim($_POST['region']),
            'salary' => trim($_POST['salary']),
            'quantity' => intval($_POST['quantity']),
            'experience' => trim($_POST['experience']),
            'education' => trim($_POST['education']),
            'work_type' => trim($_POST['work_type']),
            'summary' => trim($_POST['summary']),
            'content' => trim($_POST['content']),
            'seo_title' => trim($_POST['seo_title']),
            'seo_keywords' => trim($_POST['seo_keywords']),
            'seo_description' => trim($_POST['seo_description']),
            'sort_order' => intval($_POST['sort_order']),
            'status' => cmsContentSaveStatus($_POST['save_mode'] ?? (isset($_POST['status']) ? 'publish' : 'draft')),
            'update_time' => time()
        ];
        if ($id > 0) {
            $db->update('recruit', $data, 'id = ? AND deleted_at = 0', [$id]);
            addLog('修改', '招聘管理', '修改职位 ' . $data['title']);
        } else {
            $data['create_time'] = time();
            $db->insert('recruit', $data);
            addLog('添加', '招聘管理', '添加职位 ' . $data['title']);
        }
        echo '<script>alert("保存成功");location.href="?act=recruit"</script>';
        exit;
    }
    if ($action === 'delete') {
        $id = intval($_POST['id']);
        $row = $db->fetch("SELECT title FROM {$db->table('recruit')} WHERE id = ?", [$id]);
        $db->delete('recruit', 'id = ? AND deleted_at > 0', [$id]);
        addLog('删除', '招聘管理', '删除职位 ' . ((isset($row['title']) ? $row['title'] : $id)));
        echo json_encode(['code' => 200, 'msg' => '删除成功']);
        exit;
    }
}

if ($action === 'list') {
    $recruitChannels = $db->fetchAll("SELECT id, channel_name FROM {$db->table('channel')} WHERE channel_type = 9 ORDER BY list_order ASC");
    $channelMap = [];
    foreach ($recruitChannels as $ch) { $channelMap[$ch['id']] = $ch['channel_name']; }
    $pageResult = cmsContentWorkflowPage($db, 'recruit', $state, $_GET['page'] ?? 1, 20);
    $recruits = $pageResult['rows'];
    $page = $pageResult['page'];
    $totalPages = $pageResult['total_pages'];
    $workflowCounts = cmsContentWorkflowCounts($db, 'recruit');
    ?>
    <div class="page-container fade-in">
        <div class="page-header">
            <div class="page-title">
                <div class="page-icon"><svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg></div>
                <div><h1>招聘管理</h1><p style="font-size:12px;color:#64748b;margin:0;">列表页统一为轻量结构，顶部统计卡已移除。</p></div>
            </div>
            <div class="page-actions">
                <?php if ($state === 'trash'): ?>
                <button type="button" class="btn btn-secondary" onclick="batchAction('restore')">批量恢复</button>
                <button type="button" class="btn btn-danger" onclick="batchAction('purge')">永久删除</button>
                <?php else: ?>
                <button type="button" class="btn btn-secondary" onclick="batchAction('show')">批量显示</button>
                <button type="button" class="btn btn-secondary" onclick="batchAction('hide')">批量隐藏</button>
                <button type="button" class="btn btn-danger" onclick="batchAction('delete')">批量删除</button>
                <a href="?act=recruit&action=add" class="btn btn-primary">添加职位</a>
                <?php endif; ?>
            </div>
        </div>
        <?php cmsRenderContentWorkflowNav('recruit', $state, $workflowCounts); ?>
        <div class="content-card">
        <?php if (empty($recruits)): ?>
        <div class="empty-state">
            <div class="empty-icon"><svg style="width:40px;height:40px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M16.21 16.21A6 6 0 0 0 18 9h-1.26a8 8 0 0 0-6.33-3.33"/><path d="M9.79 9.79A6 6 0 0 0 12 4h1.26a8 8 0 0 0-3.39 5.67"/><line x1="1" y1="1" x2="23" y2="23"/></svg></div>
            <h3>暂无职位</h3>
            <p>添加第一个招聘职位</p>
            <a href="?act=recruit&action=add" class="btn btn-primary">添加职位</a>
        </div>
        <?php else: ?>
        <table class="data-table">
            <thead>
                <tr><th style="width:40px;"><input type="checkbox" id="checkAll" onchange="toggleCheckAll(this)" style="accent-color:#2563eb;width:16px;height:16px;cursor:pointer;"></th><th style="width:50px;">ID</th><th>职位名称</th><th style="width:120px;">部门</th><th style="width:100px;">薪资</th><th style="width:80px;">人数</th><th style="width:100px;">状态</th><th style="width:280px;">操作</th></tr>
            </thead>
            <tbody>
                <?php foreach ($recruits as $item): ?>
                <tr>
                    <td style="text-align:center;"><input type="checkbox" class="row-check" value="<?php echo $item['id']; ?>" style="accent-color:#2563eb;width:16px;height:16px;cursor:pointer;"></td>
                    <td class="id-cell"><?php echo $item['id']; ?></td>
                    <td>
                        <div style="font-weight:600;color:#334155;"><?php echo htmlspecialchars($item['title']); ?></div>
                        <div style="font-size:12px;color:#94a3b8;margin-top:2px;"><?php echo htmlspecialchars($item['region']); ?> | <?php echo htmlspecialchars($item['work_type']); ?></div>
                    </td>
                    <td style="color:#64748b;font-size:13px;"><?php echo htmlspecialchars($item['department']); ?></td>
                    <td style="font-weight:600;color:#10b981;font-size:13px;"><?php echo htmlspecialchars($item['salary']); ?></td>
                    <td style="font-family:monospace;color:#64748b;"><?php echo intval($item['quantity']); ?></td>
                    <td>
                        <?php if ($item['status']): ?>
                        <span class="badge badge-success">发布</span>
                        <?php else: ?>
                        <span class="badge badge-secondary">下架</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <div class="btn-group" style="display:flex;gap:6px;align-items:center;">
                            <?php if ($state !== 'trash'): ?>
                            <a href="/public/index.php?m=recruit&id=<?php echo $item['id']; ?>" target="_blank" class="btn btn-ghost"><svg style="width:14px;height:14px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg> 预览</a>
                            <a href="?act=recruit&action=edit&id=<?php echo $item['id']; ?>" class="btn btn-primary"><svg style="width:14px;height:14px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></svg> 编辑</a>
                            <a href="javascript:delRecruit(<?php echo $item['id']; ?>);" class="btn btn-ghost" style="color:#EF4444;"><svg style="width:14px;height:14px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg> 删除</a>
                            <?php else: ?>
                            <button type="button" class="btn btn-secondary" onclick="singleWorkflow('restore',<?php echo intval($item['id']); ?>)">恢复</button>
                            <button type="button" class="btn btn-danger" onclick="singleWorkflow('purge',<?php echo intval($item['id']); ?>)">永久删除</button>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
    <?php cmsRenderContentWorkflowPagination('recruit', $state, $page, $totalPages); ?>
    </div>
    <script>
    function delRecruit(id) { if (confirm('确定移入回收站？')) { fetch('?act=recruit&action=delete', {method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:'id='+id+'&csrf_token=<?php echo rawurlencode(getAdminCsrfToken()); ?>'}).then(r=>r.json()).then(d=>{alert(d.msg);if(d.code==200)location.reload();}); } }
    if (window.lucide && typeof window.lucide.createIcons === 'function') { window.lucide.createIcons(); }
    
    function toggleCheckAll(cb) { document.querySelectorAll('.row-check').forEach(function(c){c.checked=cb.checked;}); }
    function getSelectedIds() { var ids=[]; document.querySelectorAll('.row-check:checked').forEach(function(c){ids.push(c.value);}); return ids; }
    function singleWorkflow(action,id) { document.querySelectorAll('.row-check').forEach(function(c){ c.checked=String(c.value)===String(id); }); batchAction(action); }
    function batchAction(action) {
        var ids = getSelectedIds();
        if (ids.length === 0) { alert('请先勾选要操作的项'); return; }
        if (action === 'delete' && !confirm('确定移入回收站？')) return;
        if (action === 'purge' && !confirm('永久删除后无法恢复，确定继续？')) return;
        var xhr = new XMLHttpRequest();
        xhr.open('POST', '?act=recruit&action=batch', true);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                try { var d = JSON.parse(xhr.responseText); alert(d.msg||'操作完成'); if(d.code==200) location.reload(); }
                catch(e) { alert('JSON解析失败'); }
            }
        };
        xhr.send('ajax_action=' + action + '&ids=' + encodeURIComponent(JSON.stringify(ids)) + '&csrf_token=<?php echo rawurlencode(getAdminCsrfToken()); ?>');
    }

</script>
<?php
}

if ($action === 'add' || $action === 'edit') {
    $defaults = ['id'=>'','channel_id'=>0,'title'=>'','department'=>'','region'=>'','salary'=>'','quantity'=>1,'experience'=>'','education'=>'','work_type'=>'全职','summary'=>'','content'=>'','seo_title'=>'','seo_keywords'=>'','seo_description'=>'','sort_order'=>100,'status'=>1];
    if ($action === 'add' && isset($_GET['channel_id'])) {
        $defaults['channel_id'] = intval($_GET['channel_id']);
    }
    if ($action === 'edit' && isset($_GET['id'])) {
        $row = $db->fetch("SELECT * FROM {$db->table('recruit')} WHERE id = ? AND deleted_at = 0", [intval($_GET['id'])]);
        if (!$row) { echo '<script>alert("内容不存在或已在回收站");location.href="?act=recruit";</script>'; exit; }
        if ($row) { $editData = array_merge($defaults, $row); }
    } else {
        $editData = $defaults;
    }
    $isEdit = $action === 'edit';
    $recruitChannels = $db->fetchAll("SELECT id, channel_name FROM {$db->table('channel')} WHERE channel_type = 9 ORDER BY list_order ASC");
    ?>
    <div class="page-container fade-in">
        <div class="page-header">
            <div class="page-title">
                <div class="page-icon"><svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></svg></div>
                <div><h1><?php echo $isEdit ? '编辑职位' : '添加职位'; ?></h1><p style="font-size:12px;color:#64748b;margin:0;">填写招聘信息</p></div>
            </div>
            <div class="page-actions"><a href="?act=recruit" class="btn btn-secondary">&larr; 返回</a></div>
        </div>
        <form method="post" action="?act=recruit&action=save">
            <input type="hidden" name="id" value="<?php echo $editData['id']; ?>">
            <div class="form-section">
                <div class="form-section-header"><svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg> 基本信息</div>
                <div class="form-section-body">
                    <div class="form-group"><label>职位名称 <span class="required">*</span></label><input type="text" name="title" class="form-control" value="<?php echo htmlspecialchars($editData['title']); ?>" placeholder="如：PHP高级工程师" required></div>
                    <div class="form-group"><label>所属栏目</label>
                        <select name="channel_id" class="form-control" style="width:200px;">
                            <option value="0">-- 请选择栏目 --</option>
                            <?php foreach ($recruitChannels as $rc): ?>
                            <option value="<?php echo $rc['id']; ?>"<?php echo $editData['channel_id'] == $rc['id'] ? ' selected' : ''; ?>><?php echo htmlspecialchars($rc['channel_name']); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-row">
                        <div class="form-group"><label>部门</label><input type="text" name="department" class="form-control" value="<?php echo htmlspecialchars($editData['department']); ?>" placeholder="如：技术部"></div>
                        <div class="form-group"><label>工作地区</label><input type="text" name="region" class="form-control" value="<?php echo htmlspecialchars($editData['region']); ?>" placeholder="如：上海浦东"></div>
                    </div>
                    <div class="form-row">
                        <div class="form-group"><label>薪资范围</label><input type="text" name="salary" class="form-control" value="<?php echo htmlspecialchars($editData['salary']); ?>" placeholder="如：15k-25k / 面议"></div>
                        <div class="form-group"><label>招聘人数</label><input type="number" name="quantity" class="form-control" value="<?php echo intval($editData['quantity']); ?>" min="1" style="width:120px;"></div>
                    </div>
                    <div class="form-row">
                        <div class="form-group"><label>经验要求</label><input type="text" name="experience" class="form-control" value="<?php echo htmlspecialchars($editData['experience']); ?>" placeholder="如：3-5年"></div>
                        <div class="form-group"><label>学历要求</label><input type="text" name="education" class="form-control" value="<?php echo htmlspecialchars($editData['education']); ?>" placeholder="如：本科及以上"></div>
                    </div>
                    <div class="form-group"><label>工作类型</label>
                        <select name="work_type" class="form-control">
                            <option value="全职"<?php echo $editData['work_type']=='全职' ? ' selected' : ''; ?>>全职</option>
                            <option value="兼职"<?php echo $editData['work_type']=='兼职' ? ' selected' : ''; ?>>兼职</option>
                            <option value="实习"<?php echo $editData['work_type']=='实习' ? ' selected' : ''; ?>>实习</option>
                            <option value="外包"<?php echo $editData['work_type']=='外包' ? ' selected' : ''; ?>>外包</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="form-section">
                <div class="form-section-header"><svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/></svg> 职位详情</div>
                <div class="form-section-body">
                    <div class="form-group"><label>职位概要</label><textarea name="summary" class="form-control" rows="2" placeholder="简要描述"><?php echo htmlspecialchars($editData['summary']); ?></textarea></div>
                    <div class="form-group"><label>详细描述 <span class="required">*</span></label>
                        <div class="cms-ueditor-wrap" style="margin-top:6px;">
                            <textarea id="recruit_content_editor" name="content" data-cms-ueditor data-height="360" aria-label="职位详细描述"><?php echo htmlspecialchars(isset($editData['content']) ? $editData['content'] : '', ENT_QUOTES, 'UTF-8'); ?></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-section">
                <div class="form-section-header"><svg style="width:14px;height:14px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg> SEO设置</div>
                <div class="form-section-body">
                    <div class="form-group"><label>SEO标题</label><input type="text" name="seo_title" class="form-control" value="<?php echo htmlspecialchars($editData['seo_title']); ?>" placeholder="SEO标题"></div>
                    <div class="form-group"><label>SEO关键字</label><input type="text" name="seo_keywords" class="form-control" value="<?php echo htmlspecialchars($editData['seo_keywords']); ?>" placeholder="关键字1,关键字2"></div>
                    <div class="form-group"><label>SEO描述</label><textarea name="seo_description" class="form-control" rows="3" placeholder="描述内容"><?php echo htmlspecialchars($editData['seo_description']); ?></textarea></div>
                </div>
            </div>
            <div class="form-section">
                <div class="form-section-header"><svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg> 显示设置</div>
                <div class="form-section-body">
                    <div class="form-row">
                        <div class="form-group"><label>排序</label><input type="number" name="sort_order" class="form-control" value="<?php echo $editData['sort_order']; ?>" min="0" style="width:120px;"></div>
                        <div class="form-group"><label>&nbsp;</label>
                            <label style="display:flex;align-items:center;gap:12px;cursor:pointer;padding-top:8px;">
                                <input type="checkbox" name="status" value="1" <?php echo $editData['status'] ? 'checked' : ''; ?> id="stToggle" style="display:none;">
                                <div id="stSwitch" style="width:44px;height:24px;background:<?php echo $editData['status'] ? '#10b981' : '#cbd5e1'; ?>;border-radius:12px;position:relative;transition:all 0.2s;"><div style="width:18px;height:18px;background:#fff;border-radius:50%;position:absolute;top:3px;left:<?php echo $editData['status'] ? '23px' : '3px'; ?>;transition:all 0.2s;"></div></div>
                                <span id="stLabel" style="font-weight:500;color:<?php echo $editData['status'] ? '#10b981' : '#64748b'; ?>;"><?php echo $editData['status'] ? '发布' : '下架'; ?></span>
                            </label>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-actions"><button type="submit" name="save_mode" value="draft" class="btn btn-secondary btn-lg">保存隐藏</button><button type="submit" name="save_mode" value="publish" class="btn btn-success btn-lg">保存并显示</button><a href="?act=recruit" class="btn btn-secondary btn-lg">取消</a></div>
        </form>
    </div>
    <script>
    document.getElementById('stToggle') && document.getElementById('stToggle').addEventListener('change', function() {
        var sw = document.getElementById('stSwitch');
        var lb = document.getElementById('stLabel');
        if (this.checked) { sw.style.background = '#10b981'; sw.querySelector('div').style.left = '23px'; lb.textContent = '发布'; lb.style.color = '#10b981'; }
        else { sw.style.background = '#cbd5e1'; sw.querySelector('div').style.left = '3px'; lb.textContent = '下架'; lb.style.color = '#64748b'; }
    });
    if (window.lucide && typeof window.lucide.createIcons === 'function') { window.lucide.createIcons(); }
    
    function toggleCheckAll(cb) { document.querySelectorAll('.row-check').forEach(function(c){c.checked=cb.checked;}); }
    function getSelectedIds() { var ids=[]; document.querySelectorAll('.row-check:checked').forEach(function(c){ids.push(c.value);}); return ids; }
    function batchAction(action) {
        var ids = getSelectedIds();
        if (ids.length === 0) { alert('请先勾选要操作的项'); return; }
        if (action === 'delete') {
            if (!confirm('确定删除选中的 ' + ids.length + ' 项？')) return;
        }
        var xhr = new XMLHttpRequest();
        xhr.open('POST', '?act=recruit&action=batch', true);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                try { var d = JSON.parse(xhr.responseText); alert(d.msg||'操作完成'); if(d.code==200) location.reload(); }
                catch(e) { alert('JSON解析失败'); }
            }
        };
        xhr.send('ajax_action=' + action + '&ids=' + encodeURIComponent(JSON.stringify(ids)));
    }

</script>
    <?php
}
?>
