@section('heading', 'Kategori')

@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> Kategori </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"> Kategori </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>Kategori</b> </h3>
                        <div class="card-tools">
                            @can('categories.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 Kategori </h4>
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>
                                            <form action="{{ route('category.save') }}" method="POST" enctype="multipart/form-data" id="form-tambah-category">
                                                @csrf
                                                <div class="modal-body">
                                                    <div class="form-group">
                                                        <label>Nama Kategori <span class="text-danger">*</span></label>
                                                        <input type="text" name="name" value="{{ old('name') }}" placeholder="Masukkan Nama Kategori" 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>Deskripsi <span class="text-danger">*</span></label>
                                                        <textarea name="description" placeholder="Masukkan Deskripsi"
                                                            class="form-control @error('description') is-invalid @enderror"
                                                            required>{{ old('description') }}</textarea>
                                                        @error('description')
                                                        <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 Kategori 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('category.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 Kategori (wajib)</li>
                                                            <li><strong>Kolom B:</strong> Deskripsi (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 kategori</p>
                                                        <p class="mt-2 mb-0">
                                                            <a href="{{ route('category.download-template') }}" class="btn btn-success btn-sm">
                                                                <i class="fas fa-download"></i> Download Template Excel
                                                            </a>
                                                        </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" accept=".xlsx,.xls" required>
                                                            <label class="custom-file-label" for="excel_file">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 Kategori</th>
                                    <th scope="col">Deskripsi</th>
                                    <th scope="col" style="width: 15%;text-align: center">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $nomer = 1; ?>
                                @foreach ($categories as $no => $category)
                                <tr>
                                    <th>{{ $nomer++}}</th>
                                    <td>{{ $category->name }}</td>
                                    <td>{{ $category->description }}</td>
                                    <td class="text-left">
                                        @can('categories.edit')
                                            <button type="button" class="btn btn-warning text-white btn-sm m-1" title="Edit" data-toggle="modal" data-target="#modal-edit-{{ $category->id }}">
                                                <i class="fas fa-pencil-alt"></i>
                                            </button>
                                            <div class="modal fade" id="modal-edit-{{ $category->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('category.update', $category->id) }}" method="POST" enctype="multipart/form-data" class="form-edit-category">
                                                            @csrf
                                                            @method('PUT')
                                                            <div class="modal-body">
                                                                <div class="form-group">
                                                                    <label>Nama Category <span class="text-danger">*</span></label>
                                                                    <input type="text" name="name" value="{{ old('name', $category->name) }}" placeholder="Masukkan Nama Kategori" 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>Deskripsi <span class="text-danger">*</span></label>
                                                                    <textarea name="description" placeholder="Masukkan Deskripsi"
                                                                        class="form-control @error('description') is-invalid @enderror"
                                                                        required>{{ old('description', $category->description) }}</textarea>
                                                                    @error('description')
                                                                    <div class="invalid-feedback" style="display: block">
                                                                        {{ $message }}
                                                                    </div>
                                                                    @enderror
                                                                </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('categories.delete')
                                            <form action="{{ route('category.delete', $category->id) }}" method="POST" style="display:inline;" class="delete-form-{{ $category->id }}">
                                                @csrf
                                                @method('DELETE')
                                                <button type="button" class="btn btn-danger btn-sm delete m-1" data-id="{{ $category->id}}" data-name="{{ $category->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>
$(document).ready(function() {
    // Handle custom file input label untuk modal import
    $('#excel_file').on('change', function() {
        var fileName = $(this).val().split('\\').pop();
        $(this).next('.custom-file-label').html(fileName);
    });
});
</script>
@endpush

