@extends('backend.app')
@section('title',' Organizations')
@section('content')
    <h2 class="ms-2 mb-3 card-title">{{__('messages.organizations') ." / ". __('messages.branch') ." ". __('messages.list')}}</h2>
    <div class="card  mt-3 m-lg-2">
        <div class="card-body table_card  mt-2">
            @can('organization.create')
                <div class="add-new-button-hidden">
                    <button class="btn add_new_btn" data-bs-toggle="modal" data-bs-target="#organization_add_modal"
                            ><i class="plus-ico"></i> {{__('messages.add') ." ". __('messages.organizations')}}
                    </button>
                </div>
            @endcan
            <table id="myTable" class="table table-bordered dt-responsive nowrap w-100">
                <thead>
                <tr>
                    <th>{{__('messages.sl') }}</th>
                    <th>{{__('messages.name') }}</th>
                    <th>{{__('messages.owner') ." ". __('messages.name')}}</th>
                    <th>{{__('messages.address') }}</th>
                    <th>{{__('messages.phone') }}</th>
                    <th>{{__('messages.action') }}</th>
                </tr>
                </thead>
                <tbody>
                @foreach ($organizations as $key=>$item)
                    <tr>
                        <td>{{++$key}}</td>
                        <td>{{$item->name}}</td>
                        <td>{{($item->owner_name) ?? 'N/A'}}</td>
                        <td>{{($item->address) ?? 'N/A'}}</td>
                        <td>{{($item->phone) ?? 'N/A'}}</td>
                        <td class="center">
                            @can('organization.edit')
                                <a id="edit_btn" edit-id="{{ $item->id }}" class="btn" data-bs-toggle="modal"
                                   data-bs-target="#organization_edit_modal"><i class="edit-ico"></i></a>
                            @endcan
                            @can('organization.delete')
                                <a href="#" class="btn" onclick="deletePost({{ $item->id }})">
                                    <img src="{{ asset('assets/icons/svg/delete.svg') }}" alt="" />
                                    <form id="delete-form-{{ $item->id }}"
                                          action="{{ route('organizations.destroy', $item->id) }}" method="POST"
                                          style="display: none;">
                                        @csrf
                                        @method('DELETE')
                                    </form>
                                </a>
                            @endcan
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>

    {{--   organization add modal--}}
    <div class="modal fade" id="organization_add_modal" tabindex="-1" aria-labelledby="exampleModalLabel"
         aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-centered">
            <div class="modal-content p-5">
                <h4 class="card-title">{{__('messages.organizations') ." ". __('messages.information')}}</h4>
                <div class="modal-body p-0 mt-3">
                    <form action="{{ route('organizations.store') }}" method="POST" role="form" id="form1"
                          enctype="multipart/form-data">
                        @csrf
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="organizationname">{{ __('messages.name') }} <span
                                            class="text-danger">*</span></label>
                                    <input type="text" class="form-control @error('name') is-invalid @enderror"
                                           name="name" placeholder="{{__('messages.enter') ." ". __('messages.organizations')." ". __('messages.name')}}" id="organizationname" required/>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="ownername">{{__('messages.owner')." ". __('messages.name')}}<span
                                            class="text-danger">*</span></label>
                                    <input type="text" class="form-control @error('owner_name') is-invalid @enderror"
                                           name="owner_name" placeholder="{{__('messages.enter') ." ". __('messages.owner')." ". __('messages.name')}}" required id="ownername"/>
                                </div>
                            </div>
                        </div>
                        <div class="row mt-3">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="organizationaddress">{{ __('messages.address') }}</label>
                                    <input type="text" class="form-control" name="address" placeholder="{{__('messages.enter') ." ". __('messages.address')}}"
                                           id="organizationaddress"/>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="organizationphone">{{ __('messages.phone') }} <span
                                            class="text-danger">*</span></label>
                                    <input type="text" class="form-control @error('phone') is-invalid @enderror"
                                           name="phone" placeholder="{{__('messages.enter') ." ". __('messages.phone')}}" id="organizationphone" required/>
                                </div>
                            </div>
                        </div>
                        <div class="mt-5 d-flex justify-content-end ">
                            <button type="button" class="btn  me-2" data-bs-dismiss="modal"
                                    style="background: #EAECF0;">{{ __('messages.close') }}
                            </button>
                            @can('organization.create')
                                <button type="submit" class="btn btn-primary" >
                                    {{ __('messages.save') }}
                                </button>
                            @endcan
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="organization_edit_modal" tabindex="-1" aria-labelledby="exampleModalLabel"
         aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-centered">
            <div class="modal-content p-5">
                <h4 class="card-title">Organization Information</h4>
                <div class="modal-body p-0 mt-3">
                    <form action="{{ route('organizations.new.update') }}" method="POST" role="form" id="form1"
                          enctype="multipart/form-data">
                        @csrf
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="name">{{ __('messages.name') }} <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control @error('name') is-invalid @enderror"
                                           name="name" id="name" placeholder="{{__('messages.enter') ." ". __('messages.organizations')." ". __('messages.name')}}" required/>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="owner_name">{{__('messages.owner')." ". __('messages.name')}}<span
                                            class="text-danger">*</span></label>
                                    <input type="text" class="form-control @error('owner_name') is-invalid @enderror"
                                           name="owner_name" id="owner_name" placeholder="{{__('messages.enter') ." ". __('messages.owner')." ". __('messages.name')}}" required/>
                                </div>
                            </div>
                        </div>
                        <div class="row mt-3">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="address">{{ __('messages.address') }}</label>

                                    <input type="text" class="form-control" name="address" id="address"
                                           placeholder="{{__('messages.enter') ." ". __('messages.address')}}"/>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="phone">{{ __('messages.phone') }} <span
                                            class="text-danger">*</span></label>

                                    <input type="text" class="form-control @error('phone') is-invalid @enderror"
                                           name="phone" id="phone" placeholder="{{__('messages.enter') ." ". __('messages.phone')}}"/>
                                </div>
                            </div>
                        </div>
                        <input type="hidden" name="item_id" id="item_id">
                        <div class="mt-5 d-flex justify-content-end ">
                            <button type="button" class="btn  me-2" data-bs-dismiss="modal"
                                    style="background: #EAECF0;"> {{ __('messages.close') }}
                            </button>
                            @can('organization.edit')
                                <button type="submit" class="btn btn-primary" >
                                    {{ __('messages.update') }}
                                </button>
                            @endcan
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection
@push('scripts')
    <script type="text/javascript">
        $(document).ready(function ($) {
            $(document).on('click', '#edit_btn', function () {
                let id = $(this).attr('edit-id');
                $.ajax({
                    type: "GET",
                    url: "{{ url('system/organizations') }}/" + id + "/edit",
                    success: function (res) {
                        $('#name').val(res.name);
                        $('#owner_name').val(res.owner_name);
                        $('#address').val(res.address);
                        $('#phone').val(res.phone);
                        $('#item_id').val(res.id);
                    },
                    error: function (response) {
                        console.log(response);
                    }
                });
            });
        });
    </script>
@endpush
