
<?php $__env->startSection('title', 'قوالب واتساب'); ?>

<?php $__env->startSection('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"><?php echo e($edit ? 'تعديل قالب' : 'إضافة قالب'); ?></h3>
                </div>

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

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

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

                        <div class="form-group">
                            <label>نص الرسالة</label>
                            <textarea class="form-control" name="body" rows="6"><?php echo e(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); ?>
                            <select class="form-control" name="is_active">
                                <option value="1" <?php echo e((string)$act==='1'?'selected':''); ?>>مفعّل</option>
                                <option value="0" <?php echo e((string)$act==='0'?'selected':''); ?>>موقوف</option>
                            </select>
                        </div>

                    </div>

                    <div class="box-footer">
                        <button class="btn btn-primary"><?php echo e($edit ? 'تحديث' : 'إضافة'); ?></button>
                        <?php if($edit): ?>
                            <a class="btn btn-default" href="<?php echo e(route('whatsapp.templates')); ?>">إلغاء</a>
                        <?php 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>
                                <?php $__currentLoopData = ['welcome','invoice','marketing']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $c): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($c); ?>" <?php echo e(request('context')==$c?'selected':''); ?>><?php echo e($c); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <select class="form-control" name="active">
                                <option value="">كل الحالات</option>
                                <option value="1" <?php echo e(request('active')==='1'?'selected':''); ?>>مفعّل</option>
                                <option value="0" <?php echo e(request('active')==='0'?'selected':''); ?>>موقوف</option>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <button class="btn btn-primary">بحث</button>
                            <a class="btn btn-default" href="<?php echo e(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>
                                <?php $__currentLoopData = $templates; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $t): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <tr>
                                        <td><?php echo e($t->id); ?></td>
                                        <td><?php echo e($t->name); ?></td>
                                        <td><?php echo e($t->context); ?></td>
                                        <td><?php echo e($t->is_active ? 'مفعّل' : 'موقوف'); ?></td>
                                        <td>
                                            <a class="btn btn-xs btn-info" href="<?php echo e(route('whatsapp.templates', array_merge(request()->all(), ['edit'=>$t->id]))); ?>">تعديل</a>

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

                    <?php echo e($templates->links()); ?>

                </div>
            </div>
        </div>

    </div>
</section>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>