# Printer / PDF Print (In Don Hang PDF)

## Tong quan

Chuc nang **Printer / PDF Print** cho phep nguoi ban in don hang PDF tu nhieu nen tang thuong mai dien tu (Shopee, Lazada, TikTok) voi kha nang tuy chinh noi dung in. Cong cu ho tro chen them van ban, hinh anh (logo, chu ky, dau) vao file PDF truoc khi in, dong thoi tu dong luu lai lich su in an de tra cuu va in lai bat cu luc nao.

**Tinh nang chinh:**
- In don hang PDF tu Shopee, Lazada, TikTok hoac tu file PDF upload
- Tao template in voi noi dung tuy chinh (van ban, hinh anh) tai nhieu vi tri
- Chen session name, so trang, timestamp vao PDF
- Xem truoc PDF truoc khi in
- Luu log in an tu dong len S3, ho tro tai lai va in lai
- Dong bo cau hinh template qua PouchDB/CouchDB

**Yeu cau:** Tai khoan tra phi (Premium).

---

## Giao dien chinh

### Trang in PDF

**Duong dan:** `/printer/index`

Giao dien chia lam 2 phan:

| Phan | Mo ta |
|------|-------|
| **Cau hinh (trai)** | Upload file PDF, cau hinh session print, chon template in |
| **Xem truoc (phai)** | Hien thi preview PDF sau khi xu ly |

### Cac nguon PDF (param `from`)

| Nguon | Mo ta |
|-------|-------|
| **shopee** | Tai PDF tu Shopee qua `job_id` |
| **lazada** | Tai PDF tu iframe trang in Lazada |
| **tiktok** | Tai PDF tu URL TikTok |
| **url** | Tai PDF tu URL bat ky |
| **file** | Upload file PDF tu may tinh |

Khi truy cap tu Shopee Seller Center, extension se hien thi **nut noi (float button)** mau cam o goc duoi phai man hinh trang in don hang. Nhan nut nay se mo popup in PDF voi `job_id` va `shop_id` tu URL hien tai.

### Popup in nhanh

**Duong dan:** `/printer/popup`

Phien ban thu gon cua trang in chinh, hien thi trong popup extension. Chi hien thi phan cau hinh (khong co preview), giao dien compact voi font size nho hon.

---

## Cau hinh may in (Template)

**Duong dan:** `/printer/config`

Giao dien tao va quan ly template in PDF, chia lam 2 phan:

| Phan | Mo ta |
|------|-------|
| **Panel dieu khien (trai)** | Tao template moi, quan ly template da luu |
| **Xem truoc (phai)** | Hien thi preview PDF voi template da ap dung |

### Tao template moi

Moi template gom 2 thanh phan co the bat/tat doc lap:

#### Chen van ban (Text Overlay)

| Truong | Mo ta |
|--------|-------|
| **Bat/Tat** | Switch bat/tat chen van ban |
| **Noi dung** | Van ban can chen vao PDF |
| **Vi tri** | Chon tu 10 vi tri co dinh hoac tuy chinh (custom) |
| **Co chu** | Kich thuoc font (8-72) |
| **X, Y** | Toa do tuy chinh (chi khi chon vi tri "custom") |

#### Chen hinh anh (Image Overlay)

| Truong | Mo ta |
|--------|-------|
| **Bat/Tat** | Switch bat/tat chen hinh anh |
| **Chon anh** | Upload file PNG/JPEG/JPG |
| **Vi tri** | Chon tu 10 vi tri co dinh hoac tuy chinh (custom) |
| **Chieu rong** | Kich thuoc anh (10-1000 pixel) |
| **Chieu cao** | Kich thuoc anh (10-1000 pixel) |
| **X, Y** | Toa do tuy chinh (chi khi chon vi tri "custom") |

#### Cac vi tri co dinh

| Vi tri | Mo ta |
|--------|-------|
| `top-left` | Goc tren trai |
| `top-center` | Giua tren |
| `top-right` | Goc tren phai |
| `middle-left` | Giua trai |
| `middle-center` | Chinh giua |
| `middle-right` | Giua phai |
| `bottom-left` | Goc duoi trai |
| `bottom-center` | Giua duoi |
| `bottom-right` | Goc duoi phai |
| `custom` | Tuy chinh toa do X, Y |

### Quan ly template da luu

