@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 Pelanggan" data-url="{{ route('master.customer.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>Kontak</th>
                                        <th>Tanggal</th>
                                        @if (in_array(auth()->user()->role_id, [1, 3]))
                                            <th>Aksi</th>
                                        @endif
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($customers as $customer)
                                        <tr>
                                            <td>{{ $loop->iteration }}</td>
                                            <td>{{ $customer->user->biography->full_name ?? 'deleted-' }}</td>
                                            <td>{{ $customer->name }}</td>
                                            <td>{{ $customer->contact_number ?? '-' }}</td>
                                            <td>{{ formatDateIndo($customer->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 Pelanggan"
                                                                data-url="{{ route('master.customer.update', encryptId($customer->id)) }}"
                                                                data-method="put" data-name="{{ $customer->name }}"
                                                                data-contact-number="{{ $customer->contact_number }}">
                                                                <i data-feather="edit"></i>
                                                            </a>
                                                        </li>
                                                        @if (auth()->user()->role_id === 1)
                                                            <li class="delete">
                                                                <form
                                                                    action="{{ route('master.customer.delete', encryptId($customer->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">
                            <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="contact_number">Kontak
                                    <span class="form-span"><i>(format : 08xxxxxxxx ,Opsional)</i></span>
                                </label>
                                <input name="contact_number"
                                    class="form-control @error('contact_number') is-invalid @enderror" id="contact_number"
                                    type="number" placeholder="Masukan Kontak" value="{{ old('contact_number') }}">
                                @error('contact_number')
                                    <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/customer.js?ver=1.0.0') }}"></script>
@endsection
