# DOKUMENTASI API - Sistem Informasi Pendataan Pegawai

## Base URL
```
http://localhost/api
atau
http://skripsi-m.test/api
```

## Endpoint Publik

### 1. Submit Data Pegawai
**POST** `/pegawai-submit`

**Description:** Menyimpan data pegawai baru dari form publik

**Request Headers:**
```
Content-Type: application/json
```

**Request Body:**
```json
{
  "nip": "123456789123",
  "nama_lengkap": "John Doe",
  "tempat_lahir": "Sukabumi",
  "tanggal_lahir": "1990-01-15",
  "jenis_kelamin": "Laki-laki",
  "alamat": "Jl. Merdeka No. 123, Sukabumi",
  "no_hp": "081234567890",
  "email": "john@example.com",
  "unit_kerja": "Dinas Pendidikan",
  "pendidikan_terakhir": "S1",
  "jabatan_fungsional_id": 1,
  "tanggal_mulai_jabatan": "2020-01-01"
}
```

**Response (Success 201):**
```json
{
  "success": true,
  "message": "Data pegawai berhasil disimpan",
  "data": {
    "id": 1,
    "nip": "123456789123",
    "nama_lengkap": "John Doe",
    "status_validasi": "pending",
    "created_at": "2025-05-12T10:00:00Z"
  }
}
```

**Response (Error 422):**
```json
{
  "message": "The given data was invalid.",
  "errors": {
    "nip": ["NIP sudah terdaftar"],
    "email": ["Email harus unik"]
  }
}
```

---

## Endpoint Admin (Memerlukan Autentikasi)

### Authentication
Semua endpoint admin memerlukan login terlebih dahulu.

**Login:**
```
POST /login
Content-Type: application/json

{
  "email": "admin@sukabumi.gov.id",
  "password": "admin123456"
}
```

## Pegawai Management

### 2. Get Semua Pegawai
**GET** `/admin/pegawai`

**Query Parameters:**
```
search=nama
status=pending|disetujui|ditolak
jabatan_id=1
unit_kerja=Dinas
page=1
```

**Response (200):**
```json
{
  "data": [
    {
      "id": 1,
      "nip": "123456789123",
      "nama_lengkap": "John Doe",
      "email": "john@example.com",
      "unit_kerja": "Dinas Pendidikan",
      "jabatan": {
        "id": 1,
        "nama_jabatan": "Analis Kebijakan"
      },
      "status_validasi": "pending",
      "created_at": "2025-05-12T10:00:00Z"
    }
  ],
  "links": {
    "first": "...",
    "last": "...",
    "next": "..."
  },
  "meta": {
    "current_page": 1,
    "total": 50
  }
}
```

### 3. Get Detail Pegawai
**GET** `/admin/pegawai/{id}`

**Response (200):**
```json
{
  "id": 1,
  "nip": "123456789123",
  "nama_lengkap": "John Doe",
  "tempat_lahir": "Sukabumi",
  "tanggal_lahir": "1990-01-15",
  "jenis_kelamin": "Laki-laki",
  "email": "john@example.com",
  "no_hp": "081234567890",
  "alamat": "Jl. Merdeka No. 123",
  "unit_kerja": "Dinas Pendidikan",
  "pendidikan_terakhir": "S1",
  "jabatan_fungsional_id": 1,
  "tanggal_mulai_jabatan": "2020-01-01",
  "status_validasi": "pending",
  "files": [
    {
      "id": 1,
      "nama_file": "cv.pdf",
      "path": "pegawai-files/cv.pdf",
      "tipe_file": "pdf"
    }
  ],
  "created_at": "2025-05-12T10:00:00Z"
}
```

### 4. Update Data Pegawai
**PUT/PATCH** `/admin/pegawai/{id}`

**Request Body:**
```json
{
  "nama_lengkap": "Jane Doe",
  "status_validasi": "disetujui"
}
```

**Response (200):**
```json
{
  "success": true,
  "message": "Data pegawai berhasil diperbarui",
  "data": { /* ... */ }
}
```

### 5. Setujui Pegawai
**POST** `/admin/pegawai/{id}/approve`

**Response (200):**
```json
{
  "success": true,
  "message": "Data pegawai disetujui",
  "status": "disetujui"
}
```

### 6. Tolak Pegawai
**POST** `/admin/pegawai/{id}/reject`

