<?php $__env->startSection('section-title', 'Edit Product'); ?>
<?php $__env->startSection('page-title', 'Product Edit'); ?>

<?php if(check_permission('product.update')): ?>
    <?php $__env->startSection('action-button'); ?>
        <a href="<?php echo e(route('product.index')); ?>" class="btn add_list_btn">
            <i class="mr-2 feather icon-list"></i>
            All Product
        </a>
    <?php $__env->stopSection(); ?>
<?php endif; ?>

<?php $__env->startSection('content'); ?>
    <div class="row justify-content-center">
        <div class="col-md-12">
            <div class="card m-b-30 card_style">
                <div class="card-body">
                    <div class="">
                        <form class="row g-3 needs-validation" method="POST"
                            action="<?php echo e(route('product.update', $data->id)); ?>" enctype="multipart/form-data">
                            <?php echo csrf_field(); ?>
                            
                            <div class="mt-2 col-md-6">
                                <label for="name" class="form-label fw-bold">Product Name *</label>
                                <input type="text" class="form-control" name="name" placeholder="Enter product name"
                                    value="<?php echo e($data->name); ?>">
                                <div class="errors"><?php echo e($errors->has('name') ? $errors->first('name') : ''); ?></div>
                            </div>

                            
                            <div class="mt-2 col-md-6">
                                <label for="barcode" class="form-label fw-bold">Product Barcode</label>
                                <input type="text" class="form-control" placeholder="Enter product barcode"
                                    name="barcode" value="<?php echo e($data->barcode); ?>">
                                <div class="errors"><?php echo e($errors->has('barcode') ? $errors->first('barcode') : ''); ?></div>
                            </div>

                            
                            <div class="mt-2 col-md-6">
                                <label for="unit_id" class="form-label fw-bold">Category *</label>
                                <select class="select2" name="category_id">
                                    <option selected value="">Select Category</option>
                                    <?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 == $data->category_id ? 'selected' : ''); ?>><?php echo e($category->name); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                                <div class="errors"><?php echo e($errors->has('category_id') ? $errors->first('category_id') : ''); ?>

                                </div>
                            </div>

                            
                            <div class="mt-2 col-md-6">
                                <label for="brand_id" class="form-label fw-bold">Brand</label>
                                <select class="select2" name="brand_id">
                                    <option selected value="">Select Brand</option>
                                    <?php $__currentLoopData = $brands; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $brand): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($brand->id); ?>"
                                            <?php echo e($brand->id == $data->brand_id ? 'selected' : ''); ?>><?php echo e($brand->name); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>

                            <?php if($data->unit->related_unit_id == null): ?>
                                
                                <div class="mt-2 col-md-3">
                                    <label for="unit_id" class="form-label fw-bold">Main Unit</label>
                                    <select class="select2 main_unit" name="unit_id">
                                        <?php $__currentLoopData = $units; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $unit): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($unit->id); ?>"
                                                <?php echo e($unit->id == $data->unit_id ? 'selected' : ''); ?>><?php echo e($unit->name); ?>

                                            </option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>

                                
                                <div class="mt-2 col-md-3">
                                    <label for="unit_id" class="form-label fw-bold">Sub Unit</label>
                                    <select name="sub_unit_id" id="" class="form-control sub_unit">
                                        <option value="">No Related Unit Found</option>
                                    </select>
                                </div>


                                
                                <div class="mt-2 col-md-6">
                                    <div class="form-group">
                                        <label for="">Low Stock Quantity</label>
                                        <div class="opening_stocks form-row" style="padding-left: 5px; padding-right:5px;">
                                            <input type="text" name="main_qty" value="<?php echo e($data->main_qty); ?>" class="form-control col"
                                                placeholder="Pices">
                                        </div>
                                    </div>
                                </div>
                            <?php else: ?>
                                
                                <div class="mt-2 col-md-3">
                                    <label for="unit_id" class="form-label fw-bold">Main Unit</label>
                                    <select class="select2 main_unit" name="unit_id">
                                        <?php $__currentLoopData = $units; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $unit): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($unit->id); ?>"
                                                <?php echo e($unit->id == $data->unit_id ? 'selected' : ''); ?>><?php echo e($unit->name); ?>

                                            </option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>

                                
                                <div class="mt-2 col-md-3">
                                    <label for="unit_id" class="form-label fw-bold">Sub Unit</label>
                                    <select name="sub_unit_id" id="" class="form-control sub_unit">
                                        <?php $__currentLoopData = $units; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $unit): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($unit->id); ?>"
                                                <?php echo e(($unit->id == $data->unit->related_unit_id) ? 'selected' : ''); ?>>
                                                <?php echo e($unit->name); ?>

                                            </option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>


                                
                                <div class="mt-2 col-md-3">
                                    <div class="form-group">
                                        <label for="">Low Stock Quantity</label>
                                        <div class="opening_stocks form-row" style="padding-left: 5px; padding-right:5px;">
                                            <input type="text" name="main_qty" value="<?php echo e($data->main_qty); ?>" class="form-control col"
                                                placeholder="Pices">
                                        </div>
                                    </div>
                                </div>
                                <div class="mt-3 col-md-3">
                                    <div class="form-group">
                                        <label for=""></label>
                                        <div class="opening_stocks form-row" style="padding-left: 5px; padding-right:5px;">
                                            <input type="text" name="sub_qty" value="<?php echo e($data->sub_qty); ?>" class="form-control col"
                                                placeholder="Pices">
                                        </div>
                                    </div>
                                </div>
                            <?php endif; ?>

                            <?php if(env('APP_SC') == 'yes'): ?>
                                <div class="col-md-6 mt-2" style="margin-right: -6px">
                                    <label for="size" class="form-label fw-bold">Size </label>
                                    <select class="select2" name="size[]" multiple>
                                        
                                        <?php $__currentLoopData = $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); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                    <div class="errors"><?php echo e($errors->has('size') ? $errors->first('size') : ''); ?></div>
                                </div>
                                <div class="col-md-6 mt-2" style="margin-right: -6px">
                                    <label for="color" class="form-label fw-bold">Color </label>
                                    <select class="select2" name="color[]" multiple>
                                        
                                        <?php $__currentLoopData = $colors; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $color): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($color->id); ?>"> <?php echo e($color->color); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                    <div class="errors"><?php echo e($errors->has('color') ? $errors->first('color') : ''); ?></div>
                                </div>
                            <?php endif; ?>

                            
                            <div class="mt-2 col-md-6">
                                <label for="purchase_price" class="form-label fw-bold">Purchase Price *</label>
                                <input type="text" class="form-control" name="purchase_price"
                                    value="<?php echo e($data->purchase_price); ?>">
                                <div class="errors">
                                    <?php echo e($errors->has('purchase_price') ? $errors->first('purchase_price') : ''); ?></div>
                            </div>
                            <div class="mt-2 col-md-6">
                                <label for="selling_price" class="form-label fw-bold">Sale Price *</label>
                                <input type="text" class="form-control" name="selling_price"
                                    value="<?php echo e($data->selling_price); ?>">
                                <div class="errors">
                                    <?php echo e($errors->has('selling_price') ? $errors->first('selling_price') : ''); ?></div>
                            </div>
                            
                            <div class="col-md-6 mt-2" style="margin-right: -6px">
                                <label for="status" class="form-label fw-bold">Status *</label>
                                <select class="select2" name="status">
                                    <option value="1" <?php echo e(($data->status == 1) ? 'selected' : ''); ?>>Active</option>
                                    <option value="0" <?php echo e(($data->status == 0) ? 'selected' : ''); ?>>Deactive</option>
                                </select>
                                <div class="errors"><?php echo e(($errors->has('status'))?$errors->first('status'):''); ?></div>
                            </div>

                            
                            <div class="mt-2 col-md-6">
                                <label for="description" class="form-label fw-bold">Description</label>
                                <textarea class="form-control" name="description" rows="3"><?php echo e($data->description); ?></textarea>
                            </div>

                            
                            <div class="mt-2 col-md-6">
                                <label for="image" class="form-label fw-bold">Product
                                    Images</label>
                                <input class="form-control image-uploadify" type="file" name="images"
                                    accept="image/*" onchange="readURL(this);">
                            </div>

                            <div class="mt-2 col-md-6">
                                <img id="image"
                                    src="<?php echo e(!empty($data->images) ? url('public/uploads/products/' . $data->images) : url('public/backend/images/no_images.png')); ?>"
                                    width="120px" style="border-radius: 25px" height="80px" />
                                <input type="hidden" name="old_image" value="<?php echo e($data->images); ?>">
                            </div>

                            <div class="mt-3 text-center col-12">
                                <button class="btn save_btn" type="submit"> Save </button>
                            </div>

                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript">
        function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();
                reader.onload = function(e) {
                    $('#image')
                        .attr('src', e.target.result)
                        .width(80)
                        .height(80);
                };
                reader.readAsDataURL(input.files[0]);
            }
        }
    </script>
    <script>
        $('.main_unit').change(function() {
            $('.sub_unit').html('<option value="">No Related Unit Found</option>');
            var main_unit_id = $(this).find(':selected').val();
            var main_unit_text = $(this).find(':selected').text();

            let url = "<?php echo e(route('product-unit', 'my_id')); ?>".replace('my_id', main_unit_id);

            $.ajax({
                url: url,
                method: 'GET',
                success: function(data) {
                    if (data) {
                        var sub_value = '<option value="">Select Unit</option><option value="' + data
                            .related_unit_id + '">' + data.related_unit.name + '</option>';
                        $('.sub_unit').html(sub_value);

                        // Opening Stock
                        var opening_stock = "";
                        opening_stock +=
                            `<input type="text" name="main_qty" value="" class="form-control col" placeholder="${main_unit_text}">`;
                        $('.opening_stocks').html(opening_stock);
                    } else {
                        $('.sub_unit').html('<option value="" selected>No Related Unit Found</option>');
                        // opening Stock

                        var opening_stock =
                            `<input type="text" name="main_qty" value="" class="form-control col" placeholder="${main_unit_text}">`;
                        $('.opening_stocks').html(opening_stock);
                    }
                }
            });
        });

        $('.sub_unit').change(function() {
            var sub_unit_id = $(this).find(':selected').val();
            var sub_unit_text = $(this).find(':selected').text();

            var main_unit_id = $('.main_unit').find(':selected').val();
            var main_unit_text = $('.main_unit').find(':selected').text();
            var opening_stock = '';
            if (sub_unit_id == "") {
                opening_stock =
                    `<input type="text" name="main_qty" value="" class="form-control col" placeholder="${main_unit_text}">`;
            } else {
                opening_stock +=
                    `<input type="text" name="main_qty" value="" class="form-control col" placeholder="${main_unit_text}" style="margin-right:5px;">`;
                opening_stock +=
                    `<input type="text" name="sub_qty" value="" class="form-control col" placeholder="${sub_unit_text}">`;
            }

            $('.opening_stocks').html(opening_stock);

        });
    </script>

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

<?php echo $__env->make('backend.layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH D:\laragon\www\new demo multi-bank\resources\views/backend/pages/product/edit.blade.php ENDPATH**/ ?>