@extends('desktop.layouts.app')

@section('content')
<div id="boardEdit" class="container">
    <h4>{{ $board->name }} 글 수정</h4>
    <hr/>

    @if($boardMeta->write_header_content)
        <div class="head">
            {!! $boardMeta->write_header_content !!}
        </div>
    @endif

    {{-- 게시글 수정 --}}
    <div class="body">
        <form name="f_post_edit" id="fPostEdit" method="post" action="{{ $updateURL }}" enctype="multipart/form-data" accept-charset="UTF-8" autocomplete="off" rel="nofollow">
            @csrf
            @method('PUT')
            <input type="hidden" name="code" value="{{ $board->code }}"/>
            <input type="hidden" name="bid" value="{{ $post->board_id }}" />
            <input type="hidden" name="pid" value="{{ $post->id }}" />
            <input type="hidden" name="subject_min_length" id="subjectMinLength" value="{{ $boardMeta->post_subject_min_length }}"/>
            <input type="hidden" name="subject_max_length" id="subjectMaxLength" value="{{ $boardMeta->post_subject_max_length }}"/>
            <input type="hidden" name="content_min_length" id="contentMinLength" value="{{ $boardMeta->post_content_min_length }}"/>
            <input type="hidden" name="content_max_length" id="contentMaxLength" value="{{ $boardMeta->post_content_max_length }}"/>
            <input type="hidden" name="upload_file_max_size" id="uploadFileMaxSize" value="{{ $boardMeta->upload_file_max_size }}"/>
            <input type="hidden" name="passwd" value="{{ request()->input('passwd') }}"/>

            <table class="table table-borderless">
                <colgroup>
                    <col width="150px">
                    <col width="*">
                </colgroup>

                {{-- 추가기능 --}}
                @if((IS_ADMIN || !$boardMeta->use_personal))
                <tr>
                    <th>
                        <label>추가 기능</label>
                    </th>
                    <td colspan="3">
                        @admin
                            @if(!$boardMeta->use_personal) {{-- 1:1 게시판 전체공지는 제외 --}}
                            <div class="form-check form-check-inline">
                                <input type="checkbox" name="is_speaker" id="isSpeaker" class="form-check-input " value="1" @if(old('is_speaker', $post->is_speaker) == '1') checked @endif />
                                <label for="isSpeaker" class="form-check-label">전체공지</label>
                            </div>
                            @endif
                            <div class="form-check form-check-inline">
                                <input type="checkbox" name="is_notice" id="isNotice" class="form-check-input" value="1" @if(old('is_notice', $post->is_notice) == '1') checked @endif />
                                <label for="isNotice" class="form-check-label">공지</label>
                            </div>
                        @endadmin
                        @if($boardMeta->use_post_secret == '1')
                            <div class="form-check form-check-inline">
                                <input type="checkbox" name="is_secret" id="isSecret" class="form-check-input" value="1" @if($boardMeta->use_post_secret_selected || old('is_secret', $post->is_secret) == '1') checked @endif />
                                <label for="isSecret" class="form-check-label">비밀글</label>
                            </div>
                        @endif
                    </td>
                </tr>
                @endif

                {{-- 분류 --}}
                @if($boardMeta->use_category && $category)
                <tr>
                    <th>
                        <label for="category">{{ ($boardMeta->use_personal ? '문의 유형' : '분류') }}</label>
                    </th>
                    <td colspan="3">
                        <select name="category" id="category" class="form-select w-auto" data-category-required="{{ $boardMeta->use_category_required }}">
                            <option value="">분류를 선택하세요.</option>
                            @php
                            $traverse = function ($categories) use (&$traverse, $post) {
                                foreach($categories as $row) {
                                    $tab = ($row->depth > 0 ? '└' : '') . str_repeat('─', ($row->depth > 0 ? $row->depth - 1 : 0));
                                    $checked = (old('category', $post->board_category_id) == $row->board_category_id ? 'selected' : '');
                                    @endphp
                                        <option value="{{ $row->board_category_id }}" data-depth="{{ $row->depth }}" {{ $checked }}>{{$tab . ' ' . $row->name}}</option>
                                    @php
                                    $traverse($row->children);
                                }
                            };
                            $traverse($category->list);
                            @endphp
                        </select>
                    </td>
                </tr>
                @endif

                {{-- 익명 게시판 --}}
                @guest
                <tr>
                    <th>
                        <label for="username">이름</label>
                    </th>
                    <td>
                        <input type="text" name="username" id="username" class="form-control" value="{{ old('username', $post->username) }}"/>
                    </td>
                    <th class="text-center"><label for="password">비밀번호</label></th>
                    <td>
                        <input type="password" name="password" id="password" class="form-control" value=""/>
                    </td>
                </tr>
                @endguest

                {{-- 제목 --}}
                <tr>
                    <th>
                        <label for="subject">제목</label>
                    </th>
                    <td colspan="3">
                        <input type="text" name="subject" id="subject" class="form-control" required placeholder="{{ $boardMeta->post_default_subject }}" value="{{ old('subject', $post->subject) }}"/>
                    </td>
                </tr>

                {{-- 내용 --}}
                <tr>
                    <th class="align-top">
                        <label for="content">내용</label>
                    </th>
                    <td colspan="4">
                        {!! htmlEditor('content', old('content', $post->content), 'form-control dhtml-editor', $boardMeta->use_post_dhtml, false, $boardMeta->post_default_content, 8, 'content'); !!}
                    </td>
                </tr>

                {{-- Tags --}}
                @if($boardMeta->use_post_tag)
                <tr>
                    <th>
                        <label for="tags">Tag</label>
                    </th>
                    <td colspan="3">
                        <input type="text" name="tags" id="tags" class="form-control" value="{{ old('tags', join(', ', array_column($post->tags->toArray() ?? [], 'tag'))) }}" placeholder="콤마(,)로 구분해 입력하세요. (최대 10개)"/>
                    </td>
                </tr>
                @endif

                {{-- Links --}}
                @for ($i = 0; $i < ($boardMeta->link_num ?? 0); $i++)
                @php
                    $linked = $post->links->get($i);
                    if($linked) {
                        $key = $linked->id;
                        $link = $linked->url;
                    }else{
                        $key = ($i + 1);
                        $link = (old("link")[$i] ?? null);
                    }
                @endphp
                <tr>
                    <th>
                        <label for="links_{{ $key }}">Link #{{ ($i + 1) }}</label>
                    </th>
                    <td colspan="3">
                        <input type="text" name="links[{{ $key }}]" id="links_{{ $key }}" class="form-control form-control-url" value="{{ old('links.' . $key, $link) }}" />
                    </td>
                </tr>
                @endfor

                {{-- Files --}}
                @if($boardMeta->use_upload_file)
                @for ($i = 0; $i < ($boardMeta->upload_file_num ?? 0); $i++)
                <tr>
                    <th>
                        <label for="files_{{ $i }}">File #{{ $i + 1 }}</label>
                    </th>
                    <td colspan="3">
                        @if($file = $post->files->get($i))
                            @if($file->is_image)
                                <i class="far fa-image"></i>
                            @else
                                <i class="fas fa-file"></i>
                            @endif
                            <a href="{{ $file->file_url }}" download="{{ $file->origin_name }}" class="btn btn-link ps-0">{{ $file->origin_name }}</a>
                            <div class="form-check form-check-inline align-middle">
                                <input type="checkbox" name="file_is_delete[{{ $file->id }}]" id="fileIsDelete_{{ $file->id }}" class="form-check-input" value="1"/>
                                <label for="fileIsDelete_{{ $file->id }}" class="form-check-label">삭제</label>
                            </div>
                        @else
                            <input type="file" name="files[{{ $i }}]" id="files_{{ $i }}" class="form-control-file"
                                @if($boardMeta->upload_only_img_file) accept="image/gif, image/jpeg, image/png"
                                @elseif($boardMeta->upload_file_extension) accept="{{ $boardMeta->upload_file_extension }}" @endif
                            />
                            <small class="form-text text-muted">
                                <span>0</span> / {{ $boardMeta->upload_file_max_size }}MB
                            </small>
                        @endif
                    </td>
                </tr>
                @endfor
                @endif

                {{-- Captcha --}}
                @if($boardMeta->use_post_captcha)
                <tr>
                    <th>
                        <label for="captcha">자동등록방지</label>
                    </th>
                    <td>
                        <div class="captcha">
                            {!! captcha_image_html('PostEditCaptcha') !!}
                            <input type="text" name="captcha" id="captcha" class="form-control mt-2" placeholder="자동등록방지 문자를 입력하십시오."/>
                        </div>
                    </td>
                </tr>
                @endif
            </table>
        </form>
    </div>
    <hr/>
    <div class="foot">
        <div class="row">
            <div class="col align-self-center">
                <a href="{{ $provisionURL }}" target="_blank">이용약관</a> 및 <a href="{{ $boardRuleURL }}" target="_blank">게시판 규칙</a>에 위배되는 경우, 이용에 제한을 받을 수 있습니다.
            </div>
            <div class="col-auto text-end">
                <button type="button" id="btnPostSubmit" class="btn btn-primary" data-form="fPostEdit">작성</button>
                <a href="{{ $listURL }}" class="btn btn-default">취소</a>
            </div>
        </div>
    </div>
</div>
@endsection

@push('styles')
    <link rel="stylesheet" href="{{ asset('css/desktop/board/write.css') }}"/>
    @if($boardMeta->use_post_captcha)
        <link rel="stylesheet" href="{{ captcha_layout_stylesheet_url() }}"/>
    @endif
@endpush

@push('scripts')
    <script src="{{ asset('/plugin/editor/tinymce/tinymce.min.js') }}" defer></script>
    <script src="{{ asset('/plugin/editor/tinymce/config.js') }}" defer></script>
    <script src="{{ asset('js/board/post.js') }}" defer></script>
@endpush
