# Fitur Custom Code Text untuk Brand

## Overview

Fitur ini memungkinkan Anda untuk **meng-customize text yang ditampilkan di bawah barcode** (code text), khusus untuk bagian brand/company, tanpa mengubah format otomatis untuk semua label.

## Tampilan

### Sebelum (Format Otomatis):
```
[BARCODE IMAGE]
DEXADBS5G002TH26
```

### Setelah (Custom):
```
[BARCODE IMAGE]
DEXA DBS 5G002TH26
```
atau format custom lainnya yang Anda inginkan!

## Cara Menggunakan

### 1. Pada Form Manual (Create/Edit Label)

1. Buka form label: `/labels/create` atau `/labels/{id}/edit`
2. Isi field **"Custom Text di Bawah Barcode"**
3. Contoh custom format:
   - `DEXA DBS 5G002TH26` (dengan spasi)
   - `DEXA-DBS-5G002TH26` (dengan dash)
   - `DEXA | DBS | 5G002TH26` (dengan separator)
   - `5G002TH26` (hanya kode seri)
   - Format apapun yang Anda mau!

### 2. Live Preview

- Preview akan menampilkan custom text secara real-time
- Jika field kosong, akan menggunakan format otomatis
- Jika diisi, akan menampilkan persis seperti yang Anda ketik

### 3. Pada Import Excel

Saat ini import Excel menggunakan format otomatis. Jika ingin custom per baris, perlu update importer (bisa ditambahkan nanti jika diperlukan).

## Logika Prioritas

System akan menggunakan prioritas berikut untuk menentukan text di bawah barcode:

1. **Custom Code Text** (jika diisi) → Prioritas tertinggi
2. **Config `BARCODE_CODE_TEXT_NO_SPACE=true`** → Tampilkan tanpa spasi: `DEXADBS5G002TH26`
3. **Format Otomatis dengan Spasi** → `DEXA DBS 5G002TH26`

### Diagram Logika:

```
Apakah custom_code_text diisi?
├── Ya → Gunakan custom_code_text
└── Tidak
    ├── Apakah config no_space = true?
    │   ├── Ya → Tampilkan barcode_value apa adanya (DEXADBS5G002TH26)
    │   └── Tidak → Format otomatis dengan spasi (DEXA DBS 5G002TH26)
    └── Selesai
```

## Database

### Kolom Baru: `custom_code_text`

- **Type**: VARCHAR(200), NULLABLE
- **Lokasi**: Tabel `labels`
- **Default**: NULL (menggunakan format otomatis)

### Migration

File: `2026_08_04_040237_add_custom_code_text_to_labels_table.php`

```php
$table->string('custom_code_text', 200)->nullable()->after('title');
```

## API

### Preview Endpoint

Endpoint preview sudah support custom code text:

```
GET /labels/preview?company=DEXA&product=DBS&item_code=5G002TH26&custom_code_text=DEXA%20DBS%205G002TH26
```

Response akan menampilkan label dengan custom text yang diberikan.

## Contoh Penggunaan

### Contoh 1: Format dengan Spasi
```
Company: DEXA
Product: DBS
Item Code: 5G002TH26
Custom Code Text: DEXA DBS 5G002TH26

Hasil di bawah barcode: DEXA DBS 5G002TH26
```

### Contoh 2: Format dengan Separator
```
Company: DEXA
Product: DBS
Item Code: 5G002TH26
Custom Code Text: DEXA • DBS • 5G002TH26

Hasil di bawah barcode: DEXA • DBS • 5G002TH26
```

### Contoh 3: Hanya Kode Seri
```
Company: DEXA
Product: DBS
Item Code: 5G002TH26
Custom Code Text: 5G002TH26

Hasil di bawah barcode: 5G002TH26
```

### Contoh 4: Format Custom Brand
```
Company: INERTIA UTAMA
Product: ABC
Item Code: 10G123TH50
Custom Code Text: INERTIA-UTAMA ABC 10G123TH50

Hasil di bawah barcode: INERTIA-UTAMA ABC 10G123TH50
```

### Contoh 5: Kosongkan untuk Otomatis
```
Company: DEXA
Product: DBS
Item Code: 5G002TH26
Custom Code Text: [kosong]

Hasil di bawah barcode: 
- Jika BARCODE_CODE_TEXT_NO_SPACE=true → DEXADBS5G002TH26
- Jika BARCODE_CODE_TEXT_NO_SPACE=false → DEXA DBS 5G002TH26
```

