from knihovny.medicus_db import MedicusDB
import pymysql

# ==========================================
# CONFIGURATION
# ==========================================
HOST = "192.168.1.4"
DB_PATH = r"z:\Medicus 3\data\MEDICUS.FDB"
db = MedicusDB(HOST, DB_PATH)
patients = db.get_all_patients(as_dict=True)

patients_by_rc = {p["rodcis"]: p for p in patients}
print(f"Loaded {len(patients_by_rc)} registered patients")
print(patients_by_rc)
print(len(patients_by_rc))


# exit(0)

MYSQL_CONFIG = {
    "host": "192.168.1.76",
    "port": 3307,
    "user": "root",
    "password": "Vlado9674+",
    "database": "medevio",
    "charset": "utf8mb4",
    "autocommit": True
}
mysql = pymysql.connect(
    cursorclass=pymysql.cursors.DictCursor,
    **MYSQL_CONFIG
)

with mysql.cursor() as cur:
    cur.execute("select distinct rc from vzp_stav_pojisteni where prijmeni is null or jmeno is null")

    for radek in cur.fetchall():
        print(radek, radek['rc'] in patients_by_rc)
        rodcis=radek['rc']
        # if radek['jmeno'] is None or radek['prijmeni'] is None:
        #     if radek['rc'] in patients_by_rc:
        #         with mysql.cursor() as tmpcur:
        #             tmpcur.execute('update vzp_stav_pojisteni set prijmeni=%s, jmeno=%s where id=%s',
        #                            (patients_by_rc[radek['rc']]['prijmeni'],patients_by_rc[radek['rc']]['jmeno'],int(radek['id'])))
        if rodcis in patients_by_rc:
            jmeno=patients_by_rc[rodcis]['jmeno']
            prijmeni=patients_by_rc[rodcis]['prijmeni']
            with mysql.cursor() as tmpcur:
                tmpcur.execute("update vzp_stav_pojisteni set prijmeni=%s, jmeno=%s where rc=%s",(prijmeni, jmeno, rodcis))



