@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]))
                    <div class="list-product-header">
                        <div>
                            <a href="javascript:void(0)" data-bs-toggle="modal" data-bs-target="#createModal"
                                data-modal-title="Tambah Barang" data-url="{{ route('manage.inventory.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>Kuantitas</th>
                                        <th>Harga</th>
                                        <th>Total</th>
                                        <th>Gambar</th>
                                        <th>Deskripsi</th>
                                        <th>Tanggal</th>
                                        @if (in_array(auth()->user()->role_id, [1, 3]))
                                            <th>Aksi</th>
                                        @endif
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($inventory as $item)
                                        <tr>
                                            <td>{{ $loop->iteration }}</td>
                                            <td>{{ $item->user->biography->full_name ?? '-deleted-' }}</td>
                                            <td>{{ $item->name }}</td>
                                            <td>{{ $item->quantity }}</td>
                                            <td>{{ formatToRupiah($item->price) }}</td>
                                            <td>{{ formatToRupiah($item->price * $item->quantity) }}</td>
                                            <td>
                                                @if ($item->attachment && $item->attachment->formatted_attachment)
                                                    <a
                                                        href="{{ Storage::url("inventory/{$item->attachment->formatted_attachment}") }}">
                                                        <img src="{{ Storage::url("inventory/{$item->attachment->formatted_attachment}") }}"
                                                            alt="{{ $item->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>{{ $item->description }}</td>
                                            <td>{{ formatDateIndo($item->created_at) }}</td>
                                            @if (in_array(auth()->user()->role_id, [1, 3]))
                                                <td>
                                                    <ul class="action">
                                                        <li class="edit">
                                                            <a href="javascript:void(0)" data-bs-toggle="modal"
                                                                data-bs-target="#createModal" data-modal-title="Edit Barang"
                                                                data-url="{{ route('manage.inventory.update', encryptId($item->id)) }}"
                                                                data-method="put" data-name="{{ $item->name }}"
                                                                data-quantity="{{ $item->quantity }}"
                                                                data-price="{{ formatToRupiah($item->price) }}"
                                                                data-description="{{ $item->description }}">
                                                                <i data-feather="edit"></i>
                                                            </a>
                                                        </li>
                                                        @if (auth()->user()->role_id === 1)
                                                            <li class="delete">
                                                                <form
                                                                    action="{{ route('manage.inventory.delete', encryptId($item->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="quantity">Kuantitas</label>
                                <input name="quantity" class="form-control @error('quantity') is-invalid @enderror"
                                    id="quantity" type="number" placeholder="Masukan Kuantitas"
                                    value="{{ old('quantity') }}">
                                @error('quantity')
                                    <div class="invalid-feedback">
                                        {{ $message }}
                                    </div>
                                @enderror
                            </div>
                            <div class="col-lg-12 position-relative">
                                <label class="form-label" for="price">Harga
                                    <span class="form-span"><i>(Per 1 item)</i></span>
                                </label>
                                <input name="price"
                                    class="form-control rupiah-format @error('price') is-invalid @enderror" id="price"
                                    type="text" placeholder="Masukan Harga" value="{{ old('price') }}"
                                    inputmode="numeric">
                                @error('price')
                                    <div class="invalid-feedback">
                                        {{ $message }}
                                    </div>
                                @enderror
                            </div>
                            <div class="col-lg-12 position-relative">
                                <label class="form-label" for="description">Deskripsi</label>
                                <textarea name="description" class="form-control @error('description') is-invalid @enderror" id="description"
                                    rows="3" placeholder="Masukan Deskripsi">{{ old('description') }}</textarea>
                                @error('description')
                                    <div class="invalid-feedback">
                                        {{ $message }}
                                    </div>
                                @enderror
                            </div>
                            <div class="col-lg-12 position-relative">
                                <label class="form-label" for="image">Gambar
                                    <span class="form-span"><i>(.jpeg,.png,.jpg,.gif,.svg)</i></span>
                                </label>
                                <input name="image" class="form-control @error('image') is-invalid @enderror"
                                    id="image" type="file" value="{{ old('image') }}"
                                    accept="image/jpeg,image/png,image/jpg,image/gif,image/svg">
                                @error('image')
                                    <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/inventroy.js?ver=1.0.0') }}"></script>
    <script src="{{ asset('assets/js/forms/rupiah-format.js?ver=1.0.0') }}"></script>
@endsection
