<?php
    require_once('include/functions/pages.php');
    
    $tiles_pending = 0;
    $tiles_none = 0;
    $tiles = mysqli_query_logged("SELECT * FROM tiles WHERE user_id = '" . $_id . "' AND deleted = '0' AND cache_display = '1'");
    if (!$tiles_count = mysqli_num_rows($tiles))
    {
        $tiles = mysqli_query_logged("SELECT * FROM tiles WHERE user_id = '" . $_id . "' AND deleted = '0' AND cache_display = '0'");
        if (mysqli_num_rows($tiles))
        {
            $tiles_pending = 1;
        }
        else
        {
            $tiles_none = 1;
        }    
    }
    
    echo '<div class="header">Tiles Completed<br /><span style="color: #cccccc; font-size: 10px;">Page: ' . pages_ajax('javascript:tiles_completed(%%PAGE%%);', $_page, ceil($tiles_count / 25)) . '</span></div>';

    echo '<div class="content" style="padding: 0px 5px 5px 5px;">';
    if ($tiles_none)
    {
        echo '<div style="padding: 5px 0px 0px 0px;"></div>This member has not completed any tiles...<br />';
    }
    elseif ($tiles_pending)
    {
        echo '<div style="padding: 5px 0px 0px 0px;"></div>This member has completed some tiles but none of them are set to display...<br />';
    }
    else
    {
        echo '<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td>';
        $tiles = mysqli_query_logged("SELECT * FROM tiles WHERE user_id = '" . $_id . "' AND deleted = '0' AND cache_display = '1' ORDER BY tile_id DESC LIMIT " . (($_page - 1) * 25) . ", 25");
        while ($tiles_row = mysqli_fetch_assoc($tiles))
        {
            $quilts = mysqli_query_logged("SELECT * FROM quilts WHERE id = '" . $tiles_row['quilt_id'] . "'");
            if ($quilts_row = mysqli_fetch_assoc($quilts))
            {
                if ($quilts_row['tile_width'] > 100)
                {
                    $new_width = 100;
                    $new_height = $quilts_row['tile_height'] / ($quilts_row['tile_width'] / 100);
                    if ($new_height > 100)
                    {
                        $new_width = $quilts_row['tile_width'] / ($quilts_row['tile_height'] / 100);
                        $new_height = 100;
                    }
                }
                elseif ($quilts_row['tile_height'] > 100)
                {
                    $new_height = 100;
                    $new_width = $quilts_row['tile_width'] / ($quilts_row['tile_height'] / 100);
                    if ($new_height > 100)
                    {
                        $new_height = $quilts_row['tile_height'] / ($quilts_row['tile_width'] / 100);
                        $new_width = 100;
                    }
                }
                else
                {
                    $new_width = $quilts_row['tile_width'];
                    $new_height = $quilts_row['tile_height'];
                }
                echo box_image_top();
                echo '<table width="100" height="100" cellpadding="0" cellspacing="0" border="0"><tr><td valign="top">';
                echo '<a href="?s=tile&i=' . $tiles_row['tile_id'] . '"><img src="?g=tile&i=' . $tiles_row['tile_id'] . '" width="' . $new_width . '" height="' . $new_height . '" title="' . htmlspecialchars(get_username($tiles_row['user_id'])) . ': ' . htmlspecialchars($tiles_row['comment']) . '" alt="" style="border: solid 0px black;"></a><br />';
                echo '</td></tr></table>';
                echo box_image_bottom();
            }
        }
        echo '</td></tr></table>';
    }
    echo '</div>';
?>