@extends('admin.layouts.sub')

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

    <form name="f_admin_search" id="fAdminSearch" method="get" action="{{ route('admin.popup.user') }}">
        <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.nickname" @if( $params->field == 'users.nickname') 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>
                @if($userGroupData->count() > 0)
                <tr>
                    <th>회원그룹</th>
                    <td class="text-start">
                        @foreach($userGroupData as $row)
                        <div class="form-check form-check-inline">
                            <input type="checkbox" name="user_group_id[]" id="userGroupID_{{ $row->id }}" class="form-check-input" value="{{ $row->id }}" @if(in_array($row->id, $params->userGroupID)) checked @endif/>
                            <label for="userGroupID_{{ $row->id }}" class="form-check-label">{{ $row->kor_name }}</label>
                        </div>
                        @endforeach
                    </td>
                </tr>
                @endif
                @if($userGradeData->count() > 0)
                <tr>
                    <th>회원등급</th>
                    <td class="text-start">
                        @foreach($userGradeData as $row)
                        <div class="form-check form-check-inline">
                            <input type="checkbox" name="user_grade_id[]" id="userGradeID_{{ $row->id }}" class="form-check-input" value="{{ $row->id }}" @if(in_array($row->id, $params->userGradeID)) checked @endif/>
                            <label for="userGradeID_{{ $row->id }}" class="form-check-label">{{ $row->kor_name }}</label>
                        </div>
                        @endforeach
                    </td>
                </tr>
                @endif
            </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>

    <form name="f_admin_list" id="fAdminList" method="post">
        @csrf
        <div class="table-responsive">
            <table class="table table-hover table-bordered">
                <thead>
                    <tr>
                        <th><input type="checkbox" name="chk_all" id="chkAll" class="form-check-input" value="1"/></th>
                        <th>PK</th>
                        <th>ID</th>
                        <th>이메일</th>
                        <th>이름</th>
                        <th>닉네임</th>
                        <th>그룹</th>
                        <th>등급</th>
                    </tr>
                </thead>
                <tbody>
                    @if($userData->rows)
                        @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 }}"
                                    data-user-sid="{{ $row->sid }}"
                                    data-user-nickname="{{ $row->nickname }}"/>
                            </td>
                            <td>{{ $row->id }}</td>
                            <td>{{ $row->sid }}</td>
                            <td>{{ $row->email }}</td>
                            <td>{{ $row->name ?? '-' }}</td>
                            <td>{{ $row->nickname ?? '-' }}</td>
                            <td>{{ $row->userGroup->kor_name ?? '-' }}</td>
                            <td>{{ $row->userGrade->kor_name ?? '-' }}</td>
                        </tr>
                        @endforeach
                    @else
                        <tr>
                            <td colspan="8">
                                No data.
                            </td>
                        </tr>
                    @endif
                </tbody>
            </table>

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

        <div class="text-center">
            <button type="submit" class="btn btn-success">적용</button>
            <button type="button" class="btn btn-light btn-popup-cancel">닫기</button>
        </div>
        <br/>
    </form>
</div>

<script type="module">
$("#fAdminList").validate({
    onkeyup: false,
    onclick: false,
    onfocusout: false,
    showErrors: function (errorMap, errorList) {
        if (this.numberOfInvalids()) {
            alert(errorList[0].message);
        }
    },
    submitHandler: function() {
        let users = $("input.list-check-box:checked").map(function() {
            return [
                {"key": this.value, "id" : this.dataset.userSid, "name": this.dataset.userNickname}
            ];
        }).get();

        window.opener.setUser(users);
        window.close();
        return false;
    },
    rules: {
        "chk[]": {required: true}
    },
    messages: {
        "chk[]": {required: "적용할 회원을 선택해 주세요."}
    }
});
</script>
@endsection
