@extends('admin.layouts.app')

@section('content')
<div class="container-fluid">
    <h2>신고 관리 - 게시글</h2>
    <hr/>
    @include('admin.messages')
    @include('admin.board.blame.navTabs')

    <form name="f_admin_search" id="fAdminSearch" method="get" action="{{ route('admin.board.blame.post.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_blame.id" @if($params->field == 'tb_post_blame.id') selected @endif>PK</option>
                                    <option value="tb_post_blame.ip_address" @if($params->field == 'tb_post_blame.ip_address') selected @endif>IP</option>
                                    <option value="tb_post_blame.post_id" @if($params->field == 'tb_post_blame.post_id') selected @endif>게시글 PK</option>
                                    <option value="user_1.id"  @if($params->field == 'user_1.id') selected @endif>작성자 회원 PK</option>
                                    <option value="user_1.sid"  @if($params->field == 'user_1.sid') selected @endif>작성자 회원 ID</option>
                                    <option value="user_1.name"  @if($params->field == 'user_1.name') selected @endif>작성자 이름</option>
                                    <option value="user_1.email" @if($params->field == 'user_1.email') selected @endif>작성자 이메일</option>
                                    <option value="user_2.id"  @if($params->field == 'user_2.id') selected @endif>추천 회원 PK</option>
                                    <option value="user_2.sid"  @if($params->field == 'user_2.sid') selected @endif>추천 회원 ID</option>
                                    <option value="user_2.name"  @if($params->field == 'user_2.name') selected @endif>추천 이름</option>
                                    <option value="user_2.email" @if($params->field == 'user_2.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/blame/post/delete">선택삭제</button>
        </div>
    </div>
    <br/>

    <div class="row mb-2">
        <div class="col align-self-center">
            전체 : {{ $blameData->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>내용</th>
                        <th>작성자</th>
                        <th>신고자</th>
                        <th>IP</th>
                        <th>처리 상태</th>
                        <th>등록일시</th>
                        <th>비고</th>
                    </tr>
                </thead>
                <tbody>
                    @if($blameData->rows > 0)
                        @foreach($blameData->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 class="text-start">
                                <a href="{{ $row->postURL }}" target="_blank">
                                    {{ $row->subject }} <i class="fa-solid fa-arrow-up-right-from-square ms-1"></i>
                                </a>
                            </td>
                            <td>{{ $row->typeStr }}</td>
                            <td>{{ $row->reason }}</td>
                            <td>{{ $row->name_1 . ' (' . $row->sid_1 . ')' }}</td>
                            <td>{{ $row->name_2 . ' (' . $row->sid_2 . ')' }}</td>
                            <td>{{ $row->ip_address }}</td>
                            <td>
                                @if($row->status == '0')
                                    <span class="badge text-bg-danger">접수</span>
                                @else
                                    <span class="badge text-bg-info text-white">완료</span>
                                @endif
                            </td>
                            <td>{{ $row->created_at }}</td>
                            <td>
                                <button type="button" class="btn btn-secondary btn-sm text-white" data-bs-toggle="modal" data-bs-target="#adminPostBlameModal_{{ $row->id }}">변경</button>
                                <div class="modal fade admin-post-blame-modal" id="adminPostBlameModal_{{ $row->id }}" tabindex="-1" aria-labelledby="adminPostBlameModalLabel_{{ $row->id }}" aria-hidden="true">
                                    <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
                                        <div class="modal-content">
                                            <div class="modal-header bg-dark text-white">
                                                <h5 class="modal-title" id="adminPostBlameModalLabel_{{ $row->id }}">신고 결과 변경</h5>
                                                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                                            </div>
                                            <div class="modal-body text-start">
                                                <input type="hidden" name="blame_id[{{ $row->id }}]" value="{{ $row->id }}" form="fAdminPostBlame"/>
                                                <div class="mb-3">
                                                    <label class="col-form-label">신고 사유</label>
                                                    <input type="text" class="form-control-plaintext" value="- {{ $row->typeStr }}"/>
                                                </div>
                                                <div class="mb-3">
                                                    <label for="status_{{ $row->id }}_0" class="col-form-label">처리 상태</label>
                                                    <div class="mb-3">
                                                        <div class="form-check form-check-inline">
                                                            <input type="radio" name="status[{{ $row->id }}]" id="status_{{ $row->id }}_0" class="form-check-input" value="0" form="fAdminPostBlame" @if($row->status == '0') checked @endif/>
                                                            <label for="status_{{ $row->id }}_0" class="form-check-label">접수</label>
                                                        </div>
                                                        <div class="form-check form-check-inline">
                                                            <input type="radio" name="status[{{ $row->id }}]" id="status_{{ $row->id }}_1" class="form-check-input" value="1" form="fAdminPostBlame" @if($row->status == '1') checked @endif/>
                                                            <label for="status_{{ $row->id }}_1" class="form-check-label">완료</label>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="mb-3">
                                                    <label for="memo_{{ $row->id }}" class="col-form-label">처리 내용</label>
                                                    <textarea name="memo[{{ $row->id }}]" id="memo_{{ $row->id }}" class="form-control" rows="10" form="fAdminPostBlame">{{ $row->memo }}</textarea>
                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <div class="m-auto">
                                                    <button type="submit" class="btn btn-primary btn-form-submit" form="fAdminPostBlame" value="{{ $row->id }}">확인</button>
                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">닫기</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <a href="{{ $row->editURL }}" class="btn btn-sm btn-primary">보기</a>
                            </td>
                        </tr>
                        @endforeach
                    @else
                        <tr>
                            <td colspan="12">
                                No data.
                            </td>
                        </tr>
                    @endif
                </tbody>
            </table>

            <div class="pagination">
                {{ $blameData->list->withQueryString()->links() }}
            </div>
        </div>
    </form>
    <form name="f_admin_post_blame" id="fAdminPostBlame" method="post" target="_self" autocomplete="off">
        <input type="hidden" name="post_blame_id" value=""/>
    </form>
</div>

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

// 신고 결과 확인 클릭
$(document).on("click", ".btn-form-submit", function() {
    document.getElementById("fAdminPostBlame").post_blame_id.value = this.value;
});

// 신고 처리 결과 등록
$(document).on("submit", "#fAdminPostBlame", function(e) {
    let form = e.target;
    let key = form.post_blame_id.value;

    let status = form.elements["status[" + key + "]"];
    if(!status[0].checked && !status[1].checked) {
        alert("처리 결과를 선택하세요.");
        status[0].focus();
        return false;
    }

    let memo = form.elements["memo[" + key + "]"];
    if(!memo.value || memo.value === "") {
        alert("처리 내용을 입력하세요.");
        memo.focus();
        return false;
    }

    if(confirm("신고 결과를 변경하시겠습니까?")) {
        status = document.querySelectorAll('input[type="radio"][name="status[' + key + ']"]:checked')[0].value;
        memo = memo.value.trim();

        $.ajax({
            url: (ADMIN_URL + "/ajax/postBlameResultUpdate"),
            type: 'POST',
            dataType: 'JSON',
            data: {
                post_blame_id: key,
                status: status,
                memo: memo
            },
            success: function (res) {
                alert(res.message);
                if(res.success) {
                    location.reload();
                }
            },
            error: function () {
                alert("잘못된 요청입니다.");
            }
        });
    }
    return false;
});
</script>
@endsection
