@extends('admin.layouts.app')

@section('content')
<div class="container-fluid">
    <h2>첨부파일(Upload) 관리</h2>
    <hr/>
    @include('admin.messages')
    @include('admin.board.file.navTabs')

    <form name="f_admin_search" id="fAdminSearch" method="get" action="{{ route('admin.board.file.upload.index') }}">
        <input type="hidden" name="board_id" value="{{ $params->boardID }}" />
        <table class="table table-bordered">
            <colgroup>
                <col width="15%" />
                <col width="*" />
            </colgroup>
            <tbody>
                <tr>
                    <th>검색</th>
                    <td>
                        <div class="row g-2">
                            <div class="col-auto">
                                <select name="field" id="field" class="form-select">
                                    <option value="tb_post_file.id" @if($params->field == 'tb_post_file.id') selected @endif>PK</option>
                                    <option value="tb_post_file.origin_name" @if($params->field == 'tb_post_file.origin_name') selected @endif>파일명</option>
                                    <option value="tb_post_file.ip_address" @if($params->field == 'tb_post_file.ip_address') selected @endif>IP</option>
                                    <option value="tb_post.id" @if($params->field == 'tb_post.id') selected @endif>게시글 PK</option>
                                    <option value="tb_post.subject" @if($params->field == 'tb_post.subject') selected @endif>게시글 제목</option>
                                    <option value="users.id" @if($params->field == 'users.id') selected @endif>작성자 회원 PK</option>
                                    <option value="users.sid" @if($params->field == 'users.sid') selected @endif>작성자 회원 ID</option>
                                    <option value="users.name" @if($params->field == 'users.name') selected @endif>작성자 이름</option>
                                    <option value="users.email" @if($params->field == 'users.email') selected @endif>작성자 이메일</option>
                                </select>
                            </div>
                            <div class="col">
                                <input type="text" name="keyword" id="keyword" class="form-control" value="{{ $params->keyword }}" placeholder="검색어를 입력하세요." />
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th>등록일시</th>
                    <td>
                        <div class="row g-2">
                            <div class="col-auto">
                                <input type="text" name="start_date" id="startDate" class="form-control date-picker s-start-date" value="{{ $params->startDate }}" />
                            </div>
                            <div class="col-auto align-self-center">
                                ~
                            </div>
                            <div class="col-auto">
                                <input type="text" name="end_date" id="endDate" class="form-control date-picker s-end-date" value="{{ $params->endDate }}" />
                            </div>
                            <div class="col-sm text-start">
                                <div class="btn-group">
                                    <button type="button" onclick="setDateRange();" class="btn btn-secondary">오늘</button>
                                    <button type="button" onclick="setDateRange('1d');" class="btn btn-secondary">어제</button>
                                    <button type="button" onclick="setDateRange('7d');" class="btn btn-secondary">1주일</button>
                                    <button type="button" onclick="setDateRange('15d');" class="btn btn-secondary">15일</button>
                                    <button type="button" onclick="setDateRange('1m');" class="btn btn-secondary">1개월</button>
                                    <button type="button" onclick="setDateRange('2m');" class="btn btn-secondary">2개월</button>
                                    <button type="button" onclick="setDateRange('3m');" class="btn btn-secondary">3개월</button>
                                </div>
                            </div>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>

        <div class="text-center">
            <button type="submit" class="btn btn-dark">검색</button>
        </div>
    </form>
    <hr/>

    <div class="row">
        <div class="col">
            <select id="boardID" class="form-select w-auto">
                <option value="">모든 게시판</option>
                @foreach($boardData as $board)
                    <option value="{{ $board->id }}" @if($board->id == $params->boardID) selected @endif>{{ $board->name }}</option>
                @endforeach
            </select>
        </div>
        <div class="col text-end">
            <button type="button" class="btn btn-outline-dark btn-list-delete btn-list-selected disabled" data-list-delete-url="/admin/board/file/upload/destroy">선택삭제</button>
        </div>
    </div>
    <br/>

    <div class="row mb-2">
        <div class="col align-self-center">
            전체 : {{ $postFileData->total }}
        </div>
    </div>

    <form name="f_admin_list" id="fAdminList" method="post">
        @csrf
        <div class="table-responsive">
            <table class="table table-hover table-bordered table-nowrap">
                <thead>
                    <tr>
                        <th><input type="checkbox" name="chk_all" id="chkAll" class="form-check-input" value="1"/></th>
                        <th>PK</th>
                        <th>게시판</th>
                        <th>게시글</th>
                        <th>파일명</th>
                        <th>Size</th>
                        <th>Download</th>
                        <th>IP</th>
                        <th>첨부자</th>
                        <th>등록일시</th>
                        <th>비고</th>
                    </tr>
                </thead>
                <tbody>
                    @if($postFileData->rows > 0)
                        @foreach($postFileData->list as $row)
                        <tr>
                            <td>
                                <input type="checkbox" name="chk[]" id="chk_{{ $row->id }}" class="form-check-input list-check-box" value="{{ $row->id }}" />
                            </td>
                            <td>{{ $row->id }}</td>
                            <td>{{ $row->boardName }}</td>
                            <td>
                                <a href="{{ $row->postURL }}" target="_blank">
                                    {{ $row->subject }} <i class="fa-solid fa-arrow-up-right-from-square ms-1"></i>
                                </a>
                            </td>
                            <td>
                                @if($row->is_image)
                                    <i class="far fa-image"></i>
                                @else
                                    <i class="fas fa-file"></i>
                                @endif
                                @if($row->fileExists)
                                    <a href="{{ $row->fileLink }}" download>{{ $row->origin_name }}</a>
                                @else
                                    {{ $row->origin_name }}
                                @endif
                            </td>
                            <td>{{ $row->fileSize }}</td>
                            <td>{{ $row->download }}</td>
                            <td>{{ $row->ip_address }}</td>
                            <td>{{ $row->name . ' (' . $row->sid . ')' }}</td>
                            <td>{{ $row->created_at }}</td>
                            <td>
                                <a href="{{ $row->editURL }}" class="btn btn-sm btn-primary">보기</a>
                            </td>
                        </tr>
                        @endforeach
                    @else
                        <tr>
                            <td colspan="11">
                                No data.
                            </td>
                        </tr>
                    @endif
                </tbody>
            </table>

            <div class="pagination">
                {{ $postFileData->list->withQueryString()->links() }}
            </div>
        </div>
    </form>
</div>

<script type="module">
// 게시판 전환
$(document).on("change", "#boardID", function() {
    let form = document.getElementById("fAdminSearch");
    form.elements['board_id'].value = this.value;
    form.submit();
});
</script>
@endsection
