@extends('back-end.master.app')

@section('title', 'Create Poet')

@section('pageCSS')
@endsection

@section('body')
<main class="app-content">
    <div class="card">
        <div class="card-header">
            <h4>Create New Poet</h4>
        </div>
        <div class="card-body">
            @if ($errors->any())
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <form action="{{ route('poets.store') }}" method="post" enctype="multipart/form-data">
                @csrf
                <div class="row">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>First Name <span class="text-danger">*</span></label>
                            <input type="text" name="name" class="form-control" value="{{ old('name') }}" required>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Last Name</label>
                            <input type="text" name="last_name" class="form-control" value="{{ old('last_name') }}">
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Date of Birth</label>
                            <input type="date" name="dob" class="form-control" value="{{ old('dob', date('Y-m-d')) }}">
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Century <span class="text-danger">*</span></label>
                            <select name="centry_id" class="form-control select2" required>
                                <option value="">-- Select Century --</option>
                                @foreach ($centries as $cent)
                                    <option value="{{ $cent->id }}" {{ old('centry_id') == $cent->id ? 'selected' : '' }}>
                                        {{ $cent->title_ps }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Image</label>
                            <input type="file" name="image" class="form-control" accept="image/*" onchange="previewImage(event)">
                            <img id="imagePreview" src="#" alt="Preview" style="max-width: 100%; max-height: 150px; display: none; margin-top:10px;">
                        </div>
                    </div>

                    <div class="col-md-12">
                        <div class="form-group">
                            <label>Father Name</label>
                            <input type="text" name="father_name" class="form-control" value="{{ old('father_name') }}">
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Phone</label>
                            <input type="text" name="phone" class="form-control" value="{{ old('phone') }}">
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Email</label>
                            <input type="email" name="email" class="form-control" value="{{ old('email') }}">
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="form-group">
                            <label>Address <span class="text-danger">*</span></label>
                            <input type="text" name="address" class="form-control" value="{{ old('address') }}" required>
                        </div>
                    </div>

                    <div class="col-md-12">
                        <div class="form-group">
                            <label>Biography <span class="text-danger">*</span></label>
                            <textarea id="bio" name="bio" rows="6" class="form-control">{{ old('bio') }}</textarea>
                        </div>
                    </div>

                    <div class="col-md-12">
                        <button type="submit" class="btn btn-primary">Save</button>
                        <a href="{{ route('poets.index') }}" class="btn btn-secondary">Cancel</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</main>
@endsection

@section('pageJs')
<script src="https://cdn.ckeditor.com/ckeditor5/39.0.1/classic/ckeditor.js"></script>
<script>
    $('.select2').select2();

    ClassicEditor.create(document.querySelector('#bio')).catch(error => {
        console.error(error);
    });

    function previewImage(event) {
        const reader = new FileReader();
        reader.onload = function(){
            const output = document.getElementById('imagePreview');
            output.src = reader.result;
            output.style.display = 'block';
        }
        reader.readAsDataURL(event.target.files[0]);
    }

    $(".app-menu").removeClass("active");
    $("#nav_poetsi").addClass("active");
    $("#poetsi").addClass("is-expanded");
    $("#poets_history").addClass("active");
</script>
@endsection
