<?php Fun::lang() ?>
<div class="production-request">
    <div class="container-fluid">
        <hr>
        @php
            $user = findUser($production->user_id);
            $tailor =  \App\Model\people\TailorModel::find($production->tailor_id);
        @endphp
        <div class="summary-header">
            <table width="100%">
                <tr>
                    <td>
                        <table class="text-left">
                            <tr>
                                <td>{{__('administrator.invoice_number')}}</td>
                                <td> : {{$production->invoice_number}} </td>
                            </tr>
                            <tr>
                                <td>{{__('administrator.user')}}</td>
                                <td> : {{$user?$user->name:''}} </td>
                            </tr>
                            <tr>
                                <td>{{__('administrator.tailor')}}</td>
                                <td> : {{$tailor?$tailor->name:''}} </td>
                            </tr>
                            <tr>
                                <td>{{__('administrator.date')}}</td>
                                <td> : {{date('d-m-Y',strtotime($production['date']))}} </td>
                            </tr>
                        </table>
                    </td>
                    <td>
                        <table class="pull-right">
                            <tr>
                                <td>{{__('administrator.packer')}}<i class="text-danger">*</i> &nbsp;&nbsp;</td>
                                <td>{!! Form::select('packer_id',\App\Model\setup\PackerModel::pluck('name','id'),null,['class'=>'form-control','placeholder'=>__('administrator.please_select')]) !!}</td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </div>
        @php
            $cur = findCurrency($production->currency_id);
            $symbol = $cur->symbol??'';
        @endphp
        <div class="wrapper-table">
            <h3>{{__('administrator.items')}}</h3>
            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th>#</th>
                    <th>{{__('administrator.barcode')}}</th>
                    <th>{{__('administrator.items')}}</th>
                    <th>{{__('administrator.size')}}</th>
                    <th>{{__('administrator.color')}}</th>
                    <th>{{__('administrator.production_qty')}}</th>
                    <th>{{__('administrator.packing')}}</th>
                    <th>{{__('administrator.remain')}}</th>
                    <th>{{__('administrator.packing_qty')}}</th>
                </tr>
                </thead>
                <tbody>
                @php
                    $total_packing = 0;
                    $total_remain = 0;
                @endphp
                @foreach($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);

                        $remain = $row->return_qty-$row->packing_qty;
                    @endphp
                    <tr>
                        <td>{{$key++}}</td>
                        <td>{{$row->barcode}}</td>
                        <td>{{$product->product_name??''}}</td>
                        <td>{{$size->size??''}}</td>
                        <td>{{$color->name??''}}</td>
                        <td>{{$row->return_qty}}</td>
                        <td>{{$row->packing_qty}}</td>
                        <td>{{$remain}}</td>
                        <td>
                            {!! Form::text('qty[]', $remain, ['class' => 'form-control number-only packing_qty','data-max'=>$remain]) !!}
                            {{-- production_request_detail--}}
                            {!! Form::hidden('detail_id[]', $row->id) !!}
                        </td>
                    </tr>
                @endforeach
                </tbody>
                <tfoot>
                <tr>
                    <td colspan="5" class="text-right no-border">{{__('administrator.total_item')}}</td>
                    <td>{{$production->return_qty}}</td>
                    <td>{{$production->packing_qty}}</td>
                    <td>{{$production->return_qty-$production->packing_qty}}</td>
                    <td></td>
                </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>
{!! Form::hidden('production_id', $production->id) !!}
{!! Form::submit('Submit', ['class' => 'hidden']) !!}
<script>
    $(document).ready(function (){
        $('.packing_qty').on('keyup',function () {
            let max = $(this).data('max')
            max = max?parseFloat(max):0;
            let qty = $(this).val()
            qty = qty?parseFloat(qty):0;
            if(max<qty){
                $(this).val(max)
            }
        })
    })
</script>
