# Backend Game API Actions

Base URL ตัวอย่าง: `http://localhost/backend/api.php`

## หลักการ Public Game Catalog

Frontend ใช้เส้น Public เพื่อวาดหน้าเกมก่อนเสมอ

1. `public_categories`
2. `public_providers`
3. ถ้า provider มี `file = null` ให้เล่นตรงด้วย `customer_playgame`
4. ถ้า provider มี `file != null` ให้เรียก `public_games&provider=...` ก่อน แล้วค่อย `customer_playgame`

## Provider Rules

### `file = null`
ตัวอย่าง `sa`, `bfs`, `we`

- `has_game_list = false`
- `play_mode = direct`
- ไม่มีรายการเกมย่อย
- กด card แล้วเรียก `customer_playgame`
- ถ้าไม่ส่ง `gamecode` backend ใช้ `default_gamecode`

### `file != null`
ตัวอย่าง `pg`, `pp`, `joker`

- `has_game_list = true`
- `play_mode = game_list`
- กด card แล้วเรียก `public_games&provider=pg`
- เลือกเกมย่อย แล้วเรียก `customer_playgame`

## Image Pattern

ไม่เก็บ URL รูปใน JSON

- Provider image: `/images/providers/{provider}.png`
- Game local image: `/images/games/{provider}/{code}.png`
- Game external fallback: `https://www.083512.com/icons/{provider}/{code}.jpg`

## Public

| Method | Action | ใช้ทำอะไร |
|---|---|---|
| GET | `health` | เช็ค API โดยไม่ต่อ DB |
| GET | `public_categories` | รายชื่อหมวด เช่น all, slot, casino, sport |
| GET | `public_providers` | รายชื่อค่าย enabled=true เรียงตาม sort_order |
| GET | `public_providers&type=slot` | รายชื่อค่ายตามหมวด |
| GET | `public_games&provider=pg` | รายชื่อเกมตาม provider ที่มีไฟล์ `.txt` |
| GET | `public_games&provider=pg&refresh=1` | ดึงรายชื่อเกมใหม่และเขียน cache ทับ |
| GET | `public_games&provider=sa` | provider ไม่มีไฟล์ จะคืน card/direct info และ games ว่าง |

## Customer

| Method | Action | ใช้ทำอะไร |
|---|---|---|
| POST | `customer_register` | สมัครสมาชิก |
| POST | `customer_login` | Login สมาชิก เก็บ session cookie |
| POST | `customer_logout` | Logout สมาชิก |
| GET | `customer_me` | เช็คข้อมูล session สมาชิก |
| GET | `customer_balance` | ดูเครดิตสมาชิกจากระบบเกม |
| GET | `customer_lobbygame` | เข้า lobby และคืน `game_url` |
| POST | `customer_playgame` | เข้าเกมจาก `provider` + `gamecode` |

### Customer Playgame Direct
```json
{
  "action": "customer_playgame",
  "provider": "sa",
  "gamecode": "sa",
  "language": "thai"
}
```

### Customer Playgame Game List
```json
{
  "action": "customer_playgame",
  "provider": "pg",
  "gamecode": "126",
  "language": "thai"
}
```

## Admin Customer

| Method | Action | ใช้ทำอะไร |
|---|---|---|
| POST | `admin_login` | Login แอดมิน |
| POST | `admin_logout` | Logout แอดมิน |
| GET | `admin_me` | เช็ค session แอดมิน |
| POST | `admin_customer_create` | แอดมินสมัครสมาชิก |
| GET | `admin_customers_list` | รายชื่อสมาชิก |
| GET | `admin_customers_detail&id=1` | รายละเอียดสมาชิก |
| POST | `admin_customers_update` | แก้ไขข้อมูลสมาชิก |
| GET | `admin_customer_balance&id=1` | ดูเครดิตสมาชิก |
| POST | `admin_customer_credit_add` | เพิ่มเครดิต |
| POST | `admin_customer_credit_subtract` | หักเครดิตแบบกันติดลบ |
| GET | `admin_customer_credit_logs&id=1` | ประวัติปรับเครดิต |

## Admin Game Catalog

แอดมินจัดการได้แค่ เปิด/ปิด และเรียงลำดับเท่านั้น

| Method | Action | ใช้ทำอะไร |
|---|---|---|
| GET | `admin_game_providers` | ดูค่ายเกมทั้งหมด รวม enabled=false |
| GET | `admin_game_providers&type=slot` | ดูค่ายเกมทั้งหมดตามหมวด |
| POST | `admin_game_provider_toggle` | เปิด/ปิดค่ายเกม |
| POST | `admin_game_provider_reorder` | จัดลำดับค่ายเกม |

### Toggle
```json
{
  "action": "admin_game_provider_toggle",
  "provider": "pg",
  "enabled": false
}
```

### Reorder
```json
{
  "action": "admin_game_provider_reorder",
  "items": [
    { "provider": "pg", "sort_order": 1 },
    { "provider": "sa", "sort_order": 2 },
    { "provider": "bfs", "sort_order": 3 }
  ]
}
```

## ไม่มี API เหล่านี้

ไม่มีการเปิดให้แอดมินแก้ core structure ของ provider

- `admin_game_provider_create`
- `admin_game_provider_delete`
- `admin_game_provider_update`

เพราะ `provider`, `type`, `file`, `play_mode`, `has_game_list` เป็นโครงสร้างระบบที่ developer ต้องคุมเอง


หมายเหตุ: customer_playgame ไม่ต้องส่ง openGame และ returnUrl จาก frontend แล้ว backend จะ fix ค่าใน config/config.php (`game_api.play_open_game`, `game_api.play_return_url`) เอง

## Public Game Menus

```txt
GET /backend/api.php?action=public_game_menus
```

ใช้สำหรับ frontend โหลดเมนูซ้ายหน้าเลือกเกมจาก `backend/data/game_providers.json` แทนการ hardcode ใน `dashboard.html`.
