@extends('layouts.app')

@section('title')
  Dashboard
@endsection

@section('admin-content')
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>Data Master Cabang</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="{{ route('dashboard.index') }}">Dashboard</a></li>
                        </li>
                    </ol>
                </div>
            </div>
        </div>
    </section>
    <section class="content">
        <div class="container-fluid">
            <div class="card card-primary card-outline">
                <div class="card-body">
					<h4 class="header-title float-left">List Data Cabang</h4>
                    <p class="float-right mb-2">
                        @if (Auth::user()->can('cabang.create'))
                            <a class="btn btn-primary text-white" href="{{ route('admin.cabang.create') }}">
                                Add New Cabang
                            </a>
                        @endif
                    </p>
                    <div class="clearfix"></div>
                    <div class="dataTables_wrapper dt-bootstrap4 mt-2">
                        @include('backend.layouts.partials.messages')
                        <div class="table-responsive">
                            <table id="dataTable"
                                class="table table-bordered table-striped table-head-fixed dataTable dtr-inline"
                                style="width:100%">
                                <thead class="bg-light text-capitalize">
                                    <tr>
                                        <th>#</th>
                                        <th>Nama Kepala Cabang</th>
                                        <th>Email Kepala Cabang</th>
                                        <th>Region</th>
                                        <th>Code</th>
                                        <th>Nama</th>
                                        <th>Cost Centre</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($cabang as $item)
                                        <tr>
                                            <td>{{ $loop->index + 1 }}</td>
                                            <td>{{ $item->head->name }}</td>
                                            <td>{{ $item->head->email }}</td>
                                            <td>{{ $item->region->name }}</td>
                                            <td>{{ $item->code }}</td>
                                            <td>{{ $item->name }}</td>
                                            <td>{{ $item->cost->name }} ({{ $item->cost->code }})</td>
                                            <td>
                                                @if (auth()->user()->can('cabang.edit'))
                                                    <a class="btn btn-success btn-sm text-white" href="{{ route('admin.cabang.edit', $item->id) }}">Edit</a>
                                                @endif
                                                @if (auth()->user()->can('cabang.delete'))
                                                    <a class="btn btn-danger text-white btn-sm" href="javascript:void(0);"
                                                        onclick="event.preventDefault(); if(confirm('Are you sure you want to delete?')) { document.getElementById('delete-form-{{ $item->id }}').submit(); }">
                                                        {{ __('Delete') }}
                                                    </a>

                                                    <form id="delete-form-{{ $item->id }}"
                                                        action="{{ route('admin.cabang.destroy', $item->id) }}"
                                                        method="POST" style="display: none;">
                                                        @method('DELETE')
                                                        @csrf
                                                    </form>
                                                @endif
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
					</div>
                </div>
            </div>
        </div>
    </section>
@endsection

@section('scripts')
    <script>
        $(document).ready(function () {
            if ($('#dataTable').length) {
				$('#dataTable').DataTable({
					responsive: false,
					dom: 'Blfrtip',
					lengthMenu: [ [10, 50, 100, -1], [10, 50, 100, "All"] ], // Control the entries dropdown
					buttons: [
						{
							extend: 'excel',
							text: 'Excel'
						},
						{
							extend: 'pdf',
							text: 'PDF',
							orientation: 'landscape',
							pageSize: 'A4',
							exportOptions: {
								columns: ':visible'
							}
						},
						{
							extend: 'print',
							text: 'Print',
							orientation: 'landscape',
							exportOptions: {
								columns: ':visible'
							}
						},
						'colvis'
					]
				});
			}
        });
    </script>
@endsection
