<?php
    $GLOBALS['highlight'] = 'finished';

    require_once('include/functions/pages.php');
    require_once('include/functions/quilt_information.php');
    require_once('include/functions/tile_is_available.php');
    
    $_page = isset($_GET['p']) ? intval($_GET['p']) : 1;

    include('include/parts/header.php');
    
    $entries = mysqli_fetch_array(mysqli_query_logged("SELECT COUNT(*) AS num FROM quilts WHERE finished = '1'"));
    $page_count = ceil($entries['num'] / 10);
    $pages = '<span class="pages">Page: ' . pages('?s=finished', $_page, $page_count) . '</span><br />';
?>

<div class="header">
<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td valign="top">
Quilts<?php echo $entries['num'] ? '<br />' . $pages : '' ?>
<?php
    if ($GLOBALS['auth']['create_quilt'])
    {
        echo '</td><td align="right" valign="top" style="color: #cccccc; font-weight: normal;">[ <a href="?s=quilt_create">Create Quilt</a> ]';
    }
?>
</td></tr></table>
</div>
<?php
    $quilts = mysqli_query_logged("SELECT * FROM quilts WHERE finished = '1' ORDER BY modified_on DESC, id DESC LIMIT " . (($_page - 1) * 10) . ', ' . 10);
    if (mysqli_num_rows($quilts))
    {
        $i = 0;
        echo '<div class="content">' . "\r\n";
        while ($quilts_row = mysqli_fetch_array($quilts))
        {
            if ($i++)
            {
                echo '<div style="padding: 5px 0px 0px 0px;"></div>';
            }
            echo '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="250" valign="top" nowrap>';
            echo box_image_top('margin: 0px 5px 0px 0px; width: 250px;');
            echo '<a href="?s=quilt&i=' . $quilts_row['id'] . '"><img src="?g=quilt_thumb&i=' . $quilts_row['id'] . '" style="border: 0px;"></a><br />';
            echo box_image_bottom();
            echo '</td><td valign="top" class="inside" style="padding-left: 10px; font-size: 12px;">';
            echo '<a href="?s=quilt&i=' . $quilts_row['id'] . '" style="font-weight: bold;">' . $quilts_row['name'] . '</a><br />' . "\r\n";
            quilt_information($quilts_row['id']);
            /*
            $i = 0;
            $tiles = mysqli_query_logged("SELECT * FROM tiles WHERE quilt_id = '" . $quilts_row['id'] . "' AND deleted = '0' ORDER BY RAND()");
            if ($tiles_row = mysqli_num_rows($tiles))
            {
                while ($i < 10 && $tiles_row = mysqli_fetch_array($tiles))
                {
                    list($available, $display, $output) = tile_is_available($tiles_row['quilt_id'], $tiles_row['matrix_x'], $tiles_row['matrix_y']);
                    if ($display)
                    {
                        if (!$i)
                        {
                            echo '<div style="padding: 5px 0px 0px 0px;"></div>';
                        }
                        echo '<a href="?s=tile&i=' . $tiles_row['tile_id'] . '"><img src="?g=tile&i=' . $tiles_row['tile_id'] . '" width="50" height="50" border="0" title="' . htmlspecialchars(get_username($tiles_row['user_id'])) . ': ' . htmlspecialchars($tiles_row['comment']) . '" alt="" style="border: 0px;"></a>' . "\r\n";
                        $i++;
                    }
                }
            }
            */
            echo '</td></tr></table>' . "\r\n";
        }
        echo '</div>' . "\r\n";
    }
    else
    {
        echo '<div class="content">Sorry there are no quilts on the site.</div>';
    }
    if ($entries['num'])
    {
        echo '<div class="header">' . $pages . '</div>';
    }

    include('include/parts/footer.php');
?>