# Ticket Management - Quick Reference Guide

## How to Use the Ticket System

### For End Users

#### Creating a New Ticket

1. **Navigate to Tickets section** from the main menu
2. **Click "Create New IT Ticket"** or **"Create New Quality Ticket"** button
3. **Fill in the form fields:**
   - Name, Contact Number, Email (auto-populated for logged-in users)
   - Location (choose from dropdown)
   - Technician/Frontliner (auto-populated after location selection)
   - Device/System (for IT tickets) or Department (for Quality tickets)
   - Subject and Issue Description
   - Add attachments if needed (optional)
4. **Click "Create ticket"** to submit

#### Viewing a Ticket

1. **Find the ticket** in the tickets table
2. **Click the "View" (eye) icon** in the Actions column
3. **Review ticket details** in the modal that appears

#### Editing a Ticket

1. **Find the ticket** in the tickets table
2. **Click the "Edit" (pencil) icon** in the Actions column
3. **Update the fields** (Status, Comments, Attachment)
4. **Add a comment** if needed to track changes
5. **Click "Update Ticket"** to save changes

#### Deleting a Ticket

1. **Find the ticket** in the tickets table (only visible if status ≠ Cancel)
2. **Click the "Delete" (trash) icon** in the Actions column (only for Admin role)
3. **Enter the reason** for deletion when prompted
4. **Confirm deletion**

### For Developers

#### Adding a New Ticket Feature

1. **Update the form** in `createticket.blade.php`:
   ```blade
   <div class="form-group form-md-line-input">
     <input type="text" class="form-control" name="new_field" id="new_field">
     <label>New Field *</label>
     <span class="help-block">Help text here...</span>
   </div>
   ```

2. **Update the Controller** to handle the new field:
   ```php
   $ticketData = [
       'new_field' => $request->new_field,
       // ... other fields
   ];
   ```

3. **Update the Database** schema if needed:
   ```sql
   ALTER TABLE ticket ADD COLUMN new_field VARCHAR(255);
   ```

#### Adding a New Modal

1. **Create the modal** HTML structure:
   ```blade
   <div id="new_modal" class="modal fade">
     <div class="modal-body">
       <!-- Modal content -->
     </div>
   </div>
   ```

2. **Add trigger button** or link:
   ```blade
   <button data-toggle="modal" data-target="#new_modal">Open Modal</button>
   ```

3. **Add JavaScript handler** if needed

#### Database Operations

**Insert New Ticket:**
```php
DB::table('ticket')->insert([
    'ticket_number' => 'TK' . time(),
    'user_id' => $user_id,
    'name' => $name,
    // ... other fields
]);
```

**Update Ticket:**
```php
DB::table('ticket')
    ->where('ticket_id', $ticket_id)
    ->update(['status' => 'new_status']);
```

**Get Ticket with Details:**
```php
$ticket = DB::table('ticket')
    ->where('ticket_id', $id)
    ->first();
```

#### Adding AJAX Endpoints

1. **Create route** in `routes/web.php`:
   ```php
   Route::post('/ticket/new-action', [TicketController::class, 'newAction'])->name('ticket.newAction');
   ```

2. **Create method** in `TicketController.php`:
   ```php
   public function newAction(Request $request)
   {
       // Process request
       return response()->json(['status' => 'success']);
   }
   ```

3. **Call from JavaScript**:
   ```javascript
   $.ajax({
       url: '{{ route("ticket.newAction") }}',
       type: 'POST',
       data: {
           _token: '{{ csrf_token() }}',
           field: value
       },
       success: function(response) {
           // Handle response
       }
   });
   ```

### Common Issues & Solutions

#### Issue: Location dropdown not loading
**Solution:** Check if `location` table exists and has data
```php
DB::table('location')->count();
```

#### Issue: Technicians not appearing after location selection
**Solution:** Verify technicians have correct `location_id` and `role = 3`
```php
DB::table('users')->where('role', 3)->where('location_id', $locationId)->get();
```

#### Issue: File attachment not working
**Solution:** Ensure file upload path exists and has write permissions
```php
// Check file system configuration
config('filesystems.disks.local.root')
```

#### Issue: Modal not closing after form submission
**Solution:** Ensure AJAX success callback closes the modal
```javascript
success: function(response) {
    $('.modal').removeClass('in');
}
```

### Configuration Settings

**File Upload Settings:**
- Location: `/storage/uploads/`
- Max Size: 10MB (configurable in controller)
- Allowed Types: PDF, DOC, DOCX, XLS, XLSX, PNG, JPG, etc.

**Status Values:**
- 1 = Acknowledged
- 2 = Return with query
- 3 = Mark Resolved
- 4 = Return
- 5 = Cancel

**Role Values:**
- 1 = Super Admin
- 2 = Admin
- 3 = Technician
- 4 = Staff
- 5 = Supervisor
- 6 = Senior Technician
- 7 = Lead Supervisor

### Database Schema

#### Ticket Table
```sql
CREATE TABLE ticket (
    ticket_id INT PRIMARY KEY AUTO_INCREMENT,
    ticket_number VARCHAR(20) UNIQUE,
    user_id INT,
    name VARCHAR(100),
    contact_number VARCHAR(20),
    email VARCHAR(100),
    location VARCHAR(50),
    technician INT,
    system VARCHAR(100),
    subject VARCHAR(255),
    issue_desc TEXT,
    issue_status INT,
    attachment VARCHAR(255),
    created_at TIMESTAMP,
    updated_at TIMESTAMP,
    created_by INT,
    updated_by INT
);
```

### Performance Tips

1. **Add database indexes:**
   ```sql
   CREATE INDEX idx_ticket_location ON ticket(location);
   CREATE INDEX idx_ticket_status ON ticket(issue_status);
   CREATE INDEX idx_ticket_user ON ticket(user_id);
   ```

2. **Cache frequently accessed data:**
   ```php
   $locations = Cache::remember('locations', 3600, function () {
       return DB::table('location')->get();
   });
   ```

3. **Use pagination for large datasets:**
   ```php
   $tickets = DB::table('ticket')->paginate(15);
   ```

### Testing

**Create Test Data:**
```php
DB::table('ticket')->insert([
    'ticket_number' => 'TEST001',
    'user_id' => 1,
    'name' => 'Test User',
    'contact_number' => '1234567890',
    'email' => 'test@example.com',
    'location' => 'TEST',
    'technician' => 3,
    'subject' => 'Test Issue',
    'issue_desc' => 'This is a test ticket',
    'issue_status' => 1,
    'created_at' => now(),
    'updated_at' => now()
]);
```

**Verify Installation:**
1. Check if routes are registered: `php artisan route:list | grep ticket`
2. Check if controller methods exist: `php artisan tinker` → `TicketController::class`
3. Test AJAX endpoints using Postman or browser console

---

## Quick Keyboard Shortcuts
- **Create Ticket:** Click "Create New Ticket" button
- **Quick Search:** Use browser Ctrl+F in the table
- **Close Modal:** Press ESC or click "Close" button
- **Submit Form:** Ctrl+Enter (if supported)

## Support & Documentation
- Full implementation guide: `TICKET_IMPLEMENTATION_SUMMARY.md`
- Reference implementation: `createticketreference` file
- Controller methods: `app/Http/Controllers/TicketController.php`

---
**Last Updated:** December 16, 2025
**Version:** 1.0
