@extends(branchMenu())
@section('content')
    <?php Fun::lang()?>
    <style>

    </style>
    <div class="page">
        <div class=container-fluid>
            <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.driver')}}</h3>
                            </div>
                            <div class="pull-right">
                                @can('Driver Create')
                                <a class=" button_info create_driver"><span class="fa fa-plus"></span> {{__('administrator.create')}}</a>
                                @endcan
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="wrapper-table">
                <table id="data_table" width="100%" class="table table-striped table-hover ">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>{{__('administrator.name')}}</th>
                        <th>{{__('administrator.phone')}}</th>
                        <th>{{__('administrator.created_at')}}</th>
                        <th>{{__('administrator.action')}}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($data as $key => $value)
                        <tr>
                            <td>{{$key+1}}</td>
                            <td>{{$value->name}}</td>
                            <td>{{$value->phone}}</td>
                            <td>{{date('d-m-Y', strtotime($value->created_at)) }}</td>
                            <td>
                                @can('Driver Edit')
                                <a data-href="{{route('driver.edit',$value->id)}}" class=" button_warning btn_edit">{{__('administrator.edit')}}</a>
                                @endcan
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection
@section('script')

    <script type="text/javascript">
        var $form_content = `
                <input type="text" name="name" class="form-control border-red" required placeholder="{{__('administrator.name')}}" maxlength="50" >
                <br>&nbsp;<input type="text" name="phone" class="form-control border-red" placeholder="{{__('administrator.phone')}}" maxlength="30" >
                `;
        $('.create_driver').on('click', function () {
            form($form_content,'{{__('administrator.submit')}}','{{__('administrator.create')}}','{{route('driver.store')}}');
        });
        $('.btn_edit').on('click', function () {
            var url = $(this).data('href');
            $.ajax({
                url: url,
                dataType: 'json',
                method: 'get'
            }).done(function (response) {
                console.log(response);
                form($form_content,'{{__('administrator.update')}}','{{__('administrator.edit')}}','{{route('driver.store')}}',response.name,response.phone,response.id);
            }).fail(function () {
                console.log(2);
            });
        });

        function form($fcontent='',btntext='',title='',route='',name='',phone='',id=''){
            $.confirm({
                title:title+' {{__('administrator.driver')}}',
                content:$fcontent,
                theme:'modern',
                buttons:{
                    cancel:{
                        text:'{{__('administrator.cancel')}}',
                        btnClass:'button_warning',
                        action:function () {  }
                    },
                    submit:{
                        text:btntext,
                        btnClass:'button_primary',
                        action:function () {
                            let self = this;
                            let phone = self.$content.find('input[name=phone]').val();
                            let name = self.$content.find('input[name=name]').val();
                            self.buttons.submit.disable();
                            $.ajax({
                                url: route,
                                dataType: 'json',
                                data:{
                                    "_token": "{{ csrf_token() }}",
                                    name:name,
                                    phone:phone,
                                    id:id,
                                },
                                method: 'post'
                            }).done(function (response) {
                                if(response=='success'){
                                    window.location.reload();
                                }
                            }).fail(function(xhr){
                                alertErrorFormController(xhr, self)
                            });
                            return false;
                        }
                    },
                }
            });

            if(id){
                setTimeout(function () {
                    $(document).find('input[name=name]').val(name);
                    $(document).find('input[name=phone]').val(phone);
                    $(".focus").focus();
                },1);
            }
        }

        $('#data_table').DataTable({
            "oLanguage": {
                "sSearch": "{{__('administrator.search')}}:",
                "sLengthMenu": "{{__('administrator.display')}} _MENU_ {{__('administrator.record_per_page')}}",
                "sZeroRecords": "{{__('administrator.Nothing_foundsorry')}}",
                "sInfo": "{{__('administrator.showing')}} _START_ {{__('administrator.to')}} _END_ {{__('administrator.of')}} _TOTAL_ {{__('administrator.record')}}",
                "sInfoEmpty": "{{__('administrator.showing')}} 0 {{__('administrator.to')}} 0 {{__('administrator.of')}} 0 {{__('administrator.record')}}",
                "sInfoFiltered": "({{__('administrator.filter_form')}} _MAX_ {{__('administrator.total_records')}})",
                "oPaginate": {
                    "sFirst": "{{__('administrator.first')}}",
                    "sLast": "{{__('administrator.last')}}",
                    "sNext": "{{__('administrator.next')}}",
                    "sPrevious": "{{__('administrator.previous')}}"
                },
            }

        });
    </script>
@stop

    

