<?php

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if($page < 1) {
    $page = 1;
}
$fid = 54;
$limit = 20; // 每页显示条数
$offset = ($page - 1) * $limit;
// $total = DB::result_first('SELECT COUNT(*) FROM '.DB::table('common_block_item')." WHERE bid = ".ODZ_NEWS_BLOCK_ID);
$total = DB::result_first('SELECT COUNT(*) FROM '.DB::table('forum_thread')." WHERE fid = $fid AND displayorder = 0");
// 获取帖子摘要
$post = array();
$query = DB::query('SELECT tid, message FROM '.DB::table('forum_post').' WHERE fid = '.$fid.' AND first = 1');
while ($row = DB::fetch($query)) {
    $pattern = '/\[.*?\]/';
    $message = preg_replace($pattern, '', $row['message']);
//    $post[$row['tid']]['message'] = cutstr($message, 60);
    $post[$row['tid']]['message'] = $message;
}

// $sql = 'SELECT id, title, pic, picflag, makethumb, thumbpath, summary, fields FROM '.DB::table('common_block_item')." WHERE bid = ".ODZ_NEWS_BLOCK_ID." ORDER BY itemid DESC LIMIT $offset, $limit";
$sql = 'SELECT tid, subject, dateline, replies FROM '.DB::table('forum_thread')." WHERE fid = $fid AND displayorder = 0 ORDER BY displayorder DESC, tid DESC LIMIT $offset, $limit";
$query = DB::query($sql);
$list = array();
while ($row = DB::fetch($query)) {
    $summary = isset($post[$row['tid']]['message']) ? $post[$row['tid']]['message'] : '';

    $list[$row['tid']]['id'] = $row['tid'];
    $list[$row['tid']]['title'] = $row['subject'];
    $list[$row['tid']]['dateline'] = (int)$row['dateline'];
    $list[$row['tid']]['replies'] = (string)$row['replies'];
//    $list[$row['tid']]['summary'] = isset($post[$row['tid']]['message']) ? $post[$row['tid']]['message'] : '';

    $sortlist = array();
    $attachments = C::t('forum_attachment_n')->fetch_all_by_id('tid:'.$row['tid'], 'tid', $row['tid']);
    $thumburl = array();
    foreach($attachments as $aid => $attach) {
        if ($attach['isimage'] != 0) {
            $thumb = ($attach['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']) . 'forum/' . $attach['attachment'] . ($attach['thumb'] ? '.thumb.jpg' : '');
            $thumburl[] = $_G['baseurl'].$thumb; 
            break;
        }
    }
    $list[$row['tid']]['thumb'] = $thumburl;

    $list[$row['tid']]['summary'] = cutstr($summary, (count($thumburl) ? 45: 75));
    // $row['fields'] = unserialize($row['fields']);
    // $arr = array(
    //     'id' => $row['id'],
    //     'title' => $row['title'],
    //     'replies' => (string)$row['fields']['replies'],
    //     'dateline' => $row['fields']['dateline']
    // );
    // // 强制要求生成缩略图
    // if($row['makethumb']) {
    //     $thumburl = array(($row['picflag'] == 1 ? $_G['baseurl'].$_G['setting']['attachurl'] : $_G['setting']['ftp']['attachurl']).$row['thumbpath']);
    // } else {
    //     $thumburl = array();
    // }
    // $arr['thumb'] = $thumburl;
    // // 截取简介
    // $arr['summary'] = str_replace("\r\n", '', $row['summary']);
    // $arr['summary'] = str_replace(" ", '', $arr['summary']);
    // $arr['summary'] = cutstr($arr['summary'], (count($thumburl) ? 45 : 75));
    // $list[] = $arr;
}

$result = array(
    'total' => (int)$total,
    'perpage' => $limit,
    'page' => $page,
    'list' => array_values($list)
);

odz_result($result);

?>