@extends('layouts.web')

@section('css')
<link href="{{ asset('css/dataTables.bootstrap4.min.css') }}" rel="stylesheet" />
<link href="{{ asset('css/sweetalert2.min.css') }}" rel="stylesheet" />
@endsection


@section('content')
<div class="container-fluid pt-3">
    @can('isSuperadminOrAdmin')
    <a href="{{ route('branch.create') }}" class="btn btn-success rounded"><i class="fas fa-plus-circle"></i> เพิ่มสาขา</a>
    @endcan
    <div class="row mt-3">
        <div class="col">
            <div class="card card-outline card-success">
                <div class="card-body">
                    <table class="table" id="branch-table">
                        <thead>
                          <tr>
                            <th scope="col" width="5%">รหัส</th>
                            <th scope="col" width="20%">สาขา</th>
                            <th scope="col" width="40%">ที่ตั้ง</th>
                            <th scope="col" width="25%">หมายเลขโทรศัพท์</th>
                            @can('isSuperadminOrAdmin')
                            <th width="10%"></th>
                            @endcan
                          </tr>
                        </thead>
                        <tbody>
                            @foreach ($branchs as $indexKey => $branch)
                            <tr class="hand-hover">
                                <td class="align-middle">{{ $branch->code }}.</td>
                                <td class="align-middle"><a href="{{ route('branch.show',$branch->id) }}" class="course-title-link">{{ $branch->name }}</a></td>
                                <td class="align-middle">{{ $branch->address }}</td>
                                <td class="align-middle">{{ $branch->phone_1 }} @if($branch->phone_2 != null) , {{ $branch->phone_2 }} @endif</td>
                                @can('isSuperadminOrAdmin')
                                <td class="align-middle text-center">
                                    <a href="{{ route('branch.edit',$branch->id) }}" class="edit-btn ml-2" data-toggle="tooltip" data-placement="top" title="แก้ไขข้อมูล"><i class="far fa-edit"></i></a>
                                    <form id="del-frm{{ $indexKey }}" class="d-inline"
                                        method="POST"
                                        action="{{ route('branch.destroy', $branch->id ) }}"
                                        onsubmit="confirmalert(event, {{ $indexKey }}, 'สาขา')"
                                    >
                                    @csrf
                                    @method('DELETE')
                                        <button type="submit" class="btn del-btn btn-sm ml-2" data-toggle="tooltip" data-placement="top" title="ลบข้อมูล">
                                            <i class="far fa-trash-alt"></i>
                                        </button>
                                    </form>
                                </td>
                                @endcan
                            </tr>
                            @endforeach
                        </tbody>
                      </table>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('footerscript')
<script src="{{ asset('js/sweetalert2.min.js') }}"></script>
<script src="{{ asset('js/jquery.dataTables.min.js') }}"></script>
<script src="{{ asset('js/dataTables.bootstrap4.min.js') }}"></script>
<script src="{{ asset('js/custom.js') }}"></script>
<script>
    $(document).ready(function() {
        $('#branch-table').DataTable({
            "lengthChange": false,
            "info": false,
            "pageLength": 20,
            "language": {
                "emptyTable":   "ไม่พบข้อมูล",
                "paginate": {
                    "first":    "หน้าแรก",
                    "last":     "หน้าสุดท้าย",
                    "next":     "ถัดไป",
                    "previous": "ก่อนหน้า"
                },
                "search":       "ค้นหา:",
                "lengthMenu":   "จำนวนสาขาทั้งหมด _MENU_ สาขา",
                "info":         "ทั้งหมด _TOTAL_ สาขา",
                "infoEmpty":    "ไม่พบสาขา",
            }
        });
        $('#branch-table tr td:not(:last-child)').click(function () {
            var href = $(this).closest('tr').find('a').attr('href');
            location.href = href;
        });
    });
</script>
@if (session('feedback'))
<script>
    Swal.fire({
                text: '{{ session('feedback') }}',
                icon: 'success',
                confirmButtonText: 'ตกลง',
                confirmButtonColor: '#62966C'
    })
</script>
@endif
@endsection

