
<?php $__env->startSection('title', 'جزئیات کاربر'); ?>

<?php $__env->startSection('content'); ?>
<div class="container-xxl flex-grow-1 container-p-y">

    <div class="d-flex align-items-center justify-content-between mb-4">
        <div>
            <h4 class="mb-1"><?php echo e($user->first_name); ?> <?php echo e($user->last_name); ?></h4>
            <p class="text-muted mb-0 small font-monospace" dir="ltr"><?php echo e($user->mobile); ?></p>
        </div>
        <a href="<?php echo e(route('admin.users.index')); ?>" class="btn btn-outline-secondary">
            <i class="ti ti-arrow-right ti-xs me-1"></i>بازگشت به لیست
        </a>
    </div>

    <?php if(session('success')): ?>
    <div class="alert alert-success alert-dismissible mb-4" role="alert">
        <?php echo e(session('success')); ?>

        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php endif; ?>

    <?php if($errors->any()): ?>
    <div class="alert alert-danger alert-dismissible mb-4" role="alert">
        <ul class="mb-0 ps-3">
            <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $e): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <li><?php echo e($e); ?></li>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </ul>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php endif; ?>

    <div class="row gy-4">

        
        <div class="col-lg-4">
            <div class="card h-100">
                <div class="card-header d-flex align-items-center gap-2">
                    <i class="ti ti-user ti-md text-primary"></i>
                    <h5 class="mb-0">اطلاعات کاربر</h5>
                </div>
                <div class="card-body">
                    <form method="POST" action="<?php echo e(route('admin.users.update', $user->mobile)); ?>" class="mb-3">
                        <?php echo csrf_field(); ?>
                        <?php echo method_field('PUT'); ?>

                        <div class="mb-3">
                            <label class="form-label" for="userFirstName">نام</label>
                            <input type="text" name="first_name" id="userFirstName"
                                   class="form-control <?php $__errorArgs = ['first_name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                   value="<?php echo e(old('first_name', $user->first_name)); ?>" required maxlength="100">
                            <?php $__errorArgs = ['first_name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                            <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="mb-3">
                            <label class="form-label" for="userLastName">نام خانوادگی</label>
                            <input type="text" name="last_name" id="userLastName"
                                   class="form-control <?php $__errorArgs = ['last_name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                   value="<?php echo e(old('last_name', $user->last_name)); ?>" required maxlength="100">
                            <?php $__errorArgs = ['last_name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                            <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="mb-3">
                            <label class="form-label" for="userMobile">شماره موبایل</label>
                            <input type="text" name="mobile" id="userMobile" dir="ltr"
                                   class="form-control font-monospace <?php $__errorArgs = ['mobile'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                   value="<?php echo e(old('mobile', $user->mobile)); ?>" required maxlength="11"
                                   pattern="09[0-9]{9}" inputmode="numeric">
                            <?php $__errorArgs = ['mobile'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                            <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                            <div class="form-text">فرمت: 09xxxxxxxxx</div>
                        </div>

                        <button type="submit" class="btn btn-primary w-100">
                            <i class="ti ti-device-floppy ti-xs me-1"></i>ذخیره اطلاعات
                        </button>
                    </form>

                    <dl class="row mb-0 small">
                        <dt class="col-5 text-muted fw-normal">نقش</dt>
                        <dd class="col-7">
                            <span class="badge bg-label-<?php echo e($user->role === 'admin' ? 'dark' : 'secondary'); ?>">
                                <?php echo e($user->role === 'admin' ? 'مدیر' : 'کاربر'); ?>

                            </span>
                        </dd>

                        <dt class="col-5 text-muted fw-normal">تأیید موبایل</dt>
                        <dd class="col-7">
                            <?php if($user->mobile_verify): ?>
                            <span class="badge bg-label-success">تأیید شده</span>
                            <?php else: ?>
                            <span class="badge bg-label-warning">تأیید نشده</span>
                            <?php endif; ?>
                        </dd>

                        <dt class="col-5 text-muted fw-normal">وضعیت</dt>
                        <dd class="col-7">
                            <?php if($user->status): ?>
                            <span class="badge bg-label-success">فعال</span>
                            <?php else: ?>
                            <span class="badge bg-label-danger">غیرفعال</span>
                            <?php endif; ?>
                        </dd>

                        <dt class="col-5 text-muted fw-normal">تاریخ عضویت</dt>
                        <dd class="col-7"><?php echo e($user->created_at?->format('Y-m-d H:i')); ?></dd>
                    </dl>

                    <hr>

                    <a href="<?php echo e(route('admin.wallets.show', $user)); ?>" class="btn btn-outline-primary w-100 mb-2">
                        <i class="ti ti-wallet ti-xs me-1"></i>مدیریت کیف پول
                    </a>

                    <form method="POST" action="<?php echo e(route('admin.users.toggle-status', $user->mobile)); ?>">
                        <?php echo csrf_field(); ?>
                        <?php if($user->status): ?>
                        <button type="submit" class="btn btn-outline-danger w-100">
                            <i class="ti ti-user-off ti-xs me-1"></i>غیرفعال کردن کاربر
                        </button>
                        <?php else: ?>
                        <button type="submit" class="btn btn-outline-success w-100">
                            <i class="ti ti-user-check ti-xs me-1"></i>فعال کردن کاربر
                        </button>
                        <?php endif; ?>
                    </form>

                    <?php if($user->role !== 'admin'): ?>
                    <form method="POST" action="<?php echo e(route('admin.users.make-admin', $user->mobile)); ?>" id="makeAdminForm" class="mt-2">
                        <?php echo csrf_field(); ?>
                        <button type="button" class="btn btn-outline-dark w-100" id="makeAdminBtn">
                            <i class="ti ti-shield-check ti-xs me-1"></i>تبدیل به مدیر (ادمین)
                        </button>
                    </form>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        
        <div class="col-lg-8">
            <div class="row g-3 mb-4">
                <div class="col-sm-4">
                    <div class="card">
                        <div class="card-body text-center py-3">
                            <div class="text-muted small mb-1">تعداد سفارش‌ها</div>
                            <h4 class="mb-0"><?php echo e(number_format($ordersCount)); ?></h4>
                        </div>
                    </div>
                </div>
                <div class="col-sm-4">
                    <div class="card">
                        <div class="card-body text-center py-3">
                            <div class="text-muted small mb-1">مجموع خرید موفق</div>
                            <h4 class="mb-0"><?php echo e(number_format($paidOrdersSum)); ?> <small class="text-muted fs-6">تومان</small></h4>
                        </div>
                    </div>
                </div>
                <div class="col-sm-4">
                    <div class="card">
                        <div class="card-body text-center py-3">
                            <div class="text-muted small mb-1">موجودی کیف پول</div>
                            <h4 class="mb-0"><?php echo e(number_format($walletBalance)); ?> <small class="text-muted fs-6">تومان</small></h4>
                        </div>
                    </div>
                </div>
            </div>

            <div class="card">
                <div class="card-header d-flex align-items-center gap-2">
                    <i class="ti ti-shopping-cart ti-md text-primary"></i>
                    <h5 class="mb-0">سفارش‌های کاربر</h5>
                </div>
                <div class="card-body p-0">
                    <?php if($user->orders->isEmpty()): ?>
                    <div class="text-center py-5 text-muted">
                        <i class="ti ti-shopping-cart-off ti-lg mb-2 d-block"></i>
                        این کاربر هنوز سفارشی ثبت نکرده است.
                    </div>
                    <?php else: ?>
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>شماره سفارش</th>
                                    <th>مبلغ قابل پرداخت</th>
                                    <th>وضعیت</th>
                                    <th>تاریخ</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $user->orders; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $order): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <tr style="cursor:pointer" onclick="window.location='<?php echo e(route('admin.orders.show', $order)); ?>'">
                                    <td class="font-monospace small fw-medium" dir="ltr"><?php echo e($order->order_number); ?></td>
                                    <td class="fw-bold"><?php echo e(number_format($order->payable_amount)); ?> تومان</td>
                                    <td>
                                        <span class="badge bg-label-<?php echo e($order->statusColor()); ?>"><?php echo e($order->statusLabel()); ?></span>
                                    </td>
                                    <td class="text-muted small"><?php echo e($order->shamsi_date); ?></td>
                                    <td onclick="event.stopPropagation()">
                                        <a href="<?php echo e(route('admin.orders.show', $order)); ?>" class="btn btn-sm btn-outline-primary">
                                            <i class="ti ti-eye ti-xs me-1"></i>مشاهده
                                        </a>
                                    </td>
                                </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

    </div>

</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('customjs'); ?>
<?php if($user->role !== 'admin'): ?>
<script>
(function () {
    var btn = document.getElementById('makeAdminBtn');
    var form = document.getElementById('makeAdminForm');
    if (!btn || !form) return;

    btn.addEventListener('click', function () {
        if (typeof Swal === 'undefined') {
            if (confirm('آیا مطمئن هستید این کاربر به مدیر تبدیل شود؟')) {
                form.submit();
            }
            return;
        }

        Swal.fire({
            icon: 'warning',
            title: 'تبدیل به مدیر؟',
            html: 'کاربر <strong><?php echo e($user->first_name); ?> <?php echo e($user->last_name); ?></strong> به نقش <strong>مدیر</strong> تبدیل می‌شود و به پنل ادمین دسترسی پیدا می‌کند.',
            showCancelButton: true,
            confirmButtonText: 'بله، تبدیل کن',
            cancelButtonText: 'انصراف',
            confirmButtonColor: '#7367f0',
            reverseButtons: true,
            focusCancel: true,
        }).then(function (result) {
            if (result.isConfirmed) {
                form.submit();
            }
        });
    });
})();
</script>
<?php endif; ?>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('admin.layouts.layout', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/louishop/public_html/engine/resources/views/admin/users/show.blade.php ENDPATH**/ ?>