@extends('layouts.admin')
@section('styles')
    <link rel="stylesheet" type="text/css" href="{{ asset('assets/admin/css/vendors/datatables.css') }}">
@endsection
@section('app')
    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-12">
                @if (in_array(auth()->user()->role_id, [1, 2]))
                    <div class="list-product-header">
                        <div>
                            <a class="btn btn-primary" href="{{ route('master.user.create') }}">Tambah</a>
                        </div>
                    </div>
                @endif
                <div class="card">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="display" id="datatable">
                                <thead>
                                    <tr>
                                        <th>No</th>
                                        <th>Nama Lengkap</th>
                                        <th>Nama Pengguna</th>
                                        <th>Email</th>
                                        <th>Barbershop</th>
                                        <th>Kontak</th>
                                        <th>Role</th>
                                        <th>Gaji Pokok</th>
                                        <th>Status</th>
                                        <th>Tanggal Bekerja</th>
                                        @if (in_array(auth()->user()->role_id, [1, 2]))
                                            <th>Aksi</th>
                                        @endif
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($users as $user)
                                        <tr>
                                            <td>{{ $loop->iteration }}</td>
                                            <td>{{ $user->biography->full_name ?? '-' }}</td>
                                            <td>{{ $user->username }}</td>
                                            <td>{{ $user->email }}</td>
                                            <td>{{ $user->biography->contact_number ?? '-' }}</td>
                                            <td>{{ $user->barbershop->name ?? '-' }}</td>
                                            <td>{{ $user->role->name ?? '-' }}</td>
                                            <td>{{ formatToRupiah($user->base_salary) }}</td>
                                            <td>
                                                {!! $user->is_active === '1'
                                                    ? '<span class="badge bg-success">Aktif</span>'
                                                    : '<span class="badge bg-danger">Tidak Aktif</span>' !!}
                                            </td>
                                            <td>{{ formatDateIndo($user->entry_date) }}</td>
                                            @if (in_array(auth()->user()->role_id, [1, 2]))
                                                <td>
                                                    <ul class="action">
                                                        <li class="{{ $user->is_active ? 'nonactivate' : '7366ff' }}">
                                                            <a href="javascript:void(0)"
                                                                onclick="event.preventDefault(); document.getElementById('activation-form-{{ $user->id }}').submit();"
                                                                data-bs-toggle="tooltip" data-bs-placement="left"
                                                                data-bs-title="{{ $user->is_active ? 'Nonaktifkan' : 'Aktifkan' }}">
                                                                <i
                                                                    data-feather="{{ $user->is_active ? 'x' : 'check' }}"></i>
                                                            </a>
                                                            <form
                                                                action="{{ route('master.user.activation', encryptId($user->id)) }}"
                                                                method="post" id="activation-form-{{ $user->id }}"
                                                                style="display: none;">
                                                                @csrf
                                                                @method('put')
                                                            </form>
                                                        </li>
                                                        <li class="reset-password">
                                                            <form
                                                                action="{{ route('master.user.resetPassword', encryptId($user->id)) }}"
                                                                method="post">
                                                                @csrf
                                                                @method('put')
                                                                <a href="javascript:void(0)" class="reset-password"
                                                                    data-bs-toggle="tooltip" data-bs-placement="left"
                                                                    data-bs-title="Reset Kata Sandi">
                                                                    <i data-feather="unlock"></i>
                                                                </a>
                                                            </form>
                                                        </li>
                                                        <li class="show">
                                                            <a href="javascript:void(0)" data-bs-toggle="modal"
                                                                data-bs-target="#showModal" id="tooltipTrigger"
                                                                data-full-name="{{ $user->biography->full_name ?? '-' }}"
                                                                data-barbershop="{{ $user->barbershop->name ?? '-' }}"
                                                                data-pob="{{ $user->biography->pob ?? '-' }}"
                                                                data-dob="{{ formatDateIndo($user->biography->dob) ?? '-' }}"
                                                                data-address="{{ $user->biography->address ?? '-' }}"
                                                                data-gender="{{ $user->biography->gender ?? '-' }}"
                                                                data-sop="{{ $user->sop ?? '-' }}"
                                                                data-avatar="{{ optional($user->attachments()->where('type', 'avatar')->first())->formatted_attachment ? Storage::url('users/' . optional($user->attachments()->where('type', 'avatar')->first())->formatted_attachment) : '' }}"
                                                                data-ktp="{{ optional($user->attachments()->where('type', 'ktp')->latest()->first())->formatted_attachment ? Storage::url('users/' . optional($user->attachments()->where('type', 'ktp')->latest()->first())->formatted_attachment) : '' }}">
                                                                <i data-feather="eye"></i>
                                                            </a>
                                                        </li>
                                                        <li class="edit">
                                                            <a href="{{ route('master.user.edit', encryptId($user->id)) }}"
                                                                data-bs-toggle="tooltip" data-bs-placement="left"
                                                                data-bs-title="Edit">
                                                                <i data-feather="edit"></i>
                                                            </a>
                                                        </li>
                                                        @if (in_array(auth()->user()->role_id, [1, 2]))
                                                            <li class="delete">
                                                                <form
                                                                    action="{{ route('master.user.delete', encryptId($user->id)) }}"
                                                                    method="post">
                                                                    @csrf
                                                                    @method('delete')
                                                                    <a href="javascript:void(0)" class="delete-data"
                                                                        data-bs-toggle="tooltip" data-bs-placement="left"
                                                                        data-bs-title="Hapus">
                                                                        <i data-feather="trash-2"></i>
                                                                    </a>
                                                                </form>
                                                            </li>
                                                        @endif
                                                    </ul>
                                                </td>
                                            @endif
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="modal fade" id="showModal" tabindex="-1" role="dialog" aria-labelledby="showModal" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content">
                    <div class="col-xl-12">
                        <div class="card social-profile mb-0">
                            <div class="card-body">
                                <div class="social-img-wrap">
                                    <div class="social-img">
                                        <img id="user-avatar" src="" alt="profile">
                                    </div>
                                </div>
                                <div class="social-details">
                                    <h5 class="mb-1" id="user-name"></h5>
                                    <span class="f-light" id="user-barbershop"></span>
                                    <table class="table table-bordered table-striped table-sm mt-2">
                                        <tbody>
                                            <tr>
                                                <td class="text-start">Tempat Lahir</td>
                                                <td class="text-start">-></td>
                                                <td class="text-start"><span id="user-pob"></span></td>
                                            </tr>
                                            <tr>
                                                <td class="text-start">Tanggal Lahir</td>
                                                <td class="text-start">-></td>
                                                <td class="text-start"><span id="user-dob"></span></td>
                                            </tr>
                                            <tr>
                                                <td class="text-start">Alamat</td>
                                                <td class="text-start">-></td>
                                                <td class="text-start"><span id="user-address"></span></td>
                                            </tr>
                                            <tr>
                                                <td class="text-start">Jenis Kelamin</td>
                                                <td class="text-start">-></td>
                                                <td class="text-start"><span id="user-gender"></span></td>
                                            </tr>
                                            <tr>
                                                <td class="text-start" colspan="3"><span id="user-sop"></span></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                    <div class="mt-3">
                                        <img id="user-ktp" src="" alt="ktp">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('afterScripts')
    <script src="{{ asset('assets/admin/js/datatable/jquery.dataTables.min.js?ver=1.0.0') }}"></script>
    <script src="{{ asset('assets/admin/js/datatable/client-side.js?ver=1.0.0') }}"></script>
    <script src="{{ asset('assets/js/interactions/confirmation.js?ver=1.0.0') }}"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script src="{{ asset('assets/admin/js/custom/user.js?ver=1.0.0') }}"></script>
@endsection
