@extends('admin.layouts.app')

@section('content')
<div class="container-fluid">
    <h2>변경 이력 - 게시글</h2>
    <hr/>
    @include('admin.messages')
    @include('admin.board.history.navTabs')

    <form name="f_admin_search" id="fAdminSearch" method="get" action="{{ route('admin.board.history.post.store') }}">
        <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_history.before_subject" @if( $params->field == 'tb_post.before_subject') selected @endif>이전 제목</option>
                                    <option value="tb_post_history.after_subject" @if( $params->field == 'tb_post.after_subject') selected @endif>변경 제목</option>
                                    <option value="tb_post_history.before_content" @if( $params->field == 'tb_post.before_content') selected @endif>이전 내용</option>
                                    <option value="tb_post_history.after_content" @if( $params->field == 'tb_post.after_content') selected @endif>변경 내용</option>
                                    <option value="tb_post_history.ip_address" @if( $params->field == 'tb_post_history.ip_address') selected @endif>IP</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/history/post/destroy">선택삭제</button>
        </div>
    </div>
    <br/>

    <div class="row mb-2">
        <div class="col align-self-center">
            전체 : {{ $postHistoryData->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>번호</th>
                        <th>게시판</th>
                        <th>이전 제목</th>
                        <th>변경 제목</th>
                        <th>이전 내용</th>
                        <th>변경 내용</th>
                        <th>변경 회원</th>
                        <th>최초 작성자</th>
                        <th>IP</th>
                        <th>변경일시</th>
                    </tr>
                </thead>
                <tbody>
                    @if($postHistoryData->rows > 0)
                        @foreach($postHistoryData->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->num }}</td>
                            <td>{{ $row->boardName }}</td>
                            <td>{{ $row->before_subject ?? '-' }}</td>
                            <td>{{ $row->after_subject ?? '-' }}</td>
                            <td>
                                <button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#detailPostBeforeContentModal_{{ $row->id }}">
                                    상세보기
                                </button>
                                <div class="modal fade" id="detailPostBeforeContentModal_{{ $row->id }}" tabindex="-1" aria-labelledby="detailPostBeforeContentModalLabel_{{ $row->id }}" aria-hidden="true">
                                    <div class="modal-dialog modal-xl">
                                        <div class="modal-content">
                                            <div class="modal-header bg-dark text-white">
                                                <h5 class="modal-title" id="detailPostBeforeContentModalLabel_{{ $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">
                                                {!! $row->before_content ?? '-' !!}
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary m-auto" data-bs-dismiss="modal">닫기</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <button type="button" class="btn btn-sm btn-info text-white" data-bs-toggle="modal" data-bs-target="#detailPostAfterContentModal_{{ $row->id }}">
                                    상세보기
                                </button>
                                <div class="modal fade" id="detailPostAfterContentModal_{{ $row->id }}" tabindex="-1" aria-labelledby="detailPostAfterContentModalLabel_{{ $row->id }}" aria-hidden="true">
                                    <div class="modal-dialog modal-xl">
                                        <div class="modal-content">
                                            <div class="modal-header bg-dark text-white">
                                                <h5 class="modal-title" id="detailPostAfterContentModalLabel_{{ $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">
                                                {!! $row->after_content ?? '-' !!}
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary m-auto" data-bs-dismiss="modal">닫기</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td>
                                @if($row->name_1)
                                    {{ $row->name_1  }} <br/> ({{ $row->sid_1 }})
                                @else
                                    -
                                @endif
                            </td>
                            <td>
                                @if($row->name_2)
                                    {{ $row->name_2 }} <br/> ({{ $row->sid_2 }})
                                @else
                                    -
                                @endif
                            </td>
                            <td>{{ $row->ip_address }}</td>
                            <td>{!! $row->createdAt !!}</td>
                        </tr>
                        @endforeach
                    @else
                        <tr>
                            <td colspan="11">
                                No data.
                            </td>
                        </tr>
                    @endif
                </tbody>
            </table>

            <div class="pagination">
                {{ $postHistoryData->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();
});
let img = document.getElementsByTagName("img");
for (let x = 0; x < img.length; x++) {
    img.item(x).onclick = function () {
        popupCenter(this.src, this.title);
    };
    img.item(x).style.cursor = "pointer";
}
</script>
@endsection
