# 🚀 Quick Start - Fitur Hubungi Kami

## ✅ Status Fitur

| Komponen | Status | Keterangan |
|----------|--------|-----------|
| Database Migration | ✅ Done | Tabel `contacts` sudah dibuat |
| Model | ✅ Done | `Contact` model dengan scopes & methods |
| Controller | ✅ Done | `PageController@sendContact()` & `ContactController` admin |
| Routes | ✅ Done | Publik & admin routes sudah terdaftar |
| Views | ✅ Done | Form kontak & admin dashboard |
| Email Templates | ✅ Done | ContactMail, AutoReplyMail, AdminReplyMail |
| Validasi | ✅ Done | Input validation lengkap |
| Security | ✅ Done | CSRF protection, auth middleware |

---

## 🎬 Memulai

### 1. Akses Form Kontak (User)
```
https://localhost:8000/kontak
```

**Atau klik:**
- Menu navbar "Kontak" (di bagian public pages)
- Footer "Hubungi Kami"

### 2. Admin Panel Kontak
```
https://localhost:8000/admin/contacts
```

**Login sebagai admin terlebih dahulu**

---

## 📊 Database Structure

```sql
-- Tabel contacts
CREATE TABLE contacts (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  email VARCHAR(255) NOT NULL,
  subject VARCHAR(255) NOT NULL,
  message LONGTEXT NOT NULL,
  ip_address VARCHAR(255),
  user_agent TEXT,
  status VARCHAR(255) DEFAULT 'pending',  -- pending, read, replied, archived
  admin_note TEXT,
  read_at TIMESTAMP NULL,
  replied_at TIMESTAMP NULL,
  created_at TIMESTAMP,
  updated_at TIMESTAMP,
  
  INDEX idx_status (status),
  INDEX idx_email (email)
);
```

---

## 🔄 Workflow Diagram

```
┌─────────────────────────────────────────────────────────┐
│                    USER PERSPECTIVE                     │
├─────────────────────────────────────────────────────────┤

  1. Buka /kontak
       ↓
  2. Isi form (Nama, Email, Subjek, Pesan)
       ↓
  3. Submit
       ↓
  4. Validasi di server
       ↓
  5. Sukses? → Email auto-reply terkirim ke user
       ↓
  6. Pesan tersimpan di DB dengan status "pending"


┌─────────────────────────────────────────────────────────┐
│                  ADMIN PERSPECTIVE                      │
├─────────────────────────────────────────────────────────┤

  1. Login ke admin
       ↓
  2. Buka /admin/contacts
       ↓
  3. Email dari admin terkirim pemberitahuan pesan baru
       ↓
  4. Klik "Detail" pesan
       ↓
  5. Status otomatis berubah "pending" → "read"
       ↓
  6. Ketik balasan
       ↓
  7. Submit balasan
       ↓
  8. Email balasan terkirim ke user
       ↓
  9. Status berubah "read" → "replied"
```

---

## 📧 Email Configuration

### Setup Email di `.env`
```env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io      # atau Gmail, SendGrid, etc
MAIL_PORT=465
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=noreply@finanalysis.pro
MAIL_FROM_NAME="FIN-ANALYSIS PRO"
```

### Admin Email Address
Edit di `ContactMail::build()` atau `.env`:
```env
MAIL_ADMIN_ADDRESS=admin@finanalysis.pro
```

---

## 🛠️ Command Useful

### Test Email Send
```bash
php artisan tinker
>>> Mail::raw('Test email', function($msg) { 
    $msg->to('test@gmail.com')
        ->subject('Test');
});
```

### Test Contact Creation
```bash
php artisan tinker
>>> Contact::create([
    'name' => 'Test User',
    'email' => 'test@example.com',
    'subject' => 'Test Subject',
    'message' => 'This is a test message',
    'status' => 'pending'
]);
```

### Get Contact Stats
```bash
php artisan tinker
>>> Contact::count()                          // Total
>>> Contact::pending()->count()               // Pending
>>> Contact::whereNull('read_at')->count()    // Unread
>>> Contact::where('status', 'replied')->count()  // Replied
```

### Export Contacts
```bash
# Via CLI (jika ada command)
php artisan contacts:export --format=csv

# Via Web UI
# Klik tombol "Export CSV" di /admin/contacts
```

---

## 🚨 Troubleshooting

### ❌ Email tidak terkirim

