@extends('layouts.admin')
@section('styles')
    <link rel="stylesheet" type="text/css" href="{{ asset('assets/admin/css/vendors/datatables.css') }}">
    <link rel="stylesheet" href="{{ asset('assets/admin/css/vendors/select2.css') }}">
@endsection
@section('app')
    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-12">
                @if (in_array(auth()->user()->role_id, [1, 3, 4]))
                    <div class="list-product-header">
                        <div>
                            <a href="javascript:void(0)" data-bs-toggle="modal" data-bs-target="#createModal"
                                data-modal-title="Tambah Kasbon" data-url="{{ route('finance.debt.store') }}"
                                data-metho="post" class="btn btn-primary">
                                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>Pegawai</th>
                                        <th>Jumlah</th>
                                        <th>Tanggal</th>
                                        <th>Status</th>
                                        @if (in_array(auth()->user()->role_id, [1, 3, 4]))
                                            <th>Aksi</th>
                                        @endif
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($debts as $debt)
                                        <tr>
                                            <td>{{ $loop->iteration }}</td>
                                            <td>{{ $debt->user->biography->full_name ?? '-deleted-' }}</td>
                                            <td>{{ formatToRupiah($debt->amount) ?? '-' }}</td>
                                            <td>{{ formatDateIndo($debt->created_at) }}</td>
                                            <td>
                                                {!! $debt->is_paid === '1'
                                                    ? '<span class="badge bg-success">Lunas</span>'
                                                    : '<span class="badge bg-danger">Belum</span>' !!}
                                            </td>
                                            @if (in_array(auth()->user()->role_id, [1, 3, 4]))
                                                <td>
                                                    <ul class="action">
                                                        <li class="edit">
                                                            <a href="javascript:void(0)" data-bs-toggle="modal"
                                                                data-bs-target="#createModal" data-modal-title="Edit Kasbon"
                                                                data-url="{{ route('finance.debt.update', encryptId($debt->id)) }}"
                                                                data-method="put" data-employee="{{ $debt->user_id }}"
                                                                data-amount="{{ formatToRupiah($debt->amount) }}">
                                                                <i data-feather="edit"></i>
                                                            </a>
                                                        </li>
                                                        @if (auth()->user()->role_id === 1)
                                                            <li class="delete">
                                                                <form
                                                                    action="{{ route('finance.debt.delete', encryptId($debt->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="createModal" tabindex="-1" role="dialog" aria-labelledby="createModal"
            aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title"></h5>
                        <button class="btn-close py-0" type="button" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <form class="row g-3 custom-input" method="post" id="form" enctype="multipart/form-data">
                            <input type="hidden" name="_method" id="form-method">
                            @csrf
                            <div class="col-lg-12 position-relative">
                                <label class="form-label" for="employee">Pegawai
                                </label>
                                <select name="employee"
                                    class="form-control @error('employee') is-invalid @enderror select2-modal"
                                    id="employee">
                                    <option value=""></option>
                                    @foreach ($employees as $employee)
                                        <option value="{{ $employee->id }}" @selected(old('employee') == $employee->id)>
                                            {{ $employee->biography->full_name }}
                                        </option>
                                    @endforeach
                                </select>
                                @error('employee')
                                    <div class="invalid-feedback">
                                        {{ $message }}
                                    </div>
                                @enderror
                            </div>
                            <div class="col-lg-12 position-relative">
                                <label class="form-label" for="amount">Jumlah</label>
                                <input name="amount"
                                    class="form-control rupiah-format @error('amount') is-invalid @enderror" id="amount"
                                    type="text" placeholder="Masukan Jumlah" value="{{ old('amount') }}"
                                    inputmode="numeric">
                                @error('amount')
                                    <div class="invalid-feedback">
                                        {{ $message }}
                                    </div>
                                @enderror
                            </div>
                            <div class="col-lg-12 position-relative">
                                <button class="btn btn-secondary" type="button" data-bs-dismiss="modal">Tutup</button>
                                <button class="btn btn-primary">Simpan</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('afterScripts')
    <script src="{{ asset('assets/admin/js/select2/select2.js') }}"></script>
    <script src="{{ asset('assets/admin/js/select2/select2-config.js?ver=1.0.0') }}"></script>
    <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/debt.js?ver=1.0.0') }}"></script>
    <script src="{{ asset('assets/js/forms/rupiah-format.js?ver=1.0.0') }}"></script>
@endsection
