@section('heading', 'Customer')

@extends('layouts.app')

@section('content')


<style>
    .warning {
        background-color: yellow;
    }

</style>


<!-- Content Header (Page header) -->
<section class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1> Customer </h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item">
                        <a href="{{route('home')}}">Dashboard</a>
                    </li>
                    <li class="breadcrumb-item active"> Customer </li>
                </ol>
            </div>
        </div>
    </div>
</section>

<!-- Main content -->
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title"> <b>Customer</b> </h3>
                        <div class="card-tools">
                            @can('customers.create')
                                <button type="button" class="btn btn-primary text-white btn-sm m-1" title="Import Excel" data-toggle="modal" data-target="#modal-import">
                                    <i class="fas fa-file-excel"></i> Import Excel
                                </button>
                                <button type="button" class="btn btn-success text-white btn-sm m-1" title="Tambah" data-toggle="modal" data-target="#modal-tambah">
                                    <i class="fas fa-plus-circle"></i> Tambah
                                </button>
                                <div class="modal fade" id="modal-tambah">
                                    <div class="modal-dialog modal-xl">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h4 class="modal-title"> Tambah Customer </h4>
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>
                                            <form action="{{ route('customer.save') }}" method="POST" enctype="multipart/form-data" id="form-tambah-customer">
                                                @csrf
                                                <div class="modal-body">
                                                    <div class="form-group">
                                                        <label>Nama Customer <span class="text-danger">*</span></label>
                                                        <input type="text" name="name" value="{{ old('name') }}" placeholder="Masukkan Nama Customer" class="form-control @error('name') is-invalid @enderror" required>
                                                        @error('name')
                                                        <div class="invalid-feedback" style="display: block">
                                                            {{ $message }}
                                                        </div>
                                                        @enderror
                                                    </div>
                                                    <div class="form-group">
                                                        <label>NO WhatsApp <span class="text-danger">*</span></label>
                                                        <input type="text" name="phone" value="{{ old('phone') }}" placeholder="Masukkan NO WhatsApp" class="form-control @error('phone') is-invalid @enderror" required>
                                                        @error('phone')
                                                        <div class="invalid-feedback" style="display: block">
                                                            {{ $message }}
                                                        </div>
                                                        @enderror
                                                    </div>
                                                    <div class="form-group">
                                                        <label>Alamat <span class="text-danger">*</span></label>
                                                        <textarea name="address" placeholder="Masukkan Alamat"
                                                            class="form-control @error('address') is-invalid @enderror"
                                                            required>{{ old('address') }}</textarea>
                                                        @error('address')
                                                        <div class="invalid-feedback" style="display: block">
                                                            {{ $message }}
                                                        </div>
                                                        @enderror
                                                    </div>
                                                    <div class="form-group">
                                                        <label>Email <span class="text-danger">*</span></label>
                                                        <input type="email" name="email" value="{{ old('email') }}" placeholder="Masukkan Email" class="form-control @error('email') is-invalid @enderror" required>
                                                        @error('email')
                                                        <div class="invalid-feedback" style="display: block">
                                                            {{ $message }}
                                                        </div>
                                                        @enderror
                                                    </div>

                                                    <div class="modal-footer">
                                                        <button type="submit" class="btn btn-primary btn-submit">
                                                            <i class="fa fa-paper-plane"></i> Simpan
                                                        </button>
                                                        <button class="btn btn-warning text-white btn-reset" type="reset">
                                                            <i class="fa fa-redo"></i> Reset
                                                        </button>
                                                        <button type="button" class="btn btn-default" data-dismiss="modal">
                                                            <i class="fas fa-undo"></i> Kembali
                                                        </button>
                                                    </div>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>

                                <!-- Modal Import Excel -->
                                <div class="modal fade" id="modal-import">
                                    <div class="modal-dialog modal-lg">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h4 class="modal-title">Import Customer dari Excel</h4>
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>
                                            <form action="{{ route('customer.import-excel') }}" method="POST" enctype="multipart/form-data">
                                                @csrf
                                                <div class="modal-body">
                                                    <div class="alert alert-info">
                                                        <h5><i class="icon fas fa-info"></i> Format Excel:</h5>
                                                        <p>File Excel harus memiliki format berikut:</p>
                                                        <ul>
                                                            <li><strong>Kolom A:</strong> Nama Customer (wajib)</li>
                                                            <li><strong>Kolom B:</strong> No. Telepon (opsional)</li>
                                                            <li><strong>Kolom C:</strong> Email (wajib)</li>
                                                            <li><strong>Kolom D:</strong> Alamat (opsional)</li>
                                                        </ul>
                                                        <p class="mb-0"><strong>Baris 1:</strong> Header (akan di-skip)</p>
                                                        <p class="mb-0"><strong>Baris 2 dst:</strong> Data customer</p>
                                                        <p class="mt-2 mb-0">
                                                            <a href="{{ route('customer.download-template') }}" class="btn btn-success btn-sm">
                                                                <i class="fas fa-download"></i> Download Template Excel
                                                            </a>
                                                        </p>
                                                    </div>

                                                    <div class="alert alert-warning">
                                                        <h5><i class="icon fas fa-exclamation-triangle"></i> Informasi Penting:</h5>
                                                        <p class="mb-0">Sistem akan otomatis membuat <strong>akun user</strong> untuk setiap customer yang diimport.</p>
                                                        <p class="mb-0"><strong>Password default:</strong> <code>customer123</code></p>
                                                        <p class="mb-0">Customer dapat login menggunakan email dan password tersebut.</p>
                                                    </div>

                                                    <div class="form-group">
                                                        <label>Pilih File Excel <span class="text-danger">*</span></label>
                                                        <div class="custom-file">
                                                            <input type="file" name="excel_file" class="custom-file-input" id="excel_file_customer" accept=".xlsx,.xls" required>
                                                            <label class="custom-file-label" for="excel_file_customer">Pilih file...</label>
                                                        </div>
                                                        <small class="form-text text-muted">
                                                            Format: .xlsx atau .xls (Maksimal 2MB)
                                                        </small>
                                                    </div>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="submit" class="btn btn-primary">
                                                        <i class="fas fa-upload"></i> Upload & Import
                                                    </button>
                                                    <button type="button" class="btn btn-default" data-dismiss="modal">
                                                        <i class="fas fa-times"></i> Batal
                                                    </button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                             @endcan
                        </div>
                    </div>
                    <div class="card-body">
                        <table id="example1" class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th scope="col" style="text-align: center;width: 6%">NO.</th>
                                    <th scope="col">Nama Customer</th>
                                    <th scope="col">NO WhatsApp</th>
                                    <th scope="col">Alamat</th>
                                    <th scope="col">Email</th>
                                    <th scope="col" style="width: 15%;text-align: center">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $nomer = 1; ?>
                                @foreach ($customers as $no => $customer)
                                <tr>
                                    <th>{{ $nomer++}}</th>
                                    <td>{{ $customer->name }}</td>
                                    <td>{{ $customer->phone }}</td>
                                    <td>{{ $customer->address }}</td>
                                    <td>{{ $customer->email }}</td>
                                    <td class="text-left">
                                        @can('customers.edit')
                                            <button type="button" class="btn btn-warning text-white btn-sm m-1" title="Edit" data-toggle="modal" data-target="#modal-edit-{{ $customer->id }}">
                                                <i class="fas fa-pencil-alt"></i>
                                            </button>
                                            <div class="modal fade" id="modal-edit-{{ $customer->id }}">
                                                <div class="modal-dialog modal-xl">
                                                    <div class="modal-content">
                                                        <div class="modal-header">
                                                            <h4 class="modal-title"> Edit Data </h4>
                                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                                <span aria-hidden="true">&times;</span>
                                                            </button>
                                                        </div>
                                                        <form action="{{ route('customer.update', $customer->id) }}" method="POST" enctype="multipart/form-data" class="form-edit-customer">
                                                            @csrf
                                                            @method('PUT')
                                                            <div class="modal-body">
                                                                <div class="form-group">
                                                                    <label>Nama Customer <span class="text-danger">*</span></label>
                                                                    <input type="text" name="name" value="{{ old('name', $customer->name) }}" placeholder="Masukkan Nama Customer" class="form-control @error('name') is-invalid @enderror" required>
                                                                    @error('name')
                                                                    <div class="invalid-feedback" style="display: block">
                                                                        {{ $message }}
                                                                    </div>
                                                                    @enderror
                                                                </div>
                                                                <div class="form-group">
                                                                    <label>NO WhatsApp <span class="text-danger">*</span></label>
                                                                    <input type="text" name="phone" value="{{ old('phone', $customer->phone) }}" placeholder="Masukkan NO WhatsApp" class="form-control @error('phone') is-invalid @enderror" required>
                                                                    @error('phone')
                                                                    <div class="invalid-feedback" style="display: block">
                                                                        {{ $message }}
                                                                    </div>
                                                                    @enderror
                                                                </div>
                                                                <div class="form-group">
                                                                    <label>Alamat <span class="text-danger">*</span></label>
                                                                    <textarea name="address" placeholder="Masukkan Alamat"
                                                                        class="form-control @error('address') is-invalid @enderror"
                                                                        required>{{ old('address', $customer->address) }}</textarea>
                                                                    @error('address')
                                                                    <div class="invalid-feedback" style="display: block">
                                                                        {{ $message }}
                                                                    </div>
                                                                    @enderror
                                                                </div>
                                                                <div class="form-group">
                                                                    <label>Email </label>
                                                                    <input type="email" name="email" value="{{ old('email', $customer->email) }}" placeholder="Masukkan Email" class="form-control">
                                                                </div>
                                                            </div>
                                                            <div class="modal-footer">
                                                                <button type="submit" class="btn btn-primary btn-submit">
                                                                    <i class="fa fa-paper-plane"></i> Simpan
                                                                </button>
                                                                <button class="btn btn-warning text-white btn-reset" type="reset">
                                                                    <i class="fa fa-redo"></i> Reset
                                                                </button>
                                                                <button type="button" class="btn btn-default" data-dismiss="modal">
                                                                    <i class="fas fa-undo"></i> Kembali
                                                                </button>
                                                            </div>
                                                        </form>
                                                    </div>
                                                </div>
                                            </div>
                                        @endcan
                                        @can('customers.delete')
                                            <form action="{{ route('customer.delete', $customer->id) }}" method="POST" style="display:inline;" class="delete-form-{{ $customer->id }}">
                                                @csrf
                                                @method('DELETE')
                                                <button type="button" class="btn btn-danger btn-sm delete m-1" data-id="{{ $customer->id}}" data-name="{{ $customer->name}}" title="Hapus">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                        @endcan
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<!-- /.content -->

@endsection

@push('scripts')
<script>
$(function() {
    // Handle custom file input label untuk modal import
    $('#excel_file_customer').on('change', function() {
        var fileName = $(this).val().split('\\').pop();
        $(this).next('.custom-file-label').html(fileName);
    });
});
</script>
@endpush

