<?php
    $route =  route('clients.create',['req'=>'ajax']);
?>
<!-- general form elements -->
<div class="box box-success">
    
    <!-- /.box-header -->
        <div class="box-body">
            <div class="nav-tabs-custom">
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="true">بيانات الفاتورة</a></li>
                </ul>
                <div class="tab-content">
                    <div class="tab-pane active" id="tab_1">
                        <div class="row">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label>التاريخ</label>
                                    <div class="input-group date">
                                        <div class="input-group-addon">
                                            <i class="fa fa-calendar"></i>
                                        </div>
                                        <input name="invoice[date]" type="text" value="<?php echo e(($item->date)??date('Y-m-d')); ?>" id="datepicker" class="form-control pull-right ">
                                    </div>
                                    <!-- /.input group -->
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label>العميل</label>
                                    <div class="input-group">

                                        <select required data-url="<?php echo e(route('getClientsList')); ?>" name="invoice[client_id]" id="personList" class="form-control select2AjaxRequest">
                                            <option value="">--- إختر ---</option>
                                            <?php if(isset($per) && !empty($per)): ?>
                                                <option selected <?php echo e(isset($item->client_id) && $per->id==$item->client_id?'selected':''); ?> value="<?php echo e($per->id); ?>">
                                                    <?php echo e($per->id.' - '.$per->name.' - '.$per->mobile.' - '.$per->model); ?>

                                                </option>
                                            <?php endif; ?>
                                        </select>
                                        <div class="input-group-addon no-print" style="padding: 2px 5px;">
                                            <a href="<?php echo e($route); ?>" class="external" data-toggle="modal" data-target="#myModal">
                                                <i class="fa fa-2x fa-plus-circle"></i>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label>رقم الفاتورة المرجعى</label>
                                        <input name="invoice[invoice_num]" type="text" value="<?php echo e(($item->invoice_num)??''); ?>"  class="form-control pull-right ">
                                    </div>
                                </div>
                            </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>قطع الغيار</label>
                                    <select id="productList" data-placeholder="إختر قطعة الغيار" data-url="<?php echo e(route('getProductsList')); ?>" class="form-control select2ProductAjaxRequest">
                                    </select>
                                </div>
                            </div>
                        </div>

                            <div class="row">
                                <div class="col-md-12">
                                    <table class="pitems table table-bordered table-striped">
                                        <thead>
                                        <tr>
                                            <td>#</td>
                                            <td>الصنف</td>
                                            <td>السعر</td>
                                            <td>الكمية</td>
                                            <td>الخصم</td>
                                            <td>الإجمالى</td>
                                            <td></td>
                                        </tr>
                                        </thead>
                                        <tbody>
                                            <?php if(isset($item)): ?>
                                                <?php $__currentLoopData = $item->details; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <tr class="prodItem">
                                                    <td>
                                                        <?php echo e($loop->iteration); ?>

                                                        <input type="hidden" name="prod[<?php echo e($loop->iteration); ?>][product_id]" value="<?php echo e($d->id); ?>">
                                                    </td>
                                                    <td><?php echo e($d->code); ?> - <?php echo e($d->name); ?></td>
                                                    <td>
                                                        <input class ="tdinput pPrice" type="number" name="prod[<?php echo e($loop->iteration); ?>][price]" value="<?php echo e($d->pivot->price); ?>">
                                                    </td>
                                                    <td>
                                                        <input class ="tdinput qty" type="text" name="prod[<?php echo e($loop->iteration); ?>][qty]" value="<?php echo e($d->pivot->qty); ?>">
                                                    </td>
                                                    <td>
                                                        <input class ="tdinput discount" type="text" name="prod[<?php echo e($loop->iteration); ?>][discount]" value="<?php echo e($d->pivot->discount); ?>">
                                                    </td>
                                                    <td class="itemTotal">
                                                        <?php echo e($d->pivot->qty * ($d->pivot->price)); ?>

                                                    </td>
                                                    <td>
                                                        <a href="#" class="btn btn-sm btn-danger"><i class="fa fa-trash"></i></a>
                                                    </td>
                                                </tr>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                            <?php endif; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        <div class="row">
                             <div class="col-md-12 hide">
                                <div class="form-group">
                                    <label>الضريبة <i class="fa fa-percent"></i></label>
                                    <div class="input-group">
                                        <input readonly id="tax" type="number" value="0" name="return[tax]" class="form-control otherCostFees">
                                        <div class="input-group-addon">
                                            %
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label>الإجمالى</label>
                                    <input  step="any" readonly id="total" type="number" value="<?php echo e($item->total??0); ?>" name="return[total]" class="form-control">
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label>المدفوع</label>
                                    <input <?php if(!$currentUser->can('addPayment Returns')): ?> readonly <?php endif; ?> step="any" id="paid" type="number"  value="<?php echo e($item->paid??0); ?>" name="return[paid]" class="form-control">
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label>المتبقى</label>
                                    <input readonly step="any" id="due" value="<?php echo e($item->due??0); ?>" type="number" name="return[due]" class="form-control">
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>ملاحظات</label>
                                    <textarea name="return[note]" class="form-control"><?php echo e($item->note??''); ?></textarea>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>صور</label>
                                    <input id="FileUploade" name="image[]" multiple type="file" class="form-control ">
                                    <?php if(isset($item) && optional($item->getFirstMedia('images'))->getUrl()): ?>
                                        <?php $__currentLoopData = $item->getMedia('images'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $image): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <div class="col-md-2 imageContainer">
                                                <img style="width: 80px;" src="<?php echo e($image->getUrl()); ?>" />
                                                <?php if($currentUser->can('Delete Returns')): ?>
                                                <a class="btn btn-danger btn-sm deleteImg" rel="<?php echo e($image->id); ?>" href="#">
                                                    <i class="fa fa-trash close_button"></i>
                                                </a>
                                                <?php endif; ?>
                                            </div>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    <?php endif; ?>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>مرفقات</label>
                                    <input multiple name="attachments[]" type="file" class="form-control ">
                                    <?php if(isset($item)): ?>
                                        <?php $__currentLoopData = $item->getMedia('attachments'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $file): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <div class="col-md-2 imageContainer">
                                        <?php
                                            $filepath = $file->getUrl();
                                            $extension = $file->mime_type;
                                        ?>
                                        <?php if(in_array($extension,['image/jpeg','image/png'])): ?>
                                            <img style="width: 100px;" src="<?php echo e($filepath); ?>">
                                        <?php else: ?>
                                            <a style="margin-bottom: 5px;" target="_blank" href="<?php echo e($filepath); ?>" class="btn btn-sm btn-warning">View </a>
                                        <?php endif; ?>
                                        <?php if($currentUser->can('Delete Returns')): ?>
                                            <a class="btn btn-danger btn-sm deleteImg" rel="<?php echo e($file->id); ?>" href="#"><i
                                                    class="fa fa-trash close_button"></i></a>
                                        <?php endif; ?>
                                        </div>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- /.box-body -->

        <div class="box-footer">
            <button type="submit" class="btn btn-primary">حفظ</button>
        </div>
