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

@section('title', 'Edit Poetry')

@section('pageCSS')
    <link href="{{ asset('back-end/vendor/summernote/summernote-bs4.css') }}" rel="stylesheet">
    <style>
        .form-control, option { direction: rtl !important; }
    </style>
@endsection

@section('body')
<main class="app-content">
    <div class="card">
        <div class="card-header">
            <h4>Edit Poetry</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('poetries.update', $data->id) }}" method="POST">
                @csrf
                @method('PUT')
                <div class="row">
                    <div class="col-md-9">
                        <div class="form-group">
                            <label>لومړی بیت <span class="text-danger">*</span></label>
                            <input type="text" name="title" class="form-control" value="{{ old('title', $data->title) }}" required>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>لومړی الفابیټ <span class="text-danger">*</span></label>
                            <select name="alphabit" class="form-control" required>
                                <option value="">انتخاب</option>
                                @foreach($alphabets as $row)
                                    <option value="{{ $row->name }}" {{ (old('alphabit', $data->alphabit) == $row->name) ? 'selected' : '' }}>{{ $row->name }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>شاعر <span class="text-danger">*</span></label>
                            <select name="poet_id" class="form-control" required>
                                <option value="">-- انتخاب --</option>
                                @foreach($poets as $poet)
                                    <option value="{{ $poet->id }}" {{ (old('poet_id', $data->poet_id) == $poet->id) ? 'selected' : '' }}>{{ $poet->name }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>کټګورۍ <span class="text-danger">*</span></label>
                            <select name="cat_id" id="cat_id" class="form-control" required>
                                <option value="">-- انتخاب --</option>
                                @foreach($categories as $cat)
                                    <option value="{{ $cat->id }}" {{ (old('cat_id', $data->cat_id) == $cat->id) ? 'selected' : '' }}>{{ $cat->title_ps }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="form-group">
                            <label>فرعي کټګورۍ</label>
                            <select name="scat_id" id="scat_id" class="form-control">
                                <option value="">-- اختیاري --</option>
                                @foreach($subCategories as $sub)
                                    @if($sub->category_id == $data->cat_id)
                                        <option value="{{ $sub->id }}" {{ (old('scat_id', $data->scat_id) == $sub->id) ? 'selected' : '' }}>{{ $sub->title_ps }}</option>
                                    @endif
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="form-group">
                            <label>شعر <span class="text-danger">*</span></label>
                            <textarea name="body" id="summernote" class="form-control">{{ old('body', $data->body) }}</textarea>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <button type="submit" class="btn btn-primary">Update</button>
                        <a href="{{ route('poetries.index') }}" class="btn btn-secondary">Cancel</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</main>
@endsection

@section('pageJs')
    <script src="{{ asset('back-end/vendor/summernote/summernote-bs4.js') }}"></script>
    <script>
        $(document).ready(function() {
            $('#summernote').summernote({
                height: 300,
                direction: 'rtl'
            });

            $('#cat_id').change(function() {
                var catId = $(this).val();
                if (catId) {
                    $.ajax({
                        url: '{{ route('poetries.getSubCategories') }}',
                        type: 'GET',
                        data: { cat_id: catId },
                        success: function(data) {
                            var options = '<option value="">-- اختیاري --</option>';
                            $.each(data, function(i, item) {
                                options += '<option value="' + item.id + '">' + item.title_ps + '</option>';
                            });
                            $('#scat_id').html(options);
                            // Preselect if current value exists in new options
                            var currentScat = '{{ $data->scat_id }}';
                            if (currentScat) {
                                $('#scat_id').val(currentScat);
                            }
                        }
                    });
                } else {
                    $('#scat_id').html('<option value="">-- اختیاري --</option>');
                }
            });
        });

        $(".app-menu").removeClass("active");
        $("#nav_poetriesi").addClass("active");
        $("#poetriesi").addClass("is-expanded");
        $("#poetries_history").addClass("active");
    </script>
@endsection
