<?php
session_start();
define('IN_SYSTEM', true);
require_once __DIR__ . '/inc/config.php';
require_once __DIR__ . '/inc/db.class.php';
require_once __DIR__ . '/inc/functions.php';

$siteTitle = getConfig('site_title', '✨ 七夕云短视频 · 智能播放');
$siteDesc = getConfig('site_description', '');
$siteKeywords = getConfig('site_keywords', '');
$commentEnabled = getConfig('comment_enabled', '1') == '1';
$commentAudit = getConfig('comment_audit', '0') == '1';

$isLoggedIn = isset($_SESSION['user_id']) && !empty($_SESSION['user_id']);
if ($isLoggedIn) {
    $userNickname = $_SESSION['user_nickname'] ?? $_SESSION['user_username'] ?? '用户';
    $userAvatar = $_SESSION['user_avatar'] ?? 'assets/default-avatar.png';
    $userCover = $_SESSION['user_cover'] ?? 'assets/default-cover.jpg';
    $userBio = $_SESSION['user_bio'] ?? '更多功能正在开发中，程序员Mr.刘';
    $userUid = $_SESSION['user_uid'] ?? '';
} else {
    $userNickname = '未登录，点击登录';
    $userAvatar = 'assets/default-avatar.png';
    $userCover = 'assets/default-cover.jpg';
    $userBio = '登录后查看更多功能';
    $userUid = '请登录';
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
<title><?= htmlspecialchars($siteTitle) ?></title>
<meta name="description" content="<?= htmlspecialchars($siteDesc) ?>">
<meta name="keywords" content="<?= htmlspecialchars($siteKeywords) ?>">
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.min.css">
<style>
.modal-content { background: #1a1a2e !important; color: #fff !important; border-radius: 20px !important; }
.modal-header { border-bottom: 1px solid #333 !important; }
.modal-footer { border-top: 1px solid #333 !important; }
.close { color: #fff !important; opacity: 0.8; }
.form-control { background: #2a2a4a !important; border: 1px solid #444 !important; color: #fff !important; border-radius: 12px !important; }
.form-control:focus { border-color: #ff3355 !important; box-shadow: 0 0 0 2px rgba(255,51,85,0.3) !important; }
.form-control[disabled], .form-control[readonly] { background: #1a1a2e !important; color: #888 !important; cursor: not-allowed; }
.help-block { color: #aaa !important; font-size: 12px; }
.btn-danger { background: #555 !important; border: none !important; }
.btn-danger:hover { background: #777 !important; }
.comment-image-preview { max-width: 80px; max-height: 80px; border-radius: 8px; margin: 5px 0; }
.comment-input-area { display: flex; align-items: center; padding: 12px 20px; gap: 8px; border-top: 1px solid rgba(255,255,255,0.1); }
.comment-input { flex: 1; background: rgba(255,255,255,0.1); border: 1px solid rgba(255,255,255,0.15); border-radius: 24px; padding: 10px 16px; color: #fff; font-size: 14px; outline: none; }
.comment-input::placeholder { color: #888; }
.image-upload-label { cursor: pointer; font-size: 24px; padding: 5px; color: #aaa; transition: color 0.2s; }
.image-upload-label:hover { color: #ff3355; }
.comment-send-btn { background: #ff3355; border: none; border-radius: 24px; padding: 10px 18px; color: #fff; font-size: 14px; font-weight: 600; cursor: pointer; transition: background 0.2s; }
.comment-send-btn:hover { background: #e02244; }
.comment-send-btn:disabled { background: #666; cursor: not-allowed; }
</style>
</head>
<body>

<div id="contextMenu" class="context-menu">
  <div class="context-item" id="moveToWatchBtn">📤 移出到观看记录</div>
</div>

<div id="errorToast" class="error-toast"></div>
<div id="autoPlayTip" class="auto-play-tip">🔇 浏览器已静音，点击画面或滑动可开启声音</div>

<div id="announcementModal" class="announcement-modal" style="display:none;">
  <div class="announcement-content">
    <div class="announcement-header">
      <h3 id="announcementTitle">公告</h3>
      <button class="announcement-close" onclick="closeAnnouncement()">&times;</button>
    </div>
    <div class="announcement-body" id="announcementBody"></div>
    <div class="announcement-footer">
      <button class="btn btn-primary" onclick="confirmAnnouncement()">确定收到</button>
      <button class="btn btn-secondary" onclick="remindLater()">24小时后提醒</button>
    </div>
  </div>
</div>

<div class="page active" id="videoPage">
  <div class="video-list" id="videoList"></div>
  <div class="heart" id="heart">❤️</div>
  <div class="info-box">
    <div class="at-text"><a href="#" onclick="return false;">@随机小姐姐</a></div>
    <div class="tags" id="videoTags"></div>
  </div>
  <div class="toolbar">
    <!--<button class="tool" id="playSettingsBtn" title="播放设置">⚙️</button>-->
    <button class="tool" id="soundBtn" title="声音">🔊</button>
    <button class="tool like-tool" id="likeBtn" title="点赞">
      <span class="tool-icon">♡</span>
      <span class="tool-count" id="likeCount">0</span>
    </button>
    <button class="tool collect-tool" id="collectBtn" title="收藏">
      <span class="tool-icon">★</span>
      <span class="tool-count" id="collectCount">0</span>
    </button>
    <?php if ($commentEnabled): ?>
    <button class="tool" id="commentBtn" title="评论">💬</button>
    <?php endif; ?>
    <button class="tool" id="shareBtn" title="分享">↗️</button>
  </div>

  <div id="playSettingsPanel" class="play-settings-panel">
    <div class="panel-section">
      <div class="panel-label">播放速度</div>
      <div class="speed-options">
        <span data-speed="0.5" class="speed-option">0.5x</span>
        <span data-speed="1.0" class="speed-option active">1.0x</span>
        <span data-speed="1.5" class="speed-option">1.5x</span>
        <span data-speed="2.0" class="speed-option">2.0x</span>
      </div>
    </div>
    <div class="panel-section auto-next-section">
      <span class="panel-label">自动连播</span>
      <label class="switch">
        <input type="checkbox" id="autoNextToggle" checked>
        <span class="slider"></span>
      </label>
    </div>
    <div class="panel-section">
      <button class="next-video-btn" id="panelNextBtn">
        <span>⏩</span> 下一个视频
      </button>
    </div>
  </div>

  <div id="commentPanel" class="comment-panel">
    <div class="comment-header">
      <span class="comment-title">评论</span>
      <button class="comment-close" id="closeCommentPanel">&times;</button>
    </div>
    <div class="comment-list" id="commentList">
      <div class="comment-item" style="justify-content:center; color:#888;">加载中...</div>
    </div>
    <div class="comment-input-area">
      <input type="text" class="comment-input" id="commentInput" placeholder="说点什么...">
      <input type="file" id="commentImageInput" accept="image/jpeg,image/png,image/gif,image/webp" style="display:none;">
      <label for="commentImageInput" class="image-upload-label" title="上传图片">📷</label>
      <button class="comment-send-btn" id="sendCommentBtn">发送</button>
    </div>
    <div id="imagePreviewContainer" style="padding:0 20px 10px; display:none;">
      <img id="imagePreview" class="comment-image-preview" src="" alt="预览">
      <button id="removeImageBtn" style="background:none;border:none;color:#ff3355;margin-left:10px;cursor:pointer;">✕ 移除</button>
    </div>
  </div>
</div>

<div class="page" id="minePage">
  <div class="profile-page">
    <div class="profile-cover" id="profileCoverWrapper">
      <img src="<?= htmlspecialchars($userCover) ?>" alt="封面" id="profileCover">
      <?php if ($isLoggedIn): ?>
      <input type="file" id="coverUpload" accept="image/*" style="display:none;">
      <?php endif; ?>
      <div class="cover-change-hint">📷 更换封面</div>
    </div>

    <div class="profile-header">
      <div class="profile-avatar" id="profileAvatarWrapper">
        <?php if ($isLoggedIn): ?>
          <img src="<?= htmlspecialchars($userAvatar) ?>" alt="头像" id="profileAvatar">
          <input type="file" id="avatarUpload" accept="image/*" style="display:none;">
        <?php else: ?>
          <img src="assets/default-avatar.png" alt="头像" id="profileAvatar" style="cursor:pointer;" onclick="location.href='login.php'">
        <?php endif; ?>
      </div>
      <div class="profile-info">
        <h2 class="profile-nickname" id="profileNickname" style="cursor:pointer;"><?= htmlspecialchars($userNickname) ?></h2>
        <p class="profile-id" id="profileUserId">UID：<?= htmlspecialchars($userUid) ?></p>
        <p class="profile-bio" id="profileBio" style="cursor:pointer;"><?= htmlspecialchars($userBio) ?></p>
      </div>
      <?php if ($isLoggedIn): ?>
        <button class="edit-profile-btn" id="manageProfileBtn">管理主页</button>
      <?php else: ?>
        <button class="edit-profile-btn" onclick="location.href='login.php'">立即登录</button>
      <?php endif; ?>
    </div>

    <div class="profile-stats">
      <div class="stat-item">
        <span class="stat-value" id="statWatch">0</span>
        <span class="stat-label">观看</span>
      </div>
      <div class="stat-item">
        <span class="stat-value" id="statFailed">0</span>
        <span class="stat-label">失败</span>
      </div>
      <div class="stat-item">
        <span class="stat-value" id="statLikesCount">0</span>
        <span class="stat-label">点赞</span>
      </div>
      <div class="stat-item">
        <span class="stat-value" id="statCollectsCount">0</span>
        <span class="stat-label">收藏</span>
      </div>
    </div>

    <div class="profile-tabs">
      <span class="tab-item active" data-profile-tab="history">历史</span>
      <span class="tab-item" data-profile-tab="likes">点赞</span>
      <span class="tab-item" data-profile-tab="collects">收藏</span>
    </div>

    <div class="profile-content">
      <div class="tab-pane active" id="profileHistory">
        <div class="history-sub-tabs">
          <span class="sub-tab active" data-sub-tab="watch">👀 观看记录</span>
          <span class="sub-tab" data-sub-tab="failed">⚠️ 失败记录</span>
        </div>
        <div class="sub-tab-content active" id="profileWatchHistory">
          <div class="list-container" id="watchHistoryList"></div>
        </div>
        <div class="sub-tab-content" id="profileFailedHistory">
          <div class="list-container" id="failedHistoryList"></div>
        </div>
      </div>

      <div class="tab-pane" id="profileLikes">
        <div class="waterfall-grid" id="likesGridList">
          <div class="empty-state">暂无点赞</div>
        </div>
      </div>

      <div class="tab-pane" id="profileCollects">
        <div class="waterfall-grid" id="collectsGridList">
          <div class="empty-state">暂无收藏</div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="nav">
  <div class="item active" data-page="videoPage">首页</div>
  <div class="item" data-page="minePage">我的</div>
</div>

<div class="modal fade" id="editProfileModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title" style="font-weight:700;">✏️ 编辑资料</h4>
      </div>
      <div class="modal-body">
        <div class="form-group">
          <label>用户名</label>
          <input type="text" id="editUsername" class="form-control" placeholder="4-16位字母数字下划线">
          <span class="help-block">修改后需重新登录</span>
        </div>
        <div class="form-group">
          <label>昵称</label>
          <input type="text" id="editNickname" class="form-control" placeholder="显示名称">
        </div>
        <div class="form-group">
          <label>UID（不可修改）</label>
          <input type="text" id="editUid" class="form-control" readonly disabled>
          <span class="help-block">UID 由系统分配，如需修改请联系管理员</span>
        </div>
        <div class="form-group">
          <label>个性签名</label>
          <textarea id="editBio" class="form-control" rows="2" placeholder="介绍一下自己"></textarea>
        </div>
        <hr style="border-color:#444;">
        <div class="form-group">
          <label>原密码</label>
          <input type="password" id="oldPassword" class="form-control" placeholder="如需修改密码请输入">
        </div>
        <div class="form-group">
          <label>新密码</label>
          <input type="password" id="newPassword" class="form-control" placeholder="6-20位，留空不修改">
        </div>
        <div class="form-group">
          <label>确认新密码</label>
          <input type="password" id="confirmPassword" class="form-control" placeholder="再次输入新密码">
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary" id="saveProfileBtn" style="background:#ff3355; border:none;">保存</button>
        <button type="button" class="btn btn-danger" id="logoutBtn">退出登录</button>
      </div>
    </div>
  </div>
</div>

<script>
window.IS_LOGGED_IN = <?= $isLoggedIn ? 'true' : 'false' ?>;
window.USER_INFO = {
  nickname: '<?= addslashes($userNickname) ?>',
  avatar: '<?= addslashes($userAvatar) ?>',
  uid: '<?= addslashes($userUid) ?>'
};
window.COMMENT_ENABLED = <?= $commentEnabled ? 'true' : 'false' ?>;
window.COMMENT_AUDIT = <?= $commentAudit ? 'true' : 'false' ?>;
</script>
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.min.js"></script>
<script>
(function() {
  fetch('api/stats.php?action=track', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ url: location.href, referrer: document.referrer, timestamp: Date.now() })
  }).catch(e => console.error('Stat error:', e));
})();
</script>
<script src="assets/app.js?v=<?= time() ?>"></script>
</body>
</html>