<?php $__env->startSection('title'); ?> Edit Product
<?php $__env->stopSection(); ?>
<?php $__env->startSection('content'); ?>
    <style>
        .active {
            background-color: #007BFF !important;
            color: white !important;
        }
    </style>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Main content -->
        <section class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-12">

                        <div class="card card-box-shadow mt-5">

                            <div class="card-header">
                                <h3 class="card-title">Edit Product</h3>
                                <a href="<?php echo e(route('admin.product.index')); ?>"
                                 >
                                    <button id="submit_btn" class="btn btn-primary custom_btn_sm_back custom_btn btn-sm float-right">
                                        <i class="fas fa-arrow-circle-left"></i>
                                        Back
                                    </button>

                                </a>
                            </div>

                            <!-- /.card-header -->
                            <div class="card-body">

                                <form action="<?php echo e(route('admin.product.update',$find_product->id)); ?>" method="POST"
                                      enctype="multipart/form-data">
                                    <?php echo csrf_field(); ?>
                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label for="Name">Product Name:</label>
                                                <input type="text" class="form-control form__field" name="product_name"
                                                value="<?php echo e($find_product->product_name ? : ''); ?>">
                                            </div>
                                            <small class="text-danger">
                                                <?php $__errorArgs = ['product_name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                <?php echo e($message); ?>

                                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                            </small>
                                        </div>

                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label for="category">Select Categroy:</label>
                                                <select id="category" class="form-control form__field"
                                                        name="category_name">
                                                    <option value="" selected disabled>Select Category</option>

                                                    <?php $__empty_1 = true; $__currentLoopData = $get_categories; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $category): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                                        <option value="<?php echo e($category->id ?: ''); ?>"<?php echo e($category->id == $find_product->category_id ? 'selected' : ''); ?>>
                                                            <?php echo e($category->name ?: ''); ?></option>

                                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                                        <option>Category Not Found</option>
                                                    <?php endif; ?>
                                                </select>

                                                <small class="text-danger">
                                                    <?php $__errorArgs = ['category_name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                    <?php echo e($message); ?>

                                                    <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                                </small>
                                            </div>
                                        </div>



                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label for="Name">Product Price:</label>
                                                <input type="number" class="form-control form__field" name="price"
                                                       value="<?php echo e($find_product->price ? : ''); ?>">
                                            </div>
                                            <small class="text-danger">
                                                <?php $__errorArgs = ['price'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                <?php echo e($message); ?>

                                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                            </small>
                                        </div>

                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label for="Name">Product Discount Price:</label>
                                                <input type="number" class="form-control form__field" name="discount"
                                                       value="<?php echo e($find_product->discount ? : ''); ?>"
                                                       oninput="this.value = this.value.replace(/[^0-9]/g, '')">
                                            </div>
                                        </div>


                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label for="Name">Product stock :</label>
                                                <input type="number" class="form-control form__field" name="stock"
                                                       value="<?php echo e($find_product->stock ? : ''); ?>"
                                                       oninput="this.value = this.value.replace(/[^0-9]/g, '')">
                                            </div>
                                            <small class="text-danger">
                                                <?php $__errorArgs = ['stock'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                <?php echo e($message); ?>

                                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                            </small>
                                        </div>

                                        <div class="col-md-6">
                                            <div class="col-md-6"style="display:flex; gap:20px;">
                                                <label for="Name">Product Best Sale :</label>
                                                <label for="Name">Product Exclusive :</label>
                                                <label for="Name">Product Essential :</label>
                                            </div>

                                            <div class="form-group"style=" display:flex; gap:50px; background: #fff; font: inherit; box-shadow: rgba(0, 0, 0, 0.1) 0px 1px 10px 2px;outline: 0; border-radius: 20px; min-height: 50px;padding: 7px;">

                                                <input type="checkbox" data-toggle="toggle" data-on="Sale"
                                                       data-off="Off" data-onstyle="success"
                                                       data-offstyle="danger" name="best_sale" <?php echo e($find_product->best_sale === 1 ? 'checked': ''); ?>>

                                                <input type="checkbox" data-toggle="toggle" data-on="Exclusive"
                                                       data-off="Off" data-onstyle="success"
                                                       data-offstyle="danger" name="arrival" <?php echo e($find_product->arrival === 1 ? 'checked': ''); ?>>

                                                <input type="checkbox" data-toggle="toggle" data-on="Essential"
                                                       data-off="Off" data-onstyle="success"
                                                       data-offstyle="danger" name="essential" <?php echo e($find_product->essential === 1 ? 'checked': ''); ?>>
                                            </div>

                                        </div>


                                        <div class="col-md-12 mt-4">
                                            <div class="form-group">
                                                <label for="mainProductImageInput">Main Product Image:</label>
                                                <input type="file" class="form-control form__field visually-hidden"
                                                       name="product_image"
                                                       id="mainProductImageInput" accept="image/*">
                                                <label for="mainProductImageInput"
                                                       class="file-input-label form__field form-control">
                                                    <i class="fas fa-camera"></i> Choose file
                                                </label>

                                                <small class='text-danger'>
                                                    <?php $__errorArgs = ['product_image'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                    <?php echo e($message); ?>

                                                    <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                                </small>

                                            </div>

                                            <img id="mainProductImagePreview" src="<?php echo e($find_product->getFirstMediaUrl('product_image') ?: ''); ?>"
                                                 alt="Profile Preview"
                                                 style="width: 250px; height: 200px; margin-top: 10px;">

                                        </div>

                                        <div class="col-md-12 mt-5">
                                            <div class="form-group pt-4">
                                                <div class="card card-box-shadow">
                                                    <h5 class="card-header bg-dark text-white">Image Gallery
                                                        <button type="button"
                                                                class="btn btn-sm btn-outline-light float-right addImage">
                                                            <i class="la la-fw la-plus"></i>Add More Image
                                                        </button>
                                                    </h5>

                                                    <div class="card-body">
                                                        <div class="row addedField ml-5">
                                                            <?php $__currentLoopData = $find_product->getMedia('product_gallery_image'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $image): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                                <div class="image-data" data-index="<?php echo e($index); ?>">
                                                                    <div class="card shadow" style="width:300px;margin-right:20px;margin-bottom:15px;">
                                                                        <div class="image-upload">
                                                                            <div class="avatar-preview">
                                                                                <img src="<?php echo e($image->getUrl()); ?>" class="card-img-top profilePicPreview" id="profile-pic-show-<?php echo e($index); ?>" style="height:200px;">
                                                                            </div>
                                                                            <div class="card-body">
                                                                                <div class="avatar-edit my-2">
                                                                                    <label for="id-<?php echo e($index); ?>" class="btn btn-success btn-lg w-100"><?php echo app('translator')->get('Change Image'); ?></label>
                                                                                    <button class="btn btn-danger btn-lg removeBtn w-100" type="button" data-index="<?php echo e($index); ?>" data-images="<?php echo e($image->id); ?>"><?php echo app('translator')->get('Remove Image'); ?></button>
                                                                                </div>
                                                                                <input type="file" id="id-<?php echo e($index); ?>" class="changeImageBtn" name="product_gallery_images[]" accept=".png, .jpg, .jpeg" data-old-images="<?php echo e($image->id); ?>" style="display: none;">
                                                                            </div>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                        </div>
                                                    </div>
                                                </div>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="col-md-12 mt-5">
                                            <div class="form-group pt-4">
                                                <div class="card card-box-shadow">
                                                    <h5 class="card-header bg-dark text-white">Add Product Variation
                                                        <button type="button" class="btn btn-sm btn-outline-light float-right addProductVariation">
                                                            <i class="la la-fw la-plus"></i> Add Product Variation
                                                        </button>
                                                    </h5>
                                                    <div class="card-body addProductVariationField">
                                                        <!-- Existing Variations -->
                                                        <?php if(isset($find_product_variation)): ?>
                                                            <?php $__currentLoopData = $find_product_variation; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $product_variation): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                                <div class="row form-section product-variation-data mb-4 mt-4" id="formSection<?php echo e($index); ?>">
                                                                    <input type="hidden" name="primary_id[]" value="<?php echo e($product_variation->id); ?>">
                                                                    <div class="col-md-12">
                                                                        <button type="button" class="custom_btn_sm_back custom_btn btn-sm float-right productVariationRemoveBtn"
                                                                                onclick="removeFormSection(this)" data-get-remove-section="<?php echo e($product_variation->id); ?>">
                                                                            <i class="fas fa-trash-alt"></i> Remove
                                                                        </button>
                                                                    </div>
                                                                    <div class="col-md-3 mt-4">
                                                                        <div class="form-group">
                                                                            <label>Select Size:</label>
                                                                            <select class="form-control form__field" name="size_id[]">
                                                                                <option value="" disabled>Select Size</option>
                                                                                <?php $__currentLoopData = $get_sizes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $size): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                                                    <option value="<?php echo e($size->id); ?>" <?php echo e($size->id == $product_variation->size_id ? 'selected' : ''); ?>>
                                                                                        <?php echo e($size->size_name); ?>

                                                                                    </option>
                                                                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                                            </select>
                                                                        </div>
                                                                    </div>
                                                                    <div class="col-md-3 mt-4">
                                                                        <div class="form-group">
                                                                            <label>Color Name:</label>
                                                                            <input type="text" name="color_name[]" class="form-control form__field" value="<?php echo e($product_variation->colour_name); ?>" placeholder="Enter Color Name">
                                                                        </div>
                                                                    </div>
                                                                    <div class="col-md-3 mt-4">
                                                                        <div class="form-group">
                                                                            <label>Color Code:</label>
                                                                            <input type="text" name="color_code[]" class="form-control form__field" value="<?php echo e($product_variation->color_code); ?>" placeholder="Enter Color Code">
                                                                        </div>
                                                                    </div>
                                                                    <div class="col-md-3 mt-4">
                                                                        <div class="form-group">
                                                                            <label>Size Price:</label>
                                                                            <input type="text" name="size_price[]" class="form-control form__field" value="<?php echo e($product_variation->price); ?>" placeholder="Enter Size Price">
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                        <?php else: ?>
                                                            <div class="row form-section" id="formSection0">
                                                                <div class="col-md-3 mt-4">
                                                                    <div class="form-group">
                                                                        <label>Select Size:</label>
                                                                        <select class="form-control form__field"
                                                                                name="size_id[]">
                                                                            <option value="" selected disabled>Select Size
                                                                            </option>

                                                                            <?php $__empty_1 = true; $__currentLoopData = $get_sizes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $size): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                                                                <option value="<?php echo e($size->id ?: ''); ?>">
                                                                                    <?php echo e($size->size_name ?: ''); ?></option>
                                                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                                                                <option>Size Not Found</option>
                                                                            <?php endif; ?>
                                                                        </select>
                                                                    </div>
                                                                </div>

                                                                <div class="col-md-3 mt-4">
                                                                    <div class="form-group">
                                                                        <label for="color_name[]">Color Name:</label>
                                                                        <input type="text" name="color_name[]"
                                                                               class="form-control form__field"
                                                                               placeholder="Enter Color Name">
                                                                    </div>
                                                                </div>

                                                                <div class="col-md-3 mt-4">
                                                                    <div class="form-group">
                                                                        <label for="color_code[]">Color Code:</label>
                                                                        <input type="text" name="color_code[]"
                                                                               class="form-control form__field"
                                                                               placeholder="Enter Color Code">
                                                                    </div>
                                                                </div>

                                                                <div class="col-md-3 mt-4">
                                                                    <div class="form-group">
                                                                        <label for="size_price[]">Size Price:</label>
                                                                        <input type="text" name="size_price[]"
                                                                               class="form-control form__field"
                                                                               placeholder="Enter Size Price">
                                                                    </div>
                                                                </div>
                                                            </div>

                                                        <?php endif; ?>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>


                                        <div class="col-md-12 mt-4">
                                            <label for="">Short Description</label>
                                            <textarea name="description" rows="4" placeholder="Description"
                                                      class="form-control form__field" id="summernote1"><?php echo e($find_product->description ? : ''); ?></textarea>
                                            <small class="text-danger">
                                                <?php $__errorArgs = ['description'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                <?php echo e($message); ?>

                                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                            </small>

                                        </div>

                                        <div class="mx-auto  mt-5">
                                            <div class="form-group">
                                                <button id="submit_btn" class="btn btn-primary custom_btn">
                                                    <i class="fa-solid fa-cloud-arrow-up"></i> Submit
                                                </button>
                                            </div>

                                        </div>
                                    </div>

                                </form>

                            </div>
                            <!-- /.card-body -->
                        </div>

                    </div>
                    <!-- /.col -->
                </div>
                <!-- /.row -->
            </div>
            <!-- /.container-fluid -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
<?php $__env->stopSection(); ?>

<?php $__env->startSection('script'); ?>
    <script>
       var removedImageIds = [];
        $('#summernote1').summernote({
            placeholder: 'Hello Admin',
            tabsize: 2,
            height: 100
        });
        function showImagePreview(input, previewId) {
            const preview = document.getElementById(previewId);

            if (input.files && input.files[0]) {
                const reader = new FileReader();

                reader.onload = function (e) {
                    preview.src = e.target.result;
                    preview.style.display = 'block';
                };

                reader.readAsDataURL(input.files[0]);
            }
        }

        const imageInput = document.getElementById('firstimageInput');
        imageInput.addEventListener('change', function () {
            showImagePreview(this, 'firstimagePreview');
        });
    </script>

    <script>
        function handleImagePreview(fileInput, previewId) {
            if (fileInput.files && fileInput.files[0]) {
                var reader = new FileReader();
                reader.onload = function (e) {
                    $(previewId).attr('src', e.target.result);
                };
                reader.readAsDataURL(fileInput.files[0]);
            }
        }
        $(document).on('click', '.addImage', function () {
            var randomId = Math.floor(Math.random() * 10000);
            var html = `
    <div class="image-data" data-id="${randomId}">
        <div class="card shadow" style="width:300px;margin-right:20px;margin-bottom:15px;">
            <div class="image-upload">
                <div class="avatar-preview">
                    <img src="<?php echo e(asset('upload/No-Image.png')); ?>" class="card-img-top profilePicPreview" id="profile-pic-show-${randomId}" style="height:200px;">
                </div>
                <div class="card-body">
                    <div class="avatar-edit my-2">
                        <label for="add-new-file-${randomId}" class="btn btn-success btn-lg w-100">Add New File</label>
                        <button class="btn btn-danger btn-lg removeBtn w-100" type="button">Remove Image</button>
                        <input type="file" class="profilePicUpload" name="product_gallery_images[]" id="add-new-file-${randomId}" accept=".png, .jpg, .jpeg" style="display:none;">
                    </div>
                </div>
            </div>
        </div>
    </div>`;

            $('.addedField').append(html);
            $(`#add-new-file-${randomId}`).on('change', function () {
                handleImagePreview(this, `#profile-pic-show-${randomId}`);
            });
        });

        $(document).on('click', '.removeBtn', function () {
            const imageData = $(this).closest('.image-data');
            const imageId = $(this).data('images');

            // Store removed image ID if it exists
            if (imageId) {
                removedImageIds.push(imageId);
            }

            // Remove the image data from the DOM
            imageData.remove();
        });

        // To submit the removed IDs, you can append them to a hidden input field in your form.
        $('form').on('submit', function () {
            $('<input>').attr({
                type: 'hidden',
                name: 'removed_product_images',
                value: JSON.stringify(removedImageIds)
            }).appendTo('form');
        });

        // Handle the image change
        $(document).on('change', '.changeImageBtn', function () {
            const randomId = $(this).attr('id').split('-')[1]; // Get the index from the id
            handleImagePreview(this, `#profile-pic-show-${randomId}`);
        });

        $('.addProductVariation').on('click', function () {
            var randomId = Math.floor(Math.random() * 10000);
            var newFormSection = `
            <div class="row form-section product-variation-data mb-4 mt-4" id="formSection${randomId}">
                <input type="hidden" name="primary_id[]" value="">
                <div class="col-md-12">
                    <button type='button' class="custom_btn_sm_back custom_btn btn-sm float-right productVariationRemoveBtn">
                        <i class="fas fa-trash-alt"></i> Remove
                    </button>
                </div>
                <div class="col-md-3 mt-4">
                    <div class="form-group">
                        <label>Select Size:</label>
                        <select class="form-control form__field" name="size_id[]">
                            <option value="" selected disabled>Select Size</option>
                            <?php $__currentLoopData = $get_sizes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $size): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <option value="<?php echo e($size->id); ?>"><?php echo e($size->size_name); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </select>
        </div>
    </div>
    <div class="col-md-3 mt-4">
        <div class="form-group">
            <label>Color Name:</label>
            <input type="text" name="color_name[]" class="form-control form__field" placeholder="Enter Color Name">
        </div>
    </div>
    <div class="col-md-3 mt-4">
        <div class="form-group">
            <label>Color Code:</label>
            <input type="text" name="color_code[]" class="form-control form__field" placeholder="Enter Color Code">
        </div>
    </div>
    <div class="col-md-3 mt-4">
        <div class="form-group">
            <label>Size Price:</label>
            <input type="text" name="size_price[]" class="form-control form__field" placeholder="Enter Size Price">
        </div>
    </div>
</div>`;
            $('.addProductVariationField').append(newFormSection);
        });

        let removedVariationIds = []; // Array to store removed variation IDs

        $(document).on('click', '.productVariationRemoveBtn', function () {
            const variationId = $(this).data('get-remove-section'); // Get the variation ID
            if (variationId) {
                removedVariationIds.push(variationId); // Store the removed ID
            }
            $(this).closest('.form-section').remove(); // Remove the section
        });

        // When submitting the form, append the removed IDs to the form
        $('form').on('submit', function() {
            $('<input>').attr({
                type: 'hidden',
                name: 'remove_product_variation',
                value: JSON.stringify(removedVariationIds) // Convert the array to JSON string
            }).appendTo('form');
        });
    </script>


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

<?php echo $__env->make('layouts.admin-layout', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/vagrant/projects/upsence/resources/views/admin/product/update.blade.php ENDPATH**/ ?>