- **Xem truoc:** Ap dung template len PDF mau de xem ket qua
- **Xoa:** Xoa template (co xac nhan truoc khi xoa)
- Template duoc luu tren PouchDB va dong bo voi CouchDB remote qua model `ConfigPdfPrinter`

### PDF mau

Co the chon PDF mau theo nen tang (Shopee, Lazada, TikTok) hoac upload file PDF cua minh de xem truoc template.

---

## Quy trinh in don hang

### Buoc 1: Nhan file PDF

Tuy theo nguon (`from`), he thong se:

1. **Tu Shopee:** Goi API `getShopeePrintPdf(jobId)` de tai PDF tu Shopee
2. **Tu Lazada:** Lay HTML cua tab hien tai qua `bridgeGetWebcontentTabId`, trich xuat URL iframe chua PDF, tai PDF tu URL do
3. **Tu TikTok/URL:** Tai PDF truc tiep tu URL qua `getPdfFromUrl`
4. **Tu file:** Nguoi dung upload file PDF thu cong

PDF buffer duoc cache trong `CacheOrderPdfBuffer` de tranh tai lai nhieu lan.

### Buoc 2: Cau hinh session print (tuy chon)

Khi bat **Session Print**, he thong se chen them noi dung vao footer cua moi trang PDF:

| Tuy chon | Mo ta |
|----------|-------|
| **Ten session** | Van ban tuy chinh hien thi tren moi trang |
| **So trang** | Them so trang (1/n, 2/n, ...) |
| **Timestamp** | Them thoi gian in |
| **Vi tri** | Vi tri hien thi noi dung session |
| **Co chu** | Kich thuoc font cua noi dung session |

### Buoc 3: Chon template va in

1. Chon mot trong cac template da tao (hoac chon "Mac dinh" de in PDF goc)
2. He thong ap dung template (chen text/image) vao tat ca cac trang cua PDF
3. Ap dung session print (neu bat)
4. Mo PDF da xu ly:
   - **Luu file:** Tai file PDF ve may voi ten `print_{timestamp}_{jobId}.pdf`
   - **In truc tiep:** Mo tab moi va tu dong goi lenh `window.print()`

---

## Log in an

**Duong dan:** `/printer/log`

### Bo loc

| Truong | Mo ta |
|--------|-------|
| **Shop ID** | Loc theo ma shop |
| **Nen tang** | Loc theo Shopee / Lazada / TikTok |
| **Ngay tao** | Loc theo khoang thoi gian (date range) |
| **Tim kiem** | Tim theo ma van don, ma don hang, noi dung |

### Bang du lieu

| Cot | Mo ta |
|-----|-------|
| **#** | So thu tu |
| **Shop ID** | Ma cua shop |
| **Ten shop** | Ten shop |
| **Nen tang** | Shopee (cam) / Lazada (xanh duong) / TikTok (do) |
| **So trang** | So trang cua file PDF |
| **Kich thuoc** | Dung luong file (B / KB / MB) |
| **Ngay tao** | Thoi diem in (DD/MM/YYYY HH:mm:ss) |
| **Thao tac** | Tai ve / Xem / In lai |

### Thao tac voi log

| Nut | Chuc nang |
|-----|-----------|
| **Tai ve** | Tai file PDF ve may (dung `saveAs`) |
| **Xem** | Mo file PDF trong tab moi tu S3 URL |
| **In lai** | Tai PDF, chuyen sang blob URL va goi `window.print()` |

### Quy trinh luu log tu dong

Khi nguoi dung in don hang tren Shopee/Lazada/TikTok, he thong tu dong:

1. Tai PDF buffer tu API nen tang (retry toi da 5 lan, moi lan cach 5 giay)
2. Kiem tra kich thuoc buffer (phai lon hon 5KB)
3. Parse noi dung PDF de trich xuat ma don hang va ma van don
4. Upload file PDF len Amazon S3 theo duong dan: `{platform}/{ngay}/{shopId}/{printId}.pdf`
5. Luu ban ghi log vao PouchDB voi thong tin day du

Cac job in duoc xu ly qua hang doi (PQueue) voi concurrency = 1 cho moi nen tang, dam bao xu ly tuan tu.

### Tu dong don dep du lieu

- Du lieu log cu hon **30 ngay** se tu dong bi xoa khi mo trang log
- Model loader xoa du lieu cu hon **45 ngay** khi khoi dong extension

