import webview as wb
import sys
import os
import json


class Api:
  def button_click(self):
    print("button click.")
    path=os.path.split(os.path.abspath(sys.argv[0]))[0]
    print(path)
    result = wnd.create_file_dialog(dialog_type=wb.FOLDER_DIALOG,directory=path)
    print(result)
    return result[0] if result else ''

  def save_plan(slef,plan:str):
    path=os.path.split(os.path.abspath(sys.argv[0]))[0]
    file_name=wnd.create_file_dialog(dialog_type=wb.SAVE_DIALOG,directory=path,save_filename="默认方案",
                           file_types=('JSON (*.json)', 'All files (*.*)'))
    print(file_name)
    json_str=json.dumps(plan, sort_keys=True, indent=4, separators=(',', ': '),ensure_ascii=False)
    with open(file_name,"w+",encoding="utf-8") as f:
      f.write(json_str)

wnd=wb.create_window("My Web App",url="layui/index.html",js_api=Api(),width=1200)
wb.start(http_server=True,debug=True)
