<?php $__env->startSection('content'); ?>
    <?php Fun::lang()?>
    <style>
        .middle {
            padding-right: 10px !important;
            padding-top: 10px !important;;
        }
    </style>

    <div class="container">
        <div class="col-sm-12">
            <fieldset>
                <legend><?php echo e(__('administrator.adjustment')); ?></legend>
            <?php echo Form::open(array('route' => 'stock-adjustment.store','method'=>'POST','enctype'=>'multipart/form-data','class'=>'jform')); ?>

            <div class="col-sm-12 col-xs-12">
                <div class="col-sm-4 col-md-4 form-group">
                    <div class="col-sm-4 col-md-4 text-right middle">
                        <?php echo e(__('administrator.warehouse')); ?>

                    </div>
                    <div class="form-group col-sm-8 col-md-8">
                        <?php echo Form::select('warehouse_id',getWarehouse('item')->pluck('warehouse_name','id'),null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control select2']); ?>

                    </div>
                </div>
                <div class="col-sm-4 col-md-4 form-group">
                    <div class="col-sm-4 col-md-4 text-right middle">
                        <?php echo e(__('administrator.date')); ?>

                    </div>
                    <div class="form-group col-sm-8 col-md-8">
                        <?php echo Form::text('date',date('d-m-Y'), ['class' => 'form-control date_picker']); ?>

                    </div>
                </div>
                <div class="col-sm-4 col-md-4 form-group">
                    <div class="col-sm-4 col-md-4 text-right middle">
                        <?php echo e(__('administrator.user')); ?>

                    </div>
                    <div class="form-group col-sm-8 col-md-8">
                        <?php echo Form::text('user',Auth::user()->name, ['class' => 'form-control','readonly']); ?>

                    </div>
                </div>


            </div>
            <div class="col-sm-12 col-xs-12 border">
                <table class="table-responsive table table-striped">
                    <thead>
                    <th>#</th>
                    <th width="20%"><?php echo e(__('administrator.item')); ?></th>
                    <th width="10%"><?php echo e(__('administrator.size')); ?></th>
                    <th width="10%"><?php echo e(__('administrator.color')); ?></th>
                    <th><?php echo e(__('administrator.stock')); ?></th>
                    <th><?php echo e(__('administrator.operator')); ?></th>
                    <th><?php echo e(__('administrator.qty')); ?></th>
                    <th><?php echo e(__('administrator.balance')); ?></th>
                    <th><span class="button_success fa fa-plus" onclick="addRow()"></span></th>
                    </thead>
                    <tbody class="tbody">

                    </tbody>
                </table>
            </div>
                <div class="col-lg-6 "></div>
                <div class="col-lg-6 form-group ">
                    <div class="col-sm-4 col-md-4 text-right middle">
                        <?php echo e(__('administrator.note')); ?>

                    </div>
                    <div class="form-group col-sm-8 col-md-8">
                        <?php echo Form::textarea('note',null, ['class' => 'form-control','rows'=>3]); ?>

                    </div>
                </div>
                <div class="col-sm-12 text-right">
                <input type="button" class="button_warning" value="<?php echo e(__('administrator.back')); ?>" onclick="window.location.replace('<?php echo e(route('stock-adjustment.index')); ?>')">
                <input type="submit" class="button_primary" value="<?php echo e(__('administrator.submit')); ?>">
                </div>
                <?php echo Form::close(); ?>

            </fieldset>
        </div>
    </div>

<?php $__env->stopSection(); ?>
<?php $__env->startSection('script'); ?>
    <script type="text/javascript">
        var count = 1;
        $(document).ready(function () {
            addRow();
            $(document).on('change', '.search_product', function () {
                changeProSize($(this),'getSize');
            });
            $(document).on('change', '.size', function () {
                changeProSize($(this),'getColor')
            });
            $(document).on('change', '.color', function () {
                changeProSize($(this),'getStock')
            });

            $(document).on('change', 'select[name="operator[]"], .stock_qty', function () {
                operator($(this));
            });
            $(document).on('keyup', 'input[name="qty[]"]', function () {
                let _this = $(this);
                operator(_this);
            });
        });

        function operator(_this) {
            let qty = _this.closest('tr').find('input[name="qty[]"]').val();
            let stock_qty = _this.closest('tr').find('.stock_qty').val();
            let operator = _this.closest('tr').find('select[name="operator[]"]').val();
            qty = qty ? parseFloat(qty) : 0;
            stock_qty = stock_qty ? parseFloat(stock_qty) : 0;
            let balance = eval(stock_qty + operator + qty);
            if (balance < 0) {
                balance = stock_qty;
                // _this.closest('tr').find('input[name="qty[]"]').val(stock_qty);
            }
            if(operator==='-'&&(qty>stock_qty)){
                _this.closest('tr').find('input[name="qty[]"]').val(stock_qty);
            }
            _this.closest('tr').find('input[name="balance[]"]').val(balance)
        }

        function changeProSize(_this,getType) {
            let product_id = _this.closest('tr').find('.search_product').val();
            let size_code = _this.closest('tr').find('.size').val();
            let color_code = _this.closest('tr').find('.color').val();
            let warehouse_id = $('select[name=warehouse_id]').val();
            let type = 'product';

            $.ajax({
                url: '<?php echo e(route('stock-adjustment.create')); ?>',
                dataType: 'json',
                method: 'get',
                data: {
                    warehouse_id, product_id, type, size_code,color_code,getType
                },
                success: function (data) {

                    let option_color = `<option value=""><?php echo e(__('administrator.please_select')); ?></option>`;
                    let option_size = option_color;
                    if (getType==='getSize') {
                        _this.closest('tr').find('.color').html('');
                        _this.closest('tr').find('.stock_qty').val(0);
                        _this.closest('tr').find('.size').html('');
                        if (data.size) {
                            $.each(data.size, function (ii, vv) {
                                option_size += `<option value="${ii}">${vv}</option>`;
                            });
                        }
                        _this.closest('tr').find('.size').html(option_size);
                    }
                    if (getType==='getColor') {
                        _this.closest('tr').find('.color').html('');
                        _this.closest('tr').find('.stock_qty').val(0);
                        if (data.color) {
                            $.each(data.color, function (ii, vv) {
                                option_color += `<option value="${ii}">${vv}</option>`;
                            });
                        }
                        _this.closest('tr').find('.color').html(option_color);
                    }
                    if (getType==='getStock') {
                        _this.closest('tr').find('.stock_qty').val(0);
                        _this.closest('tr').find('.stock_qty').val(data.qty);
                    }
                },
                error(xhr) {
                    alertErrorFormController(xhr);
                }
            });
        }

        function addRow() {
            let html = `
            <tr>
            <td>${count}</td>
            <td>
            <?php echo Form::select('product_id[]',[],null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control search_product']); ?>

            </td>
            <td>
            <?php echo Form::select('size_code[]',[],null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control size']); ?>

            </td>
            <td>
            <?php echo Form::select('color_code[]',[],null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control color']); ?>

            </td>
            <td>
            <?php echo Form::text('stock[]',0, ['placeholder'=>__('administrator.stock'),'class' => 'form-control stock_qty','readonly']); ?>

            </td>
            <td>
            <?php echo Form::select('operator[]',['+'=>__('administrator.+'),'-'=>__('administrator.-')],null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control select3']); ?>

            </td>
            <td>
            <?php echo Form::text('qty[]',null, ['placeholder'=>__('administrator.qty'),'class' => 'form-control number-only']); ?>

            </td>
            <td>
            <?php echo Form::text('balance[]',0, ['placeholder'=>__('administrator.balance'),'class' => 'form-control','readonly']); ?>

            </td>
            <td>
            <span class="button_danger fa fa-times" onclick="$(this).closest('tr').remove()"></span>
            </td>
            </tr>
            `;
            $('.tbody').append(html);
            if(count>1){
                search_something('product', '.search_product');
            }
            count++
        }

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

<?php echo $__env->make(branchMenu(), \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/nsowos/public_html/shr-brand/resources/views/administrator/adjustment/create.blade.php ENDPATH**/ ?>