@extends('layouts.app')
@section('title', 'قوالب واتساب')

@section('content')
<section class="content">
    <div class="row">

        <div class="col-md-5">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title">{{ $edit ? 'تعديل قالب' : 'إضافة قالب' }}</h3>
                </div>

                <form method="post" action="{{ route('whatsapp.templates.save') }}">
                    <div class="box-body">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">
                        <input type="hidden" name="id" value="{{ $edit->id ?? '' }}">

                        <div class="form-group">
                            <label>اسم القالب</label>
                            <input class="form-control" name="name"
                                   value="{{ old('name', $edit->name ?? '') }}">
                        </div>

                        <div class="form-group">
                            <label>النوع (Context)</label>
                            @php $ctx = old('context', $edit->context ?? 'marketing'); @endphp
                            <select class="form-control" name="context">
                                <option value="welcome"  {{ $ctx=='welcome'?'selected':'' }}>welcome (ترحيب)</option>
                                <option value="invoice"  {{ $ctx=='invoice'?'selected':'' }}>invoice (فاتورة)</option>
                                <option value="marketing"{{ $ctx=='marketing'?'selected':'' }}>marketing (تسويق)</option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label>نص الرسالة</label>
                            <textarea class="form-control" name="body" rows="6">{{ old('body', $edit->body ?? '') }}</textarea>
                            <small class="text-muted">ملاحظة: المتغيرات بنضيفها بعدين (مثل {name}...)</small>
                        </div>

                        <div class="form-group">
                            <label>الحالة</label>
                            @php $act = old('is_active', $edit->is_active ?? 1); @endphp
                            <select class="form-control" name="is_active">
                                <option value="1" {{ (string)$act==='1'?'selected':'' }}>مفعّل</option>
                                <option value="0" {{ (string)$act==='0'?'selected':'' }}>موقوف</option>
                            </select>
                        </div>

                    </div>

                    <div class="box-footer">
                        <button class="btn btn-primary">{{ $edit ? 'تحديث' : 'إضافة' }}</button>
                        @if($edit)
                            <a class="btn btn-default" href="{{ route('whatsapp.templates') }}">إلغاء</a>
                        @endif
                    </div>
                </form>
            </div>
        </div>

        <div class="col-md-7">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title">القوالب</h3>
                </div>

                <div class="box-body">
                    <form method="get" class="row" style="margin-bottom:10px">
                        <div class="col-md-4">
                            <select class="form-control" name="context">
                                <option value="">كل الأنواع</option>
                                @foreach(['welcome','invoice','marketing'] as $c)
                                    <option value="{{ $c }}" {{ request('context')==$c?'selected':'' }}>{{ $c }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-4">
                            <select class="form-control" name="active">
                                <option value="">كل الحالات</option>
                                <option value="1" {{ request('active')==='1'?'selected':'' }}>مفعّل</option>
                                <option value="0" {{ request('active')==='0'?'selected':'' }}>موقوف</option>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <button class="btn btn-primary">بحث</button>
                            <a class="btn btn-default" href="{{ route('whatsapp.templates') }}">تفريغ</a>
                        </div>
                    </form>

                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>الاسم</th>
                                    <th>النوع</th>
                                    <th>الحالة</th>
                                    <th width="160">إجراءات</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($templates as $t)
                                    <tr>
                                        <td>{{ $t->id }}</td>
                                        <td>{{ $t->name }}</td>
                                        <td>{{ $t->context }}</td>
                                        <td>{{ $t->is_active ? 'مفعّل' : 'موقوف' }}</td>
                                        <td>
                                            <a class="btn btn-xs btn-info" href="{{ route('whatsapp.templates', array_merge(request()->all(), ['edit'=>$t->id])) }}">تعديل</a>

                                            <form method="post" action="{{ route('whatsapp.templates.delete', $t->id) }}" style="display:inline">
                                                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                                                <button class="btn btn-xs btn-danger" onclick="return confirm('حذف القالب؟')">حذف</button>
                                            </form>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                    {{ $templates->links() }}
                </div>
            </div>
        </div>

    </div>
</section>
@endsection