---

## Cau truc du lieu

### OrderPdfBucket (Log in an)

```
- _id: ID ban ghi (PouchDB)
- printId: ID phien in (job_id hoac SHA256 cua URL)
- shopId: Ma shop
- shopName: Ten shop
- page: So trang PDF
- size: Kich thuoc file (bytes)
- type: Loai file (mac dinh: "pdf")
- platform: Nen tang ("shopee" | "lazada" | "tiktok")
- search: Chuoi ma don hang/van don trich xuat tu PDF (phan cach bang dau phay)
- text: Noi dung van ban cua PDF
- s3Id: ID file tren S3
- s3Url: URL truy cap file tren S3
- s3Path: Duong dan file tren S3
- createdAt: Thoi diem tao (Unix timestamp ms)
- updatedAt: Thoi diem cap nhat (Unix timestamp ms)
```

### PrintPdfConfigOverlay (Template in)

```
- text:
  - active: Bat/tat chen van ban
  - content: Noi dung van ban
  - position: Vi tri ("top-left", "bottom-center", "custom", ...)
  - x: Toa do X (khi position = "custom")
  - y: Toa do Y (khi position = "custom")
  - fontSize: Co chu
  - isBold: In dam
- image:
  - active: Bat/tat chen hinh anh
  - data: Du lieu file anh (File object)
  - base64: Anh dang base64
  - position: Vi tri
  - x: Toa do X (khi position = "custom")
  - y: Toa do Y (khi position = "custom")
  - width: Chieu rong anh (pixel)
  - height: Chieu cao anh (pixel)
```

### PrintSession (Session in)

```
- active: Bat/tat session print
- isPagging: Hien thi so trang
- isBold: In dam
- isTimestamp: Hien thi timestamp
- content: Noi dung session name
- position: Vi tri hien thi
- fontSize: Co chu
```

### ConfigPdfPrinter (Cau hinh template)

```
- _id: ID ban ghi (PouchDB)
- name: Ten template
- config: PrintPdfConfigOverlay (cau hinh text va image overlay)
```

### Regex trich xuat ma don hang

| Nen tang | Loai | Pattern |
|----------|------|---------|
| **Shopee** | Ma van don | `SPX` + 5 ky tu alphanumeric tro len |
| **Shopee** | Ma don hang | 8 chu so + 6 ky tu alphanumeric |
| **Lazada** | Ma van don | `LMP` + 10-20 ky tu alphanumeric |
| **Lazada** | Ma don hang | 11-15 chu so |
| **TikTok** | Ma van don | 12 chu so |
| **TikTok** | Ma don hang | 18 chu so |

---

## Luu y quan trong

1. **Yeu cau tai khoan Premium:** Tinh nang in PDF chi kha dung cho tai khoan tra phi.
2. **Cache PDF:** PDF buffer duoc cache de tranh tai lai nhieu lan trong cung phien lam viec.
3. **Retry tu dong:** Khi tai PDF that bai, he thong tu dong thu lai toi da 5 lan, moi lan cach 5 giay. PDF co kich thuoc duoi 5KB bi coi la khong hop le.
4. **Luu tru S3:** File PDF in duoc tu dong upload len S3 theo cau truc `{platform}/{YYYY-MM-DD}/{shopId}/{printId}.pdf`.
5. **Dong bo template:** Cau hinh template duoc luu tren PouchDB local va dong bo voi CouchDB remote, cho phep su dung tren nhieu thiet bi.
6. **Tu dong don dep:** Log in cu hon 30-45 ngay se tu dong bi xoa de tiet kiem dung luong.
7. **Xu ly tuan tu:** Cac job luu log in duoc xu ly tuan tu (concurrency = 1) cho moi nen tang de tranh xung dot.
8. **Ho tro da nen tang:** Cung mot he thong template va log duoc su dung cho ca Shopee, Lazada va TikTok.
9. **Float button tren Shopee:** Khi dang o trang in don hang tren Shopee Seller Center (co `job_id` trong URL), extension tu dong hien thi nut in nhanh o goc duoi phai.
10. **Luu file hoac in truc tiep:** Nguoi dung co the chon luu file PDF ve may hoac mo truc tiep de in. Tuy chon nay duoc luu lai qua Pinia store.
