import frappe
import json

@frappe.whitelist()
def save_load_items(load_name, items):
    items = json.loads(items)
    load_doc = frappe.get_doc("Load", load_name)
    for item_data in items:
        for row in load_doc.material_items:
            if row.name == item_data["name"]:
                for field, value in item_data.items():
                    if field != "name":
                        row.set(field, value)
                break
    load_doc.save(ignore_permissions=True)
    frappe.db.commit()
    return {"status": "ok", "message": "Saved " + str(len(items)) + " items"}
