@extends(branchMenu())
@section('content')
    <?php Fun::lang();
    $sym = user_currency();
    $sym = $sym->symbol ?? '';
    ?>
    <style>
        table tfoot tr td:first-child {
            text-align: right;
        }

        .padding-5 td {
            padding-top: 10px;
            padding-bottom: 20px;
        }
    </style>
    <div class="page ">
        <div class="container-fluid ">
            <div class="row">
                {!! Form::open(array('route' => 'production-request.store','method'=>'POST','enctype'=>'multipart/form-data','id'=>'submit_post','class'=>'jform')) !!}
                <div class="col-sm-12 col-xs-12 padding-top-bottom-10 bg-white">
                    <div class="master-form">
                        <h3 class="margin-ud-10">{{__('administrator.production_request')}}</h3>
                        <div class="title-note">
                            <h3><span class="fa fa-exclamation-triangle"></span> {{__('administrator.create')}}
                                <i>({{__('administrator.fields_in_red_are_required')}})</i></h3>
                        </div>
                        <div class="col-md-8 col-md-offset-2">
                            <fieldset>
                                <legend>{{__('administrator.cut_piece')}}</legend>
                            </fieldset>
                            <div class="row">
                                <div class="col-sm-4 col-md-4 col-lg-4 form-group">
                                    <div class="col-sm-6 col-md-6 col-lg-6">
                                        <label class="required">{{__('administrator.invoice_number')}}</label>
                                    </div>
                                    <div class="col-sm-6 col-md-6 col-lg-6">
                                        {!! Form::text('invoice_number',$cutting->invoice_number, ['placeholder'=>__('administrator.invoice_number'),'class' => 'form-control']) !!}
                                        {!! Form::hidden('cutting_id',request()->cutting_id) !!}
                                    </div>
                                </div>
                                <div class="col-sm-4 col-md-4 col-lg-4 form-group">
                                    <div class="col-sm-5 col-md-5 col-lg-5">
                                        <label class="required">{{__('administrator.tailor_shop')}}</label>
                                    </div>
                                    <div class="col-sm-7 col-md-7 col-lg-7">
                                        {!! Form::select('tailor_type',getTailorType(),null,array('placeholder'=>__('administrator.please_select'),'class'=>' form-control tailor_type')) !!}
                                    </div>
                                </div>
                                <div class="col-sm-4 col-md-4 col-lg-4 form-group">
                                    <div class="col-sm-5 col-md-5 col-lg-5">
                                        <label class="required">{{__('administrator.tailor')}}</label>
                                    </div>
                                    <div class="col-sm-7 col-md-7 col-lg-7">
                                        {!! Form::select('tailor_id',[],null,array('placeholder'=>__('administrator.please_select'),'class'=>' form-control')) !!}
                                    </div>
                                </div>
                            </div>

                            <table class="table table-hover">
                                <thead>
                                <tr>
                                    <th>#</th>
                                    <th>{{__('administrator.code')}}</th>
                                    <th>{{__('administrator.size')}}</th>
                                    <th>{{__('administrator.color')}}</th>
                                    <th>{{__('administrator.total_qty')}}</th>
                                    <th>{{__('administrator.remain')}}</th>
                                    <th>{{__('administrator.qty')}}</th>
                                    <th width="150px">{{__('administrator.sewing_price')}}</th>
                                    <th>{{__('administrator.check')}}</th>
                                </tr>
                                </thead>
                                <tbody class="table_cut_piece">
                                @php $sum_cutpiece = 0; @endphp
                                @foreach($detail as $key => $production_cutting)
                                    @php
                                        $total_qty= $production_cutting->qty * $production_cutting->size_qty;
                                        $sum_cutpiece +=$total_qty;
                                        $check = checkProductExist($production_cutting->barcode);
                                        $template = \App\Model\production\ProductionTemplateModel::where([
                                            'product_id' => $production_cutting->code_of_pant,
                                            'barcode' => $production_cutting->barcode,
                                            'size_code' => $production_cutting->size_code,
                                            'color_code' => $production_cutting->color_code,
                                            ])->selectRaw('sewing_price,id,currency_id')->first();
                                        $currency = findCurrency($template->currency_id??user_currency()->id);
                                    @endphp
                                    <tr class="{{$check?:'bg-danger'}}">
                                        <td>{{$key+=1}}
                                            {!! Form::hidden('detail_id[]',$production_cutting->id) !!}
                                        </td>
                                        <td>
                                            {{$production_cutting->barcode}}
                                        </td>
                                        <td>
                                            {{findSize($production_cutting->size_code)->size??''}}
                                        </td>
                                        <td>
                                            {{findColor($production_cutting->color_code)->name??''}}
                                        </td>
                                        <td>
                                            {{$production_cutting->total_qty}} ឯកតា
                                        </td>
                                        <td>
                                            {{$production_cutting->remain_qty}} ឯកតា
                                        </td>
                                        <td width="150px">
                                            {!! Form::number('c_qty[]', 0,['class'=>'form-control piece_qty','data-stock'=>$production_cutting->remain_qty]) !!}
                                        </td>
                                        <td>
                                            <div class="input-group">
                                                {!! Form::hidden('cutting_price[]', $production_cutting->cutting_price??0) !!}
                                                {!! Form::number('sewing_price[]', $template->sewing_price??0,['class'=>'form-control sewing_price','step'=>'0.001']) !!}
                                                <span class="input-group-addon cursor">{{$currency->symbol}} </span>
                                            </div>
                                            {!! Form::hidden('symbol[]',($currency->symbol??''),['class'=>'symbol']) !!}
                                            {!! Form::hidden('template_id[]',($template->id??0)) !!}
                                        </td>
                                        <td><span class="fa {{$check?' fa-check text-success':' fa-times text-danger'}} "></span></td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                        <div class="col-md-10">
                            <div class="form-group footer-save">
                                {!! Form::hidden('checked_item') !!}
                                <button type="submit" class=" button_primary pull-right"><span class="fa fa-save"></span> {{__('administrator.save')}}</button>
                                <button type="button" onClick="history.go(-1)" class=" button_warning pull-right">
                                    <span class=" fa fa-arrow-circle-left"></span> {{__('administrator.back')}}
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                {!! Form::close() !!}
            </div>
        </div>
    </div>
@endsection
@section('script')
    <script type="text/javascript">
        $(document).ready(function () {
            check_submit();
            $(document).on('keyup', '.piece_qty', function () {
                let stock = toFloat($(this).data('stock'));
                let qty = toFloat($(this).val());
                if (stock < qty) {
                    $(this).val(stock)
                }
            });
            $(document).on('change', 'select[name=tailor_type]', function () {
                let type = $(this).val();
                $.ajax({
                    url: "{{ route('find-warehouse-by-type') }}",
                    method: "post",
                    dataType: 'json',
                    data: {
                        type,
                    },
                    success: function (response) {
                        let row = '<option value="">{{__('administrator.please_select')}}</option>';
                        $.each(response, function (ii, vv) {
                            row += '<option value="' + ii + '">' + vv + '</option>';
                        });
                        $('select[name=tailor_id]').html(row);
                    }
                });
            });
        });

        function check_submit() {
            let check = $('.table_cut_piece').find('span').hasClass('text-danger');
            if (check) {
                $('button[type=submit]').prop('disabled', true);
            } else {
                $('button[type=submit]').prop('disabled', false);
            }
        }
    </script>
@endsection
