@extends('layouts.jform')
@section('content')
    {!! Form::open(['method' => 'POST','url' => $master->route,'enctype'=>'multipart/form-data','class'=>'jform',]) !!}
    <?php Fun::lang() ?>
    <div class="production-request1">
        <div class="container-fluid">
            <h2>{{$master->return?__('administrator.return_item'):__('administrator.import_stock')}}</h2>
            <hr>
            @php
                $user = findUser($master->user_id);
            @endphp
            <table width="100%">
                <tr>
                    <td>
                        <table class="text-left">
                            <tr>
                                <td>{{__('administrator.invoice_number')}}</td>
                                <td> : {{$master->invoice_number}} </td>
                            </tr>
                            <tr>
                                <td>{{__('administrator.user')}}</td>
                                <td> : {{$user?$user->name:''}} </td>
                            </tr>
                            <tr>
                                <td>{{__('administrator.date')}}</td>
                                <td> : {{date('d-m-Y',strtotime($master['date']))}} </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
            <br>
            <br>
            <table class="table table-striped table-hover " width="100%">
                <thead>
                <tr>
                    <th>#</th>
                    <th>{{__('administrator.barcode')}}</th>
                    <th>{{__('administrator.items')}}</th>
                    <th>{{__('administrator.size')}}</th>
                    <th>{{__('administrator.color')}}</th>
                    <th>{{__('administrator.packing_qty')}}</th>
                    <th>{{__('administrator.import_qty')}}</th>
                    <th>{{__('administrator.return_qty')}}</th>
                    <th>{{__('administrator.left_qty')}}</th>
                    <th>{{$master->return?__('administrator.return_qty'):__('administrator.import_qty')}}</th>
                </tr>
                </thead>
                <tbody>
                @php
                    $total_done = 0;
                    $total_import = 0;
                    $total_return = 0;
                    $total_left = 0;
                    $packing_done = \App\Model\production\packing\ProductionPackingDoneModel::where('packing_id',$master->id)->pluck('id');
                    $production = \App\Model\production\ProductionRequestModel::where('packing_id',$master->id)->pluck('id');
                @endphp
                @foreach($master->detail as $key => $row)
                    @php
                        $product_unit = findProductUnitByCode($row->barcode,'product');
                        $product = getProductById($product_unit->product_id);
                        $size = findSize($product_unit->size_code);
                        $color = findColor($product_unit->color_code);
                        $unit = findUnit($product_unit->unit_id);

                        $packing_done_qty = \App\Model\production\packing\ProductionPackingDoneDetailModel::whereIn('packing_done_id',$packing_done)
                        ->where('barcode',$row->barcode)
                        ->sum('qty');
                        $return_qty = \App\Model\production\ProductionRequestDetailModel::whereIn('production_id',$production)
                        ->where(['barcode'=> $row->barcode,'type'=>'item'])
                        ->sum('qty');
                        $left_qty = $row->qty-$packing_done_qty-$return_qty;

                        $total_import +=$packing_done_qty;
                        $total_return +=$return_qty;
                        $total_left +=$left_qty;
                        $total_done+=$row->qty;
                    @endphp
                    <tr>
                        <td>{{$key+1}}</td>
                        <td>{{$row->barcode}}</td>
                        <td>{{$product->product_name??''}}</td>
                        <td>{{$size->size??''}}</td>
                        <td>{{$color->name??''}}</td>
                        <td>{{$row->qty}}</td>
                        <td>{{$packing_done_qty}}</td>
                        <td>{{$return_qty}}</td>
                        <td>{{$left_qty}}</td>
                        <td>
                            {!! Form::text('qty[]',0, ['placeholder'=>__('administrator.import_qty'),'class' => 'form-control qty number-only','data-left'=>$left_qty]) !!}
                            {!! Form::hidden('barcode[]',$row->barcode) !!}
                            {!! Form::hidden('packing_detail_id[]',$row->id) !!}
                        </td>
                    </tr>
                @endforeach
                </tbody>
                <tfoot>
                <tr>
                    <td colspan="5" class="text-right no-border">{{__('administrator.total_item')}}</td>
                    <td>{{$total_done}}</td>
                    <td>{{$total_import}}</td>
                    <td>{{$total_return}}</td>
                    <td>{{$total_left}}</td>
                    <td></td>
                </tr>
                <tr>
                @if(!$master->return)
                    <tr>
                        <td colspan="5" class="text-right no-border">{{__('administrator.to_warehouse')}}</td>
                        <td colspan="4">{!! Form::select('warehouse_id',getWarehouse('item')->pluck('warehouse_name','id'),null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control select2']) !!}</td>
                        <td>{!! Form::text('total_qty',0, ['class' => 'form-control','readonly']) !!}</td>
                    </tr>
                @endif
                </tfoot>
            </table>

            @if($master->return)
                <table>
                    <tr>
                        <td>{{__('administrator.note')}}&nbsp;&nbsp;</td>
                        <td>
                            {!! Form::textarea('note', null,['class' => 'form-control','rows'=>3]) !!}
                        </td>
                    </tr>
                </table>

            @endif
        </div>
    </div>
    {!! Form::submit('Submit', ['class' => 'hidden']) !!}
    {!! Form::close() !!}
@endsection
@section('jq')
    <script>
        $(document).ready(function () {
            $('table').on('keyup', '.qty', function () {
                let _qty = $(this).val();
                let left = $(this).data('left');
                left = left ? parseFloat(left) : 0;
                _qty = _qty ? parseFloat(_qty) : 0;
                if (_qty > left) {
                    $(this).val(left);
                }
            });
        });
    </script>
@endsection