**Request Body:**
```json
{
  "catatan_penolakan": "Data tidak lengkap. Silakan upload ijazah."
}
```

**Response (200):**
```json
{
  "success": true,
  "message": "Data pegawai ditolak",
  "status": "ditolak"
}
```

### 7. Hapus Pegawai
**DELETE** `/admin/pegawai/{id}`

**Response (200):**
```json
{
  "success": true,
  "message": "Data pegawai berhasil dihapus"
}
```

---

## Jabatan Fungsional Management

### 8. Get Semua Jabatan
**GET** `/admin/jabatan`

**Response (200):**
```json
{
  "data": [
    {
      "id": 1,
      "nama_jabatan": "Analis Kebijakan",
      "kategori": "Analisis",
      "pegawai_count": 5,
      "created_at": "2025-05-12T10:00:00Z"
    }
  ]
}
```

### 9. Get Detail Jabatan
**GET** `/admin/jabatan/{id}`

**Response (200):**
```json
{
  "id": 1,
  "nama_jabatan": "Analis Kebijakan",
  "kategori": "Analisis",
  "pegawai": [ /* list pegawai */ ]
}
```

### 10. Buat Jabatan Baru
**POST** `/admin/jabatan`

**Request Body:**
```json
{
  "nama_jabatan": "Analisis Data",
  "kategori": "Analisis"
}
```

**Response (201):**
```json
{
  "success": true,
  "message": "Jabatan berhasil ditambahkan",
  "data": { /* ... */ }
}
```

### 11. Update Jabatan
**PUT/PATCH** `/admin/jabatan/{id}`

**Request Body:**
```json
{
  "nama_jabatan": "Senior Analyst",
  "kategori": "Analisis"
}
```

**Response (200):**
```json
{
  "success": true,
  "message": "Jabatan berhasil diperbarui"
}
```

### 12. Hapus Jabatan
**DELETE** `/admin/jabatan/{id}`

**Response (200):**
```json
{
  "success": true,
  "message": "Jabatan berhasil dihapus"
}
```

**Response (Error 422):**
```json
{
  "success": false,
  "message": "Tidak dapat menghapus jabatan yang masih memiliki pegawai"
}
```

---

## Dashboard Statistics

### 13. Get Dashboard Stats
**GET** `/admin/dashboard/stats`

**Response (200):**
```json
{
  "total_pegawai": 50,
  "pegawai_pending": 10,
  "pegawai_approved": 35,
  "pegawai_rejected": 5,
  "total_jabatan": 8,
  "pegawai_per_jabatan": [
    {
      "nama_jabatan": "Analis Kebijakan",
      "count": 12
    }
  ]
}
```

---

## Error Responses

### 400 Bad Request
```json
{
  "message": "Invalid request"
}
```

### 401 Unauthorized
```json
{
  "message": "Unauthenticated"
}
```

### 403 Forbidden
```json
{
  "message": "Unauthorized access. Admin only."
}
```

### 404 Not Found
```json
{
  "message": "Resource not found"
}
```

### 422 Unprocessable Entity
```json
{
  "message": "The given data was invalid.",
  "errors": { /* validation errors */ }
}
```

### 500 Internal Server Error
```json
{
  "message": "Internal Server Error"
}
```

---

## HTTP Status Codes

| Code | Meaning |
|------|---------|
| 200 | Success |
| 201 | Created |
| 204 | No Content |
| 400 | Bad Request |
| 401 | Unauthorized |
| 403 | Forbidden |
| 404 | Not Found |
| 422 | Unprocessable Entity |
| 500 | Internal Server Error |

---

## Contoh Implementasi JavaScript

### Fetch Data Pegawai
```javascript
fetch('http://localhost/api/admin/pegawai', {
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content
  }
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
```

### Submit Form Pegawai
```javascript
const formData = new FormData(document.getElementById('pegawaiForm'));

fetch('http://localhost/api/pegawai-submit', {
  method: 'POST',
  body: formData,
  headers: {
    'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content
  }
})
.then(response => response.json())
.then(data => {
  if (data.success) {
    console.log('Berhasil:', data.message);
  } else {
    console.error('Gagal:', data.errors);
  }
})
.catch(error => console.error('Error:', error));
```

---

**API Version:** 1.0.0  
**Last Updated:** 12 Mei 2025
