@extends(branchMenu())
@section('content')
    <?php Fun::lang()?>
    <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.manage_supplier')}}</h3>
                            </div>
                            <div class="pull-right">
                                @can('Suppliers Create')
                                    <a onclick="getHtmlForm('{{route('suppliers.create')}}','{{__('administrator.create').' '.__('administrator.supplier')}}','30%')" class=" button_info"><span class="fa fa-plus"></span> {{__('administrator.create')}}</a>

                                @endcan
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="wrapper-table" style="margin-top: 30px">
                <table id="data_table" width="100%" class="table table-striped table-hover ">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>{{__('administrator.supplier_for')}}</th>
                        <th>{{__('administrator.supplier_name')}}</th>
                        <th>{{__('administrator.supplier_phone')}}</th>
                        <th>{{__('administrator.supplier_address')}}</th>
                        <th>{{__('administrator.created_at')}}</th>
                        <th>{{__('administrator.action')}}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($data as $key => $value)
                        @php
                            $check = \App\Model\stock_inventory\PurchaseModel::where('supplier_id',$value->id)->first();
                        @endphp
                        <tr>
                            <td>{{$key+1}}</td>
                            <td>{{__('administrator.'.$value->type)}}</td>
                            <td><span class="sup_name">{{$value->supplier_name}}</span></td>
                            <td>{{$value->supplier_phone}}</td>
                            <td>{{$value->supplier_address}}</td>
                            <td>{{date('d-m-Y', strtotime($value->created_at)) }}</td>
                            <td>
                                @can('Suppliers Edit')
                                    <a data-href="{{route('suppliers.edit',$value->id)}}" class=" button_warning btn-edit">{{__('administrator.edit')}}
                                    </a>
                                @endcan
                                @can('Suppliers Show')
                                    @if($check)
                                        <a data-href="{{route('suppliers.show',$value->id)}}"
                                           class=" button_primary btn_supplier_info_public">{{__('administrator.history')}}</a>
                                    @endif
                                @endcan
                                @can('Suppliers Delete')
                                    @if(!$check)
                                        <a class="button_danger btn_delete"
                                           data-href="{{route('suppliers.delete')}}?id={{$value->id}}">{{__('administrator.delete')}}</a>
                                    @endif
                                @endcan()

                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @endsection
        @section('script')

            <script type="text/javascript">
                $('#data_table').DataTable({
                    "lengthMenu":[20,30,50,100]

                });
                $('#data_table').on('click','.btn-edit',function () {
                    let href = $(this).data('href');
                    getHtmlForm(href,'{{__('administrator.edit').' '.__('administrator.supplier')}}','30%')
                });
                $('#data_table').on('click', '.btn_delete', function () {
                    let text = $(this).closest('tr').find('.sup_name').text();
                    let url = $(this).data('href');
                    $.confirm({
                        theme: 'modern',
                        title: '{{__('administrator.are_you_sure')}}',
                        content: 'Supplier Name :' + text,
                        buttons: {
                            delete: {
                                btnClass: 'button_danger',
                                text: '{{__('administrator.delete')}}',
                                action: function () {
                                    $.ajax({
                                        method: 'get',
                                        url: url,
                                        success: function (res) {
                                            if (res) {
                                                window.location.reload();
                                            }
                                        },
                                        error: function (xhr) {
                                            alertErrorFormController(xhr);
                                        }
                                    });
                                }
                            },
                            cancel: {
                                btnClass: 'button_warning',
                                text: '{{__('administrator.cancel')}}',
                                action: function () {

                                }
                            },
                        }
                    });
                });


            </script>
@stop


