#!/usr/local/bin/python3
# -*- Coding: utf-8 -*-

import cgi
import sys
from db import database
from input import makepage_input
from show import makepage_show
from analyze import makepage_analyze
from config import makepage_config

#標準入出力のエンコード指定
sys.stdin.reconfigure(encoding='utf-8')
sys.stdout.reconfigure(encoding='utf-8')

# File
with open('index.html', encoding="utf-8" ,mode='r') as f:
    index_html = f.read()

#cgiオブジェクト作成
params = cgi.FieldStorage()

#パラメータを変数に格納
func        = params.getfirst('func', '')
nowdate     = params.getfirst('now_date', '')
item        = params.getfirst('item_list', '0')
assessment   = params.getfirst('assessment', '0')
comment     = params.getfirst('comment', '')
currentpage = params.getfirst('page', '1')

#bodyタグ用変数の初期化
bodycontents =''

if func == '1' :
    # 登録：表示
    bodycontents = makepage_input()

if func == '2' :

    # DBへの登録有無
    if  nowdate != '' and item != '' and assessment != '':
        # DB利用
        db = database("jibun.db")
        # DB登録関数の呼び出し
        db.insert_record(int(item), int(assessment), comment)

    # 記録：表示
    bodycontents = makepage_show(currentpage)

if func == '3' :
    # 分析：表示
    bodycontents = makepage_analyze()

if func == '4' :
    # 設定：表示
    bodycontents = makepage_config()

if func == '9' :
    # DB利用
    db = database("jibun.db")
    # 設定：初期化
    bodycontents = db.delete_all_record()

#<body>内容を出力
htmlText = index_html.format(content = bodycontents)
print(htmlText)