<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
    <meta name="description" content=""/>
    <meta name="author" content=""/>
    <title>社区详情表</title>
    <link href="/static/css/styles.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js"
            crossorigin="anonymous"></script>
</head>
<style>
    .nav-item:hover .dropdown-menu {
        display: block;
    }
</style>
<body class="sb-nav-fixed">
<nav class="sb-topnav navbar navbar-expand navbar-dark bg-dark">
    <a class="navbar-brand ps-3" href="index.html">游戏评分系统</a>
    <!--<button class="btn btn-link btn-sm order-1 order-lg-0 me-4 me-lg-0" id="sidebarToggle" href="#!"><i
            class="fas fa-bars"></i></button>-->
    <form class="d-none d-md-inline-block form-inline ms-auto me-0 me-md-3 my-2 my-md-0">
    </form>
    <ul class="navbar-nav ms-auto ms-md-0 me-3 me-lg-4">
        <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" id="navbarDropdown" href="#" role="button" data-bs-toggle="dropdown"
               aria-expanded="false">
                <i class="fas fa-user fa-fw"></i>
                {{ email }}
            </a>
            <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                <li><a class="dropdown-item" href="/login">退出登录</a></li>
            </ul>
        </li>
    </ul>
</nav>

<div id="layoutSidenav">
    <div id="layoutSidenav_nav">
        <nav class="sb-sidenav accordion sb-sidenav-dark" id="sidenavAccordion">
            <div class="sb-sidenav-menu">
                <div class="nav">
                    <div class="sb-sidenav-menu-heading">首页</div>
                    <a class="nav-link active" href="/home">
                        <div class="sb-nav-link-icon">
                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-display" viewBox="0 0 16 16">
                              <path d="M0 4s0-2 2-2h12s2 0 2 2v6s0 2-2 2h-4c0 .667.083 1.167.25 1.5H11a.5.5 0 0 1 0 1H5a.5.5 0 0 1 0-1h.75c.167-.333.25-.833.25-1.5H2s-2 0-2-2V4zm1.398-.855a.758.758 0 0 0-.254.302A1.46 1.46 0 0 0 1 4.01V10c0 .325.078.502.145.602.07.105.17.188.302.254a1.464 1.464 0 0 0 .538.143L2.01 11H14c.325 0 .502-.078.602-.145a.758.758 0 0 0 .254-.302 1.464 1.464 0 0 0 .143-.538L15 9.99V4c0-.325-.078-.502-.145-.602a.757.757 0 0 0-.302-.254A1.46 1.46 0 0 0 13.99 3H2c-.325 0-.502.078-.602.145z"/>
                            </svg>
                        </div>
                        首页
                    </a>
                    <div class="sb-sidenav-menu-heading">分支列表</div>
                   <a class="nav-link " href="/community_info/{{ community_id }}">
                        <div class="sb-nav-link-icon">
                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pie-chart" viewBox="0 0 16 16">
                                <path d="M7.5 1.5A7.5 7.5 0 0 0 1 9.5a1 1 0 0 1 1-1H7a1 1 0 0 1 1-1H9a1 1 0 0 1 1 1H15a1 1 0 0 1 1 1A7.5 7.5 0 0 0 7.5 1.5zM3 9a1 1 0 0 1 1-1H6v1H3zm1 3.49V10h1.5a.5.5 0 0 1 0 1H4zM7 13h1.5a.5.5 0 0 1 0 1H7a1 1 0 0 1-1-1V9.5a.5.5 0 0 1 1 0V12zm6.5-2.5a.5.5 0 0 1 0 1H10a.5.5 0 0 1 0-1h4.5zm2-3A1.5 1.5 0 0 0 13 6.5V8H3V6.5a1.5 1.5 0 0 0-1.5-1.5h-1A1.5 1.5 0 0 0 0 6.5v1.5A1.5 1.5 0 0 0 1.5 9H13a1.5 1.5 0 0 0 1.5-1.5V6.5z"/>
                            </svg>
                        </div>
                        社区详情
                    </a>
                    <div class="collapse" id="collapsePages" aria-labelledby="headingTwo"
                         data-bs-parent="#sidenavAccordion">
                        <nav class="sb-sidenav-menu-nested nav accordion" id="sidenavAccordionPages">
                            <a class="nav-link collapsed" href="#" data-bs-toggle="collapse"
                               data-bs-target="#pagesCollapseAuth" aria-expanded="false"
                               aria-controls="pagesCollapseAuth">
                                Authentication
                                <div class="sb-sidenav-collapse-arrow"><i class="fas fa-angle-down"></i></div>
                            </a>
                            <div class="collapse" id="pagesCollapseAuth" aria-labelledby="headingOne"
                                 data-bs-parent="#sidenavAccordionPages">
                                <nav class="sb-sidenav-menu-nested nav">
                                    <a class="nav-link" href="login.html">Login</a>
                                    <a class="nav-link" href="register.html">Register</a>
                                    <a class="nav-link" href="password.html">Forgot Password</a>
                                </nav>
                            </div>
                            <a class="nav-link collapsed" href="#" data-bs-toggle="collapse"
                               data-bs-target="#pagesCollapseError" aria-expanded="false"
                               aria-controls="pagesCollapseError">
                                Error
                                <div class="sb-sidenav-collapse-arrow"><i class="fas fa-angle-down"></i></div>
                            </a>
                            <div class="collapse" id="pagesCollapseError" aria-labelledby="headingOne"
                                 data-bs-parent="#sidenavAccordionPages">
                                <nav class="sb-sidenav-menu-nested nav">
                                    <a class="nav-link" href="401.html">401 Page</a>
                                    <a class="nav-link" href="404.html">404 Page</a>
                                    <a class="nav-link" href="500.html">500 Page</a>
                                </nav>
                            </div>
                        </nav>
                    </div>
                </div>
            </div>
            <div class="sb-sidenav-footer">
                Game Ratings
            </div>
        </nav>
    </div>



    <div id="layoutSidenav_content">
        <main>
            <div class="container-fluid px-4">
                <h5 class="mt-2">社区详情</h5>
                <ol class="breadcrumb mb-2">
                    <li class="breadcrumb-item"><a href="index.html"></a></li>
                </ol>
                <div class="card mb-4">
                    <div class="card-body">
                        <h3>社区名称：{{ community_name | tojson }}</h3>
                        <p>社区描述：{{ community_description | tojson }}</p>
                    </div>
                    <button onclick="joinCommunity({{ user_id }}, {{ community_id }})">加入社区</button>
                </div>

                <div id="gameRatingsChart" style="width: 100%; height: 400px;"></div>
                
                <div class="card mb-4">
                    <div class="card-header">
                        <i class="fas fa-users me-1"></i>
                        社区成员
                    </div>
                    <div class="card-body">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>UserID</th>
                                    <th>UserName</th>
                                    <th>评论数</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for member in members_list %}
                                <tr>
                                    <td>{{ member.userID }}</td>
                                    <td>{{ member.UserName }}</td>
                                    <td>{{ member.commentCount }}</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>

                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>游戏名</th>
                            <th>评论</th>
                            <th>评分</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for comment in comments %}
                        <tr>
                            <td>{{ comment[0] }}</td>
                            <td>{{ comment[1] }}</td>
                            <td>{{ comment[2] }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>

            </div>
        </main>
        <footer class="py-4 bg-light mt-auto">
            <div class="container-fluid px-4">
                <div class="d-flex align-items-center justify-content-between small">
                    <div class="text-muted">Copyright &copy; 游戏评分管理平台 2024</div>
                </div>
            </div>
        </footer>
    </div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
        crossorigin="anonymous"></script>
<script src="static/js/scripts.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js" crossorigin="anonymous"></script>
<script src="/static/lib/echarts.js"></script>
<script src="static/assets/demo/chart-area-demo.js"></script>
<script src="static/assets/demo/chart-bar-demo.js"></script>
<script src="static/assets/demo/chart-pie-demo.js"></script>

<script>
    function joinCommunity(userId, communityId) {
        fetch(`/community_info/${communityId}`, {
            method: 'POST'
        })
        .then(response => {
            if (response.ok) {
                return response.json();
            } else {
                response.json().then(data => {
                    alert(data.error); // 显示后端返回的错误消息
                });
                return null;
            }
        })
        .then(data => {
            if (data) {
                if (data.success) {
                    alert(data.message); // 显示成功加入社区的消息
                } else {
                    alert(data.error); // 显示其他错误消息
                }
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('加入社区时发生错误。'); // 显示通用错误消息
        });
    }
</script>

<script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
<script>
    var chartDom = document.getElementById('gameRatingsChart');
    var myChart = echarts.init(chartDom);
    var option;

    var gameRatingsData = {{game_ratings_list | tojson}};

    option = {
        title: {
            text: '社区喜好游戏',
            left: 'center'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        xAxis: {
            type: 'category',
            data: gameRatingsData.map(item => item.gameName)
        },
        yAxis: {
            type: 'value'
        },
        series: [{
            data: gameRatingsData.map(item => item.averageScore),
            type: 'bar',
            showBackground: true,
            backgroundStyle: {
                color: 'rgba(180, 180, 180, 0.2)'
            }
        }]
    };

    myChart.setOption(option);
</script>
<script>
    async function fetchGameRatings(communityId) {
        const response = await fetch(`/community_game_ratings/${communityId}`);
        const data = await response.json();
        
        // 更新图表数据
        const gameRatingsData = data.map(item => ({
            gameName: item.gameName,
            averageScore: item.averageScore
        }));
        
        myChart.setOption({
            xAxis: {
                data: gameRatingsData.map(item => item.gameName)
            },
            series: [{
                data: gameRatingsData.map(item => item.averageScore)
            }]
        });
    }

    // 在页面加载完成后获取社区游戏评分数据
    document.addEventListener('DOMContentLoaded', function() {
        const communityId = 1; // 假设社区ID为1
        fetchGameRatings(communityId);
    });
</script>
<script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const communityId = 1; // 假设社区ID为1

        // 获取社区成员数据
        async function fetchCommunityMembers(communityId) {
            const response = await fetch(`/community_members/${communityId}`);
            const data = await response.json();
            members = data.members_list;
            // 渲染成员数据到表格
            const membersTable = document.getElementById('membersTableBody');
            membersTable.innerHTML = ''; // 清空表格
            members.forEach(member => {
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td>${member.userID}</td>
                    <td>${member.UserName}</td>
                    <td>${member.commentCount}</td>
                `;
                membersTable.appendChild(row);
            });
        }

        // 在页面加载完成后获取社区成员数据
        fetchCommunityMembers(communityId);
    });
</script>
</body>
</html>
