import unitls.baseFunction as baseFunction
from unitls.LogerinTxt import app_logger
from unitls.StaticDataclass import get_dic
from .flightDB import flightDB


def mapDispaly(fdb:flightDB,selectedtime):
    try:
        mapDispalydata = {}
        nowDayStr = selectedtime
        sortdatas= fdb.queryTabel('sortFlight{}'.format(nowDayStr), '*', "航班编号!=''")
        sorttable= baseFunction.TuplefindInList2(sortdatas , "1" , "3" , 5)
        #sorttable1 = fdb.queryTabel("sortFlight{}".format(nowDayStr),"*","CAST(级别 AS integer)='1' or CAST(级别 AS integer)='3'")
        #{机位：[机号，机型，发动机，航班类型，航班号，ta，td，进港机场，出港机场，状态，放行，维修人员，二送人员，保留，工作包，TASKID，flightid，color，备注]}

        datas = fdb.queryTabel('TaskFlightinfo{}'.format(nowDayStr), '*', "航班编号!=''")
        for row in sorttable:
            # print(row)
            otheroneSts = []
            flightinfodata = baseFunction.TuplefindInList1(datas , row[1 ] , 0)[0 ]
            if flightinfodata[3] == "TR":
                iddd=row[1]+"-1"
                iddddd = row[1] + "-2"
                jfjSts= baseFunction.TuplefindInList3(sortdatas , iddd , 0 , 5)
                sfjSts = baseFunction.TuplefindInList3(sortdatas , iddddd , 0 , 5)
                #jfjSts= fdb.queryTabel("sortFlight{}".format(nowDayStr),"级别","编号='%s'"%iddd)[0][0]
                #sfjSts = fdb.queryTabel("sortFlight{}".format(nowDayStr), "级别", "编号='%s'" % iddddd)[0][0]
               # print(otheroneSts)

            if (flightinfodata[3] =="AP" and flightinfodata[11] !="") or (flightinfodata[3] !="AP" and flightinfodata[10] !=""):
                if flightinfodata[3] == "AP" and flightinfodata[11] != "":
                    bay=flightinfodata[11]
                    color="#99CCCC"
                elif flightinfodata[3] == "AF" and flightinfodata[10] != "" and row[5]=="3":
                    bay = flightinfodata[10]
                    color = "#0095d9"
                elif flightinfodata[3] == "停场" and flightinfodata[10] != "" and row[5]=="3":
                    bay = flightinfodata[10]
                    color = "#0095d9"
                elif flightinfodata[3] == "AF" and flightinfodata[10] != "" and row[5]=="1":
                    bay = "Y"+flightinfodata[10]
                    color = "#f6ad49"
                elif flightinfodata[3] == "TAF" and flightinfodata[10] != "" and flightinfodata[6] == "":
                    bay = "Y"+flightinfodata[10]
                    color = "#f6ad49"
                elif flightinfodata[3] == "TAF" and flightinfodata[10] != "" and flightinfodata[6] != "":
                    bay = flightinfodata[10]
                    color = "#4c6cb3"
                elif flightinfodata[3] == "TR" and flightinfodata[10] != "" and jfjSts=="1":
                    bay = "Y"+flightinfodata[10]
                    color = "#f6ad49"
                elif flightinfodata[3] == "TR" and flightinfodata[10] != "" and jfjSts=="4" and sfjSts=="1":
                    bay = flightinfodata[10]
                    color = "#69b076"

                acno=flightinfodata[1]
                airpalneType=flightinfodata[26]
                EngType=flightinfodata[25]
                flightType = get_dic('taskType')[flightinfodata[3]]
                flightNos = flightinfodata[21]
                DEP_CH=flightinfodata[23].split("</br>")[0] #进港机场
                DEP_CH1=flightinfodata[23].split("</br>")[1] #出港机场
                flightsts = ""#flightinfodata[10]TASKINFO没有这个节点
                wx=flightinfodata[12]
                fx = flightinfodata[13]
                es = flightinfodata[19]
                flightid=flightinfodata[0]
                message=""#flightinfodata[32]

                e = "" #保留
                F = ""#工作包
                g = "" #taskid
                ta = ""
                td=""
                if flightinfodata[16] != "":
                    e = "有保留"
                if flightinfodata[18] != "":
                    F = "有工作包"
                if flightinfodata[0] != "":
                    g = flightinfodata[0]
                if "-" not in flightinfodata[9].split('</br>')[0]:
                    ta = flightinfodata[9].split('</br>')[0]
                if "-" not in flightinfodata[9].split('</br>')[1]:
                    td = flightinfodata[9].split('</br>')[1]
                if "CZ" in str(flightNos) or "OQ" in str(flightNos):
                    nanhang="1"
                elif "3U" not in str(flightNos) or "CSC" not in str(flightNos):
                    nanhang = "0"
                else:
                    nanhang = "3"

                try:
                    mapDispalydata[bay] = {"机号":acno,
                                            "机型":airpalneType,
                                            "发动机":EngType,
                                            "航班类型":flightType,
                                            "航班号":flightNos,
                                            "到达":ta,
                                            "起飞":td,
                                            "进港机场":DEP_CH,
                                            "出港机场":DEP_CH1,
                                            "状态":flightsts,
                                            "放行":fx,
                                            "维修人员":wx,
                                            "二送人员":es,
                                            "保留":e,
                                            "工作包":F,
                                            "TASKID":g,
                                            "flightid":flightid,
                                            "color":color,
                                            "备注":message,
                                            "南航":nanhang
                                            }
                except Exception as e:
                    app_logger.log_error(e)
                    pass
        return mapDispalydata
    except Exception as e:
        app_logger.log_error(e)