from random import randrange

from flask import Flask, render_template, request, redirect
from pyecharts import options as opts
from pyecharts.charts import Bar, Geo, Map
from pyecharts.charts import Bar
from pyecharts.faker import Faker

import readdata_amro
import test, readdata
import ddrobot
import amro_progress

app = Flask(__name__)


def bar_base() -> Geo:
    c = (
        Geo()
            .add_schema(maptype="china")
            .add("geo", [list(z) for z in zip(Faker.provinces, Faker.values())])
            .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
            .set_global_opts(
            visualmap_opts=opts.VisualMapOpts(is_piecewise=True),
            title_opts=opts.TitleOpts(title="Geo-VisualMap（分段型）"),
        )
    )
    return c


@app.route("/")
def index():
    return render_template("index2.html")

@app.route("/getData")
def getData():
    #flightList = readdata_amro.searchData(AMRO)
    flightList = readdata_amro.loadData('20230303')
    data = readdata_amro.changedata(flightList)
    #data = readdata.getData2()
    print(data)
    return data

@app.route("/getSelectInf", methods=['POST','GET'])
def getSelectInf():
    print("getSelectInf")
    chList = readdata.chList
    if request.method == 'POST':
        result = request.form
        print(result['bay'])
        flightList = readdata_amro.loadData('20230303')
        dic = readdata_amro.changedata(flightList)
        data = dic[result['bay']]

        ndata = {}
        ldata = []
        for key in data:
            ldata.append(data[key])
        for i in range(len(chList)):
            ndata[chList[i]] = ldata[i]
        print(ndata)
        return render_template('table.html', data=ndata)
    return 'err'

@app.route("/getDigital")
def getDigital():
    flightList = readdata_amro.loadData('20230303')
    data = readdata_amro.changeAlldata(flightList)
    chList = readdata.chList2
    return render_template('table-digital.html', data=data, chList=chList)

@app.route("/sendMsgBay", methods=['POST','GET'])
def sendMsgBay():
    chList = readdata.chList
    if request.method == 'POST':
        result = request.form
        print(result['bay'])
        dic = readdata.getData2()
        data = dic[result['bay']]

        ndata = {}
        ldata = []
        for key in data:
            ldata.append(data[key])
        for i in range(len(chList)):
            ndata[chList[i]] = ldata[i]
        print(ndata)
        s = "\n> - 机位：{} \n> - 机号：{} 机型：{} {}\n - 任务类型：{}\n - 进港时间：{} 出港时间：{}\n - 航班号：{}\n - 进港起飞：{} 离港到达：{}\n - 放行：{}\n - 工作者：{}\n - 备注：{}".format(
            result['bay'],ndata['机号'],ndata['机型'],ndata['发动机'],ndata['任务类型'],ndata['进港时间'],ndata['出港时间'],ndata['航班号'],ndata['进港起飞'],ndata['离港到达'],ndata['放行人员'],ndata['维修人员'],ndata['备注'])
        atuser = []
        fxList = ndata['放行人员'].split(',')
        wxList = ndata['维修人员'].split(',')
        for i in fxList:
            atuser.append(i)
        for n in wxList:
            atuser.append(n)
        ddrobot.sendMsg(s, atuser)
        return "sendMsg"

@app.route("/sendMsgNo", methods=['POST','GET'])
def sendMsgNo():
    if request.method == 'POST':
        data = readdata.getAllData()
        result = request.form
        no = int(result['no'])
        print(data)
        msg = data[no]
        print(msg)
        s = "\n> - 机位：{} \n> - 机号：{} 机型：{} {}\n - 任务类型：{}\n - 进港时间：{} 出港时间：{}\n - 航班号：{}\n - 进港起飞：{} 离港到达：{}\n - 放行：{}\n - 工作者：{}\n - 备注：{}".format(
            msg['bay'], msg['bnum'], msg['actype'], msg['engtype'], msg['tasktype'], msg['timein'], msg['timeout'], msg['ftnum'], msg['pos1'], msg['pos2'], msg['FX'], msg['WX'], msg['remark'])
        atuser = []
        fxList = msg['FX'].split(',')
        wxList = msg['WX'].split(',')
        for i in fxList:
            atuser.append(i)
        for n in wxList:
            atuser.append(n)
        ddrobot.sendMsg(s, atuser)
    return "sendMsgNo"


@app.route("/barChart")
def get_bar_chart():
    c = bar_base()
    return c.dump_options_with_quotes()




if __name__ == "__main__":
    import os
    if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
        AMRO = amro_progress.amro()
        AMRO.quitWeb()
    app.run(host='0.0.0.0',port=6010, debug=True)
