<?php
include("./includes/common.php");

$title = '文件查看 - '.$conf['title'];
$is_file=true;
include SYSTEM_ROOT.'header.php';

$csrf_token = md5(mt_rand(0,999).time());
$_SESSION['csrf_token'] = $csrf_token;

$hash = isset($_GET['hash'])?$_GET['hash']:exit("<script language='javascript'>window.location.href='./';</script>");
$row = $DB->getRow("SELECT * FROM pre_file WHERE hash=:hash", [':hash'=>$hash]);
if(!$row)exit('<!DOCTYPE html><html><head><meta charset="utf-8"><script src="https://s4.zstatic.net/ajax/libs/jquery/1.12.4/jquery.min.js"></script><script src="https://s4.zstatic.net/ajax/libs/layer/2.3/layer.js"></script></head><body><script>layer.alert("文件不存在", {icon: 2, closeBtn: 0}, function(){ window.location.href="./"; });</script></body></html>');

// 检查文件所有者的配额
$userrow_owner = $DB->getRow("SELECT * FROM pre_user WHERE uid=:uid", [':uid'=>$row['uid']]);
$is_owner = ($islogin2 && $row['uid']==$uid);
$quota_owner = check_user_quota($userrow_owner, false, 'traffic');
$is_quota_exceeded_public = ($quota_owner['code'] !== 0);
$is_quota_exceeded = ($is_owner ? false : $is_quota_exceeded_public);

$name = $row['name'];
$type = $row['type'];

$downurl = 'down.php?hash='.$row['hash'];
$viewurl = 'view.php?hash='.$row['hash'].'.'.$type;

$v = isset($row['lasttime']) ? strtotime($row['lasttime']) : time();
$query = 'v='.$v;
$downurl_all = $siteurl.$downurl.'&'.$query;
$viewurl_all = $siteurl.$viewurl.'&'.$query;

if(empty($row['share_key'])){
    $shareKey = substr(md5(uniqid((string)$row['uid'], true).mt_rand()), 0, 16);
    $DB->exec("UPDATE pre_file SET share_key=:k WHERE id=:fid", [':k'=>$shareKey, ':fid'=>$row['id']]);
    $row['share_key'] = $shareKey;
}
$shareLink = $siteurl . 'share.php?key=' . $row['share_key'];
$thisurl = $shareLink;

if($islogin2 && $row['uid']==$uid || !$islogin2 && ((isset($_SESSION['fileids']) && in_array($row['id'], $_SESSION['fileids'])) || in_array($row['id'], get_guest_file_ids())) && strtotime($row['addtime'])>strtotime("-7 days")){
  $is_mine = true;
}

$view_type = get_view_type($type);

