@extends('layouts.app')
@section('title','User Management')
@section('content')
    <section class="section">
        <div class="section-header">
            <h1>Master User</h1>
            <div class="section-header-breadcrumb">
                <div class="breadcrumb-item">
                    <a href="">Home</a>
                </div>
                <div class="breadcrumb-item active">Master User</div>
            </div>
        </div>

        <div class="section-body">
            <div class="row mt-4 active" id="tab-semuapengajuan" data-tab-group="mygroup-tab">
                <div class="col-12">
                <div class="card">
                    <div class="card-body">
                        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#add-modal">Tambah Data</button>
                        <br>
                        <div class="clearfix mb-3"></div>
                        <div class="table-responsive">
                            <table class="table table-striped dataTable" id="table-daftar-user" role="grid">
                                <thead>
                                    <tr style="text-align: center;">
                                    <th>No.</th>
                                    <th>Nama</th>
                                    <th>Email</th>
                                    <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @php
                                        $no = 1;
                                    @endphp
                                    @foreach ($user as $tr)
                                        <tr style="text-align: center;">
                                            <td>{{ $no }}</td>
                                            <td style="text-align : left">{{ $tr->name}}</td>
                                            <td style="text-align : left">{{ $tr->email}}</td>
                                            <td>
                                                <button type="button" class="btn btn-info" data-toggle="modal" data-target="#update-modal{{$tr->id}}">Ubah</button>
                                                <button type="button" class="btn btn-danger" data-toggle="modal" data-target="#delete-modal{{$tr->id}}">Hapus</button>

                                                {{-- Modal Add User --}}
                                                <div class="modal fade" id="add-modal" data-backdrop="false" style="background-color: rgba(0, 0, 0, 0.5);">
                                                    <div class="modal-dialog">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <h4 class="modal-title bold">Tambah User Baru</h4>
                                                            </div>

                                                            <form method="POST" action="{{ route('admin.create_user') }}" enctype="multipart/form-data">
                                                                @csrf
                                                                <div class="modal-body">
                                                                    <div class="form-group" style="text-align: left">
                                                                        <label for="name">Nama</label>
                                                                        <input type="text" class="form-control" id="name" name="name" required @if ($errors->has('name')) has-error @endif>
                                                                        @if ($errors->has('name'))
                                                                            <label id="login-error" class="error" for="name" style="color: red">{{$errors->first('name')}}</label>
                                                                        @endif
                                                                    </div>
                                                                    <div class="form-group" style="text-align: left">
                                                                        <label for="email">Email</label>
                                                                        <input type="email" class="form-control" id="email" name="email" required @if ($errors->has('email')) has-error @endif>
                                                                        @if ($errors->has('email'))
                                                                            <label id="login-error" class="error" for="email" style="color: red">{{$errors->first('email')}}</label>
                                                                        @endif
                                                                    </div>
                                                                    <div class="form-group" style="text-align: left">
                                                                        <label for="password">Password</label>
                                                                        <input type="password" class="form-control" id="password" name="password" required @if ($errors->has('password')) has-error @endif>
                                                                        @if ($errors->has('password'))
                                                                            <label id="login-error" class="error" for="password" style="color: red">{{$errors->first('password')}}</label>
                                                                        @endif
                                                                    </div>
                                                                    <div class="form-group" style="text-align: left">
                                                                        <label for="password-confirmation">Password Confirm</label>
                                                                        <input type="password" class="form-control" id="password-confirmation" name="password-confirmation" required @if ($errors->has('password-confirmation')) has-error @endif>
                                                                        @if ($errors->has('password-confirmation'))
                                                                            <label id="login-error" class="error" for="password-confirmation" style="color: red">{{$errors->first('password-confirmation')}}</label>
                                                                        @endif
                                                                    </div>
                                                                </div>
                                                                <div class="modal-footer">
                                                                    <button type="submit" class="btn btn-primary ml-1">Simpan</button>
                                                                    <button type="button" class="btn btn-light-secondary" data-dismiss="modal">Batal</button>
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>
                                                {{-- End of Modal Add User --}}

                                                {{-- Modal Update User --}}
                                                <div class="modal fade" id="update-modal{{$tr->id}}" data-backdrop="false" style="background-color: rgba(0, 0, 0, 0.5);">
                                                    <div class="modal-dialog">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <h4 class="modal-title bold">Ubah User</h4>
                                                            </div>

                                                            <form method="POST" action="{{ route('admin.update_user', ['id' => $tr->id ]) }}" enctype="multipart/form-data">
                                                                @csrf
                                                                @method('put')

                                                                <div class="modal-body">
                                                                    <div class="form-group" style="text-align: left">
                                                                        <label for="name">Nama</label>
                                                                        <input type="text" class="form-control" id="name" name="name" value="{{ $tr->name }}" required @if ($errors->has('name')) has-error @endif>
                                                                        @if ($errors->has('name'))
                                                                            <label id="login-error" class="error" for="name" style="color: red">{{$errors->first('name')}}</label>
                                                                        @endif
                                                                    </div>
                                                                    <div class="form-group" style="text-align: left">
                                                                        <label for="email">Email</label>
                                                                        <input type="email" class="form-control" id="email" name="email" value="{{ $tr->email }}" required @if ($errors->has('email')) has-error @endif>
                                                                        @if ($errors->has('email'))
                                                                            <label id="login-error" class="error" for="email" style="color: red">{{$errors->first('email')}}</label>
                                                                        @endif
                                                                    </div>
                                                                    <div class="form-group" style="text-align: left">
                                                                        <label for="password">Password</label>
                                                                        <input type="password" class="form-control" id="password" name="password" @if ($errors->has('password')) has-error @endif>
                                                                        @if ($errors->has('password'))
                                                                            <label id="login-error" class="error" for="password" style="color: red">{{$errors->first('password')}}</label>
                                                                        @endif
                                                                    </div>
                                                                </div>
                                                                <div class="modal-footer">
                                                                    <button type="submit" class="btn btn-primary ml-1">Simpan</button>
                                                                    <button type="button" class="btn btn-light-secondary" data-dismiss="modal">Batal</button>
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>
                                                {{-- End of Modal Update User --}}

                                                {{-- Modal Delete User --}}
                                                <div class="modal fade" id="delete-modal{{$tr->id}}" data-backdrop="false" style="background-color: rgba(0, 0, 0, 0.5); padding-top:15%;">
                                                    <div class="modal-dialog">
                                                        <div class="modal-content" style="padding-top : 10px">
                                                            <form method="GET" action="{{ route('admin.delete_user', ['id' => $tr->id ]) }}" enctype="multipart/form-data">
                                                                @csrf
                                                                <div class="modal-body" @if ($errors->has('nama')) has-error @endif>
                                                                    <div class="form-group" style="text-align: center">
                                                                        <h5 class="address-title text-center mb-1" id="addNewAddressTitle">Apakah Anda yakin akan menghapus data ini?</h5>
                                                                    </div>
                                                                    <button type="submit" class="btn btn-primary ml-1">Hapus</button>
                                                                    <button type="button" class="btn btn-light-secondary" data-dismiss="modal">Batal</button>
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>
                                                {{-- End of Modal Delete User --}}
                                            </td>
                                        </tr>
                                        @php
                                            $no++;
                                        @endphp
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection

@push('js')
    <script>
        $(document).ready(function() {
            $("#table-daftar-user").dataTable();
        });
    </script>
@endpush