# -*- coding: utf-8 -*-
import sys, os
sys.stdout.reconfigure(encoding='utf-8')
import openpyxl

BASE = os.path.join(os.path.dirname(os.path.abspath(__file__)), '광역_사이트맵')
# (기관, 광역상대경로, 검수일, 보드행수)
ITEMS = [
    ('계룡시', '충청남도/1.계룡시', '2026-05-31', 429, 1),
    ('공주시', '충청남도/2.공주시', '2026-05-31', 400, 1),
    ('금산군', '충청남도/3.금산군', '2026-05-31', 342, 1),
    ('논산시', '충청남도/4.논산시', '2026-05-31', 576, 1),
    ('당진시', '충청남도/5.당진시', '2026-06-01', 345, 1),
    ('보령시', '충청남도/6.보령시', '2026-05-31', 631, 1),
    ('부여군', '충청남도/7.부여군', '2026-06-01', 570, 1),
    ('고창군', '전북특별자치도/1.고창군', '2026-06-01', 352, 2),
    ('군산시', '전북특별자치도/2.군산시', '2026-06-02', 430, 2),
    ('남원시', '전북특별자치도/4.남원시', '2026-06-02', 654, 2),
    ('무주군', '전북특별자치도/5.무주군', '2026-06-02', 347, 2),
]

def datarows(folder, inst):
    import glob
    prov = folder.split('/')[0]
    cands = glob.glob(os.path.join(BASE, folder.replace('/', os.sep), f'{prov}_{inst}.xlsx'))
    if not cands:
        return None, None
    p = cands[0]
    wb = openpyxl.load_workbook(p, read_only=True); ws = wb.active
    n = sum(1 for row in ws.iter_rows(min_row=3, min_col=2, max_col=2) if row[0].value not in (None, ''))
    wb.close()
    return n, p

print(f"{'기관':<6}{'프리':<5}{'검수일':<12}{'보드행수':<8}{'실제행수':<8} 비고")
fl_tot = {1: [0, 0], 2: [0, 0]}
for inst, folder, d, board, fl in ITEMS:
    n, p = datarows(folder, inst)
    note = '' if n == board else f'⚠ 불일치(보드{board}≠실제{n})'
    print(f"{inst:<6}{('프'+str(fl)):<5}{d:<12}{str(board):<8}{str(n):<8} {note}")
    fl_tot[fl][0] += 1
    fl_tot[fl][1] += (n if isinstance(n, int) else 0)

print('\n프리랜서별 합계(실제행수 기준):')
print(f'  프리랜서1 (충남): {fl_tot[1][0]}곳, {fl_tot[1][1]}행')
print(f'  프리랜서2 (전북): {fl_tot[2][0]}곳, {fl_tot[2][1]}행')
print(f'  전체: {fl_tot[1][0]+fl_tot[2][0]}곳, {fl_tot[1][1]+fl_tot[2][1]}행')
