@extends(branchMenu())
@section('content')
    <style>
        .file-drag-handle, .glyphicon-plus-sign, .kv-file-upload, .kv-file-download, .fileinput-upload {
            display: none !important;
        }
    </style>
    <?php Fun::lang() ?>

    <div class="page">
        <div class=container>
            <div class="top-content">
                <div class="row">
                    <div class="col-sm-12 col-xs-12">
                        <div class="title-add">
                            <div class="pull-left">
                                <h3>{{__('administrator.edit_product')}}</h3>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {!! Form::model($product, ['method' => 'PATCH','route' => ['products.update', $product->id],'enctype'=>'multipart/form-data','class'=>'jform']) !!}
            @php
                $dup = @$_GET['dd']?encrypt(@$_GET['dd']):'';
            @endphp
            <div class="row" id="tabs">
                <div class="tab-content">
                    <div id="home" class="tab-pane fade in active">
                        <div class="col-sm-12">
                            <div class="master-form">
                                <div class="title-note">
                                    <h3>
                                        <span class="fa fa-exclamation-triangle"></span> {{__('administrator.edit')}}
                                        <i>({{__('administrator.fields_in_red_are_required')}})</i></h3>
                                </div>
                                <div class="form-group col-sm-12 col-xs-12">
                                    <div class="col-sm-2 col-xs-12 ">
                                        <label class="required" for="name">{{__('administrator.item_name')}} </label>
                                    </div>
                                    <div class="col-sm-10 col-xs-12">
                                        {!! Form::text('product_name', ($dup?($product->product_name.' (1)'):$product->product_name), array('placeholder' => __('administrator.item_name'),'class' => 'form-control input-sm','','maxlength'=>'50')) !!}
                                        {!! Form::hidden('duplicate', $dup) !!}
                                    </div>
                                </div>
                                <div class="form-group col-sm-12 col-xs-12">
                                    <div class="col-sm-2 col-xs-12 ">
                                        <label class="required" for="name">{{__('administrator.barcode')}} </label>
                                    </div>
                                    <div class="col-sm-9 col-xs-11">
                                        {!! Form::text('barcode', ($dup?($product->barcode.' (1)'):$product->barcode), array('placeholder' => __('administrator.product_code'),'class' => 'form-control input-sm')) !!}
                                    </div>
                                    <div class="col-sm-1 col-xs-1">
                                        <button type="button" class="btn-info btn_gen_barcode"><span class="fa fa-cogs"></span></button>
                                    </div>
                                </div>

                                <div class="form-group col-sm-12 col-xs-12">
                                    <div class="col-sm-2 col-xs-12 ">
                                        <label for="name" class="required">{{__('administrator.unit')}}</label>
                                    </div>
                                    <div class="col-sm-10 paddin-0">
                                        <div class="col-sm-12" id="get_selectUnit">
                                            {!! Form::select('unit_id', $unit, $product_unit[0]->unit_id, ['placeholder' => __('administrator.please_select'),'class' => 'form-control','required']) !!}
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group col-sm-12 col-xs-12">
                                    <div class="col-sm-2 col-xs-12 ">
                                        <label for="name" class="required">{{__('administrator.brands')}}</label>
                                    </div>
                                    <div class="col-sm-10 paddin-0">
                                        <div class="col-sm-12">
                                            {!! Form::select('brand', $brand, null, ['placeholder' => __('administrator.please_select'),'class' => 'form-control select2']) !!}
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group col-sm-12 col-xs-12">
                                    <div class="col-sm-2 col-xs-12 ">
                                        <label for="name" class="required">{{__('administrator.category')}}</label>
                                    </div>
                                    <div class="col-sm-10 paddin-0">
                                        <div class="col-sm-12">
                                            {!! Form::select('category_id', $category, null, ['placeholder' => __('administrator.please_select'),'class' => 'form-control select2','']) !!}
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group col-sm-12 col-xs-12">
                                    <div class="col-sm-2 col-xs-12 ">
                                        <label>{{__('administrator.qty_alert')}}</label>
                                    </div>
                                    <div class="col-sm-10 paddin-0">
                                        <div class="col-sm-12">
                                            {!! Form::text('alert_qty', null,['placeholder' =>0,'class' => 'form-control']) !!}
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group col-sm-12 col-xs-12">
                                    <div class="col-sm-10 col-xs-10 col-md-10 col-sm-offset-2 col-xs-offset-2 col-md-offset-2">
                                        <table class="table table-bordered">
                                            <thead>
                                            <tr>
                                                <th>#</th>
                                                <th width="110px"><label class="required">{{__('administrator.size')}}</label></th>
                                                <th width="100px"><label class="required">{{__('administrator.color')}}</label></th>
                                                <th><label>{{__('administrator.cost')}} <br>({{__('administrator.riel')}})</label></th>
                                                <th><label>{{__('administrator.cost')}} <br>({{__('administrator.usd')}})</label></th>
                                                <th width="120px"><label class="required">{{__ ('administrator.barcode')}}</label>
                                                </th>
                                                <th style="text-align: center"><span class="cursor button_success fa fa-plus" id="add_unit"></span></th>
                                            </tr>
                                            </thead>
                                            <tbody id="tbody">
                                            @foreach($product_unit as $key => $row)
                                                <tr>
                                                    <td class="count_index{{$key+1}}">{{$key+1}}{!! Form::hidden('id[]', $row->id) !!}</td>
                                                    <td>{!! Form::select('size_code[]',$size, $row->size_code, array('placeholder' => __('administrator.size'),'class' => 'form-control input-sm size_code')) !!}</td>
                                                    <td>{!! Form::select('color_code[]',$color, $row->color_code, array('placeholder' => __('administrator.color'),'class' => 'form-control input-sm color_code')) !!}</td>
                                                    <td>{!! Form::text('cost_riel[]', $row->cost_riel, array('placeholder' => __('administrator.cost'),'class' => 'form-control input-sm number-only cost_riel','required')) !!}</td>
                                                    <td>{!! Form::text('cost_usd[]', $row->cost_usd, array('placeholder' => __('administrator.cost'),'class' => 'form-control input-sm number-only cost_usd','required')) !!}</td>
                                                    <td>{!! Form::text('barcode_child[]', $row->barcode, array('placeholder' => __('administrator.barcode'),'class' => 'form-control input-sm barcode_child')) !!}</td>
                                                    <td style="text-align: center">
                                                        <span class="cursor button_danger fa {{($row->qty_per_unit==1?' fa-ban ':' remove_row fa-close ')}}"></span>
                                                    </td>
                                                </tr>
                                            @endforeach
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                                <div class="col-sm-12 col-xs-12">
                                    <div class="form-group footer-save">
                                        <button type="button" class=" button_primary pull-right submit_form"><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>
                    </div>
                </div>
            </div>
            {!! Form::close() !!}
        </div>
    </div>
