<?php $__env->startSection('title', translate('Update product')); ?>

<?php $__env->startPush('css_or_js'); ?>
    <meta name="csrf-token" content="<?php echo e(csrf_token()); ?>">
    <link href="<?php echo e(asset('public/assets/admin/css/tags-input.min.css')); ?>" rel="stylesheet">
<?php $__env->stopPush(); ?>

<?php $__env->startSection('content'); ?>
    <div class="content container-fluid">
        <!-- Page Header -->
        <div class="page-header">
            <div class="row align-items-center">
                <div class="col-sm mb-2 mb-sm-0">
                    <h1 class="page-header-title"><i
                            class="tio-edit"></i> <?php echo e(\App\CentralLogics\translate('product')); ?> <?php echo e(\App\CentralLogics\translate('update')); ?></h1>
                </div>
            </div>
        </div>
        <!-- End Page Header -->
        <div class="row gx-2 gx-lg-3">
            <div class="col-sm-12 col-lg-12 mb-3 mb-lg-2">
                <form action="javascript:" method="post" id="product_form"
                      enctype="multipart/form-data">
                    <?php echo csrf_field(); ?>
                    <?php ($language=\App\Model\BusinessSetting::where('key','language')->first()); ?>
                    <?php ($language = $language->value ?? null); ?>
                    <?php if($language): ?>
                        <ul class="nav nav-tabs mb-4">

                            <?php $__currentLoopData = json_decode($language); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $lang): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <li class="nav-item">
                                    <a class="nav-link lang_link <?php echo e($lang == 'en'? 'active':''); ?>" href="#" id="<?php echo e($lang); ?>-link"><?php echo e(\App\CentralLogics\Helpers::get_language_name($lang).'('.strtoupper($lang).')'); ?></a>
                                </li>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                        </ul>
                        <?php $__currentLoopData = json_decode($language); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $lang): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <?php
                            if(count($product['translations'])){
                                $translate = [];
                                foreach($product['translations'] as $t)
                                {

                                    if($t->locale == $lang && $t->key=="name"){
                                        $translate[$lang]['name'] = $t->value;
                                    }
                                    if($t->locale == $lang && $t->key=="description"){
                                        $translate[$lang]['description'] = $t->value;
                                    }

                                }
                            }
                            ?>
                            <div class="card p-4 <?php echo e($lang != 'en'? 'd-none':''); ?> lang_form" id="<?php echo e($lang); ?>-form">
                                <div class="form-group">
                                    <label class="input-label" for="<?php echo e($lang); ?>_name"><?php echo e(\App\CentralLogics\translate('name')); ?> (<?php echo e(strtoupper($lang)); ?>)</label>
                                    <input type="text" <?php echo e($lang == 'en'? 'required':''); ?> name="name[]" id="<?php echo e($lang); ?>_name" value="<?php echo e($translate[$lang]['name']??$product['name']); ?>" class="form-control" placeholder="New Product" >
                                </div>
                                <input type="hidden" name="lang[]" value="<?php echo e($lang); ?>">
                                <div class="form-group pt-4">
                                    <label class="input-label"
                                           for="<?php echo e($lang); ?>_description"><?php echo e(\App\CentralLogics\translate('short')); ?> <?php echo e(\App\CentralLogics\translate('description')); ?>  (<?php echo e(strtoupper($lang)); ?>)</label>
                                    <div id="<?php echo e($lang); ?>_editor" style="min-height: 15rem;"><?php echo $translate[$lang]['description']??$product['description']; ?></div>
                                    <textarea name="description[]" style="display:none" id="<?php echo e($lang); ?>_hiddenArea"></textarea>
                                </div>
                            </div>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    <?php else: ?>
                        <div class="card p-4" id="english-form">
                            <div class="form-group">
                                <label class="input-label" for="exampleFormControlInput1"><?php echo e(\App\CentralLogics\translate('name')); ?> (EN)</label>
                                <input type="text" name="name[]" value="<?php echo e($product['name']); ?>" class="form-control" placeholder="New Product" required>
                            </div>
                            <input type="hidden" name="lang[]" value="en">
                            <div class="form-group pt-4">
                                <label class="input-label"
                                       for="exampleFormControlInput1"><?php echo e(\App\CentralLogics\translate('short')); ?> <?php echo e(\App\CentralLogics\translate('description')); ?> (EN)</label>
                                <div id="editor" style="min-height: 15rem;"><?php echo $product['description']; ?></div>
                                <textarea name="description[]" style="display:none" id="hiddenArea"></textarea>
                            </div>
                        </div>
                    <?php endif; ?>
                    <div id="from_part_2">
                        <div class="row">
                            <div class="col-6">
                                <div class="form-group">
                                    <label class="input-label"
                                           for="exampleFormControlInput1"><?php echo e(\App\CentralLogics\translate('price')); ?></label>
                                    <input type="number" value="<?php echo e($product['price']); ?>" min="1" max="100000000" name="price"
                                           class="form-control" step="0.01"
                                           placeholder="Ex : 100" required>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="form-group">
                                    <label class="input-label"
                                           for="exampleFormControlInput1"><?php echo e(\App\CentralLogics\translate('unit')); ?></label>
                                    <select name="unit" class="form-control js-select2-custom">
                                        <option value="kg" <?php echo e($product['unit']=='kg'?'selected':''); ?>>
                                            <?php echo e(\App\CentralLogics\translate('kg')); ?>

                                        </option>
                                        <option value="gm" <?php echo e($product['unit']=='gm'?'selected':''); ?>>
                                            <?php echo e(\App\CentralLogics\translate('gm')); ?>

                                        </option>
                                        <option value="ltr" <?php echo e($product['unit']=='ltr'?'selected':''); ?>>
                                            <?php echo e(\App\CentralLogics\translate('ltr')); ?>

                                        </option>
                                        <option value="pc" <?php echo e($product['unit']=='pc'?'selected':''); ?>>
                                            <?php echo e(\App\CentralLogics\translate('pc')); ?>

                                        </option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="form-group">
                                    <label class="input-label"
                                           for="exampleFormControlInput1"><?php echo e(\App\CentralLogics\translate('tax')); ?></label>
                                    <input type="number" value="<?php echo e($product['tax']); ?>" min="0" max="100000" name="tax"
                                           class="form-control" step="0.01"
                                           placeholder="Ex : 7" required>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="form-group">
                                    <label class="input-label"
                                           for="exampleFormControlInput1"><?php echo e(\App\CentralLogics\translate('tax')); ?> <?php echo e(\App\CentralLogics\translate('type')); ?></label>
                                    <select name="tax_type" class="form-control js-select2-custom">
                                        <option
                                            value="percent" <?php echo e($product['tax_type']=='percent'?'selected':''); ?>><?php echo e(\App\CentralLogics\translate('percent')); ?>

                                        </option>
                                        <option
                                            value="amount" <?php echo e($product['tax_type']=='amount'?'selected':''); ?>><?php echo e(\App\CentralLogics\translate('amount')); ?>

                                        </option>
                                    </select>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-4">
                                <div class="form-group">
                                    <label class="input-label"
                                           for="exampleFormControlInput1"><?php echo e(\App\CentralLogics\translate('discount')); ?></label>
                                    <input type="number" min="0" value="<?php echo e($product['discount']); ?>" max="100000"
                                           name="discount" class="form-control"
                                           placeholder="Ex : 100" required>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="input-label"
                                           for="exampleFormControlInput1"><?php echo e(\App\CentralLogics\translate('discount')); ?> <?php echo e(\App\CentralLogics\translate('type')); ?></label>
                                    <select name="discount_type" class="form-control js-select2-custom">
                                        <option value="percent" <?php echo e($product['discount_type']=='percent'?'selected':''); ?>>
                                            <?php echo e(\App\CentralLogics\translate('percent')); ?>

                                        </option>
                                        <option value="amount" <?php echo e($product['discount_type']=='amount'?'selected':''); ?>>
                                            <?php echo e(\App\CentralLogics\translate('amount')); ?>

                                        </option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-4 col-4">
                                <div class="form-group">
                                    <label class="input-label"
                                           for="exampleFormControlInput1"><?php echo e(\App\CentralLogics\translate('stock')); ?></label>
                                    <input type="number" min="0" max="100000000" value="<?php echo e($product['total_stock']); ?>" name="total_stock" class="form-control"
                                           placeholder="Ex : 100">
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6 col-6">
                                <div class="form-group">
                                    <label class="input-label"
                                           for="exampleFormControlSelect1"><?php echo e(\App\CentralLogics\translate('category')); ?><span
                                            class="input-label-secondary">*</span></label>
                                    <select name="category_id" id="category-id" class="form-control js-select2-custom"
                                            onchange="getRequest('<?php echo e(url('/')); ?>/admin/product/get-categories?parent_id='+this.value,'sub-categories')">
                                        <?php $__currentLoopData = $categories; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $category): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option
                                                value="<?php echo e($category['id']); ?>" <?php echo e($category->id==$product_category[0]->id ? 'selected' : ''); ?> ><?php echo e($category['name']); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6 col-6">
                                <div class="form-group">
                                    <label class="input-label"
                                           for="exampleFormControlSelect1"><?php echo e(\App\CentralLogics\translate('sub_category')); ?><span
                                            class="input-label-secondary"></span></label>
                                    <select name="sub_category_id" id="sub-categories"
                                            data-id="<?php echo e(count($product_category)>=2?$product_category[1]->id:''); ?>"
                                            class="form-control js-select2-custom"
                                            onchange="getRequest('<?php echo e(url('/')); ?>/admin/product/get-categories?parent_id='+this.value,'sub-sub-categories')">

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

                        <div class="row"
                             style="border: 1px solid #80808045; border-radius: 10px;padding-top: 10px;margin: 1px">
                            <div class="col-12">
                                <div class="form-group">
                                    <label class="input-label"
                                           for="exampleFormControlSelect1"><?php echo e(\App\CentralLogics\translate('attribute')); ?><span
                                            class="input-label-secondary"></span></label>
                                    <select name="attribute_id[]" id="choice_attributes"
                                            class="form-control js-select2-custom"
                                            multiple="multiple">
                                        <?php $__currentLoopData = \App\Model\Attribute::orderBy('name')->get(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $attribute): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option
                                                value="<?php echo e($attribute['id']); ?>" <?php echo e(in_array($attribute->id,json_decode($product['attributes'],true))?'selected':''); ?>><?php echo e($attribute['name']); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-12 mt-2 mb-2">
                                <div class="customer_choice_options" id="customer_choice_options">
                                    <?php echo $__env->make('admin-views.product.partials._choices',['choice_no'=>json_decode($product['attributes']),'choice_options'=>json_decode($product['choice_options'],true)], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
                                </div>
                            </div>
                            <div class="col-md-12 mt-2 mb-2">
                                <div class="variant_combination" id="variant_combination">
                                    <?php echo $__env->make('admin-views.product.partials._edit-combinations',['combinations'=>json_decode($product['variations'],true)], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label><?php echo e(\App\CentralLogics\translate('product')); ?> <?php echo e(\App\CentralLogics\translate('image')); ?></label><small
                                style="color: red">* ( <?php echo e(\App\CentralLogics\translate('ratio')); ?> 1:1 )</small>
                            <div>
                                <div class="row mb-3">
                                    <?php $__currentLoopData = json_decode($product['image'],true); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $img): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <div class="col-3">
                                            <img style="height: 200px;width: 100%"
                                                 src="<?php echo e(asset('storage/app/public/product')); ?>/<?php echo e($img); ?>">
                                            <a href="<?php echo e(route('admin.product.remove-image',[$product['id'],$img])); ?>"
                                               style="margin-top: -35px;border-radius: 0"
                                               class="btn btn-danger btn-block btn-sm"><?php echo e(translate('Remove')); ?></a>
                                        </div>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </div>
                                <div class="row" id="coba"></div>
                            </div>
                        </div>
                    </div>
                    <hr>
                    <button type="submit" class="btn btn-primary"><?php echo e(\App\CentralLogics\translate('submit')); ?></button>
                </form>
            </div>
        </div>
    </div>

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

<?php $__env->startPush('script'); ?>

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

<?php $__env->startPush('script_2'); ?>
    <script src="<?php echo e(asset('public/assets/admin/js/spartan-multi-image-picker.js')); ?>"></script>
    <script>
        $(".lang_link").click(function(e){
            e.preventDefault();
            $(".lang_link").removeClass('active');
            $(".lang_form").addClass('d-none');
            $(this).addClass('active');

            let form_id = this.id;
            let lang = form_id.split("-")[0];
            console.log(lang);
            $("#"+lang+"-form").removeClass('d-none');
            if(lang == 'en')
            {
                $("#from_part_2").removeClass('d-none');
            }
            else
            {
                $("#from_part_2").addClass('d-none');
            }


        })
    </script>
    <script type="text/javascript">
        $(function () {
            $("#coba").spartanMultiImagePicker({
                fieldName: 'images[]',
                maxCount: 4,
                rowHeight: '215px',
                groupClassName: 'col-3',
                maxFileSize: '',
                placeholderImage: {
                    image: '<?php echo e(asset('public/assets/admin/img/400x400/img2.jpg')); ?>',
                    width: '100%'
                },
                dropFileLabel: "Drop Here",
                onAddRow: function (index, file) {

                },
                onRenderedPreview: function (index) {

                },
                onRemoveRow: function (index) {

                },
                onExtensionErr: function (index, file) {
                    toastr.error('<?php echo e(translate("Please only input png or jpg type file")); ?>', {
                        CloseButton: true,
                        ProgressBar: true
                    });
                },
                onSizeErr: function (index, file) {
                    toastr.error('<?php echo e(translate("File size too big")); ?>', {
                        CloseButton: true,
                        ProgressBar: true
                    });
                }
            });
        });
    </script>

    <script>
        function getRequest(route, id) {
            $.get({
                url: route,
                dataType: 'json',
                success: function (data) {
                    $('#' + id).empty().append(data.options);
                },
            });
        }

        $(document).ready(function () {
            setTimeout(function () {
                let category = $("#category-id").val();
                let sub_category = '<?php echo e(count($product_category)>=2?$product_category[1]->id:''); ?>';
                let sub_sub_category = '<?php echo e(count($product_category)>=3?$product_category[2]->id:''); ?>';
                getRequest('<?php echo e(url('/')); ?>/admin/product/get-categories?parent_id=' + category + '&&sub_category=' + sub_category, 'sub-categories');
                getRequest('<?php echo e(url('/')); ?>/admin/product/get-categories?parent_id=' + sub_category + '&&sub_category=' + sub_sub_category, 'sub-sub-categories');
            }, 1000)
        });
    </script>

    <script>
        $(document).on('ready', function () {
            $('.js-select2-custom').each(function () {
                var select2 = $.HSCore.components.HSSelect2.init($(this));
            });
        });
    </script>

    <script src="<?php echo e(asset('public/assets/admin')); ?>/js/tags-input.min.js"></script>

    <script>
        $('#choice_attributes').on('change', function () {
            $('#customer_choice_options').html(null);
            $.each($("#choice_attributes option:selected"), function () {
                add_more_customer_choice_option($(this).val(), $(this).text());
            });
        });

        function add_more_customer_choice_option(i, name) {
            let n = name.split(' ').join('');
            $('#customer_choice_options').append('<div class="row"><div class="col-md-3"><input type="hidden" name="choice_no[]" value="' + i + '"><input type="text" class="form-control" name="choice[]" value="' + n + '" placeholder="Choice Title" readonly></div><div class="col-lg-9"><input type="text" class="form-control" name="choice_options_' + i + '[]" placeholder="Enter choice values" data-role="tagsinput" onchange="combination_update()"></div></div>');
            $("input[data-role=tagsinput], select[multiple][data-role=tagsinput]").tagsinput();
        }

        function combination_update() {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $.ajax({
                type: "POST",
                url: '<?php echo e(route('admin.product.variant-combination')); ?>',
                data: $('#product_form').serialize(),
                success: function (data) {
                    $('#variant_combination').html(data.view);
                    if (data.length > 1) {
                        $('#quantity').hide();
                    } else {
                        $('#quantity').show();
                    }
                }
            });
        }
    </script>

    <script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>

    <script>
        <?php if($language): ?>
        <?php $__currentLoopData = json_decode($language); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $lang): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        var <?php echo e($lang); ?>_quill = new Quill('#<?php echo e($lang); ?>_editor', {
            theme: 'snow'
        });
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        <?php else: ?>
        var en_quill = new Quill('#editor', {
            theme: 'snow'
        });
        <?php endif; ?>

        $('#product_form').on('submit', function () {

            <?php if($language): ?>
            <?php $__currentLoopData = json_decode($language); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $lang): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            var <?php echo e($lang); ?>_myEditor = document.querySelector('#<?php echo e($lang); ?>_editor');
            $("#<?php echo e($lang); ?>_hiddenArea").val(<?php echo e($lang); ?>_myEditor.children[0].innerHTML);
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            <?php else: ?>
            var myEditor = document.querySelector('#editor');
            $("#hiddenArea").val(myEditor.children[0].innerHTML);
            <?php endif; ?>
            var formData = new FormData(this);

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.post({
                url: '<?php echo e(route('admin.product.update',[$product['id']])); ?>',
                // data: $('#product_form').serialize(),
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                success: function (data) {
                    if (data.errors) {
                        for (var i = 0; i < data.errors.length; i++) {
                            toastr.error(data.errors[i].message, {
                                CloseButton: true,
                                ProgressBar: true
                            });
                        }
                    } else {
                        toastr.success('<?php echo e(translate("product updated successfully!")); ?>', {
                            CloseButton: true,
                            ProgressBar: true
                        });
                        setTimeout(function () {
                            location.href = '<?php echo e(route('admin.product.list')); ?>';
                        }, 2000);
                    }
                }
            });
        });
    </script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.admin.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/penastud/public_html/pi/store/resources/views/admin-views/product/edit.blade.php ENDPATH**/ ?>