<?php Fun::lang() ?>
<style>
    .fee_footer td {
        padding: 5px;
    }

    .fee_footer {
        margin-right: 10px;
    }
</style>
<div class="page1 print_cutting">
    <div class="container-fluid">
        <div class="col-md-12">
            <h4 style="float: left; color: #2979bc">{{__('administrator.cutting')}}</h4>
        </div>
        <div class="col-sm-12 col-sm-12 col-xs-12">
            <table class="table add-fabric-table">
                <tr>
                    <td>{{__('administrator.date')}}</td>
                    <td>{{date('d-m-Y',strtotime($cutting->date))}}</td>
                    <td rowspan="3" style="font-size: 26px">{{__('administrator.invoice_number')}}</td>
                    <td rowspan="3">{{$cutting->invoice_number}}</td>
                    <td>{{__('administrator.total_template')}}</td>
                    <?php
                    $total_template = 0;
                    $total_qty = 0;
                    $sum_cutpiece = 0;
                    $total_fabric = 0;
                    $sum_totalyard = 0;
                    $key = 1;
                    foreach ($detail as $row) {
                        if ($row->type == 'cut_piece') {
                            $total_template += $row->size_qty;
                        }
                    }
                    ?>
                    <td>{{$total_template}}</td>
                </tr>
                <tr>
                    <td>{{__('administrator.qty_per_clothes')}}</td>
                    <td><span class="qty_per_clothes"></span></td>
                </tr>
                <tr>
                    <td>{{__('administrator.warehouse')}}</td>
                    <td>{{findWarehouse($cutting->from_warehouse)->warehouse_name}}</td>
                </tr>

            </table>
        </div>
        <div class="col-sm-6 col-sm-12 col-xs-12">
            <div style=" margin-top: 4px">
                <table class="table add-fabric-table">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>{{__('administrator.fabric')}}</th>
                        <th>{{__('administrator.color')}}</th>
                        <th>{{__('administrator.code')}}</th>
                        <th>{{__('administrator.qty')}}</th>
                        <th>{{__('administrator.yard')}}</th>
                        <th>{{__('administrator.total')}}</th>
                    </tr>
                    </thead>
                    <tbody class="fabric-tbody">
                    @foreach($fabric_detail as  $fabric)
                        <?php
                        $product = getProductById($fabric->fabric_id);

                        $total_yard = $fabric->qty * $fabric->qty_per_unit;
                        $total_fabric += $fabric->qty;
                        $sum_totalyard += $total_yard;

                        $color = findColor($product->color_code)->name??'';

                        ?>
                        <tr>
                            <td>{{$key++}}</td>
                            <td>{{$product->product_name ?? '' }}</td>
                            <td>{{$fabric->barcode}}</td>
                            <td>{{$color }}</td>
                            <td>{{$fabric->qty}}</td>
                            <td>{{$fabric->qty_per_unit}}</td>
                            <td>{{$total_yard}}</td>
                        </tr>
                    @endforeach
                    @php
                        $total_row = 10;
                        $total_data = count($fabric_detail);
                        $left_row = $total_row - $total_data;
                    @endphp
                    @if($total_row>$total_data)
                        @for ($i=0;$i<$left_row; $i++)
                            <tr>
                                <td>{{ $key++}}</td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>
                        @endfor
                    @endif
                    <tr>
                        <td colspan="5"></td>
                        <td rowspan="3">{{__('administrator.total_piece')}} : <br><br> {{$total_fabric}} ដើម</td>
                        <td rowspan="3">{{__('administrator.total_yard')}} : <br><br> {{$sum_totalyard}} យ៉ាត
                            <input type="hidden" class="total_yard" value="{{$sum_totalyard}}">
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="col-sm-6 col-sm-12 col-xs-12">
            <div style=" margin-top: 4px">
                <table class="table add-fabric-table">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>{{__('administrator.product')}}</th>
                        <th>{{__('administrator.barcode')}}</th>
                        <th>{{__('administrator.size')}}</th>
                        <th>{{__('administrator.color')}}</th>
                        <th>{{__('administrator.qty')}}</th>
                    </tr>
                    </thead>
                    <tbody class="fabric-tbody">
                    @foreach($detail as $key => $production_cutting)
                        @php
                            $total_qty= $production_cutting->qty * $production_cutting->size_qty;
                            $sum_cutpiece +=$total_qty;
                            $size = findSize($production_cutting->size_code)->size??'';
                            $color = findColor($production_cutting->color_code)->name??'';
                            $product = \App\Model\product\ProductUnitModel::join('nso007_products','nso007_products.id','nso007_product_unit.product_id')
                            ->where('nso007_product_unit.barcode',$production_cutting->barcode)
                            ->where('nso007_products.type','product')
                            ->select('product_name')
                            ->first();
                        @endphp
                        <tr>
                            <td>{{$key+=1}}</td>
                            <td>{{$product->product_name??'------'}}</td>
                            <td>{{$production_cutting->barcode}}</td>
                            <td>{{$size}}</td>
                            <td>{{$color}}</td>
                            <td>{{$production_cutting->qty}}</td>
                        </tr>
                    @endforeach
                    @php
                        $total_row = 11;
                        $total_data = count($detail);
                        $left_row = $total_row-$total_data;
                    @endphp
                    @if($total_row>$total_data)
                        @for ($i=0;$i <$left_row; $i++)
                            <tr>
                                <td>{{ $key+=1}}</td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>
                        @endfor
                    @endif
                    <tr>
                        <td colspan="5" class="text-right">{{__('administrator.total')}} </td>
                        <td >{{$sum_cutpiece}}<input type="hidden" class="total_sum_cutpiece" value="{{$sum_cutpiece}}"></td>
                    </tr>
                    </tbody>
                </table>
                @if($cutting->cost_per_piece)
                    <table class="fee_footer pull-right">
                        <tr>
                            <td class="text-right">{{__('administrator.tailor_fee')}}</td>
                            <td> :</td>
                            <td class="text-left">฿{{$cutting->cost_per_piece}} / {{__('administrator.piece')}}</td>
                        </tr>
                        <tr>
                            <td class="text-right">{{__('administrator.total_tailor_fee')}}</td>
                            <td> :</td>
                            <td class="text-left">฿{{$sum_cutpiece*$cutting->cost_per_piece}}</td>
                        </tr>
                    </table>
                @endif
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function () {
        let total_sum_cutpiece = $('.total_sum_cutpiece').val();
        let total_yard = $('.total_yard').val();
        let total = parseFloat(total_yard) / parseFloat(total_sum_cutpiece);
        $('.qty_per_clothes').text(total.toFixed(2));
    });
</script>
