<?php $__env->startSection('title', __('messages.branches')); ?>

<?php $__env->startSection('content'); ?>
<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="fw-bold mb-0">Branch Management</h5>
        <span class="text-muted fs-xs">Setup Company Branches, Invoice Prefixes & Default Warehouses</span>
    </div>
    <button class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#createBranchModal">
        <i class="bi bi-plus-lg me-1"></i> Add New Branch
    </button>
</div>

<div class="card-erp">
    <div class="table-responsive">
        <table class="table table-dense table-hover mb-0">
            <thead>
                <tr>
                    <th>Branch Code</th>
                    <th>Name (EN)</th>
                    <th>Name (AR)</th>
                    <th>Invoice Prefix</th>
                    <th>Manager</th>
                    <th>City</th>
                    <th>Status</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php $__empty_1 = true; $__currentLoopData = $branches; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $branch): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <tr>
                        <td><code class="fw-bold text-primary"><?php echo e($branch->code); ?></code></td>
                        <td class="fw-bold text-dark"><?php echo e($branch->name_en); ?></td>
                        <td><?php echo e($branch->name_ar ?? '-'); ?></td>
                        <td><span class="badge bg-secondary font-monospace"><?php echo e($branch->invoice_prefix); ?></span></td>
                        <td><?php echo e($branch->manager_name ?? '-'); ?></td>
                        <td><?php echo e($branch->city ?? '-'); ?></td>
                        <td>
                            <?php if($branch->status === 'active'): ?>
                                <span class="badge bg-success">Active</span>
                            <?php else: ?>
                                <span class="badge bg-danger">Inactive</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-end">
                            <button class="btn btn-dense btn-outline-primary me-1" data-bs-toggle="modal" data-bs-target="#editBranchModal<?php echo e($branch->id); ?>">
                                <i class="bi bi-pencil"></i> Edit
                            </button>
                            <form action="<?php echo e(route('branches.destroy', $branch->id)); ?>" method="POST" class="d-inline" onsubmit="return confirm('Delete branch?')">
                                <?php echo csrf_field(); ?>
                                <?php echo method_field('DELETE'); ?>
                                <button type="submit" class="btn btn-dense btn-outline-danger"><i class="bi bi-trash"></i></button>
                            </form>
                        </td>
                    </tr>

                    <!-- Edit Modal -->
                    <div class="modal fade" id="editBranchModal<?php echo e($branch->id); ?>" tabindex="-1">
                        <div class="modal-dialog modal-lg">
                            <div class="modal-content">
                                <form method="POST" action="<?php echo e(route('branches.update', $branch->id)); ?>">
                                    <?php echo csrf_field(); ?>
                                    <?php echo method_field('PUT'); ?>
                                    <div class="modal-header py-2">
                                        <h6 class="modal-title fw-bold">Edit Branch: <?php echo e($branch->name_en); ?></h6>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                    </div>
                                    <div class="modal-body">
                                        <div class="row g-3">
                                            <div class="col-md-4">
                                                <label class="form-label small fw-semibold">Branch Code</label>
                                                <input type="text" name="code" class="form-control form-control-sm" value="<?php echo e($branch->code); ?>" required>
                                            </div>
                                            <div class="col-md-4">
                                                <label class="form-label small fw-semibold">Branch Name (EN)</label>
                                                <input type="text" name="name_en" class="form-control form-control-sm" value="<?php echo e($branch->name_en); ?>" required>
                                            </div>
                                            <div class="col-md-4">
                                                <label class="form-label small fw-semibold">Branch Name (AR)</label>
                                                <input type="text" name="name_ar" class="form-control form-control-sm" value="<?php echo e($branch->name_ar); ?>">
                                            </div>
                                            <div class="col-md-4">
                                                <label class="form-label small fw-semibold">Invoice Prefix (e.g. JED)</label>
                                                <input type="text" name="invoice_prefix" class="form-control form-control-sm" value="<?php echo e($branch->invoice_prefix); ?>" required>
                                            </div>
                                            <div class="col-md-4">
                                                <label class="form-label small fw-semibold">Manager Name</label>
                                                <input type="text" name="manager_name" class="form-control form-control-sm" value="<?php echo e($branch->manager_name); ?>">
                                            </div>
                                            <div class="col-md-4">
                                                <label class="form-label small fw-semibold">Status</label>
                                                <select name="status" class="form-select form-select-sm">
                                                    <option value="active" <?php echo e($branch->status === 'active' ? 'selected' : ''); ?>>Active</option>
                                                    <option value="inactive" <?php echo e($branch->status === 'inactive' ? 'selected' : ''); ?>>Inactive</option>
                                                </select>
                                            </div>
                                            <div class="col-md-4">
                                                <label class="form-label small fw-semibold">City</label>
                                                <input type="text" name="city" class="form-control form-control-sm" value="<?php echo e($branch->city); ?>">
                                            </div>
                                            <div class="col-md-4">
                                                <label class="form-label small fw-semibold">Phone</label>
                                                <input type="text" name="phone" class="form-control form-control-sm" value="<?php echo e($branch->phone); ?>">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="modal-footer py-2">
                                        <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">Close</button>
                                        <button type="submit" class="btn btn-sm btn-primary">Update Branch</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr><td colspan="8" class="text-center py-3 text-muted">No branches found.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- Create Branch Modal -->
<div class="modal fade" id="createBranchModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <form method="POST" action="<?php echo e(route('branches.store')); ?>">
                <?php echo csrf_field(); ?>
                <div class="modal-header py-2">
                    <h6 class="modal-title fw-bold">Create New Branch</h6>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Branch Code <span class="text-danger">*</span></label>
                            <input type="text" name="code" class="form-control form-control-sm" placeholder="e.g. JED" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Branch Name (EN) <span class="text-danger">*</span></label>
                            <input type="text" name="name_en" class="form-control form-control-sm" placeholder="Jeddah Main Branch" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Branch Name (AR)</label>
                            <input type="text" name="name_ar" class="form-control form-control-sm" placeholder="فرع جدة الرئيسي">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Invoice Prefix <span class="text-danger">*</span></label>
                            <input type="text" name="invoice_prefix" class="form-control form-control-sm" placeholder="JED" required>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Manager Name</label>
                            <input type="text" name="manager_name" class="form-control form-control-sm">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Status</label>
                            <select name="status" class="form-select form-select-sm">
                                <option value="active">Active</option>
                                <option value="inactive">Inactive</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="modal-footer py-2">
                    <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-sm btn-primary">Save Branch</button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\saudi\resources\views\settings\branches\index.blade.php ENDPATH**/ ?>