@endsection
@section('script')
    <script type="text/javascript">
        $(document).ready(function () {
            $('.submit_form').on('click', function () {
                let array = [];
                let count = 0;
                $('.barcode_child').each(function (ii, vv) {
                    array[ii] = vv.value;
                });
                let sorted = array.sort();
                for (let i = 0; i < sorted.length - 1; i++) {
                    if (sorted[i + 1] === sorted[i]) {
                        count++;
                    }
                }
                if (count) {
                    $.alert('{{__('administrator.barcode_is_duplicated')}}', '{{__('administrator.warning')}}');
                } else {
                    $('.jform').submit();
                }
            });

            $('#add_unit').on('click', function () {
                add_unit();
            });
            $(document).on('click', '.remove_row', function () {
                $(this).closest("tr").remove();
            });
            $(document).on('change', '.size_code,.color_code', function () {
                concat_code($(this));
            });
        });


        function add_unit() {
            let count = $('#tbody tr').length;
            let row = `<tr>
                                        <td class="count_index` + (count + 1) + `">` + (count + 1) + `{!! Form::hidden('id[]', 0) !!}</td>
                                        <td>
                                        {!! Form::select('size_code[]', $size,null, ['placeholder' => __('administrator.size'),'class' => 'form-control size_code','']) !!}
            </td>
            <td>
{!! Form::select('color_code[]', $color,null, ['placeholder' => __('administrator.color'),'class' => 'form-control color_code','']) !!}
            </td>
            <td><input name="cost_riel[]" type="text" class="form-control input-sm number-only cost_riel" placeholder="{{__('administrator.cost')}}" required></td>
                                        <td><input name="cost_usd[]" type="text" class="form-control input-sm number-only cost_usd" placeholder="{{__('administrator.cost')}}" required></td>

            <td><input name="barcode_child[]" type="text" class="form-control input-sm number-only barcode_child" value="" placeholder="{{__('administrator.barcode')}}"></td>
                                        <td style="text-align: center"><span class="cursor button_danger fa fa-close remove_row" ></span></td>
                             </tr>`;
            $('#tbody').append(row);
            let old_row = $(document).find('.count_index' + count).closest('tr');
            let old_size = old_row.find('.size_code').val();
            let old_color = old_row.find('.color_code').val();
            let old_cost_usd = old_row.find('.cost_usd').val();
            let old_cost_riel = old_row.find('.cost_riel').val();
            let old_wholesale_price_usd = old_row.find('.wholesale_price_usd').val();
            let old_wholesale_price_riel = old_row.find('.wholesale_price_riel').val();
            let old_retail_price_usd = old_row.find('.retail_price_usd').val();
            let old_retail_price_riel = old_row.find('.retail_price_riel').val();
            let old_barcode_child = old_row.find('.barcode_child').val();

            let new_row = $(document).find('.count_index' + (count + 1)).closest('tr');
            new_row.find('.size_code').val(old_size).change();
            new_row.find('.color_code').val(old_color).change();
            new_row.find('.cost_usd').val(old_cost_usd);
            new_row.find('.cost_riel').val(old_cost_riel);
            new_row.find('.wholesale_price_usd').val(old_wholesale_price_usd);
            new_row.find('.wholesale_price_riel').val(old_wholesale_price_riel);
            new_row.find('.retail_price_usd').val(old_retail_price_usd);
            new_row.find('.retail_price_riel').val(old_retail_price_riel);
            new_row.find('.barcode_child').val(old_barcode_child);
        }

        function generate_sub_barcode(this_) {
            let size_text = this_.closest('tr').find('.size_id option:selected').text();
            size_text = parseFloat(size_text);
            let size = this_.closest('tr').find('.size_id').val();
            let barcode = $('.barcode').val();
            if (barcode) {
                if (size_text > 10) {
                    this_.closest('tr').find('.barcode_child').val(barcode + '' + size_text);
                } else {
                    if (size) {
                        this_.closest('tr').find('.barcode_child').val(barcode + '' + size);
                    }
                }
            } else {
                this_.closest('tr').find('.barcode_child').val('');
            }
        }

        function concat_code(this_) {
            let color = this_.closest('tr').find('.color_code').val();
            let size = this_.closest('tr').find('.size_code').val();
            let barcode = $('input[name=barcode]').val();
            this_.closest('tr').find('.barcode_child').val(barcode + '' + size + '' + color);
        }
    </script>
@endsection
