from openpyxl import load_workbook
from openpyxl.styles import *
import time
import warnings
warnings.filterwarnings('ignore')

#判断是否在校
#此处为入校申请
def inschool (u,a):
    wb = load_workbook('Student.xlsx')
    ws = wb.active
    u_inschool = u+1
    state = ws.cell(row=u_inschool, column=3).value
    
    if state == 'y' and a == 1:
        print("您已在学校，不能提交入校申请")
        #在校状态不能申请入校
    elif state == 'n' and a == 1:
        print("您可以正常申请入校")
        #离校状态可以正常申请入校
        return 1
        
#填写表格
def write_form (a,c,p):
    print("=============================================")
    print("开始填表")
    #申请类型
    application = int(a)
    
    #姓名
    name = input("请输入姓名：")
    
    #学号
    id = input("请输入学号：")
    
    #联系电话
    phone = input("请输入联系电话：")
    
    #时间
    Time = ''
    while Time == '':
        t1 = input("请输入入校时间（格式样例：2022-07-22 23:00:00）（要求为当日且不能早于6点晚于22点）：")
        # t1 = str('2022-10-23 21:00:00')
        Time = str(time_judge(t1))
    
    #校门    
    door = ''
    while door == '':
        d = int(input("请选择校门（1.东门 2.南门 3.西门 4.北门）："))
        door = door_choice(d)
    
    #以下为前端数据，此处用输入代替
    #地址
    province = input("请输入省市：")
    district = input("请输入区：")
    place = input("请输入详细地址：")
    
    #健康码
    health_code = ''
    while health_code == '':
        h = str(input("请输入健康码状态（绿/黄/红）："))
        health_code = health_code_judge(h)
    
    #行程码
    Tour_code = input("请输入行程码状态：")
    
    #核酸
    NAT = ''
    while NAT == '':
        n = str(input("请输入核酸状态（阴/阳）："))
        NAT = NAT_judge(n)
    
    #申请状态
    application_count = int(c + 1)
    
    #通过状态
    Pass = int(p)
    
    #提交确认
    submit = Submit(name,id,phone,Time,door,province,district,place,health_code,Tour_code,NAT)
    if submit == 'n':
        print("填写取消，返回上级菜单")
        return 0
   
    #封装
    data = data_set(application,name,id,phone,Time,door,province,district,place,health_code,Tour_code,NAT,application_count,Pass)
    #print(data)
    
    #写入excel
    write(data)
    
    #调整excel格式
    style('Form.xlsx')

#判断时间合理性
def time_judge(t11):
    t1 = time.strptime(t11, "%Y-%m-%d %H:%M:%S")
    t = time.localtime()
    year = str(t.tm_year)
    mon = str(t.tm_mon)
    day2 = str(t.tm_mday)
    hour = str(t.tm_hour)
    minute = str(t.tm_min)
    sec = str(t.tm_sec)
    t22 = str(year + '-' + mon + '-' + day2 + ' ' + hour + ':' + minute + ':' + sec)
    t2 = time.strptime(t22, "%Y-%m-%d %H:%M:%S")
    
    #判断填写时间是否比当前时间早
    tl = (time.mktime(t1)-time.mktime(t2))
    if tl <= 0:
        print("请输入比当前时间晚的时间")
        return ''
    
    #判断日期是否为当日
    day1 = str(t1.tm_mday)
    if day1 != day2:
        print("申请只能为当日")
        return ''
    
    hour1 = t1.tm_hour
    minute1 = t1.tm_min
    sec1 = t1.tm_sec
    
    #判断申请时间是否晚于6点
    if hour1 < 6:
        print("入校必须晚于6点！")
        return ''
    
    #判断申请时间是否早于10点
    if hour1 > 22:
        print("入校必须早于22点！")
        return ''
    elif hour1 == 22 and minute1 != 0 and sec1 != 0:
        print("入校必须早于22点！")
        return ''

    return t11
     
#选择校门
def door_choice(d):
    if d == 1:
        return 'East'
    elif d == 2:
        return 'South'
    elif d == 3:
        return 'West'
    elif d == 4:
        return 'North'
    else:
        print("请选择正确的校门！")
        return ''
    
#判断健康码信息填写正确性
def health_code_judge(h):
    if h != "绿" and h != "黄" and h != "红":
        print("请输入正确的健康码信息")
        return ''
    else:
        return h
    
#判断核酸信息填写正确性
def NAT_judge(n):
    if n != "阴" and n != "阳":
        print("请输入正确的核酸信息")
        return ''
    else:
        return n
 
#提交确认
def Submit(name,id,phone,Time,door,province,district,place,health_code,Tour_code,NAT):
    print("=============================================")
    print("申请类型为：入校申请")
    print("姓名：" + name)
    print("学号：" + id)
    print("联系电话：" + phone)
    print("时间：" + Time)
    print("校门：" + door)
    print("省市：" + province)
    print("区：" + district)
    print("详细地址：" + place)
    print("健康码信息：" + health_code)
    print("行程码信息：" + Tour_code)
    print("核酸：" + NAT)
    print("申请状态：待审核")
    print("通过状态：待审核")
    submit = input("以上为填写信息，是否提交（y/n）")
    return submit
    
#数据封装
def data_set(application,name,id,phone,time,door,province,district,place,health_code,Tour_code,NAT,application_count,Pass):
    data_form = {
        "application": application,
        "name": name,
        "id": id,
        "phone": phone,
        "time": time,
        "door": door,
        "province": province,
        "district": district,
        "place": place,
        "health_code": health_code,
        "Tour_code": Tour_code,
        "NAT": NAT,
        "application_count": application_count,
        "Pass": Pass
    }
    return data_form

#数据写入excel
def write(data):
    wb = load_workbook('Form.xlsx')
    ws = wb.active
    d = (
        data["application"], 
        data["name"], 
        data["id"],
        data["phone"], 
        data["time"], 
        data["door"],
        data["province"], 
        data["district"], 
        data["place"],
        data["health_code"], 
        data["Tour_code"], 
        data["NAT"],
        data["application_count"],
        data["Pass"]
    )
    ws.append(d)
    wb.save('Form.xlsx')
    print("提交成功！")

#调整excel格式
def style(file):
    wb = load_workbook(file)
    ws = wb.active
    for row in ws.iter_rows():
        for cell in row:
            if cell is not None:
                cell.alignment = Alignment(horizontal='center',vertical='center')
    wb.save(file)

#代码运行主体
x = 1
while x:
    user = int(input("请选择用户（1.张三 2.李四 3.王五）:"))
    print("您选择的是入校申请")
    #申请类型：1为入校申请 2为出校申请 此处默认为1
    application = 1
    #申请状态（0无1有）
    application_count = 0
    #通过状态默认为0
    Pass = 0
    if inschool(user,application):
        write_form(application,application_count,Pass)
        x = 0
    else:
        print("请返回上级菜单，提交申请请选择出校申请")