@extends('admin.layouts.app')

@section('content')
<div class="container-fluid">
    <h2>회원 관리</h2>
    <hr/>
    @include('admin.messages')

    <form name="f_admin_search" id="fAdminSearch" method="get" action="{{ route('admin.user.list.index') }}">
        <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="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>
                                    <option value="users.created_at" @if( $params->field == 'users.created_at') selected @endif>회원가입 일시</option>
                                    <option value="users.last_login_at" @if( $params->field == 'users.last_login_at') 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 class="text-start">
                        <div class="form-check form-check-inline">
                            <input type="checkbox" name="is_admin" id="isAdmin" class="form-check-input" value="1" @if($params->isAdmin) checked @endif/>
                            <label for="isAdmin" class="form-check-label">관리자</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input type="checkbox" name="is_denied" id="isDenied" class="form-check-input" value="1" @if($params->isDenied) checked @endif/>
                            <label for="isDenied" class="form-check-label">차단</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input type="checkbox" name="is_withdraw" id="isWithdraw" class="form-check-input" value="1" @if($params->isWithdraw) checked @endif />
                            <label for="isWithdraw" class="form-check-label">탈퇴</label>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>

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

    <div class="row mb-2">
        <div class="col align-self-center">
            <label>전체 : {{ $userData->total }} / {{ $userData->rows }}</label>
        </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/user/list/destroy">선택삭제</button>
            <button type="button" class="btn btn-outline-dark btn-list-withdraw btn-list-selected disabled" data-list-withdraw-url="/admin/user/list/withdraw">선택탈퇴</button>
            <a href="{{ route('admin.user.list.create') }}" class="btn btn-success">새로등록</a>
        </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>SID</th>
                        <th>이메일</th>
                        <th>이름</th>
                        <th>로그인 일시</th>
                        <th>탈퇴 일시</th>
                        <th>가입 일시</th>
                        <th>비고</th>
                    </tr>
                </thead>
                <tbody>
                    @if($userData->rows > 0)
                        @foreach($userData->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->userGroup->kor_name ?? '-' }}</td>
                            <td>{{ $row->userGrade->kor_name ?? '-' }}</td>
                            <td>{{ $row->sid }}</td>
                            <td>
                                {{ $row->email }}
                                @if($row->is_withdraw)
                                    <small class="badge bg-danger">탈퇴</small>
                                @endif
                            </td>
                            <td>{{ ($row->name ?? $row->nickname) }}</td>
                            <td>{!! $row->lastLoginAt !!}</td>
                            <td>{!! $row->deletedAt !!}</td>
                            <td>{!! $row->createdAt !!}</td>
                            <td>
                                <a href="{{ $row->editURL }}" class="btn btn-primary btn-sm">수정</a>
                            </td>
                        </tr>
                        @endforeach
                    @else
                        <tr>
                            <td colspan="10">
                                No data.
                            </td>
                        </tr>
                    @endif
                </tbody>
            </table>

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

<script type="module">
$(document).on('click', '.btn-list-withdraw', function () { // 선택탈퇴
    let form = document.f_admin_list;
    if ($("input:checkbox.list-check-box:checked", form).length < 1) {
        alert('자료를 하나 이상 선택하세요.');
        return false;
    }

    let input = document.createElement('input');
    input.setAttribute('type', 'hidden');
    input.setAttribute('name', '_method');

    if(!confirm("선택한 회원을 탈퇴 하시겠습니까?")) {
        return false;
    }

    form.appendChild(input);
    form.action = this.dataset.listWithdrawUrl;
    form.submit();
});
</script>
@endsection
