<?php
$name = str_slug($form['label'], '');
?>
@push('bottom')
    <script type="text/javascript">
        $(function () {
            $('#form-group-{{$name}} .select2').select2();
        })
    </script>
@endpush
<div >

    @if($form['columns'])
        <div class="col-sm-12">

            <div id='form-{{$name}}' >
                <h4 >{{$form['label']}}
                    @if($form['required'])
                        <span class='text-danger' title='{!! trans('crudbooster.this_field_is_required') !!}'>*</span>
                    @endif
                </h4>
                <div class="">

                    <div class='row'>
                        <div class='col-sm-10'>
                            <div class="">
                                
                                
										
                                    
                                    @foreach($form['columns'] as $col)
                                        
                                        <?php $name_column = $col['name'];?>
                                        <?php $value = $row->$name_column;?>
                                        
                                        <?php 
                                            $validation = explode('|',$col['validation']);
                                            
                                            $required = 'required=' . in_array("required", $validation);
                                            // $readonly = 'readonly=' . in_array("readonly", $validation);
                                            // $min = str_replace(":","=",in_array("min", $validation));
                                            // $max = str_replace(":","=",in_array("max", $validation));
                                        ?>
                                        
                                            {{-- @if($col['type']!='hidden')
                                                <label class="control-label col-sm-2">{{$col['label']}}
                                                    @if(!empty($required)) <span class="text-danger" title="{{trans('crudbooster.this_field_is_required')}}">*</span> @endif
                                                </label>
                                            @endif --}}
                                            
                                                @if($col['type']=='text')
                                                    <div class="{{$col['width']?:'col-sm-10'}} form-group" id='form-group-{{$col['name']}}' style="padding-right: 30px !important">
                                                        <div>
                                                            <label class="control-label ">{{$col['label']}}
                                                                @if(!empty($required)) <span class="text-danger" title="{{trans('crudbooster.this_field_is_required')}}">*</span> @endif
                                                            </label>
                                                        </div>
                                                        <input type='text' title="{{$col['label']}}"
                                                               {{$required}} {{$readonly}} {!!$col['placeholder']!!} {{$disabled}} {{$validation['max']?"maxlength=".$validation['max']:""}} class='form-control '
                                                               name="{{$col['name']}}" id="{{$col['name']}}" value='{{ $value }}'/>
                                                        @if($col['check'])       
                                                        <input class="form-check-input" type="checkbox" value="" id="{{$col['name']}}-checker" name="{{$col['name']}}-checker" {{ $value==$col['check']?"checked":"0" }}>
                                                        <label class="form-check-label" for="{{$col['name']}}-checker">
                                                            {{$col['check']}}
                                                          </label>
                                                        @endif
                                                        <div class="text-danger">{!! $errors->first($col['name'])?"<i class='fa fa-info-circle'></i> ".$errors->first($col['name']):"" !!}</div>
                                                        <p class='help-block'>{{ @$col['help'] }}</p>
                                                    </div>
                                                    
                                                @elseif($col['type']=='date')
                                                    <div class="{{$col['width']?:'col-sm-10'}} form-group" id='form-group-{{$col['name']}}' style="padding-right: 30px !important">
                                                        <div>
                                                            <label class="control-label ">{{$col['label']}}
                                                                @if(!empty($required)) <span class="text-danger" title="{{trans('crudbooster.this_field_is_required')}}">*</span> @endif
                                                            </label>
                                                        </div>
                                                        <div class="input-group">
                                                            <span class="input-group-addon open-datetimepicker"><a><i class='fa fa-calendar '></i></a></span>
                                                            <input type='text' title="{{$col['label']}}" 
                                                                {{$required}} {{$readonly}} {!!$placeholder!!} {{$disabled}} class="form-control notfocus input_date" name="{{$col['name']}}" id="{{$col['name']}}" value='{{ $value }}'/>
                                                        </div>
                                                        <div class="text-danger">{!! $errors->first($col['name'])?"<i class='fa fa-info-circle'></i> ".$errors->first($col['name']):"" !!}</div>
                                                        <p class='help-block'>{{ @$col['help'] }}</p>
                                                    </div>

                                                @elseif($col['type']=='number')
                                                    
                                                    <div class="{{$col['width']?:'col-sm-10'}} form-group" id='form-group-{{$col['name']}}' style="padding-right: 30px !important">
                                                        <div>
                                                            <label class="control-label ">{{$col['label']}}
                                                                @if(!empty($required)) <span class="text-danger" title="{{trans('crudbooster.this_field_is_required')}}">*</span> @endif
                                                            </label>
                                                            <!--@foreach($validation as $valid)-->
                                                            <!--    {{ $valid }}-->
                                                            <!--@endforeach-->
                                                        </div>
                                                        
                                                        <input  id='{{$name_column}}' type='number' {{ $required }} min=0
                                                            {{ ($col['min'])?"min='".$validation['min']."'":"" }} {{ ($validation['max'])?"max='$validation[max]'":"" }} name='{{$col["name"]}}'
                                                            class='form-control {{$validation['required']?"required":""}}'
                                                                {{($validation['readonly']===true)?"readonly":""}} value='{{ ($value?$value:0) }}'
                                                        />
                                                    </div>
                                                
                                                @elseif($col['type']=='hidden')
                                                    <input type="{{$col['type']}}" id="{{$name.$col["name"]}}" name="child-{{$name.$col["name"]}}"
                                                           value="{{$col["value"]}}">
                                                
                                                @elseif($col['type']=='drive')
                                                    <input   id='{{$name_column}}' type='text'
                                                           {{ ($col['max'])?"maxlength='".$col['max']."'":"" }} name='child-{{$col["name"]}}'
                                                           class='{{$name_column}} form-control {{$col['required']?"required":""}}'
                                       
                                                            {{($col['readonly']===true)?"readonly":""}}
                                                            
                                                    />
                                                
                                                @endif

                                                @if($col['help'])
                                                    <div class='help-block'>
                                                        {{$col['help']}}
                                                    </div>
                                                @endif
                                            
                                      

                                        @if($col['formula'])
                                            <?php
                                            $formula = $col['formula'];
                                            $formula_function_name = 'formula'.str_slug($name.$col['name'], '');
                                            $script_onchange = "";
                                            foreach ($form['columns'] as $c) {
                                                if (strpos($formula, "[".$c['name']."]") !== false) {
                                                    $script_onchange .= "
											$('#$name$c[name]').change(function() {
												$formula_function_name();
											});
											";
                                                }
                                                $formula = str_replace("[".$c['name']."]", "\$('#".$name.$c['name']."').val()", $formula);
                                            }
                                            ?>
                                            @push('bottom')
                                                <script type="text/javascript">
                                                    function {{ $formula_function_name }}() {
                                                        var v = {!! $formula !!};
                                                        $('#{{$name_column}}').val(v);
                                                    }

                                                    $(function () {
                                                        {!! $script_onchange !!}
                                                    })
                                                    
                                                    $('#{!!$col['name']!!}-checker').change(
                                                    function(){
                                                        if (this.checked) {
                                                            alert('checked');
                                                        }
                                                    });
           
                                                </script>
                                            @endpush
                                        @endif
                                        @push('bottom')
                                            <script type="text/javascript">
                           
                                                $('#{!!$col['name']!!}-checker').change(
                                                    function(){
                                                        if (this.checked) {
                                                           $('#{!!$col['name']!!}').val("{!!$col['check']!!}");
                                                            $('#{!!$col['name']!!}').prop('readonly', true);
                                                        }else{
                                                            $('#{!!$col['name']!!}').val("");
                                                            $('#{!!$col['name']!!}').prop('readonly', false);
                                                        }
                                                });
                                                
                                                $('#{!!$col['name']!!}').change(
                                                    function(){
                                                          $('#{!!$col['name']!!}-checker').prop('checked', false);
                                                    });
       
                                            </script>
                                        @endpush
                                    @endforeach

                             

                            </div>
                        </div>
                    </div>

                </div>
                <!-- /.box-body -->
            </div>


        </div>
        


    @else

        <div style="border:1px dashed #c41300;padding:20px;margin:20px">
            <span style="background: yellow;color: black;font-weight: bold">CHILD {{$name}} : COLUMNS ATTRIBUTE IS MISSING !</span>
            <p>You need to set the "columns" attribute manually</p>
        </div>
    @endif
</div>

