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

    require_once('include/functions/comments.php');
    require_once('include/functions/community_thread.php');
    require_once('include/functions/quilt_information.php');
    require_once('include/functions/tile_is_available.php');
    
    $_id = isset($_GET['i']) ? intval($_GET['i']) : 0;
    
    $quilts = mysqli_query_logged("SELECT * FROM quilts WHERE id = " . sq($_id));
    if ($quilts_row = mysqli_fetch_array($quilts))
    {
        if ($quilts_row['finished'])
        {
            $GLOBALS['highlight'] = 'finished';
        }
    }
    else
    {
        header('Location: ./?s=quilts');
        die;
    }

    if (isset($_POST['quilt_zoom']) && $_POST['quilt_zoom'])
    {
        make_cookie('quilt_zoom', $_POST['quilt_zoom']);
        header('Location: ./?s=quilt&i=' . $quilts_row['id']);
        die;
    }
    
    include('include/parts/header.php');

    echo '<div class="header">';
    echo '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr>';
    echo '<td><a href="?s=' . ($quilts_row['finished'] ? 'finished' : 'quilts') . '">Quilts</a> - ' . $quilts_row['name'] . '</td>';
    echo '<td align="right">Full: [ <a href="?g=quilt_full&i=' . $quilts_row['id'] . '&format=jpg">JPG</a> ] [ <a href="?g=quilt_full&i=' . $quilts_row['id'] . '">PNG</a> ] ';
    echo 'Quilt Zoom: <form name="quilt_zoom" action="?s=quilt&i=' . $quilts_row['id'] . '" method="post" style="margin: 0px; display: inline;"><select name="quilt_zoom" style="font-size: 11px; border: 0px;" onChange="document.quilt_zoom.submit();">';
    echo '<option value="1.2"' . (isset($_COOKIE['quilt_zoom']) && $_COOKIE['quilt_zoom'] == 1.2 ? ' selected' : '') . '>120%</option>';
    echo '<option value="1.1"' . (isset($_COOKIE['quilt_zoom']) && $_COOKIE['quilt_zoom'] == 1.1 ? ' selected' : '') . '>110%</option>';
    echo '<option value="1"' . (!isset($_COOKIE['quilt_zoom']) || (isset($_COOKIE['quilt_zoom']) && $_COOKIE['quilt_zoom'] == 1) ? ' selected' : '') . '>100%</option>';
    echo '<option value="0.90"' . (isset($_COOKIE['quilt_zoom']) && $_COOKIE['quilt_zoom'] == 0.9 ? ' selected' : '') . '>90%</option>';
    echo '<option value="0.80"' . (isset($_COOKIE['quilt_zoom']) && $_COOKIE['quilt_zoom'] == 0.8 ? ' selected' : '') . '>80%</option>';
    echo '<option value="0.70"' . (isset($_COOKIE['quilt_zoom']) && $_COOKIE['quilt_zoom'] == 0.7 ? ' selected' : '') . '>70%</option>';
    echo '<option value="0.60"' . (isset($_COOKIE['quilt_zoom']) && $_COOKIE['quilt_zoom'] == 0.6 ? ' selected' : '') . '>60%</option>';
    echo '<option value="0.50"' . (isset($_COOKIE['quilt_zoom']) && $_COOKIE['quilt_zoom'] == 0.5 ? ' selected' : '') . '>50%</option>';
    echo '<option value="0.40"' . (isset($_COOKIE['quilt_zoom']) && $_COOKIE['quilt_zoom'] == 0.4 ? ' selected' : '') . '>40%</option>';
    echo '</select></form></td>';
    echo '</tr></table>';
    echo '</div>' . "\r\n";

    echo '<div class="content">';
    if ($_notice)
    {
        echo '<span style="color: red;">' . $_notice . '</span><br />';
        echo '<div style="padding: 5px 0px 0px 0px;"></div>';
    }
    quilt_information($quilts_row['id']);
    $distinct = mysqli_query_logged("SELECT DISTINCT(user_id) AS user_id FROM tiles WHERE quilt_id = '" . $quilts_row['id'] . "' AND deleted = '0'");
    if (mysqli_num_rows($distinct))
    {
        $array = array();
        echo '<div style="padding: 10px 0px 0px 0px;"></div>' . "\r\n";
        echo 'The following artists worked on this quilt: ';
        while ($distinct_row = mysqli_fetch_array($distinct))
        {
            $count = mysqli_fetch_assoc(mysqli_query_logged("SELECT COUNT(*) AS count FROM tiles WHERE quilt_id = '" . $quilts_row['id'] . "' AND user_id = '" . $distinct_row['user_id'] . "' AND deleted = '0'"));
            $username = get_username($distinct_row['user_id']);
            $array[$username] = substr('0000' . $count['count'], -4) . name_key(strtolower($username));
        }
        arsort($array);
        $i = 0;
        foreach ($array as $key => $value)
        {
            if ($i)
            {
                echo ', ';
            }
            echo '<a href="?s=profile&u=' . $key . '">' . $key . '</a> (' . intval(substr($value, 0, 4)) . ')';
            $i++;
        }
        echo '<br />';
    }
    
    function name_key($name)
    {
        $return = '';
        for ($i = 0; $i < strlen($name); $i++)
        {
            $return .= '-' . substr('000' . intval(255 - ord(substr($name, $i, 1))), -3);
        }
        return $return;
    }
    
    echo '<div style="padding: 10px 0px 0px 0px;"></div>' . "\r\n";
    echo '<span style="color: #bfdc92;">' . $quilts_row['description'] . '</span><br />' . "\r\n";
    echo '</div>';
    
    $tiles_pending = mysqli_query_logged("SELECT * FROM tiles_pending WHERE quilt_id = '" . $quilts_row['id'] . "' AND user_id = '" . $GLOBALS['auth']['id'] . "'");
    if ($tiles_pending_row = mysqli_fetch_array($tiles_pending))
    {
           echo '<div class="header">You are currently working on a tile in this project.</div>' . "\r\n";
        echo '<div class="content">';
        echo '<table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top">';
        echo '<img src="?g=tile_sides&i=' . $quilts_row['id'] . '&x=' . $tiles_pending_row['matrix_x'] . '&y=' . $tiles_pending_row['matrix_y'] . '&borders=' . $tiles_pending_row['borders'] . '" width="' . ($quilts_row['tile_width'] + ($quilts_row['side_pixels'] * 2)) . '" height="' . ($quilts_row['tile_height'] + ($quilts_row['side_pixels'] * 2)) . '" border="0"><br />';
        echo '<div style="padding: 5px 0px 0px 0px;"></div>';
        echo '<form action="?a=tile_cancel&i=' . $quilts_row['id'] . '&x=' . $tiles_pending_row['matrix_x'] . '&y=' . $tiles_pending_row['matrix_y'] . '" method="post" style="margin: 0px;">';
        echo '<input type="submit" value="Cancel Tile" style="border: solid 1px red; background: black; color: red;">';
        echo '</form>';
        echo '</td><td valign="top">';
        echo '<form action="?a=upload_tile&i=' . $quilts_row['id'] . '&x=' . $tiles_pending_row['matrix_x'] . '&y=' . $tiles_pending_row['matrix_y'] . '" method="post" enctype="multipart/form-data" style="margin: 0px;">';
        echo '<table border="0" cellpadding="2" cellspacing="0">';
        echo '<tr><td style="width: 150px; text-align: right;">Checkout Date:</td><td>' . nice_date($tiles_pending_row['started_on'], 'F j, Y @ g:ia') . '</td></tr>';
        echo '<tr><td style="width: 150px; text-align: right;">Due Date:</td><td>' . nice_date($tiles_pending_row['started_on'], 'F j, Y @ g:ia', $quilts_row['timelimit']) . '</td></tr>';
        echo '<tr><td style="width: 150px; text-align: right;">Checkin Tile:</td><td><input name="tile" type="file" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>';
        echo '<tr><td style="width: 150px; text-align: right;">Comment:</td><td><input name="comment" size="40" style="border: solid 2px black; margin: 0px; padding: 3px;"></td></tr>';
        echo '<tr><td style="width: 150px; text-align: right;"></td><td><input type="submit" value="Checkin This Tile" style="border: solid 1px green; background: black; color: green;"></td></tr>';
        echo '</table>';
        echo '</form>';
        echo '</td></tr></table>';
        echo '</div>';
    }
?>
</div>
</div>
</div>

<table border="0" cellpadding="0" cellspacing="0" align="center">
<?php
    for ($y = 1; $y <= $quilts_row['quilt_height']; $y++)
    {
        $zoom = isset($_COOKIE['quilt_zoom']) ? $_COOKIE['quilt_zoom'] : 1;
        echo '<tr>' . "\r\n";
        for ($x = 1; $x <= $quilts_row['quilt_width']; $x++)
        {
            list($available, $display, $output) = tile_is_available($quilts_row['id'], $x, $y, $zoom);
            echo $output . "\r\n";
        }
        echo '</tr>' . "\r\n";
    }
?>
</table>

<div class="major_fill">
<div class="minor_fill">
<div class="fixup_fill">
<?php
    community_thread('quilts');

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