<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
    <link rel="icon" type="image/png" sizes="16x16" href="<?php echo e(asset('backend/assets/img/kyamclogo.png')); ?>">
    <link rel="stylesheet" href="<?php echo e(asset('backend/assets/css/pace.css')); ?>">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title><?php echo $__env->yieldContent('title'); ?> - KYAMCH JOURNAL</title>
    <meta name="csrf-token" content="<?php echo e(csrf_token()); ?>">
    <!-- CSS -->
    <link href="https://fonts.googleapis.com/css?family=Poppins:200,300,400,500,600" rel="stylesheet" type="text/css">
    <link href="<?php echo e(asset('backend/assets/vendors/material-icons/material-icons.css')); ?>" rel="stylesheet"
        type="text/css">
    <link href="<?php echo e(asset('backend/assets/vendors/mono-social-icons/monosocialiconsfont.css')); ?>" rel="stylesheet"
        type="text/css">
    <link href="<?php echo e(asset('backend/assets/vendors/feather-icons/feather.css')); ?>" rel="stylesheet" type="text/css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.perfect-scrollbar/1.4.0/css/perfect-scrollbar.min.css"
        rel="stylesheet" type="text/css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"
        type="text/css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css" rel="stylesheet"
        type="text/css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/jqvmap/1.5.1/jqvmap.min.css" rel="stylesheet" type="text/css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/magnific-popup.min.css" rel="stylesheet"
        type="text/css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">
    <link href="<?php echo e(asset('backend/assets/css/style.css')); ?>" rel="stylesheet" type="text/css">
    <!-- Head Libs -->
    <script src="<?php echo e(asset('backend/assets/js/modernizr.min.js')); ?>"></script>
    <!-- SweetAlert2 CDN -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script data-pace-options='{ "ajax": false, "selectors": [ "img" ]}'
        src="<?php echo e(asset('backend/assets/js/pace.min.js')); ?>"></script>
    <?php echo $__env->yieldPushContent('backend_styles'); ?>
</head>

