

@page
@using Microsoft.AspNetCore.Http
@using JtkMainServer.Managers
@using Newtonsoft.Json.Linq
@*
    For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;

    var placeId = HttpContext.Request.Query["placeId"][0];
    var pcMapJson = DbManager.GetPlace(placeId).PcMap;

    // PC맵 번호 리스트
    var rows = new Dictionary<int, Dictionary<int, int>>();
    var maxRow = 0;
    var maxCol = 0;
    var pcMap = JObject.Parse(pcMapJson);
    foreach (var pair in pcMap) {
        var row = pair.Value["row"].ToObject<int>();
        var col = pair.Value["col"].ToObject<int>();
        var num = pair.Value["num"].ToObject<int>();
        maxRow = Math.Max(maxRow, row);
        maxCol = Math.Max(maxCol, col);
        if (rows.ContainsKey(row) == false) {
            rows[row] = new Dictionary<int, int>();
        }
        rows[row][col] = num;
    }

    // table list화
    var table = new List<List<int>>();
    //var totalPcCount = PlaceManager.GetTotalPcCount(placeId);
    var totalPcCount = 0;
    for (var r = 0; r <= maxRow; r++) {
        table.Add(new List<int>());
        if (rows.TryGetValue(r, out var cols)) {
            for (var c = 0; c <= maxCol; c++) {
                if (cols.TryGetValue(c, out var num)) {
                    table[r].Add(num);
                    totalPcCount++;
                } else {
                    table[r].Add(-1);
                }
            }
        } else {
            for (var c = 0; c <= maxCol; c++) {
                table[r].Add(-1);
            }
        }
    }
    var usingPcNumbers = PlaceManager.GetUsingPcNumbers(placeId);
    var notUsingPcCount = 0;
    if (usingPcNumbers != null) {
        notUsingPcCount = totalPcCount - usingPcNumbers.Length;
    }
}
@if (table != null) {
    @if (usingPcNumbers != null) {
        <p>총 @(totalPcCount)석 중 @(notUsingPcCount)석 이용 가능합니다.</p>
        <p><span style="color: darkseagreen">■</span> 빈 자리&nbsp;&nbsp;&nbsp;<span style="color: indianred">■</span> 사용 중</p>
        <div id="innerdiv" style="border-radius: 10px;  white-space: nowrap; box-sizing:content-box; overflow-x: auto; border:1px solid #CCC">
            <table style="width: @($"{(table.Count > 0 ? table[0].Count * (26 + 4) - 4 : 10)}px");">
                @foreach (var row in table) {
                    <tr>
                        @foreach (var num in row) {
                            @if (num < 0) {
                                <td style="background-color: transparent"></td>
                            } else {
                                @if (usingPcNumbers.Contains(num)) {
                                    <td style="background-color: indianred">@num</td>
                                } else {
                                    <td style="background-color: darkseagreen">@num</td>
                                }
                            }
                        }
                    </tr>
                }
            </table>
        </div>
    } else {

        <p>총 @(totalPcCount)석 중 0석 이용 가능합니다.</p>
        <p><span style="color: dimgray">■</span> 알 수 없음</p>
        <div style="border-radius: 10px;  white-space: nowrap; box-sizing:content-box; overflow-x: auto; border:1px solid #CCC">
            <table>
                @foreach (var row in table) {
                    <tr>
                        @foreach (var num in row) {
                            @if (num < 0) {
                                <td style="background-color: transparent"></td>
                            } else {
                                <td style="background-color: dimgray">@num</td>
                            }
                        }
                    </tr>
                }
            </table>
        </div>
    }
} else {
    <span>표시할 수 없습니다.</span>
}