<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>记录查询</title>
    <link href="{{ asset('css/bootstrap.min.css') }}" rel="stylesheet">
    <script src="{{ asset('js/bootstrap.min.js') }}"></script>
</head>
<body>
    <div class="container mt-5">
        <h2 class="text-center mb-4">记录查询</h2>

        @if(session('error'))
            <div class="alert alert-danger">
                {{ session('error') }}
            </div>
        @endif

        <form action="{{ route('record.query') }}" method="POST" class="mb-5">
            @csrf
            <div class="row g-3">
                <div class="col-md-3">
                    <label for="start_date" class="form-label">开始日期</label>
                    <input type="date" class="form-control" id="start_date" name="start_date" value="{{ $startDate ?? '' }}" required>
                </div>
                <div class="col-md-3">
                    <label for="end_date" class="form-label">结束日期</label>
                    <input type="date" class="form-control" id="end_date" name="end_date" value="{{ $endDate ?? '' }}" required>
                </div>
                <div class="col-md-3">
                    <label for="record_type" class="form-label">记录类型</label>
                    <select class="form-select" id="record_type" name="record_type" required>
                        <option value="" {{ empty($recordType ?? '') ? 'selected' : '' }}>请选择类型</option>
                        <option value="1" {{ ($recordType ?? '') == 1 ? 'selected' : '' }}>预约</option>
                        <option value="2" {{ ($recordType ?? '') == 2 ? 'selected' : '' }}>登记</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label for="exam_type" class="form-label">体检类型</label>
                    <select class="form-select" id="exam_type" name="exam_type">
                        <option value="" {{ empty($examType ?? '') ? 'selected' : '' }}>全部</option>
                        <option value="1" {{ ($examType ?? '') == 1 ? 'selected' : '' }}>健康证</option>
                        <option value="2" {{ ($examType ?? '') == 2 ? 'selected' : '' }}>老年人段</option>
                    </select>
                </div>
            </div>
            <div class="mt-4 text-center">
                <button type="submit" class="btn btn-primary btn-lg">查询</button>
            </div>
        </form>

        @if(isset($records))
            <div class="mt-5">
                <h3>查询结果</h3>
                <p>共 {{ $records->total() }} 条记录</p>

                <form action="{{ route('record.export') }}" method="POST" style="display: inline;">
                    @csrf
                    <input type="hidden" name="start_date" value="{{ $startDate }}">
                    <input type="hidden" name="end_date" value="{{ $endDate }}">
                    <input type="hidden" name="record_type" value="{{ $recordType }}">
                    <input type="hidden" name="exam_type" value="{{ $examType }}">
                    <button type="submit" class="btn btn-success mb-3">导出CSV</button>
                </form>

                <div class="table-responsive">
                    <table class="table table-bordered table-hover">
                        <thead class="table-dark">
                            <tr>
                                <th>姓名</th>
                                <th>身份证号</th>
                                <th>体检日期</th>
                                <th>机构名称</th>
                                @if($recordType == 2)
                                <th>行业类型</th>
                                @endif
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($records as $record)
                                <tr>
                                    <td>{{ $record->name ?? '' }}</td>
                                    <td>{{ $record->id_card_num ?? '' }}</td>
                                    <td>{{ $record->created_at ?? '' }}</td>
                                    <td>{{ $record->org_name ?? '' }}</td>
                                    @if($recordType == 2)
                                    <td>{{ $record->industry_type ?? '' }}</td>
                                    @endif
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>

                <div class="d-flex justify-content-center">
                    {{ $records->links() }}
                </div>
            </div>
        @endif
    </div>
</body>
</html>
