# -*- coding: utf-8 -*-
"""Voi moi tuan, in doan chua ten NCC kem CUM dang chua no (theo tieu de cum gan nhat)."""
import glob, os, re
from docx import Document

ROOT = r"C:\Claw 2\Bao cao - Hanh chinh\Dac-Phai-VIen-KT\Bao-Cao-Tuan"
FILES = []
for f in glob.glob(os.path.join(ROOT, "T*", "Bao_cao_tong_hop_Tuan*.docx")):
    m = re.search(r"Tuan(\d+)", os.path.basename(f))
    if m and not os.path.basename(f).startswith("~$"):
        FILES.append((int(m.group(1)), f))
FILES.sort()

WATCH = ["MTH", "Syncoat", "Visan", "Brochem", "Poly Poxy"]

for t, f in FILES:
    if t < 37:
        continue
    d = Document(f)
    print("\n############ TUAN %d ############" % t)
    cum = ""
    for para in d.paragraphs:
        s = para.text.strip()
        if not s:
            continue
        if re.match(r"^\d+\.\s+Cụm ", s):
            cum = s[:70]
        for w in WATCH:
            if re.search(re.escape(w), s, re.I):
                tag = w
                print("  [%s] %-52s %s" % (tag, cum[:52], s[:230]))
                break
    # bang muc III
    for tb in d.tables:
        for r in tb.rows:
            row = " | ".join(c.text.strip() for c in r.cells)
            if any(re.search(re.escape(w), row, re.I) for w in WATCH):
                print("  [BANG] %s" % row[:250])