<body class="sidebar-dark sidebar-expand navbar-brand-dark content-dark right-sidebar-dark">
    <div id="wrapper" class="wrapper">
        <!-- HEADER & TOP NAVIGATION -->
        <?php echo $__env->make('backend.layouts.nav', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
        <!-- /.navbar -->
        <div class="content-wrapper">
            <!-- SIDEBAR -->
            <?php echo $__env->make('backend.layouts.sidebar', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
            <!-- /.site-sidebar -->
            <main class="main-wrapper clearfix">
                <?php echo $__env->yieldContent('backend_content'); ?>
            </main>
            <!-- /.main-wrappper -->


        </div>
        <!-- /.content-wrapper -->
        <!-- FOOTER -->
        <?php echo $__env->make('backend.layouts.footer', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
        <!-- My Profile Edit Modal -->
        <div class="modal fade" id="myProfileModal" tabindex="-1" role="dialog" aria-hidden="true"
            data-backdrop="static">
            <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
                <div class="modal-content" style="border-radius: 12px; border: none;">
                    <div class="modal-header bg-light border-bottom-0 pb-3 pt-4 px-4">
                        <h5 class="modal-title font-weight-bold text-dark">Edit My Profile</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <form id="myProfileForm" enctype="multipart/form-data">
                        <?php echo csrf_field(); ?>
                        <div class="modal-body px-4">

                            <!-- Profile Photo Section -->
                            <div class="d-flex align-items-center mb-4 p-3 bg-light rounded"
                                style="border: 1px solid #e2e8f0;">
                                <div class="mr-3"
                                    style="width: 70px; height: 70px; border-radius: 50%; overflow: hidden; border: 2px solid #cbd5e1;">
                                    <?php
                                        $authUser = auth()->user();
                                        $hasProfileImg = $authUser && $authUser->user_image && file_exists(public_path('uploads/users/' . $authUser->user_image));
                                        $profileImgUrl = $hasProfileImg ? asset('uploads/users/' . $authUser->user_image) : 'https://ui-avatars.com/api/?name=' . urlencode($authUser->first_name . ' ' . $authUser->last_name) . '&background=6366f1&color=ffffff';
                                    ?>
                                    <img id="profile-preview-img" src="<?php echo e($profileImgUrl); ?>"
                                        style="width: 100%; height: 100%; object-fit: cover;">
                                </div>
                                <div>
                                    <label class="font-weight-bold text-dark mb-1">Change Profile Photo</label>
                                    <input type="file" class="form-control-file" name="user_image" accept="image/*"
                                        onchange="previewProfileImage(this)">
                                    <small class="text-muted">Allowed: jpg, jpeg, png, webp (Max: 2MB)</small>
                                </div>
                            </div>

                            <!-- First & Last Name -->
                            <div class="row">
                                <div class="col-md-6 form-group mb-3">
                                    <label class="font-weight-bold text-dark">First Name <span
                                            class="text-danger">*</span></label>
                                    <input type="text" class="form-control" name="first_name"
                                        value="<?php echo e(auth()->user()->first_name); ?>" required>
                                </div>
                                <div class="col-md-6 form-group mb-3">
                                    <label class="font-weight-bold text-dark">Last Name <span
                                            class="text-danger">*</span></label>
                                    <input type="text" class="form-control" name="last_name"
                                        value="<?php echo e(auth()->user()->last_name); ?>" required>
                                </div>
                            </div>

                            <!-- Username & Email (Disabled Email) -->
                            <div class="row">
                                <div class="col-md-6 form-group mb-3">
                                    <label class="font-weight-bold text-dark">Username <span
                                            class="text-danger">*</span></label>
                                    <input type="text" class="form-control" name="username"
                                        value="<?php echo e(auth()->user()->username); ?>" required>
                                </div>
                                <div class="col-md-6 form-group mb-3">
                                    <label class="font-weight-bold text-dark">Email Address <small
                                            class="text-muted">(Read Only)</small></label>
                                    <input type="email" class="form-control bg-light"
                                        value="<?php echo e(auth()->user()->email); ?>" readonly style="cursor: not-allowed;">
                                </div>
                            </div>

                            <!-- New Password -->
                            <div class="form-group mb-3">
                                <label class="font-weight-bold text-dark">New Password <small class="text-muted">(Leave
                                        blank if you don't want to change)</small></label>
                                <input type="password" class="form-control" name="password"
                                    placeholder="Enter new password (min 6 characters)">
                            </div>
                        </div>

                        <div class="modal-footer border-top-0 px-4 pb-4">
                            <button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
                            <button type="submit" class="btn btn-primary" id="updateProfileBtn">Save Changes</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <!--/ #wrapper -->
    <!-- Scripts -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/metisMenu/2.7.9/metisMenu.min.js"></script>
    <script
        src="https://cdnjs.cloudflare.com/ajax/libs/jquery.perfect-scrollbar/1.4.0/perfect-scrollbar.min.js"></script>
    <script src="<?php echo e(asset('backend/assets/js/bootstrap.min.js')); ?>"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/countup.js/1.9.2/countUp.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-sparklines/2.1.2/jquery.sparkline.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqvmap/1.5.1/jquery.vmap.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqvmap/1.5.1/maps/jquery.vmap.usa.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/jquery.magnific-popup.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
    <script src="<?php echo e(asset('backend/assets/js/template.js')); ?>"></script>
    <script src="<?php echo e(asset('backend/assets/js/custom.js')); ?>"></script>
    <script>
        // Optional: configure toastr options
        toastr.options = {
            "closeButton": true,
            "progressBar": true,
            "positionClass": "toast-top-right",
            "timeOut": "3000"
        };

        // Check if Laravel session has the 'success' key
        <?php if(Session::has('success')): ?>
            toastr.success("<?php echo e(Session::get('success')); ?>");
        <?php endif; ?>

        // You can also add other alerts like error or warning while you are here
        <?php if(Session::has('error')): ?>
            toastr.error("<?php echo e(Session::get('error')); ?>");
        <?php endif; ?>
    </script>
    <script>
        // Global AJAX Setup for CSRF
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        // GLOBAL AJAX ERROR HANDLER (অটোমেটিক কাজ করবে পুরো প্রজেক্টে)
        $(document).ajaxError(function (event, xhr, settings, thrownError) {

            // ১. যদি ইউজার এর পারমিশন না থাকে (Status 403)
            if (xhr.status === 403) {
                $('.modal').modal('hide'); // খোলা থাকা যেকোনো Modal অটোমেটিক বন্ধ হয়ে যাবে

                let errorMsg = 'You do not have permission to perform this action.';
                if (xhr.responseJSON && xhr.responseJSON.message) {
                    errorMsg = xhr.responseJSON.message;
                }

                Swal.fire({
                    icon: 'error',
                    title: 'Access Denied! 🚫',
                    text: errorMsg,
                    footer: '<small class="text-muted">Please contact your administrator to request access.</small>',
                    confirmButtonColor: '#d33',
                    confirmButtonText: 'Understood'
                });
            }

            // ২. যদি রাউট বা ডেটা না পাওয়া যায় (Status 404)
            else if (xhr.status === 404) {
                $('.modal').modal('hide');
                Swal.fire({
                    icon: 'warning',
                    title: 'Not Found! 🔍',
                    text: 'The requested page or resource could not be found.',
                    confirmButtonColor: '#f39c12'
                });
            }

            // ৩. যদি সার্ভার ক্র্যাশ করে বা অন্য কোনো এরর হয় (Status 500)
            else if (xhr.status === 500) {
                $('.modal').modal('hide');
                Swal.fire({
                    icon: 'error',
                    title: 'Server Error! 💥',
                    text: 'Something went wrong on the server. Please try again later.',
                    confirmButtonColor: '#d33'
                });
            }
        });
    </script>
    <script>
        function editMyProfile() {
            $('#myProfileModal').modal('show');
        }

        function previewProfileImage(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();
                reader.onload = function (e) {
                    $('#profile-preview-img').attr('src', e.target.result);
                }
                reader.readAsDataURL(input.files[0]);
            }
        }

        $('#myProfileForm').submit(function (e) {
            e.preventDefault();
            let formData = new FormData(this);
            let btn = $('#updateProfileBtn');
            let originalText = btn.html();
            btn.html('<i class="fa fa-spinner fa-spin mr-1"></i> Saving...').prop('disabled', true);

            $.ajax({
                type: "POST",
                url: "<?php echo e(route('admin.users.profile.update')); ?>",
                data: formData,
                contentType: false,
                processData: false,
                success: function (response) {
                    btn.html(originalText).prop('disabled', false);
                    $('#myProfileModal').modal('hide');
                    Swal.fire({
                        icon: 'success',
                        title: 'Success!',
                        text: response.success,
                        timer: 1500,
                        showConfirmButton: false
                    }).then(() => {
                        location.reload();
                    });
                },
                error: function (xhr) {
                    btn.html(originalText).prop('disabled', false);
                    let errorMsg = 'Something went wrong!';
                    if (xhr.status === 422 && xhr.responseJSON.errors) {
                        errorMsg = Object.values(xhr.responseJSON.errors)[0][0];
                    }
                    Swal.fire({ icon: 'error', title: 'Oops...', text: errorMsg });
                }
            });
        });
    </script>
    <?php echo $__env->yieldPushContent('backend_scripts'); ?>
</body>

</html><?php /**PATH I:\laragon\www\kyamchjournal\resources\views/backend/master.blade.php ENDPATH**/ ?>