if($view_type == 'image'){
  $filetype = 1;
  $title = '图片查看器';
  $title_icon = 'fa-picture-o';
  $htmlcode = htmlspecialchars('<img src="'.$viewurl_all.'"/>');
  $ubbcode = '[img]'.$viewurl_all.'[/img]';
  $linktitle = '图片链接';
}elseif($view_type == 'audio'){
  $filetype = 2;
  $title = '音乐播放器';
  $title_icon = 'fa-music';
  $htmlcode = htmlspecialchars('<audio id="bgmMusic" src="'.$viewurl_all.'" autoplay="autoplay" loop="loop" preload="auto"></audio>');
  $htmlcode2 = htmlspecialchars('<iframe src="'.$siteurl.'player.php?hash='.$hash.'" width="407" scrolling="no"frameborder="0"height="70"></iframe>');
  $ubbcode = '[audio=X]'.$viewurl_all.'[/audio]';
  $linktitle = '音乐链接';
}elseif($view_type == 'video'){
  $filetype = 3;
  $title = '视频播放器';
  $title_icon = 'fa-video-camera';
  $htmlcode = htmlspecialchars('<video id="movies" src="'.$viewurl_all.'" autobuffer="true" controls="" width="100
  %"></video>');
  $htmlcode2 = htmlspecialchars('<iframe src="'.$siteurl.'player.php?hash='.$hash.'" width="800" height="500" scrolling="no" frameborder="0"></iframe>');
  $ubbcode = '[movie=320*180]'.$viewurl_all.'[/movie]';
  $linktitle = '视频链接';
}else{
  $filetype = 0;
  $title = '文件查看';
  $title_icon = 'fa-file';
  $htmlcode = htmlspecialchars('<a href="'.$downurl_all.'" target="_blank">'.$name.'</a>');
  $ubbcode = '[url='.$downurl_all.']'.$name.'[/url]';
  if($view_type == 'office'){
    $office_url = 'https://view.officeapps.live.com/op/view.aspx?src='.rawurlencode($downurl_all);
  }
}
?>
<style>
.file-view-wrapper {
    padding: 20px 0;
}
.file-view-header {
    background: linear-gradient(135deg, #0f766e 0%, #14b8a6 100%);
    border-radius: 20px;
    padding: 25px 30px;
    margin-bottom: 25px;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: relative;
    overflow: hidden;
}
.file-view-header::before {
    content: '';
    position: absolute;
    right: -30px;
    top: -30px;
    width: 120px;
    height: 120px;
    background: rgba(255,255,255,0.1);
    border-radius: 50%;
}
.file-view-header .header-info {
    position: relative;
    z-index: 1;
}
.file-view-header .header-info h1 {
    font-size: 22px;
    font-weight: 700;
    margin: 0 0 5px;
    display: flex;
    align-items: center;
    gap: 10px;
}
.file-view-header .header-info p {
    font-size: 14px;
    opacity: 0.9;
    margin: 0;
    max-width: 500px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.file-view-card {
    background: #fff;
    border-radius: 20px;
    box-shadow: 0 10px 40px rgba(15, 118, 110, 0.1);
    overflow: hidden;
    margin-bottom: 25px;
}
.file-view-card-header {
    padding: 20px 25px;
    border-bottom: 1px solid #f0f0f0;
    display: flex;
    align-items: center;
    gap: 12px;
}
.file-view-card-header h3 {
    font-size: 18px;
    font-weight: 600;
    color: #333;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 10px;
}
.file-view-card-header h3 i {
    color: #14b8a6;
}
.file-view-card-body {
    padding: 25px;
}
.file-preview-area {
    background: #fafafa;
    border-radius: 16px;
    padding: 30px;
    text-align: center;
    min-height: 300px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
}
.file-preview-area img {
    max-width: 100%;
    max-height: 500px;
    border-radius: 12px;
    box-shadow: 0 5px 20px rgba(0,0,0,0.1);
}
.file-preview-icon {
    width: 100px;
    height: 100px;
    background: linear-gradient(135deg, #0f766e 0%, #14b8a6 100%);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
}
.file-preview-icon i {
    font-size: 45px;
    color: #fff;
}
.file-preview-name {
    font-size: 18px;
    font-weight: 600;
    color: #333;
    margin-bottom: 8px;
}
.file-preview-size {
    color: #888;
    font-size: 14px;
    margin-bottom: 20px;
}
.file-preview-actions {
    display: flex;
    gap: 15px;
    flex-wrap: wrap;
    justify-content: center;
}
.btn-file-action {
    padding: 12px 28px;
    border-radius: 12px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
    border: none;
}
.btn-file-primary {
    background: linear-gradient(135deg, #0f766e 0%, #14b8a6 100%);
    color: #fff;
}
.btn-file-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(15, 118, 110, 0.35);
    color: #fff;
}
.btn-file-info {
    background: #e6f9f7;
    color: #0f766e;
    border: 1px solid #14b8a6;
}
.btn-file-info:hover {
    background: #14b8a6;
    color: #fff;
}
.file-tabs {
    display: flex;
    border-bottom: 2px solid #f0f0f0;
    margin-bottom: 20px;
}
.file-tab {
    padding: 15px 25px;
    font-size: 14px;
    font-weight: 500;
    color: #666;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 8px;
}
.file-tab:hover {
    color: #14b8a6;
}
.file-tab.active {
    color: #0f766e;
    border-bottom-color: #14b8a6;
}
.file-tab i {
    font-size: 16px;
}
.tab-content-pane {
    display: none;
}
.tab-content-pane.active {
    display: block;
}
.link-input-group {
    display: flex;
    gap: 10px;
    margin-bottom: 15px;
}
.link-input-group label {
    width: 100px;
    padding-top: 10px;
    font-weight: 500;
    color: #555;
    font-size: 14px;
}
.link-input-group input {
    flex: 1;
    padding: 12px 15px;
    border: 2px solid #e8e8e8;
    border-radius: 10px;
    font-size: 14px;
    background: #fafafa;
    color: #555;
}
.link-input-group input:focus {
    border-color: #14b8a6;
    outline: none;
}
.btn-copy {
    padding: 12px 20px;
    background: linear-gradient(135deg, #0f766e 0%, #14b8a6 100%);
    border: none;
    border-radius: 10px;
    color: #fff;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}
.btn-copy:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(15, 118, 110, 0.3);
}
.file-info-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
}
.file-info-item {
    background: #fff;
    padding: 18px 20px;
    border-radius: 14px;
    display: flex;
    flex-direction: column;
    border: 2px solid #e8f5f3;
    border-left: 4px solid #14b8a6;
    transition: all 0.2s;
    box-shadow: 0 2px 8px rgba(15, 118, 110, 0.06);
}
.file-info-item:hover {
    border-color: #14b8a6;
    box-shadow: 0 4px 16px rgba(15, 118, 110, 0.12);
    transform: translateY(-2px);
}
.file-info-item .label {
    font-size: 12px;
    color: #0f766e;
    font-weight: 600;
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    display: flex;
    align-items: center;
    gap: 6px;
}
.file-info-item .label::before {
    content: '';
    width: 6px;
    height: 6px;
    background: #14b8a6;
    border-radius: 50%;
}
.file-info-item .value {
    font-size: 16px;
    font-weight: 600;
    color: #1a1a2e;
    line-height: 1.4;
}
.manage-actions {
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.manage-action-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 15px 20px;
    background: #fafafa;
    border-radius: 12px;
    transition: all 0.2s;
}
.manage-action-item:hover {
    background: #f0fdfa;
}
.manage-action-info {
    display: flex;
    align-items: center;
    gap: 15px;
}
.manage-action-icon {
    width: 40px;
    height: 40px;
    background: linear-gradient(135deg, #0f766e 0%, #14b8a6 100%);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 16px;
}
.manage-action-text h4 {
    font-size: 15px;
    font-weight: 600;
    color: #333;
    margin: 0 0 3px;
}
.manage-action-text p {
    font-size: 13px;
    color: #888;
    margin: 0;
}
.btn-manage-action {
    padding: 8px 18px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    background: #e6f9f7;
    color: #0f766e;
    border: 1px solid #14b8a6;
}
.btn-manage-action:hover {
    background: #14b8a6;
    color: #fff;
}
.btn-manage-danger {
    background: #fee2e2;
    color: #dc2626;
    border-color: #fca5a5;
}
.btn-manage-danger:hover {
    background: #dc2626;
    color: #fff;
}
.manage-action-icon.danger {
    background: linear-gradient(135deg, #dc2626 0%, #ef4444 100%);
}
.sidebar-card {
    background: #fff;
    border-radius: 20px;
    box-shadow: 0 10px 40px rgba(15, 118, 110, 0.1);
    overflow: hidden;
    margin-bottom: 25px;
}
.sidebar-card-header {
    padding: 18px 20px;
    border-bottom: 1px solid #f0f0f0;
    background: #fafafa;
}
.sidebar-card-header h3 {
    font-size: 16px;
    font-weight: 600;
    color: #333;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 10px;
}
.sidebar-card-header h3 i {
    color: #14b8a6;
}
.sidebar-card-body {
    padding: 20px;
}
.qrcode-wrapper {
    display: flex;
    justify-content: center;
    padding: 10px;
}
.password-protected {
    text-align: center;
    padding: 60px 20px;
}
.password-protected .lock-icon {
    width: 80px;
    height: 80px;
    background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 20px;
}
.password-protected .lock-icon i {
    font-size: 36px;
    color: #fff;
}
.password-protected h3 {
    font-size: 20px;
    font-weight: 600;
    color: #333;
    margin: 0 0 10px;
}
.password-protected p {
    color: #888;
    font-size: 14px;
}
.quota-warning {
    background: linear-gradient(135deg, rgba(251, 191, 36, 0.1) 0%, rgba(245, 158, 11, 0.05) 100%);
    border: 1px solid #fcd34d;
    border-radius: 12px;
    padding: 15px 20px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 12px;
    color: #b45309;
}
.quota-warning i {
    font-size: 20px;
}
.quota-error {
    background: linear-gradient(135deg, rgba(220, 38, 38, 0.1) 0%, rgba(220, 38, 38, 0.05) 100%);
    border: 1px solid #fca5a5;
    border-radius: 12px;
    padding: 30px;
    text-align: center;
}
.quota-error i {
    font-size: 48px;
    color: #dc2626;
    margin-bottom: 15px;
    display: block;
}
.quota-error h4 {
    font-size: 16px;
    color: #dc2626;
    margin: 0 0 8px;
}
.quota-error p {
    color: #888;
    font-size: 14px;
    margin: 0;
}
@media (max-width: 768px) {
    .file-view-wrapper {
        padding: 10px 0;
    }
    .file-view-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 15px;
        padding: 20px 15px;
        border-radius: 15px;
        margin-bottom: 15px;
    }
    .file-view-header h2 {
        font-size: 18px;
        white-space: nowrap;
    }
    .file-view-header .btn {
        min-height: 44px;
        font-size: 14px;
        padding: 10px 16px;
        white-space: nowrap;
    }
    .file-info-grid {
        grid-template-columns: 1fr;
        gap: 10px;
    }
    .file-info-item {
        padding: 12px 15px;
    }
    .file-info-item .label {
        font-size: 11px;
    }
    .file-info-item .value {
        font-size: 14px;
    }
    .file-info-item .value.link-text {
        font-size: 12px;
        word-break: break-all;
    }
    .link-input-group {
        flex-direction: column;
        gap: 10px;
    }
    .link-input-group label {
        width: auto;
        font-size: 13px;
    }
    .link-input-group .input-group {
        width: 100%;
    }
    .link-input-group .form-control {
        min-height: 44px;
        font-size: 13px;
    }
    .link-input-group .btn {
        min-height: 44px;
        font-size: 14px;
    }
    .file-tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        gap: 5px;
        padding: 0 10px;
    }
    .file-tab {
        white-space: nowrap;
        padding: 10px 16px;
        font-size: 13px;
    }
    .file-view-card {
        border-radius: 15px;
        margin-bottom: 15px;
    }
    .file-view-card .panel-body {
        padding: 15px;
    }
    .download-options {
        flex-direction: column;
        gap: 10px;
    }
    .download-options .btn {
        width: 100%;
        min-height: 44px;
    }
    .manage-actions {
        gap: 8px;
    }
    .manage-action-item {
        flex-direction: column;
        align-items: flex-start;
        gap: 12px;
        padding: 12px 15px;
    }
    .manage-action-info {
        width: 100%;
    }
    .manage-action-text h4 {
        font-size: 14px;
    }
    .manage-action-text p {
        font-size: 12px;
    }
    .btn-manage-action {
        width: 100%;
        padding: 10px 16px;
        font-size: 13px;
        white-space: nowrap;
        min-height: 40px;
    }
}
</style>

<div class="file-view-wrapper">
<div class="container">
<?php
$is_owner = ($islogin2 && $row['uid']==$uid) || (!$islogin2 && ((isset($_SESSION['fileids']) && in_array($row['id'], $_SESSION['fileids'])) || in_array($row['id'], get_guest_file_ids())) && strtotime($row['addtime'])>strtotime("-7 days"));
if($row['pwd']!=null && !$is_owner){
    $is_valid = false;
    $info = password_get_info($row['pwd']);
    $input_pwd = isset($_GET['pwd']) ? $_GET['pwd'] : '';
    if ($info['algo'] != 0) {
        if (password_verify($input_pwd, $row['pwd'])) {
            $is_valid = true;
        }
    } else {
        if ($row['pwd'] == $input_pwd) {
            $is_valid = true;
        }
    }
    
    if (!$is_valid) { ?>
  <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
  <title>请输入密码下载文件</title>
  <script src="https://s4.zstatic.net/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <style>
  .pwd-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.6); z-index: 9999; display: flex; align-items: center; justify-content: center; }
  .pwd-modal { background: #fff; border-radius: 16px; padding: 40px; max-width: 400px; width: 90%; text-align: center; box-shadow: 0 10px 40px rgba(0,0,0,0.2); }
  .pwd-modal .icon { width: 80px; height: 80px; background: linear-gradient(135deg, #0f766e 0%, #14b8a6 100%); border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 20px; }
  .pwd-modal .icon i { font-size: 36px; color: #fff; }
  .pwd-modal h3 { margin: 0 0 25px; font-size: 20px; color: #333; }
  .pwd-modal input { width: 100%; padding: 14px 16px; border: 2px solid #e8e8e8; border-radius: 10px; font-size: 16px; margin-bottom: 15px; box-sizing: border-box; text-align: center; }
  .pwd-modal input:focus { outline: none; border-color: #14b8a6; }
  .pwd-modal button { width: 100%; padding: 14px; background: linear-gradient(135deg, #0f766e 0%, #14b8a6 100%); color: #fff; border: none; border-radius: 10px; font-size: 16px; font-weight: 600; cursor: pointer; }
  .pwd-modal button:hover { opacity: 0.9; }
  .pwd-modal .error-msg { color: #dc2626; font-size: 14px; margin-top: 10px; display: none; }
  </style>
  <div class="pwd-overlay">
    <div class="pwd-modal">
      <div class="icon"><i class="fa fa-lock"></i></div>
      <h3>请输入访问密码</h3>
      <form method="get" action="">
        <input type="hidden" name="hash" value="<?php echo $row['hash']; ?>">
        <input type="password" name="pwd" id="pwd-input" placeholder="请输入密码" autofocus required>
        <button type="submit" id="pwd-submit">确定</button>
      </form>
      <div class="error-msg" id="pwd-error"></div>
    </div>
  </div>
  <script>
  $(function() {
    var hash = '<?php echo $row['hash']; ?>';
    var urlParams = new URLSearchParams(window.location.search);
    if (urlParams.has('pwd')) {
      $('#pwd-error').text('密码错误，请重试').show();
    }
  });
  </script>
<?php
  exit;
    }
}

?>
    <div class="file-view-header">
        <div class="header-info">
            <h1><i class="fa <?php echo $title_icon?>"></i> <?php echo $title?></h1>
            <p><?php echo htmlspecialchars($name)?></p>
        </div>
        <a href="./" class="btn-file-action" style="background: rgba(255,255,255,0.2); color: #fff; border: 1px solid rgba(255,255,255,0.4);">
            <i class="fa fa-home"></i> 返回首页
        </a>
    </div>

    <div class="row">
        <div class="col-sm-9">
            <div class="file-view-card">
                <div class="file-view-card-body">
<?php
if($is_owner && $is_quota_exceeded_public){
  echo '<div class="quota-warning"><i class="fa fa-exclamation-triangle"></i> <span>提示：您的配额已超出限制（'.$quota_owner['msg'].'），其他用户暂时无法通过外链下载或预览此文件。</span></div>';
}
if($is_quota_exceeded){
  echo '<div class="quota-error">
  <i class="fa fa-exclamation-triangle"></i>
  <h4>'.$name.'</h4>
  <p>'.size_format($row['size']).'</p>
  <p style="color: #dc2626; margin-top: 10px;">'.$quota_owner['msg'].'，文件暂时无法预览或下载。</p>
</div>';
}elseif($filetype==1){
  echo '<div class="file-preview-area"><a href="'.$viewurl.'" title="点击查看原图"><img alt="loading" src="'.$viewurl.'"></a></div>';
}elseif($filetype==2){
  echo '<div id="aplayer" style="margin: -25px;"></div>';
}elseif($filetype==3 && $row['block']==0){
  echo '<div class="videoplayer" style="border-radius: 12px; overflow: hidden;"></div>';
}elseif($filetype==3){
  echo '<div class="file-preview-area">
  <div class="file-preview-icon"><i class="fa '.type_to_icon($type).'"></i></div>
  <div class="file-preview-name">'.$name.'</div>
  <p style="color: #f59e0b; font-size: 14px;">视频文件需审核通过后才能在线播放和下载，请等待审核通过！</p>
</div>';
}else{
  echo '<div class="file-preview-area">
  <div class="file-preview-icon"><i class="fa '.type_to_icon($type).'"></i></div>
  <div class="file-preview-name">'.$name.'</div>
  <div class="file-preview-size">'.size_format($row['size']).'</div>
  <div class="file-preview-actions">
    <a href="'.$downurl.'" class="btn-file-action btn-file-primary"><i class="fa fa-download"></i> 下载文件</a>
    '.($view_type=='office'?'<a href="'.$office_url.'" class="btn-file-action btn-file-info" target="_blank"><i class="fa fa-eye"></i> 在线预览</a>':'').'
  </div>
</div>';
}
?>
                </div>
            </div>

            <div class="file-view-card">
                <div class="file-tabs">
                    <div class="file-tab active" data-tab="link"><i class="fa fa-link"></i> 文件链接</div>
                    <div class="file-tab" data-tab="code"><i class="fa fa-code"></i> 代码调用</div>
                    <div class="file-tab" data-tab="info"><i class="fa fa-info-circle"></i> 文件详情</div>
                    <?php if($is_mine){?><div class="file-tab" data-tab="manager"><i class="fa fa-cog"></i> 管理</div><?php }?>
                </div>

                <div class="file-view-card-body">
                    <div class="tab-content-pane active" id="tab-link">
                        <?php if($is_quota_exceeded){ echo '<div class="quota-error" style="padding: 20px;"><i class="fa fa-exclamation-triangle" style="font-size: 24px;"></i><p>'.$quota_owner['msg'].'，外链分享已失效。</p></div>'; }else{ ?>
                        <div class="link-input-group">
                            <label>文件直链：</label>
                            <input type="text" id="link1" readonly value="<?php echo $downurl_all?>">
                            <button class="btn-copy" data-clipboard-text="<?php echo $downurl_all?>"><i class="fa fa-copy"></i> 复制</button>
                        </div>
                        <div class="link-input-group">
                            <label>普通链接：</label>
                            <input type="text" id="link2" readonly value="<?php echo $thisurl?>">
                            <button class="btn-copy" data-clipboard-text="<?php echo $thisurl?>"><i class="fa fa-copy"></i> 复制</button>
                        </div>
                        <?php } ?>
                    </div>
                    
                    <div class="tab-content-pane" id="tab-code">
                        <?php if($is_quota_exceeded){ echo '<div class="quota-error" style="padding: 20px;"><i class="fa fa-exclamation-triangle" style="font-size: 24px;"></i><p>'.$quota_owner['msg'].'，代码调用已失效。</p></div>'; }else{ ?>
                        <div class="link-input-group <?php echo $filetype<2?'hide':'';?>">
                            <label>播放器代码：</label>
                            <input type="text" id="code1" readonly value="<?php echo $htmlcode2?>">
                            <button class="btn-copy" data-clipboard-text="<?php echo $htmlcode2?>"><i class="fa fa-copy"></i> 复制</button>
                        </div>
                        <div class="link-input-group">
                            <label>HTML代码：</label>
                            <input type="text" id="code2" readonly value="<?php echo $htmlcode?>">
                            <button class="btn-copy" data-clipboard-text="<?php echo $htmlcode?>"><i class="fa fa-copy"></i> 复制</button>
                        </div>
                        <div class="link-input-group">
                            <label>UBB代码：</label>
                            <input type="text" id="code3" readonly value="<?php echo $ubbcode?>">
                            <button class="btn-copy" data-clipboard-text="<?php echo $ubbcode?>"><i class="fa fa-copy"></i> 复制</button>
                        </div>
                        <?php } ?>
                    </div>
                    
                    <div class="tab-content-pane" id="tab-info">
                        <div class="file-info-grid">
                            <div class="file-info-item">
                                <span class="label">上传者IP</span>
                                <span class="value"><?php echo preg_replace('/\d+$/','*',$row['ip'])?></span>
                            </div>
                            <div class="file-info-item">
                                <span class="label">上传时间</span>
                                <span class="value"><?php echo $row['addtime']?></span>
                            </div>
                            <div class="file-info-item">
                                <span class="label">下载次数</span>
                                <span class="value"><?php echo $row['count']?></span>
                            </div>
                            <div class="file-info-item">
                                <span class="label">文件大小</span>
                                <span class="value"><?php echo size_format($row['size']).' ('.$row['size'].' 字节)'?></span>
                            </div>
                        </div>
                    </div>
                    
                    <?php if($is_mine){?>
                    <div class="tab-content-pane" id="tab-manager">
                        <input type="hidden" id="hash" name="hash" value="<?php echo $hash?>">
                        <input type="hidden" id="csrf_token" name="csrf_token" value="<?php echo $csrf_token?>">
                        
                        <div class="manage-actions">
                            <div class="manage-action-item">
                                <div class="manage-action-info">
                                    <div class="manage-action-icon"><i class="fa fa-edit"></i></div>
                                    <div class="manage-action-text">
                                        <h4>重命名</h4>
                                        <p>修改文件名称</p>
                                    </div>
                                </div>
                                <button class="btn-manage-action" onclick="rename_file(<?php echo $row['id']?>, '<?php echo $row['name']?>')">操作</button>
                            </div>
                            
                            <div class="manage-action-item">
                                <div class="manage-action-info">
                                    <div class="manage-action-icon"><i class="fa fa-share"></i></div>
                                    <div class="manage-action-text">
                                        <h4>移动到</h4>
                                        <p>移动文件到其他文件夹</p>
                                    </div>
                                </div>
                                <button class="btn-manage-action" onclick="move_file(<?php echo $row['id']?>)">操作</button>
                            </div>
                            
                            <div class="manage-action-item">
                                <div class="manage-action-info">
                                    <div class="manage-action-icon"><i class="fa fa-copy"></i></div>
                                    <div class="manage-action-text">
                                        <h4>复制到</h4>
                                        <p>复制文件到其他文件夹</p>
                                    </div>
                                </div>
                                <button class="btn-manage-action" onclick="copy_file(<?php echo $row['id']?>)">操作</button>
                            </div>
                            
                            <div class="manage-action-item">
                                <div class="manage-action-info">
                                    <div class="manage-action-icon"><i class="fa <?php echo $row['hide']==1?'fa-eye':'fa-eye-slash'?>"></i></div>
                                    <div class="manage-action-text">
                                        <h4><?php echo $row['hide']==1?'取消隐藏':'隐藏文件'?></h4>
                                        <p>在首页公共列表中<?php echo $row['hide']==1?'显示':'隐藏'?>该文件</p>
                                    </div>
                                </div>
                                <button class="btn-manage-action" onclick="toggle_hide(<?php echo $row['id']?>)">操作</button>
                            </div>
                            
                            <div class="manage-action-item">
                                <div class="manage-action-info">
                                    <div class="manage-action-icon"><i class="fa fa-lock"></i></div>
                                    <div class="manage-action-text">
                                        <h4>密码保护</h4>
                                        <p>设置或修改访问密码</p>
                                    </div>
                                </div>
                                <button class="btn-manage-action" onclick="set_password(<?php echo $row['id']?>)">操作</button>
                            </div>

                            <div class="manage-action-item">
                                <div class="manage-action-info">
                                    <div class="manage-action-icon"><i class="fa fa-share-alt"></i></div>
                                    <div class="manage-action-text">
                                        <h4>分享设置</h4>
                                        <p>设置分享模式、密码和公告</p>
                                    </div>
                                </div>
                                <button class="btn-manage-action" onclick="set_file_share(<?php echo $row['id']?>)">操作</button>
                            </div>

                            <div class="manage-action-item">
                                <div class="manage-action-info">
                                    <div class="manage-action-icon danger"><i class="fa fa-trash"></i></div>
                                    <div class="manage-action-text">
                                        <h4 style="color: #dc2626;">删除文件</h4>
                                        <p>永久删除该文件</p>
                                    </div>
                                </div>
                                <button class="btn-manage-action btn-manage-danger" onclick="delete_confirm()">删除</button>
                            </div>
                        </div>
                    </div>
                    <?php }?>
                </div>
            </div>
        </div>
        
        <div class="col-sm-3">
            <div class="sidebar-card">
                <div class="sidebar-card-header">
                    <h3><i class="fa fa-exclamation-circle"></i> 温馨提示</h3>
                </div>
                <div class="sidebar-card-body">
                    <?php echo $conf['gg_file']?>
                </div>
            </div>
            
            <div class="sidebar-card hidden-xs">
                <div class="sidebar-card-header">
                    <h3><i class="fa fa-qrcode"></i> 手机扫码下载</h3>
                </div>
                <div class="sidebar-card-body">
                    <div class="qrcode-wrapper">
                        <div id="qrcode" style="width: 180px; height: 180px;"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
<?php include SYSTEM_ROOT.'footer.php';?>
<script src="assets/js/qrcode.min.js"></script>
<script>
if(typeof QRCode !== 'undefined' && document.getElementById('qrcode')){
  new QRCode(document.getElementById('qrcode'), {
    text: <?php echo json_encode($thisurl, JSON_UNESCAPED_SLASHES); ?>,
    width: 180,
    height: 180,
    correctLevel: QRCode.CorrectLevel.M
  });
}
</script>
<?php if($filetype==2){?>
<script type="text/javascript" src="https://s4.zstatic.net/ajax/libs/aplayer/1.10.1/APlayer.min.js"></script>
<script type="text/javascript">
var ap = new APlayer({
  container: document.getElementById('aplayer'),
  loop: 'none',
  theme: '#14b8a6',
  audio: [{
      title: '<?php echo $name?>',
      author: 'none',
      url: '<?php echo $viewurl_all?>',
      cover: './assets/img/music.png',
  }]
});
</script>
<?php }elseif($filetype==3 && $row['block']==0){?>
<script type="text/javascript" src="assets/js/ckplayer.min.js"></script>
<?php if($type=='m3u8'){$plug='hls.js';?><script src="https://s4.zstatic.net/ajax/libs/hls.js/1.2.4/hls.min.js"></script><?php }?>
<?php if($type=='flv'||$type=='f4v'){$plug='flv.js';?><script src="https://s4.zstatic.net/ajax/libs/flv.js/1.6.2/flv.min.js"></script><?php }?>
<script type="text/javascript">
  var videoObject = {
    container: '.videoplayer',
    plug:'<?php echo $plug?>',
    video:'<?php echo $viewurl_all?>',
    webFull:true,
  };
  var player=new ckplayer(videoObject);
</script>
<?php }?>
<!-- Folder Selector Template -->
<div id="folder-selector-tpl" style="display:none;">
    <div style="padding: 10px;">
        <div style="margin-bottom: 10px;">
            当前位置: <span id="fs-current-path">/</span>
            <button class="btn btn-xs btn-default pull-right" onclick="loadSelectorFolder(0)">根目录</button>
            <button class="btn btn-xs btn-default pull-right" onclick="loadSelectorFolder(fsCurrentParentId)" style="margin-right:5px;">上一级</button>
        </div>
        <div id="fs-tree" class="folder-tree-container"></div>
    </div>
</div>

<script src="https://s4.zstatic.net/ajax/libs/layer/2.3/layer.js"></script>
<script src="https://s4.zstatic.net/ajax/libs/clipboard.js/1.7.1/clipboard.min.js"></script>
<script src="assets/js/file-tabs.js"></script>
<script>

// Folder Selector Variables
var fsCurrentFolderId = 0;
var fsCurrentParentId = 0;
var fsSelectedFolderId = null;

function delete_confirm(){
  var hash = $("#hash").val();
  var csrf_token = $("#csrf_token").val();
  var confirmobj = layer.confirm('删除文件后不可恢复，确定删除吗？', {
	  btn: ['确定','取消'], icon: 0
	}, function(){
    var ii = layer.load(2);
	  $.ajax({
      type : 'POST',
      url : 'ajax.php?act=deleteFile',
      data : {hash:hash, csrf_token:csrf_token},
      dataType : 'json',
      success : function(data) {
        layer.close(ii);
        if(data.code == 0){
          layer.close(confirmobj);
          layer.alert('删除成功', {icon:1}, function(){window.location.href="./?m=mine";});
        }else{
          layer.close(confirmobj);
          layer.alert(data.msg, {icon:2});
        }
      },
      error:function(data){
        layer.close(ii);
        layer.msg('服务器错误');
      }
	  });
	}, function(){
	  layer.close(confirmobj);
	});
}

function rename_file(id, name) {
    layer.prompt({title: '重命名文件', value: name}, function(value, index){
        var loadIndex = layer.load(2);
        $.post('folder_api.php?act=rename_file', {file_id: id, new_name: value}, function(res){
            layer.close(loadIndex);
            if(res.code == 0) {
                layer.alert('重命名成功', {icon: 1}, function(i){
                    layer.close(i);
                    location.reload();
                });
            }
            else layer.alert(res.msg, {icon: 2});
            layer.close(index);
        }, 'json').fail(function(){
            layer.close(loadIndex);
            layer.alert('请求失败，请检查网络', {icon: 2});
        });
    });
}

function set_password(id) {
    var loadIndex = layer.load(2);
    $.post('folder_api.php?act=get_password', {file_id: id}, function(res){
        layer.close(loadIndex);
        var oldPwdVal = '';
        if(res.code == 0 && res.password) {
            oldPwdVal = res.password;
        }

        layer.open({
             type: 1,
             title: '设置/修改密码',
             content: '<div style="padding:20px"><input type="text" id="old_pwd" class="form-control" placeholder="原密码(若无留空)" value="'+oldPwdVal+'"><br><input type="password" id="new_pwd" class="form-control" placeholder="新密码(留空则清除)"></div>',
             btn: ['确定', '取消'],
             yes: function(index, layero){
                 var oldPwd = $('#old_pwd').val();
                 var newPwd = $('#new_pwd').val();
                 var saveLoadIndex = layer.load(2);
                 $.post('folder_api.php?act=set_password', {file_id: id, password: newPwd, old_password: oldPwd}, function(res){
                     layer.close(saveLoadIndex);
                     if(res.code == 0) { layer.alert('密码设置成功', {icon: 1}, function(i){ layer.close(i); layer.close(index); }); }
                     else layer.alert(res.msg, {icon: 2});
                 }, 'json').fail(function(){
                     layer.close(saveLoadIndex);
                     layer.alert('请求失败，请检查网络', {icon: 2});
                 });
             }
         });
    }, 'json').fail(function(){
        layer.close(loadIndex);
        layer.alert('请求失败，请检查网络', {icon: 2});
    });
}

function set_file_share(id) {
    var loadIndex = layer.load(2);
    $.post('folder_api.php?act=get_file_share_info', {file_id: id}, function(res){
        layer.close(loadIndex);
        if(res.code != 0) {
            layer.alert(res.msg, {icon: 2});
            return;
        }
        var data = res.data;
        var shareNotice = data.share_notice;

        var isMobile = window.innerWidth <= 768;
        var html = '<div style="padding:20px">';
        html += '<div class="form-group"><label>分享公告（支持HTML标签，留空则不显示）</label><textarea id="share_notice" class="form-control" rows="5" placeholder="可以输入文字、图片HTML等">' + shareNotice + '</textarea></div>';
        html += '</div>';

        layer.open({
            type: 1,
            title: '分享设置',
            area: isMobile ? ['90%', 'auto'] : ['500px', 'auto'],
            maxHeight: isMobile ? '85vh' : '80vh',
            content: html,
            btn: ['保存设置', '取消'],
            btnAlign: isMobile ? 'c' : 'r',
            yes: function(index, layero){
                var newShareNotice = $('#share_notice').val();
                var saveLoadIndex = layer.load(2);
                $.post('folder_api.php?act=set_file_share', {
                    file_id: id,
                    share_notice: newShareNotice
                }, function(res){
                    layer.close(saveLoadIndex);
                    if(res.code == 0) {
                        layer.alert('分享设置保存成功', {icon: 1}, function(i){
                            layer.close(i);
                            layer.close(index);
                            location.reload();
                        });
                    } else {
                        layer.alert(res.msg, {icon: 2});
                    }
                }, 'json').fail(function(){
                    layer.close(saveLoadIndex);
                    layer.alert('请求失败，请检查网络', {icon: 2});
                });
            }
        });
    }, 'json').fail(function(){
        layer.close(loadIndex);
        layer.alert('请求失败，请检查网络', {icon: 2});
    });
}

function toggle_hide(id) {
    var csrf_token = $("#csrf_token").val();
    var loadIndex = layer.load(2);
    $.post('ajax.php?act=toggle_hide', {id: id, csrf_token: csrf_token}, function(res){
        layer.close(loadIndex);
        if(res.code == 0) {
            layer.alert('操作成功', {icon: 1}, function(i){
                layer.close(i);
                location.reload();
            });
        } else {
            layer.alert(res.msg, {icon: 2});
        }
    }, 'json').fail(function(){
        layer.close(loadIndex);
        layer.alert('请求失败，请检查网络', {icon: 2});
    });
}

function move_file(id) {
    openFolderSelector(function(targetId){
         var loadIndex = layer.load(2);
         $.post('folder_api.php?act=move', {target_id: targetId, file_ids: [id]}, function(res){
             layer.close(loadIndex);
             if(res.code == 0) {
                 layer.alert('移动成功', {icon: 1}, function(i){
                     layer.close(i);
                     location.reload();
                 });
             }
             else layer.alert(res.msg, {icon: 2});
         }, 'json').fail(function(){
             layer.close(loadIndex);
             layer.alert('请求失败，请检查网络', {icon: 2});
         });
    });
}

function copy_file(id) {
    openFolderSelector(function(targetId){
         var loadIndex = layer.load(2);
         $.post('folder_api.php?act=batch_copy', {file_ids: [id], target_id: targetId}, function(res){
             layer.close(loadIndex);
             if(res.code == 0) { layer.alert('复制成功', {icon: 1}); }
             else layer.alert(res.msg, {icon: 2});
         }, 'json').fail(function(){
             layer.close(loadIndex);
             layer.alert('请求失败，请检查网络', {icon: 2});
         });
    });
}

// Folder Selector Logic (Duplicated from disk.php for simplicity, ideally shared js)
function openFolderSelector(callback) {
    fsSelectedFolderId = null;
    fsCurrentFolderId = 0;
    
    layer.open({
        type: 1,
        title: '选择目标文件夹',
        area: ['300px', '400px'],
        content: $('#folder-selector-tpl').html(),
        btn: ['确定', '取消'],
        success: function(layero, index){
            loadSelectorFolder(0);
        },
        yes: function(index, layero){
            if (fsSelectedFolderId === null) {
                layer.msg('请选择一个文件夹');
                return;
            }
            callback(fsSelectedFolderId);
            layer.close(index);
        }
    });
}

function loadSelectorFolder(folderId) {
    fsCurrentFolderId = folderId;
    var $container = $('.layui-layer-content');
    $container.find('#fs-tree').html('<div style="text-align:center;padding:10px;"><i class="fa fa-spinner fa-spin"></i> 加载中...</div>');
    
    $.get('folder_api.php', {act: 'list', parent_id: folderId}, function(res) {
        if(res.code == 0) {
            var pathStr = '/';
            if (res.path && res.path.length > 0) {
                pathStr = '';
                res.path.forEach(function(p){ pathStr += '/' + p.name; });
            }
            if (res.path && res.path.length > 0) {
                var current = res.path[res.path.length-1];
                fsCurrentParentId = current.parent_id;
            } else {
                fsCurrentParentId = 0;
            }
            
            $container.find('#fs-current-path').text(pathStr);
            $container.find('button[onclick*="loadSelectorFolder(fsCurrentParentId)"]').attr('onclick', 'loadSelectorFolder('+fsCurrentParentId+')');

            var html = '';
            html += '<div class="folder-tree-item" onclick="selectFsItem('+folderId+', this)">';
            html += '<i class="fa fa-folder-open"></i> . (当前文件夹)';
            html += '</div>';
            
            if(res.folders) {
                res.folders.forEach(function(f) {
                    html += '<div class="folder-tree-item" onclick="selectFsItem('+f.folder_id+', this)" ondblclick="loadSelectorFolder('+f.folder_id+')">';
                    html += '<i class="fa fa-folder"></i> '+f.name;
                    html += '<button class="btn btn-xs btn-default pull-right" onclick="event.stopPropagation();loadSelectorFolder('+f.folder_id+')">进入</button>';
                    html += '</div>';
                });
            }
            $container.find('#fs-tree').html(html);
        } else {
            $container.find('#fs-tree').html('<div class="text-danger">加载失败</div>');
        }
    }, 'json');
}

function selectFsItem(folderId, elem) {
    $('.folder-tree-item').removeClass('selected');
    $(elem).addClass('selected');
    fsSelectedFolderId = folderId;
}

$(document).ready(function(){
  var clipboard = new Clipboard('.btn-copy');
  clipboard.on('success', function (e) {
    layer.msg('复制成功！', {icon: 1});
  });
  clipboard.on('error', function (e) {
    layer.msg('复制失败，请长按链接后手动复制', {icon: 2});
  });
})
</script>
</body>
</html>
