@extends('admin.layouts.app')

@section('content')
<div class="container-fluid">
    <h2>배너 @if($bannerID) 수정 @else 등록 @endif</h2>
    <hr/>
    @include('admin.messages')

    <form name="f_admin_write" id="fAdminWrite" method="post" action="{{ $actionURL }}" enctype="multipart/form-data">
        @csrf
        @if($bannerID)
            <input type="hidden" name="banner_id" value="{{ $bannerID }}" />
            @method('PUT')
        @endif
        <div class="row mb-3">
            <label for="bannerGroupID" class="col-sm-2 col-form-label">위치</label>
            <div class="col-sm-10">
                <select name="banner_group_id" id="bannerGroupID" class="form-select">
                    <option name="">위치 선택</option>
                    @foreach($bannerGroupData as $bng)
                        <option value="{{ $bng->id }}" @if(($bannerData->banner_group_id ?? old('banner_group_id')) == $bng->id) selected @endif>{{ $bng->name }}</option>
                    @endforeach
                </select>
            </div>
        </div>
        <div class="row mb-3">
            <label class="col-sm-2 col-form-label">이미지</label>
            <div class="col-sm-10">
                @if($bannerData->image ?? '')
                    <img src="{{ asset($bannerData->image) }}" class="img-thumbnail" onerror="this.src='{{ NO_IMAGE_NORMAL_SRC }}';"/>
                    <input type="hidden" name="image_url" id="imageURL" value="{{ $bannerData->image }}" />
                    <div class="form-check mt-2">
                        <input type="checkbox" name="image_del" id="imageDel" class="form-check-input" value="1" />
                        <label for="imageDel" class="form-check-label">삭제</label>
                    </div>
                @else
                    <input type="file" name="image" id="image" value="{{ $bannerData->image ?? old('image') }}" accept="image/gif,image/jpeg,image/png"/>
                    <div class="form-text text-muted">
                         크기 상관없이 3MB ( gif, jpeg, png ) 확장자로 등록해 주십시요.
                    </div>
                @endif
            </div>
        </div>
        <div class="row mb-3">
            <label for="subject" class="col-sm-2 col-form-label">제목</label>
            <div class="col-sm-10">
                <textarea name="subject" id="subject" class="form-control" rows="4">{{ $bannerData->subject ?? old('subject') }}</textarea>
            </div>
        </div>
        <div class="row mb-3">
            <label for="summary" class="col-sm-2 col-form-label">요약 설명</label>
            <div class="col-sm-10">
                <textarea name="summary" id="summary" class="form-control" rows="4">{{ $bannerData->summary ?? old('summary') }}</textarea>
            </div>
        </div>
        <div class="row mb-3">
            <label for="url" class="col-sm-2 col-form-label">URL</label>
            <div class="col-sm-10">
                <input type="text" name="url" id="url" class="form-control" value="{{ $bannerData->url ?? old('url') }}" />
            </div>
        </div>
        <div class="row mb-3">
            <label for="startDate" class="col-sm-2 col-form-label">시작일</label>
            <div class="col-sm-10 form-inline">
                <input type="date" name="start_date" id="startDate" class="form-control date-picker" value="{{ $bannerData->start_date ?? old('start_date') }}" />
            </div>
        </div>
        <div class="row mb-3">
            <label for="endDate" class="col-sm-2 col-form-label">종료일</label>
            <div class="col-sm-10 form-inline">
                <input type="date" name="end_date" id="endDate" class="form-control date-picker" value="{{ $bannerData->end_date ?? old('end_date') }}" />
            </div>
        </div>
        <div class="row mb-3">
            <label for="target" class="col-sm-2 col-form-label">새창 여부</label>
            <div class="col-sm-10">
                <div class="form-inline">
                    <select name="target" id="target" class="form-select">
                        <option value="0" @if($bannerData->target ?? old('target') == '0') selected @endif>현재창</option>
                        <option value="1" @if($bannerData->target ?? old('target') == '1') selected @endif>새창</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="row mb-3">
            <label for="device" class="col-sm-2 col-form-label">표시기기</label>
            <div class="col-sm-10">
                <div class="form-inline">
                    <select name="device" id="device" class="form-select">
                        <option value="0" @if($bannerData->device ?? old('device') == '0') selected @endif>모든기기</option>
                        <option value="1" @if($bannerData->device ?? old('device') == '1') selected @endif>PC</option>
                        <option value="2" @if($bannerData->device ?? old('device') == '2') selected @endif>모바일</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="row mb-3">
            <label for="width" class="col-sm-2 col-form-label">크기</label>
            <div class="col-sm-10">
                <div class="row">
                    <div class="col-auto">
                        <label class="form-label">가로</label>
                        <div class="input-group">
                            <input type="number" name="width" id="width" class="form-control" value="{{ $bannerData->width ?? old('width') }}" />
                            <div class="input-group-text">
                                px
                            </div>
                        </div>
                    </div>
                    <div class="col-auto">
                        <label class="form-label">세로</label>
                        <div class="input-group">
                            <input type="number" name="height" id="height" class="form-control" value="{{ $bannerData->height ?? old('height') }}" />
                            <div class="input-group-text">
                                px
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row mb-3">
            <label for="order" class="col-sm-2 col-form-label">순서</label>
            <div class="col-sm-10">
                <div class="form-inline">
                    <input type="number" name="order" id="order" class="form-control" value="{{ $bannerData->order ?? old('order') }}" />
                </div>
            </div>
        </div>
        <div class="row mb-3">
            <label for="activated_1" class="col-sm-2">활성화 여부</label>
            <div class="col-sm-10">
                <div class="form-check form-check-inline">
                    <input type="radio" name="activated" id="activated_1" class="form-check-input" value="1" @if(($bannerData->activated ?? old('activated')) == '1') checked @endif/>
                    <label for="activated_1" class="form-check-label">
                        활성
                    </label>
                </div>
                <div class="form-check form-check-inline">
                    <input type="radio" name="activated" id="activated_0" class="form-check-input" value="0" @if(($bannerData->activated ?? old('activated')) == '0') checked @endif />
                    <label for="activated_0" class="form-check-label">
                        비활성
                    </label>
                </div>
            </div>
        </div>
        @isset($bannerData->updated_at)
        <div class="row mb-3">
            <label class="col-sm-2">수정일시</label>
            <div class="col-sm-10">
                {{ $bannerData->updated_at }}
            </div>
        </div>
        @endisset
        @isset($bannerData->created_at)
        <div class="row">
            <label class="col-sm-2">등록일시</label>
            <div class="col-sm-10">
                {{ $bannerData->created_at }}
            </div>
        </div>
        @endisset
        <hr/>
        <div class="text-end">
            <button type="submit" class="btn btn-success">저장하기</button>
            <button type="button" class="btn btn-light btn-write-cancel" value="{{ route('admin.page.banner.index') }}">취소하기</button>
        </div>
    </form>
</div>

<script type="module">
$("#fAdminWrite").validate({
    rules: {
        banner_group_id: {required: true},
        image: {required: false, accept: "image/gif,image/jpeg,image/png"},
        subject: {required: false, maxlength: 50},
        summary: {required: false, maxlength: 255},
        url: {required: false, url: true},
        start_date: {alpha_dash: true, minlength: 10, maxlength: 10},
        end_date: {alpha_dash: true, minlength: 10, maxlength: 10},
        width: {required: true, number: true},
        height: {required: true, number: true},
        device: {number: true},
        order: {number: true},
        activated: {required: true}
    }
});
</script>
@endsection
