@extends('layouts.app')
@section('title', 'Pengaturan')
@section('page-title', 'Pengaturan Sistem')

@section('content')
<h5 class="font-weight-bold mb-3">Pengaturan</h5>

<div class="card card-primary card-outline card-tabs">
    <div class="card-header p-0 pt-1 border-bottom-0">
        <ul class="nav nav-tabs" id="settings-tabs" role="tablist">
            <li class="nav-item">
                <a class="nav-link active" id="tabs-business-tab" data-toggle="pill" href="#tabs-business" role="tab">Informasi Usaha</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" id="tabs-assets-tab" data-toggle="pill" href="#tabs-assets" role="tab">Logo & TTD</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" id="tabs-users-tab" data-toggle="pill" href="#tabs-users" role="tab">Manajemen User</a>
            </li>
        </ul>
    </div>
    <div class="card-body">
        <div class="tab-content" id="settings-tabsContent">
            {{-- Tab Informasi Usaha --}}
            <div class="tab-pane fade show active" id="tabs-business" role="tabpanel">
                <form method="POST" action="{{ route('settings.update') }}">
                @csrf
                <div class="row">
                    <div class="col-md-7">
                        <div class="form-group">
                            <label>Nama Usaha <span class="text-danger">*</span></label>
                            <input type="text" name="business_name" class="form-control" value="{{ old('business_name', $settings['business_name'] ?? '') }}" required>
                        </div>
                        <div class="form-group">
                            <label>Alamat Usaha <span class="text-danger">*</span></label>
                            <textarea name="business_address" class="form-control" rows="3" required>{{ old('business_address', $settings['business_address'] ?? '') }}</textarea>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Nomor Telepon</label>
                                    <input type="text" name="business_phone" class="form-control" value="{{ old('business_phone', $settings['business_phone'] ?? '') }}">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Tarif Pajak (%)</label>
                                    <div class="input-group">
                                        <input type="number" name="tax_rate" class="form-control" value="{{ old('tax_rate', $settings['tax_rate'] ?? 11) }}" min="0" max="100" step="0.01" required>
                                        <div class="input-group-append"><span class="input-group-text">%</span></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-5">
                        <div class="form-group">
                            <label>Email Bisnis</label>
                            <input type="email" name="business_email" class="form-control" value="{{ old('business_email', $settings['business_email'] ?? '') }}">
                        </div>
                        <div class="form-group">
                            <label>Website Bisnis</label>
                            <input type="text" name="business_website" class="form-control" value="{{ old('business_website', $settings['business_website'] ?? '') }}">
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Prefix Invoice</label>
                                    <input type="text" name="invoice_prefix" class="form-control" value="{{ old('invoice_prefix', $settings['invoice_prefix'] ?? 'INV') }}" required>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Prefix Tanda Terima</label>
                                    <input type="text" name="receipt_prefix" class="form-control" value="{{ old('receipt_prefix', $settings['receipt_prefix'] ?? 'TTR') }}" required>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <hr>
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Simpan Informasi Usaha</button>
                </form>
            </div>

            {{-- Tab Logo & TTD --}}
            <div class="tab-pane fade" id="tabs-assets" role="tabpanel">
                <form method="POST" action="{{ route('settings.update') }}" enctype="multipart/form-data">
                @csrf
                <div class="row">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label class="font-weight-bold">Logo Usaha</label>
                            <div class="mb-2 p-3 border rounded text-center bg-light" style="height: 160px; display: flex; align-items: center; justify-content: center;">
                                @if(!empty($settings['business_logo_path']))
                                    <img src="{{ asset('storage/'.$settings['business_logo_path']) }}" alt="Logo" style="max-height: 140px; max-width: 100%">
                                @else
                                    <div class="text-muted"><i class="fas fa-image fa-3x d-block mb-1 opacity-25"></i><span>Belum ada logo</span></div>
                                @endif
                            </div>
                            <input type="file" name="business_logo" class="form-control-file border p-1 rounded w-100" accept="image/*">
                            <small class="text-muted">Logo akan muncul di kop surat.</small>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label class="font-weight-bold">Tanda Tangan</label>
                            <div class="mb-2 p-3 border rounded text-center bg-light" style="height: 160px; display: flex; align-items: center; justify-content: center;">
                                @if(!empty($settings['signature_path']))
                                    <img src="{{ asset('storage/'.$settings['signature_path']) }}" alt="TTD" style="max-height: 140px; max-width: 100%">
                                @else
                                    <div class="text-muted"><i class="fas fa-pen fa-3x d-block mb-1 opacity-25"></i><span>Belum ada TTD</span></div>
                                @endif
                            </div>
                            <input type="file" name="signature" class="form-control-file border p-1 rounded w-100" accept="image/*">
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label class="font-weight-bold">Stempel Usaha</label>
                            <div class="mb-2 p-3 border rounded text-center bg-light" style="height: 160px; display: flex; align-items: center; justify-content: center;">
                                @if(!empty($settings['stamp_path']))
                                    <img src="{{ asset('storage/'.$settings['stamp_path']) }}" alt="Stempel" style="max-height: 140px; max-width: 100%">
                                @else
                                    <div class="text-muted"><i class="fas fa-certificate fa-3x d-block mb-1 opacity-25"></i><span>Belum ada stempel</span></div>
                                @endif
                            </div>
                            <input type="file" name="stamp" class="form-control-file border p-1 rounded w-100" accept="image/*">
                        </div>
                    </div>
                </div>
                <hr>
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Simpan Aset Visual</button>
                </form>
            </div>

            {{-- Tab Manajemen User --}}
            <div class="tab-pane fade" id="tabs-users" role="tabpanel">
                <div class="row">
                    <div class="col-md-4">
                        <div class="card card-outline card-success">
                            <div class="card-header"><h3 class="card-title">Tambah User Baru</h3></div>
                            <div class="card-body">
                                <form method="POST" action="{{ route('users.store') }}">
                                @csrf
                                    <div class="form-group">
                                        <label>Nama Pengguna</label>
                                        <input type="text" name="name" class="form-control" placeholder="Nama Lengkap" required>
                                    </div>
                                    <div class="form-group">
                                        <label>Username</label>
                                        <input type="text" name="username" class="form-control" placeholder="Username untuk login" required>
                                    </div>
                                    <div class="form-group">
                                        <label>Email (Opsional)</label>
                                        <input type="email" name="email" class="form-control" placeholder="email@contoh.com">
                                    </div>
                                    <div class="form-group">
                                        <label>Password</label>
                                        <input type="password" name="password" class="form-control" required>
                                    </div>
                                    <button type="submit" class="btn btn-success btn-block"><i class="fas fa-plus"></i> Tambah User</button>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-8">
                        <div class="table-responsive">
                            <table class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>Nama</th>
                                        <th>Username</th>
                                        <th>Email</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($users as $user)
                                    <tr>
                                        <td>{{ $user->name }}</td>
                                        <td><code>{{ $user->username }}</code></td>
                                        <td>{{ $user->email ?: '-' }}</td>
                                        <td>
                                            @if($user->id !== auth()->id())
                                            <form action="{{ route('users.destroy', $user) }}" method="POST" onsubmit="return confirm('Hapus user ini?')">
                                                @csrf @method('DELETE')
                                                <button class="btn btn-xs btn-danger"><i class="fas fa-trash"></i></button>
                                            </form>
                                            @else
                                            <span class="badge badge-info shadow-sm">Anda</span>
                                            @endif
                                        </td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection