<?php
$currentPage = 'repost';
$pageType = 'repost';
$bodyClass = 'repost-page';
$hideTabBar = true;
$userId = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
$this->setLayout('app');
?>
<style>
    .repost-container {
        padding: 16px;
        background: var(--card-color);
        min-height: 100vh;
    }
    
    .original-post {
        background: var(--background-color);
        border-radius: 12px;
        padding: 16px;
        margin-bottom: 16px;
    }
    
    .original-post-header {
        display: flex;
        margin-bottom: 8px;
    }
    
    .original-post-avatar {
        width: 36px;
        height: 36px;
        border-radius: 50%;
        margin-right: 10px;
    }
    
    .original-post-user {
        font-weight: 600;
        font-size: 14px;
        color: var(--text-color);
    }
    
    .original-post-content {
        font-size: 14px;
        line-height: 1.5;
        color: var(--text-color);
        word-break: break-all;
    }
    
    .repost-textarea {
        width: 100%;
        min-height: 120px;
        border: 1px solid var(--border-color);
        border-radius: 12px;
        padding: 14px;
        font-size: 15px;
        line-height: 1.5;
        resize: none;
        background: var(--background-color);
        color: var(--text-color);
    }
    
    .repost-textarea:focus {
        outline: none;
        border-color: var(--primary-color);
    }
    
    .repost-options {
        margin-top: 16px;
        display: flex;
        align-items: center;
        gap: 16px;
    }
    
    .repost-option {
        display: flex;
        align-items: center;
        gap: 6px;
        font-size: 14px;
        color: var(--text-secondary-color);
        cursor: pointer;
    }
    
    .repost-option input {
        width: 18px;
        height: 18px;
    }
    
    .repost-submit {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        padding: 12px 16px;
        background: var(--card-color);
        border-top: 1px solid var(--border-color);
    }
    
    .repost-submit button {
        width: 100%;
        padding: 14px;
        background: var(--primary-color);
        color: white;
        border: none;
        border-radius: 10px;
        font-size: 16px;
        font-weight: 600;
        cursor: pointer;
    }
    
    .repost-submit button:disabled {
        opacity: 0.5;
        cursor: not-allowed;
    }
</style>

<div class="repost-container">
    <div class="original-post" id="originalPost">
        <div class="empty-state">
            <div class="empty-icon">⏳</div>
            <div class="empty-text">加载中...</div>
        </div>
    </div>
    
    <textarea class="repost-textarea" id="repostContent" placeholder="说点什么..." maxlength="500"></textarea>
    
    <div class="repost-options">
        <label class="repost-option">
            <input type="checkbox" id="alsoComment">
            同时评论
        </label>
    </div>
</div>

<div class="repost-submit">
    <button id="repostSubmit">转发</button>
</div>

<script>
const API_BASE = '<?php echo $this->url('api/mobile'); ?>';
const postId = <?php echo isset($id) ? $id : (isset($_GET['id']) ? intval($_GET['id']) : 0); ?>;

document.addEventListener('DOMContentLoaded', function() {
    if (postId) {
        loadOriginalPost();
    }
    
    document.getElementById('repostSubmit').addEventListener('click', submitRepost);
});

async function loadOriginalPost() {
    try {
        const response = await fetch(API_BASE + '/postDetail?id=' + postId);
        const result = await response.json();
        
        if (result.code === 0 && result.data && result.data.post) {
            const post = result.data.post;
            document.getElementById('originalPost').innerHTML = `
                <div class="original-post-header">
                    <img class="original-post-avatar" src="${post.avatar || '/static/avatar-default.png'}" alt="">
                    <div class="original-post-user">${escapeHtml(post.username)}</div>
                </div>
                <div class="original-post-content">${post.content}</div>
            `;
        }
    } catch (error) {
        console.error('加载失败:', error);
    }
}

async function submitRepost() {
    const content = document.getElementById('repostContent').value.trim();
    const alsoComment = document.getElementById('alsoComment').checked;
    const submitBtn = document.getElementById('repostSubmit');
    
    submitBtn.disabled = true;
    submitBtn.textContent = '转发中...';
    
    try {
        const response = await fetch(API_BASE + '/repost', {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({
                post_id: postId,
                content: content,
                also_comment: alsoComment ? 1 : 0
            })
        });
        
        const result = await response.json();
        
        if (result.code === 0) {
            alert('转发成功！');
            window.location.href = '<?php echo $this->url('mobile'); ?>';
        } else {
            alert(result.message || '转发失败');
            submitBtn.disabled = false;
            submitBtn.textContent = '转发';
        }
    } catch (error) {
        alert('网络错误');
        submitBtn.disabled = false;
        submitBtn.textContent = '转发';
    }
}

function escapeHtml(text) {
    const div = document.createElement('div');
    div.textContent = text;
    return div.innerHTML;
}
</script>
