@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, 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 Pengeluaran" data-url="{{ route('finance.expense.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>Penginput</th>
                                        <th>Nama</th>
                                        <th>Jumlah</th>
                                        <th>Bukti</th>
                                        <th>Tanggal</th>
                                        @if (in_array(auth()->user()->role_id, [1, 3, 4]))
                                            <th>Aksi</th>
                                        @endif
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($expenses as $expense)
                                        <tr>
                                            <td>{{ $loop->iteration }}</td>
                                            <td>{{ $expense->user->biography->full_name ?? '-deleted-' }}</td>
                                            <td>{{ $expense->name }}</td>
                                            <td>{{ formatToRupiah($expense->amount) ?? '-' }}</td>
                                            <td>
                                                @if ($expense->attachment && $expense->attachment->formatted_attachment)
                                                    <a
                                                        href="{{ Storage::url("expenses/{$expense->attachment->formatted_attachment}") }}">
                                                        <img src="{{ Storage::url("expenses/{$expense->attachment->formatted_attachment}") }}"
                                                            alt="{{ $expense->name }}" width="77" height="77">
                                                    </a>
                                                @else
                                                    <a href="{{ asset('assets/images/no-imge.png') }}">
                                                        <img src="{{ asset('assets/images/no-imge.png') }}"
                                                            alt="Default Image" style="width: 77px; height: 77px">
                                                    </a>
                                                @endif
                                            </td>
                                            <td>{{ formatDateIndo($expense->created_at) }}</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 Pengeluaran"
                                                                data-url="{{ route('finance.expense.update', encryptId($expense->id)) }}"
                                                                data-method="put" data-name="{{ $expense->name }}"
                                                                data-amount="{{ formatToRupiah($expense->amount) }}"
                                                                data-use-cash="{{ $expense->use_cash }}">
                                                                <i data-feather="edit"></i>
                                                            </a>
                                                        </li>
                                                        @if (auth()->user()->role_id === 1)
                                                            <li class="delete">
                                                                <form
                                                                    action="{{ route('finance.expense.delete', encryptId($expense->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="name">Nama</label>
                                <input name="name" class="form-control @error('name') is-invalid @enderror"
                                    id="name" type="text" placeholder="Masukan Nama" value="{{ old('name') }}">
                                @error('name')
                                    <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">
                                <label class="form-label" for="use_cash">Gunakan Kas</label>
                                <div class="form-check radio ps-0">
                                    <ul class="radio-wrapper">
                                        <li>
                                            <input class="form-check-input" id="radio-yes" type="radio" name="use_cash"
                                                value="1" @checked(old('use_cash') == '1')>
                                            <label class="form-check-label" for="radio-yes">
                                                <i data-feather="check"></i>
                                            </label>
                                        </li>
                                        <li>
                                            <input class="form-check-input" id="radio-no" type="radio"
                                                name="use_cash" value="2" @checked(old('use_cash') == '2')>
                                            <label class="form-check-label" for="radio-no">
                                                <i data-feather="x"></i>
                                            </label>
                                        </li>
                                    </ul>
                                </div>
                                @error('use_cash')
                                    <div class="invalid-feedback">
                                        {{ $message }}
                                    </div>
                                @enderror
                            </div>
                            <div class="col-lg-12 position-relative">
                                <label class="form-label" for="proof">Bukti
                                    <span class="form-span"><i>(.jpeg,.png,.jpg,.gif,.svg)</i></span>
                                </label>
                                <input name="proof" class="form-control @error('proof') is-invalid @enderror"
                                    id="proof" type="file" value="{{ old('proof') }}"
                                    accept="image/jpeg,image/png,image/jpg,image/gif,image/svg">
                                @error('proof')
                                    <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/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/expense.js?ver=1.0.0') }}"></script>
    <script src="{{ asset('assets/js/forms/rupiah-format.js?ver=1.0.0') }}"></script>
@endsection