## Keuntungan Fitur Ini

✅ **Fleksibel**: Setiap label bisa punya format text berbeda
✅ **Override**: Bisa override format otomatis per label
✅ **Live Preview**: Langsung terlihat hasilnya saat mengetik
✅ **Optional**: Jika tidak diisi, tetap menggunakan format otomatis
✅ **No Breaking Changes**: Label yang sudah ada tetap menggunakan format lama
✅ **Support Semua Karakter**: Bisa menggunakan spasi, dash, bullet, dll

## Tips Penggunaan

1. **Untuk Konsistensi Brand**
   - Set custom text yang sama untuk semua produk dari brand tertentu
   - Contoh: Semua produk DEXA gunakan format "DEXA DBS {kode}"

2. **Untuk Readability**
   - Gunakan spasi atau separator untuk memudahkan pembacaan
   - Contoh: "DEXA DBS 5G002TH26" lebih mudah dibaca dari "DEXADBS5G002TH26"

3. **Untuk Kode Pendek**
   - Jika hanya ingin menampilkan kode seri, isi dengan kode saja
   - Contoh: "5G002TH26" saja tanpa brand

4. **Untuk Multi-brand**
   - Perusahaan dengan banyak brand bisa custom format per brand
   - Contoh: "DEXA-GROUP | DBS | 5G002TH26"

## Testing

### Test Manual

1. Buka form create label
2. Isi data:
   - Company: DEXA
   - Product: DBS
   - Item Code: 5G002TH26
3. Di field "Custom Text di Bawah Barcode", ketik: `DEXA DBS 5G002TH26`
4. Lihat preview → Text di bawah barcode harus sesuai custom text
5. Kosongkan field custom → Preview berubah ke format otomatis
6. Isi lagi dengan format lain → Preview berubah sesuai input

### Test Live Preview

1. Ketik di field "Custom Text di Bawah Barcode"
2. Preview harus update otomatis setelah 0.5 detik
3. Setiap perubahan text harus langsung terlihat di preview

### Test Database

1. Simpan label dengan custom text
2. Edit label → Custom text harus tampil di form
3. Cetak label → Text di bawah barcode harus sesuai custom
4. Hapus custom text → Kembali ke format otomatis

## Troubleshooting

### Custom text tidak muncul di preview
- Check browser console untuk error
- Pastikan field ID adalah `f-custom-code`
- Clear cache: `php artisan view:clear`

### Custom text tidak tersimpan
- Check validation error
- Maksimal 200 karakter
- Clear cache: `php artisan cache:clear`

### Custom text tidak tampil di cetak
- Pastikan accessor `getCodeTextAttribute()` sudah di-update
- Check model Label untuk logika prioritas
- Clear view cache: `php artisan view:clear`

## File yang Dimodifikasi

1. **Database**
   - `database/migrations/2026_08_04_040237_add_custom_code_text_to_labels_table.php`

2. **Model**
   - `app/Models/Label.php` → Update accessor `getCodeTextAttribute()`
   - Added `custom_code_text` to fillable

3. **Controller**
   - `app/Http/Controllers/LabelController.php` → Update `preview()` method

4. **Request Validation**
   - `app/Http/Requests/LabelRequest.php` → Added validation rules

5. **View**
   - `resources/views/labels/form.blade.php` → Added form field & JS

## Struktur Kode

### Model Accessor
```php
public function getCodeTextAttribute(): string
{
    // Priority 1: Custom text
    if (!empty($this->custom_code_text)) {
        return trim($this->custom_code_text);
    }
    
    // Priority 2: Config no space
    if (config('barcode.code_text_no_space', false)) {
        return $this->barcode_value;
    }
    
    // Priority 3: Auto format with spaces
    return $this->autoFormatWithSpaces();
}
```

### Form Field
```html
<input type="text" name="custom_code_text" 
       placeholder="Otomatis dari barcode value"
       maxlength="200">
```

### JavaScript Preview
```javascript
var customCodeText = document.getElementById('f-custom-code').value.trim();
params.append('custom_code_text', customCodeText);
```

## Versi

- **Version**: 1.0
- **Created**: August 4, 2026
- **Status**: ✅ READY FOR USE

## Support

Jika ada masalah atau pertanyaan tentang fitur ini, silakan check:
1. File ini untuk dokumentasi
2. Browser console untuk debugging JavaScript
3. Laravel logs untuk debugging backend: `storage/logs/laravel.log`
