# Ticket Management System - Quick Setup Guide

## ✅ Implementation Complete

### Files Updated/Created:
1. **createticket.blade.php** (1504 lines) - Complete rewrite with all features
2. **TicketController.php** (243 lines) - Added 6 new methods for ticket operations
3. **routes/web.php** - Added 5 new AJAX endpoints
4. **Backup**: createticket_backup.blade.php (for reference)

## Verify Database Tables Exist

Run these commands in your Laravel app:

```bash
php artisan migrate
```

Ensure these tables exist:
- ticket
- users
- location
- device
- department
- subject
- status
- comments
- branch (optional)

## Test Ticket Creation

### Step 1: Navigate to Ticket Management
```
http://localhost:8000/tickets
```

### Step 2: Test Each Role

#### Admin/Manager (Role 1, 2)
- Click "Create New IT Ticket"
- Fill all required fields
- Submit and verify ticket is created

#### Technician (Role 3)
- Click "Create New IT Ticket"
- Select device and location
- Submit ticket

#### Employee (Role 4)
- Only see own tickets
- Can edit own tickets

#### Supervisor (Role 5)
- Click "Create New Quality Ticket"
- Select department, subject, SLA
- Select supervisors
- Submit ticket

#### Support (Role 6, 7)
- Click "Create New IT Ticket"
- Fill device and technician info

## Common Issues & Solutions

### Issue 1: Modal Not Opening
**Solution**: 
- Check browser console for JS errors
- Verify jQuery is loaded
- Ensure Bootstrap is included

### Issue 2: Location Filtering Not Working
**Solution**:
- Verify `/ticket/get-technicians` route exists
- Check CSRF token in form
- Ensure location_id is sent in AJAX request

### Issue 3: File Upload Not Working
**Solution**:
- Check `storage/` folder exists and is writable
- Set proper permissions: `chmod -R 777 storage/`
- Configure upload path in `.env`

### Issue 4: Status Update Not Working
**Solution**:
- Verify user role allows status change
- Check `/ticket/update` route
- Ensure ticket_id is passed correctly

## Database Schema (Minimum Required)

```sql
-- Users Table
ALTER TABLE users ADD COLUMN IF NOT EXISTS location_id INT;
ALTER TABLE users ADD COLUMN IF NOT EXISTS dept_id INT;
ALTER TABLE users ADD COLUMN IF NOT EXISTS mobile VARCHAR(20);

-- Location Table
CREATE TABLE IF NOT EXISTS location (
    location_id INT PRIMARY KEY AUTO_INCREMENT,
    location_name VARCHAR(100),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Device Table
CREATE TABLE IF NOT EXISTS device (
    device_id INT PRIMARY KEY AUTO_INCREMENT,
    device_name VARCHAR(100),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Department Table
CREATE TABLE IF NOT EXISTS department (
    dept_id INT PRIMARY KEY AUTO_INCREMENT,
    dept_name VARCHAR(100),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Subject Table
CREATE TABLE IF NOT EXISTS subject (
    subject_id INT PRIMARY KEY AUTO_INCREMENT,
    subject_issue VARCHAR(100),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Status Table
CREATE TABLE IF NOT EXISTS status (
    status_id INT PRIMARY KEY AUTO_INCREMENT,
    status_name VARCHAR(50),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Ticket Table Modifications
ALTER TABLE ticket ADD COLUMN IF NOT EXISTS assigned_staff_id VARCHAR(255);
ALTER TABLE ticket ADD COLUMN IF NOT EXISTS supervisor INT;
ALTER TABLE ticket ADD COLUMN IF NOT EXISTS attachment VARCHAR(255);
```

## Enable Features

### 1. File Uploads
Create directory: `storage/app/tickets`
```bash
mkdir -p storage/app/tickets
chmod 777 storage/app/tickets
```

### 2. Database Seeding (Optional)
Insert sample data:
```bash
php artisan tinker

# Add sample locations
DB::table('location')->insert(['location_id' => 1, 'location_name' => 'HWCH']);
DB::table('location')->insert(['location_id' => 2, 'location_name' => 'HWCZ']);

# Add sample devices
DB::table('device')->insert(['device_name' => 'Desktop']);
DB::table('device')->insert(['device_name' => 'Laptop']);

# Add status
DB::table('status')->insert(['status_id' => 1, 'status_name' => 'Acknowledged']);
DB::table('status')->insert(['status_id' => 2, 'status_name' => 'Return with query']);
DB::table('status')->insert(['status_id' => 3, 'status_name' => 'Resolved']);
DB::table('status')->insert(['status_id' => 4, 'status_name' => 'Return']);
DB::table('status')->insert(['status_id' => 5, 'status_name' => 'Cancel']);
```

## Production Checklist

- [ ] All database tables exist with correct columns
- [ ] File upload directory created and writable
- [ ] CSRF protection enabled
- [ ] User roles properly assigned
- [ ] Locations assigned to users
- [ ] Device list populated
- [ ] Department list populated
- [ ] Subject list populated
- [ ] Status list populated
- [ ] Email notifications configured (optional)
- [ ] Backup strategy in place
- [ ] Error logging configured

## API Endpoints Reference

| Method | Endpoint | Purpose | Auth |
|--------|----------|---------|------|
| GET | /tickets | View all tickets | Required |
| POST | /ticket/get-technicians | Get technicians by location | Required |
| POST | /ticket/view | Get ticket details | Required |
| POST | /ticket/edit | Get ticket for editing | Required |
| POST | /ticket/update | Update ticket | Required |
| POST | /ticket/delete | Delete ticket | Required |

## Support

For issues:
1. Check `storage/logs/laravel.log`
2. Verify database connection
3. Check user role and permissions
4. Review browser console for JS errors
5. Verify all required routes are defined

---

**Status**: ✅ Ready for Testing
**Version**: 1.0
**Date**: December 16, 2025