**Solusi:**
1. Check `.env` MAIL configuration
2. Verify credentials SMTP
3. Check firewall/security rules
4. Test dengan Mailtrap (free service)
5. Check `storage/logs/laravel.log`

### ❌ Form tidak bisa disubmit

**Solusi:**
1. Check browser console (F12)
2. Verify CSRF token ada di form
3. Check file permissions `storage/` & `bootstrap/cache/`
4. Clear cache: `php artisan cache:clear`

### ❌ Tidak ada database records

**Solusi:**
1. Run migration: `php artisan migrate`
2. Verify database connection di `.env`
3. Check `contacts` table exists: `php artisan tinker` → `DB::table('contacts')->get()`

### ❌ Admin tidak bisa akses /admin/contacts

**Solusi:**
1. Login sebagai user dengan role 'admin'
2. Check user role di database: `SELECT * FROM users WHERE id=X;`
3. Verify middleware 'admin' di routes

---

## 📈 Features Overview

### User Features
- ✅ Form kontak yang user-friendly
- ✅ Validasi input real-time
- ✅ Auto-reply instant
- ✅ Responsive design (mobile-friendly)

### Admin Features
- ✅ Dashboard dengan statistik
- ✅ List pesan dengan pagination
- ✅ Filter & search
- ✅ Detail view pesan
- ✅ Reply inline dengan email sending
- ✅ Status management
- ✅ Bulk delete
- ✅ CSV export

### Backend Features
- ✅ Database tracking (IP, User Agent)
- ✅ Status workflow (pending → read → replied)
- ✅ Email queuing (ready for background jobs)
- ✅ Error handling & logging
- ✅ Security validasi & CSRF

---

## 👥 User Roles & Permissions

| User Type | Access |
|-----------|--------|
| Guest | Bisa isi form kontak |
| Authenticated User | Bisa isi form kontak |
| Admin | Bisa manage semua pesan |

**Note:** Semua user (guest/auth) bisa kirim pesan. Hanya admin yang bisa lihat dashboard.

---

## 🎨 UI/UX Highlights

### Contact Form Page
- 📍 Lokasi terintegrasi Google Maps
- ⏰ Jam operasional jelas
- 📞 Multiple kontak channels
- 🎨 Gradient colors & modern design
- 📱 Fully responsive

### Admin Dashboard
- 📊 Stat cards dengan KPI penting
- 🔍 Advanced search & filtering
- 📧 Email status indicators
- ⚡ Quick actions (Reply, Delete)
- 📊 Export functionality

---

## 📝 Files Reference

```
app/
├── Models/
│   └── Contact.php
├── Http/Controllers/
│   ├── PageController.php          (sendContact method)
│   └── Admin/ContactController.php
├── Mail/
│   ├── ContactMail.php
│   ├── AutoReplyMail.php
│   └── AdminReplyMail.php
│
database/
└── migrations/
    └── 2026_04_02_080622_create_contacts_table.php

resources/views/
├── pages/
│   └── contact.blade.php
├── admin/contacts/
│   ├── index.blade.php
│   └── show.blade.php
└── emails/
    ├── contact.blade.php
    ├── auto-reply.blade.php
    └── admin-reply.blade.php

routes/
└── web.php (kontak & admin/contacts routes)
```

---

## 🔐 Security Checklist

- [x] CSRF protection pada form
- [x] Input validation di backend
- [x] Admin routes middleware protected
- [x] Database queries safe from SQL injection
- [x] Email sanitization
- [x] Error message user-friendly (tidak expose system info)
- [x] IP tracking untuk audit trail
- [x] Rate limiting ready (bisa ditambahkan)

---

## 🚀 Next Steps / Improvements

1. **Rate Limiting**
   ```php
   'contact_form' => 'limit:5,60'  // 5 times per 60 minutes
   ```

2. **File Attachments**
   - Add file upload to contact form
   - Store in `storage/uploads/contacts/`

3. **Ticket System**
   - Add priority levels
   - Add category tags
   - Assign to team members

4. **Real-time Notifications**
   - WebSocket notifications untuk admin
   - Toast alerts untuk status updates

5. **Template Replies**
   - Save pre-made reply templates
   - Quick reply buttons

6. **Analytics**
   - Contact statistics page
   - Monthly report generation
   - Response time metrics

---

**Fitur sudah siap digunakan! 🎉**

Untuk testing manual, lihat: [TESTING_GUIDE_CONTACT_FEATURE.md](TESTING_GUIDE_CONTACT_FEATURE.md)
