{% extends "layout.html" %}

{% block title %}成员列表 - 家谱管理系统{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-people"></i> 家谱成员管理</h2>
    <div class="d-flex gap-2">
        <form class="d-flex" action="{{ url_for('members') }}" method="GET">
            <div class="input-group">
                <input type="text" name="name" class="form-control" placeholder="输入姓名搜索..." value="{{ search_name or '' }}">
                <button class="btn btn-outline-primary" type="submit"><i class="bi bi-search"></i></button>
                {% if search_name %}
                    <a href="{{ url_for('members') }}" class="btn btn-outline-secondary">重置</a>
                {% endif %}
            </div>
        </form>
        <a href="{{ url_for('add_member') }}" class="btn btn-primary text-nowrap"><i class="bi bi-plus-lg"></i> 录入新成员</a>
    </div>
</div>

<div class="card shadow-sm border-0">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th class="px-4">序号</th>
                        <th>基本信息</th>
                        <th>分房 / 堂号</th>
                        <th>居住地</th>
                        <th>状态</th>
                        <th class="text-center">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for member in members %}
                    <tr>
                        <td class="px-4 text-muted small">{{ loop.index + (page - 1) * 20 }}</td>
                        <td>
                            <div class="fw-bold">
                                {{ member.name }}{% if member.simplified_name %}（{{ member.simplified_name }}）{% endif %}
                            </div>
                            <div class="small text-muted">
                                {{ '男' if member.sex == 1 else '女' }} | {{ member.birthday_str or '未知' }}
                            </div>
                        </td>
                        <td>
                            <div class="small">
                                {{ member.family_branch or member.hall_name or '-' }}
                            </div>
                        </td>
                        <td>
                            <div class="small text-truncate" style="max-width: 150px;">
                                {{ member.residential_address or '-' }}
                            </div>
                        </td>
                        <td>
                            {% if member.is_pass_away == 1 %}
                                <span class="badge bg-secondary">已故</span>
                            {% else %}
                                <span class="badge bg-success">健在</span>
                            {% endif %}
                            {% if member.modified_time_str %}
                                <div class="text-muted" style="font-size: 0.75rem;">修改: {{ member.modified_time_str }}</div>
                            {% else %}
                                <div class="text-muted" style="font-size: 0.75rem;">录入: {{ member.create_time_str or '-' }}</div>
                            {% endif %}
                        </td>
                        <td class="text-center">
                            <div class="btn-group btn-group-sm">
                                <a href="{{ url_for('member_detail', member_id=member.id) }}" class="btn btn-outline-info">
                                    <i class="bi bi-eye"></i> 详情
                                </a>
                                <a href="{{ url_for('edit_member', member_id=member.id) }}" class="btn btn-outline-primary">
                                    <i class="bi bi-pencil"></i> 编辑
                                </a>
                                <button type="button" class="btn btn-outline-danger" onclick="confirmDelete({{ member.id }}, '{{ member.name }}')">
                                    <i class="bi bi-trash"></i> 删除
                                </button>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                    {% if not members %}
                    <tr>
                        <td colspan="6" class="text-center py-5 text-muted">
                            <i class="bi bi-person-x fs-1 d-block mb-2"></i>
                            暂无成员数据
                        </td>
                    </tr>
                    {% endif %}
                </tbody>
            </table>
        </div>
    </div>
    
    <!-- 分页控件 -->
    {% if total_pages > 1 %}
    <nav class="mt-4">
        <ul class="pagination justify-content-center">
            <li class="page-item {{ 'disabled' if page == 1 }}">
                <a class="page-link" href="{{ url_for('members', page=page-1, name=search_name) }}">上一页</a>
            </li>
            
            {% for p in range(1, total_pages + 1) %}
                {% if p == page %}
                    <li class="page-item active"><span class="page-link">{{ p }}</span></li>
                {% else %}
                    {# Show first, last, and around current page #}
                    {% if p == 1 or p == total_pages or (p >= page - 2 and p <= page + 2) %}
                        <li class="page-item"><a class="page-link" href="{{ url_for('members', page=p, name=search_name) }}">{{ p }}</a></li>
                    {% elif p == page - 3 or p == page + 3 %}
                        <li class="page-item disabled"><span class="page-link">...</span></li>
                    {% endif %}
                {% endif %}
            {% endfor %}
            
            <li class="page-item {{ 'disabled' if page == total_pages }}">
                <a class="page-link" href="{{ url_for('members', page=page+1, name=search_name) }}">下一页</a>
            </li>
        </ul>
        <div class="text-center text-muted small mt-2">
            共 {{ total }} 条记录，当前第 {{ page }} / {{ total_pages }} 页
        </div>
    </nav>
    {% endif %}
</div>
{% endblock %}

{% block extra_js %}
<script>
function confirmDelete(id, name) {
    if (confirm(`确定要删除成员 "${name}" 吗？此操作将同时删除与其相关的所有家谱关系，且不可恢复。`)) {
        const form = document.createElement('form');
        form.method = 'POST';
        form.action = `/manager/delete_member/${id}`;
        document.body.appendChild(form);
        form.submit();
    }
}
</script>
{% endblock %}
