@extends('mobile.layouts.app')

@section('content')
<div id="boardWrite" class="container">
    <h5>{{ $board->name }} 글쓰기</h5>
    <hr/>

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

    {{-- 게시글 작성 --}}
    <div class="body">
        <form name="f_post_write" id="fPostWrite" method="post" action="{{ $storeURL }}" enctype="multipart/form-data" accept-charset="UTF-8" autocomplete="off" rel="nofollow">
            @csrf
            <input type="hidden" name="code" value="{{ $board->code }}"/>
            <input type="hidden" name="bid" value="{{ $board->id }}" />
            <input type="hidden" name="pid" value="" />
            <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 }}"/>

            <table class="table table-borderless">
                <colgroup>
                    <col width="50%"/>
                    <col width="50%"/>
                </colgroup>

                {{-- 추가기능 --}}
                @if((IS_ADMIN || !$boardMeta->use_personal))
                <tr>
                    <td colspan="2">
                        <label class="form-label">추가 기능</label>
                        <div>
                            @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') == '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') == '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(old('is_secret', $boardMeta->use_post_secret_selected) == '1') checked @endif />
                                    <label for="isSecret" class="form-check-label">비밀글</label>
                                </div>
                            @endif
                        </div>
                    </td>
                </tr>
                @endif

                {{-- 분류 --}}
                @if($boardMeta->use_category && $category)
                <tr>
                    <td colspan="2">
                        <label for="category" class="form-label">{{ ($boardMeta->use_personal ? '문의 유형' : '분류') }}</label>
                        <select name="category" id="category" class="form-select">
                            <option value="">분류를 선택하세요.</option>
                            @php
                            $traverse = function ($categories) use (&$traverse) {
                                foreach($categories as $row) {
                                    $tab = ($row->depth > 0 ? '└' : '') . str_repeat('─', ($row->depth > 0 ? $row->depth - 1 : 0));
                                    $checked = (old('category') == $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>
                    <td>
                        <label for="username" class="form-label">작성자</label>
                        <input type="text" name="username" id="username" class="form-control" value="{{ old('username') }}"/>
                    </td>
                    <td>
                        <label for="password" class="form-label">비밀번호</label>
                        <input type="password" name="password" id="password" class="form-control" value=""/>
                    </td>
                </tr>
                @endguest

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

                {{-- 내용 --}}
                <tr>
                    <td colspan="2">
                        <label for="content" class="form-label">내용</label>
                        {!! htmlEditor('content', old('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>
                    <td colspan="2">
                        <label for="tags" class="form-label">Tag</label>
                        <input type="text" name="tags" id="tags" class="form-control" value="{{ old('tags') }}" placeholder="콤마(,)로 구분해 입력하세요. (최대 10개)"/>
                    </td>
                </tr>
                @endif

                {{-- Links --}}
                @for ($i = 1; $i <= ($boardMeta->link_num ?? 0); $i++)
                <tr>
                    <td colspan="2">
                        <label for="links_{{ $i }}" class="form-label">Link #{{ $i }}</label>
                        <input type="text" name="links[{{ $i }}]" id="links_{{ $i }}" value="{{ old('links.' . $i) }}" class="form-control form-control-url" />
                    </td>
                </tr>
                @endfor

                {{-- Files --}}
                @if($boardMeta->use_upload_file)
                    @for ($i = 1; $i <= ($boardMeta->upload_file_num ?? 0); $i++)
                    <tr>
                        <td colspan="2">
                            <label for="files_{{ $i }}" class="form-label w-100">File #{{ $i }}</label>
                            <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> @if($boardMeta->upload_file_max_size) / {{ $boardMeta->upload_file_max_size ?? 0 }}MB @endif
                            </small>
                        </td>
                    </tr>
                    @endfor
                @endif

                {{-- Captcha --}}
                @if($boardMeta->use_post_captcha)
                <tr>
                    <td>
                        <label for="captcha" class="form-label">자동등록방지</label>
                        <div class="captcha">
                            {!! captcha_image_html('PostWriteCaptcha') !!}
                            <input type="text" name="captcha" id="captcha" class="form-control" 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="fPostWrite">작성</button>
                <a href="{{ $listURL }}" class="btn btn-default">취소</a>
            </div>
        </div>
    </div>
</div>
@endsection

@push('styles')
    <link rel="stylesheet" href="{{ asset('css/mobile/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
