@extends('layout.main')
@section('body')
    @php
        $menu = 'Data Master';
        $pageTitle = 'Data User';
    @endphp

    @include('layout.navbar')
    @include('layout.sidebar')

    {{-- Main Content --}}
    <div class="main-container">
        <div class="pd-ltr-20 xs-pd-20-10">
            <div class="min-height-200px">
                <div class="page-header">
                    <div class="row">
                        <div class="col-md-6 col-sm-12">
                            <div class="title">
                                <h4>{{ $menu }}</h4>
                            </div>
                            <nav aria-label="breadcrumb" role="navigation">
                                <ol class="breadcrumb">
                                    <li class="breadcrumb-item"><a href="{{ route('dashboard.index') }}">Dashboard</a></li>
                                    @if (isset($pageTitle))
                                        <li class="breadcrumb-item active" aria-current="page">{{ $pageTitle }}</li>
                                    @else
                                        <li class="breadcrumb-item active" aria-current="page"></li>
                                    @endif
                                </ol>
                            </nav>
                        </div>
                    </div>
                </div>
                <div class="card-box mb-30">
                    <div class="pd-20">
                        <h4 class="text-blue h4 mb-0">Data User</h4>
                    </div>
                    <div class="card-header d-flex align-items-center">
                        <!-- Button trigger modal -->
                        <button type="button" class="btn btn-primary mx-2" data-toggle="modal" data-target="#add-user">
                            <i class="icon dw dw-user"></i> Tambah User</button>
                        <button type="button" class="btn btn-primary mx-2" data-toggle="modal" data-target="#import-user">
                            <i class="icon dw dw-download"></i> Import User</button>
                        <a href="/export-user"><button type="button" class="btn btn-primary mx-2">
                            <i class="icon dw dw-upload"></i> Export User</button></a>
                        <form class="form-inline ml-auto" action="{{ route('user.index') }}" method="GET">
                            <div class="input-group">
                                <input type="search" class="form-control" name="search" placeholder="Search">
                                <div class="input-group-append">
                                    <button class="btn btn-primary" type="submit">
                                        <i class="icon dw dw-search"></i>
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="pb-10">
                        <div class="table-responsive">
                            <table class="data-table table stripe hover nowrap">
                                <thead>
                                    <tr>
                                        <th>No</th>
                                        <th>Nama</th>
                                        <th>Username</th>
                                        <th>Email</th>
                                        <th>Role</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse ($users as $index => $user)
                                        <tr>
                                            <td>{{ $index + $users->firstItem() }}</td>
                                            <td>{{ $user->name }}</td>
                                            <td>{{ $user->username }}</td>
                                            <td>{{ $user->email }}</td>
                                            <td>{{ $user->role }}</td>
                                            <td>
                                                <a href="{{ route('user.edit', $user->id) }}"
                                                    class="badge btn-primary" data-toggle="modal"
                                                    data-target="#edit-user-{{ $user->id }}" title="Edit">
                                                    <i class="icon dw dw-edit2"></i>
                                                </a>
                                                <a href="{{ route('user.destroy', $user->id) }}" class="badge btn-danger"
                                                    data-toggle="modal" data-target="#delete-user-{{ $user->id }}"
                                                    title="Delete">
                                                    <i class="icon dw dw-delete-3"></i>
                                                </a>
                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="5" class="text-center">Tidak ada data.</td>
                                        </tr>
                                    @endforelse
                                </tbody>
                            </table>
                            <div>
                                <div style="display: flex; justify-content: space-between; align-items: center; margin: 0 20px;">
                                    <div style="font-size: 14px;">
                                        Showing data {{ $users->firstItem() }} to {{ $users->lastItem() }} of {{ $users->total() }} entries
                                    </div>
                                    <div style="font-size: 14px;">{{ $users->links() }}</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="footer-wrap pd-20 mb-20 card-box">
                <strong>Sistem Pendukung Keputusan Pemilihan Ekstrakurikuler Metode SMARTER</strong>
            </div>
        </div>
    </div>

    {{-- Modal Add --}}
    <div class="modal fade" id="add-user" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel"
        style="display: none;" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                @if (session('error'))
                    <div class="alert alert-danger">{{ session('error') }}</div>
                @endif
                <div class="modal-header">
                    <h4 class="modal-title" id="myLargeModalLabel">
                        Tambah User
                    </h4>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                </div>
                <div class="modal-body">
                    <form action="{{ route('user.store') }}" class="needs-validation" novalidate="" method="POST">
                        @csrf
                        <div class="card-body">
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Nama</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="name" name="name" required="">
                                    <div class="invalid-feedback">
                                        Tolong isi Nama User!
                                    </div>
                                </div>
                            </div>
                            @if ($errors->has('name'))
                                <div class="alert alert-danger">{{ $errors->first('name') }}</div>
                            @endif
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Username</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="username" name="username"
                                        required="">
                                    <div class="invalid-feedback">
                                        Tolong Isi Username!
                                    </div>
                                </div>
                            </div>
                            @if ($errors->has('username'))
                                <div class="alert alert-danger">{{ $errors->first('username') }}</div>
                            @endif
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Email</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="email" name="email"
                                        required="">
                                    <div class="valid-feedback">
                                        Lengkap!
                                    </div>
                                </div>
                            </div>
                            @if ($errors->has('role'))
                                <div class="alert alert-danger">{{ $errors->first('role') }}</div>
                            @endif
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Role</label>
                                <div class="col-sm-10">
                                    <select class="form-control" name="role" id="role" required>
                                        <option value="" disabled>Pilih Role</option>
                                        @foreach(\App\Models\User::getPossibleEnumValues('role') as $role)
                                            <option value="{{ $role }}" {{ Auth::user()->role == $role ? 'selected' : '' }}>{{ $role }}</option>
                                        @endforeach
                                    </select>
                                    <div class="valid-feedback">
                                        Lengkap!
                                    </div>
                                </div>
                            </div>
                            @if ($errors->has('role'))
                                <div class="alert alert-danger">{{ $errors->first('role') }}</div>
                            @endif
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                            <button type="submit" class="btn btn-primary">Simpan</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    {{-- Modal Edit --}}
    @foreach ($users as $user)
    <div class="modal fade" id="edit-user-{{ $user->id }}" tabindex="-1" role="dialog"
        aria-labelledby="myLargeModalLabel" style="display: none;" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="myLargeModalLabel">
                        Edit User
                    </h4>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                </div>
                <div class="modal-body">
                    <form action="{{ route('user.update', $user->id) }}" class="needs-validation"
                        novalidate="" method="POST">
                        @csrf
                        @method('PUT')
                        <div class="card-body">
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Nama</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="name" name="name"
                                        required="" value="{{ $user->name }}" readonly>
                                    <div class="invalid-feedback">
                                        Tolong isi Nama User!
                                    </div>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Username</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="username" name="username"
                                        required="" value="{{ $user->username }}" readonly>
                                    <div class="invalid-feedback">
                                        Tolong isi Nama Username!
                                    </div>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Email</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="email" name="email"
                                        required="" value="{{ $user->email }}" readonly>
                                    <div class="invalid-feedback">
                                        Tolong isi Email!
                                    </div>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Role</label>
                                <div class="col-sm-10">
                                    <select class="form-control" name="role" id="role" required>
                                        <option value="" disabled>Pilih Role</option>
                                        @foreach(\App\Models\User::getPossibleEnumValues('role') as $role)
                                            <option value="{{ $role }}" {{ $user->role == $role ? 'selected' : '' }}>{{ $role }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                            <button type="submit" class="btn btn-primary">Simpan</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

        <!-- Modal Delete -->
        <div class="modal fade" id="delete-user-{{ $user->id }}" tabindex="-1" role="dialog"
            aria-labelledby="myLargeModalLabel" style="display: none;" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title" id="myLargeModalLabel">
                            Konfirmasi Hapus Data User
                        </h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                    </div>
                    <div class="modal-body">
                        <p>Apakah Anda yakin ingin menghapus data user {{ $user->name }} ini?</p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                        <form action="{{ route('user.destroy', $user->id) }}" method="POST">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-danger">Hapus</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <!-- Modal Import-->
        <div class="modal fade center-modal" id="import-user" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" style="display: none;" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="myLargeModalLabel">Import User</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                </div>
                <div class="modal-body">
                    <form action="{{ route('import-user') }}" class="needs-validation" novalidate=""
                        method="POST" enctype="multipart/form-data">
                        @csrf
                        <div class="card-body">
                            <div class="form-group row">
                                <div class="col-sm-12">
                                    <input type="file" class="form-control" id="file" name="file"
                                        required="">
                                    <div class="invalid-feedback">
                                        Tolong upload sebuah file!
                                    </div>
                                    <label class="col-sm-12 col-form-label"> - Silakan mengunduh template terlebih dahulu
                                        <br> - Upload file dengan bentuk format (.xlxs)
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <a href="{{ route('download-user-template') }}" class="btn btn-info">Unduh Template</a>
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                            <button type="submit" class="btn btn-primary">Simpan</button>
                        </div>
                    </form>
                </div>
            </div>
            </div>
        </div>
    </div>
    @endforeach
@endsection