</div>
<!-- /.box -->
<?php $__env->startPush('css'); ?>

<?php $__env->stopPush(); ?>
<?php $__env->startPush('js'); ?>
    <script>
        var tags = false;
        $(".select2").select2();
        $('#datepicker,datepicker22').datepicker({
            autoclose: true,
            rtl: true,
            format: 'yyyy-mm-dd',
            language: "ar",
        });
        $('body').on('submit', 'form', function(e) {
            //e.preventDefault();
            var $btn = $(this).find(':submit');
            if($btn.hasClass('disabled'))return false;
            $btn.attr('data-loading-text',"<i class='fa fa-circle-o-notch fa-spin'></i> حفظ");
            $btn.button('loading');
            setTimeout(function() {
                $btn.button('reset');
            }, 3000);
        });
         $(document).on("input",".otherCostFees",function(e){
            calaculateTotal();
        });
        $(document).on("click",".btn-danger",function(e){
            e.preventDefault();
            $(this).closest('tr').remove();
            calaculateTotal();
        });
        $(document).on("click",".btn-danger",function(e){
            e.preventDefault();
            $(this).closest('tr').remove();
            calaculateTotal();
        });


        $(document).on("change","#productList",function(e){
            e.preventDefault();
            var selected = $("#productList option:selected");
            if(!selected.val()) return false;
            var num = $('.pitems').find('tr').length;
            var found = false;
            $(".pitems tr").filter(function() {
                found = $(this).text().toLowerCase().indexOf(selected.text()) > -1
            });
            if(found){
                swal({
                    title: 'الصنف مضاف',
                    text: "هذا الصنف تم اضافته بالفعل للفاتورة",
                    type: "error",
                    confirmButtonText: "تمام",
                });
                return false;
            }
            var text = selected.text();
            var myArray = text.split("- ");
            let price = myArray.pop();

            let availableQty = myArray.pop();
            let ItemTitle = myArray.join("-");

            var cost = selected.attr('cost')===undefined?0:selected.attr('cost');
            //var price = selected.attr('price')===undefined?0:selected.attr('price');
            var itemtotal = parseFloat(price);
            var discount = 0;
            var row = '<tr class="prodItem">';
            row += '<td>'+num+'</td>';
            row += '<td>' +
                '<input type="hidden" name="prod['+num+'][product_id]" value="'+selected.val()+'">'+
                ItemTitle+
                '</td>';
            row += '<td><input class = "tdinput pPrice" type="text" name="prod['+num+'][price]" value="'+price+'"></td>';
            row += '<td><input class = "tdinput qty" type="text" name="prod['+num+'][qty]" value="1"></td>';
            row += '<td><input class="tdinput discount" type="text" name="prod['+num+'][discount]" value="'+discount+'"></td>';

            row += '<td class="itemTotal">'+itemtotal+'</td>';
            row += '<td>' +
                '<a href="#" class="btn btn-sm btn-danger"><i class="fa fa-trash"></i></a>';
            row +='</td>' ;
            row += '</tr>';
            $('.pitems').append(row);
            calaculateTotal();
            $(this).val('').trigger('change');
        });
        $(document).on("input",".carPrice,.discount,.qty,.pPrice",function(e){
            calaculateTotal();
        });
        $(document).on("input","#paid",function(e){
            var total = parseFloat($('#total').val());
            var paid = parseFloat($(this).val());
            $("#due").val(total-paid);
        });
        function calaculateTotal(){
            var grandtotal =0;
            $(".prodItem").each(function() {
                var price = parseFloat($(this).find('.pPrice').val());
                var qty = parseFloat($(this).find('.qty').val());
                var discount = parseFloat($(this).find('.discount').val());
                var tot = qty * price;
                tot -= discount;
                grandtotal += parseFloat(tot);
                $(this).find('.itemTotal').html(tot);
            });

            var tax = 0;
            var shipping = 0;
            var fees = 0;
            if($("#tax").val()) {
                tax = parseFloat($("#tax").val());
            }

            //grandtotal += (shipping+fees);
            grandtotal += (grandtotal*(tax/100));
            grandtotal = Math.floor(grandtotal);
            $("#total").val(grandtotal);
            var paid = parseFloat($('#paid').val());
            $("#due").val(grandtotal-paid);
        }
    </script>
<?php $__env->stopPush(); ?>
<?php $__env->startPush('css'); ?>
    <style>
        input.tdinput {
            display: block;
            width: 100%;
            height: 28px;
            padding: 0;
            font-size: 14px;
            line-height: 1.42857143;
            color: #555;
            background-color: #dff0d8;
            border: 0px solid #fff;
            border-radius: 4px;
            border: 0px solid #fff!important;
            text-align: center;
        }
    </style>
<?php $__env->stopPush(); ?>
<?php /**PATH /home/mwarsha2023/public_html/test4.mwarsha.com/resources/views/returns/_form.blade.php ENDPATH**/ ?>