import sys
from PyQt6.QtCore import QTimer, QUrl
from PyQt6.QtGui import QAction, QIcon, QPixmap
from PyQt6.QtWidgets import *
from Functions.QTMain import MainWidget
from Functions.apiFunction import api_fc
import ctypes
import datetime
import base64
from PyQt6.QtCore import QByteArray
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("myappid")
class MenuDemo(QMainWindow):
    def __init__(self, parent=None):
        super(MenuDemo, self).__init__(parent)
        # 从Base64创建图标
        icon_base64 = """
               [encoded data, 360532 characters]

               """
        icon_data = base64.b64decode(icon_base64)
        pixmap = QPixmap()
        pixmap.loadFromData(QByteArray(icon_data))
        icon = QIcon(pixmap)

        self.setWindowTitle('天府机务分部准备组标准化综合办公平台')
        self.resize(1400, 900)  # 宽×高
        self.setMinimumSize(1400, 900)
        self.setWindowIcon(icon)
        # 最小窗口尺寸
        # self.setMaximumSize(2000, 1000)  # 最大窗口尺寸
        self.child = MainWidget()
        ############主窗口控件####################

        menuBar = self.menuBar()
        peopleMenu = menuBar.addMenu('人员管理')
        logsMenu = menuBar.addMenu('信息管理')
        accountMenu = menuBar.addMenu('账户管理')
        analysisMenu = menuBar.addMenu('统计分析')
        flightMenu = menuBar.addMenu('航班管理')
        serverMenu =menuBar.addMenu('服务器配置')
        taskMenu = menuBar.addMenu('通知管理')
        GJMenu = menuBar.addMenu('工具管理')

        peopleMenu.addAction(QAction('白班考勤获取', self))
        peopleMenu.addAction(QAction('夜班考勤获取', self))
        peopleMenu.addAction(QAction('添加人员信息', self))
        peopleMenu.triggered[QAction].connect(self.people)

        logsMenu.addAction(QAction('清空待办', self))
        logsMenu.addAction(QAction('更新准备组任务', self))
        logsMenu.addAction(QAction('更新风险数据库', self))
        logsMenu.addAction(QAction('导出风险数据库', self))
        logsMenu.addAction(QAction('查看日志', self))
        logsMenu.addAction(QAction('查看拨号进程', self))
        logsMenu.addAction(QAction('清空待办(超管)', self))
        logsMenu.addAction(QAction('打开授权认证', self))
        logsMenu.addAction(QAction('关闭授权认证', self))
        logsMenu.triggered[QAction].connect(self.logs)

        accountMenu.addAction(QAction('注册账号', self))
        accountMenu.addAction(QAction('修改密码', self))
        accountMenu.addAction(QAction('变更权限', self))
        accountMenu.triggered[QAction].connect(self.account)

        #analysisMenu.addAction(QAction('统计分析', self))
        analysisMenu.addAction(QAction('显示地图', self))
        analysisMenu.addAction(QAction('排班辅助', self))

        analysisMenu.triggered[QAction].connect(self.analysis)

        flightMenu.addAction(QAction('停场模板下载', self))
        flightMenu.addAction(QAction('停场航班导入', self))
        flightMenu.addAction(QAction('全天航班导出(未含保障结束)', self))
        flightMenu.addAction(QAction('交班航班导出', self))
        flightMenu.addAction(QAction('航班安排备份导出', self))
        flightMenu.addAction(QAction('维修记录清点导出', self))
        flightMenu.addAction(QAction('人工添加航班', self))
        flightMenu.addAction(QAction('航班交接', self))
        flightMenu.triggered[QAction].connect(self.flight)

        serverMenu.addAction(QAction('配置服务器IP', self))
        serverMenu.addAction(QAction('配置服务器IP电话', self))
        serverMenu.addAction(QAction('服务器AMRO账号登录', self))
        serverMenu.addAction(QAction('服务器AMRO cookie登录', self))
        serverMenu.addAction(QAction('服务器AMRO注销', self))
        serverMenu.addAction(QAction('配置本地IP电话', self))
        #serverMenu.addAction(QAction('测试呼出拨号', self))
        #serverMenu.addAction(QAction('测试通讯建立', self))
        #serverMenu.addAction(QAction('测试通讯挂断', self))
        #serverMenu.addAction(QAction('测试上传腾讯云', self))
        #serverMenu.addAction(QAction('测试腾讯云信息上报', self))
        serverMenu.triggered[QAction].connect(self.server)

        taskMenu.addAction(QAction('打开个人警告信息推送', self))
        taskMenu.addAction(QAction('关闭个人警告信息推送', self))
        taskMenu.triggered[QAction].connect(self.task)

        GJMenu.addAction(QAction('一中队工具清查', self))
        GJMenu.triggered[QAction].connect(self.GJ)

        ############子窗口####################
        self.setCentralWidget(self.child)
        #print(self.child.loginSts)
        self.child.loginWidget.pushButton.clicked.connect(self.showChild)




    def remoteCheck(self):
        res = api_fc.request_post_without_token("/static/remoteCheck", checknumber=self.child.version)
        if res["返回值"] != "ok":
            QMessageBox.warning(self, "提示", "有新版更新！请前往NAS共享平台[机务一队/天府机务分部/31准备组无纸化办公/1航班助手/软件]获取【最新版版本号:%s】！！"%res["返回值"])
            self.close()

    def GJ(self, q):
        if q.text() == '一中队工具清查':
            if int(self.child.authority) != 3:
                relay = QMessageBox.warning(self, "警告", "确认开始工具查询吗？该操作将推送一中队查询信息并@一中队钉钉成员！！", QMessageBox.StandardButton.Yes,QMessageBox.StandardButton.Close)
                if relay == QMessageBox.StandardButton.Yes:
                    try:
                        api_fc.request_post("/static/MM_GJJH_LIST",team=1)
                    except:
                        QMessageBox.warning(self, "工具查询功能失效", "工具查询功能失效请联系管理员！")
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")

    def task(self, q):
        if q.text() == '打开个人警告信息推送':
            if int(self.child.authority) != 3:
                relay = QMessageBox.warning(self, "警告", "确认打开个人警告信息推送？该操作将实时推送警告给个人！！", QMessageBox.StandardButton.Yes,QMessageBox.StandardButton.Close)
                if relay == QMessageBox.StandardButton.Yes:
                    try:
                        api_fc.request_post("/changeSinglepoepleNotice",boolsts=1)
                    except:
                        QMessageBox.warning(self, "个人警告信息推送功能失效", "个人警告信息推送功能失效功能失效请联系管理员！")
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
        elif q.text() == '关闭个人警告信息推送':
            if int(self.child.authority) != 3:
                relay = QMessageBox.warning(self, "警告", "确认关闭个人警告信息推送？该操作将不再实时推送警告给个人！！", QMessageBox.StandardButton.Yes,QMessageBox.StandardButton.Close)
                if relay == QMessageBox.StandardButton.Yes:
                    try:
                        api_fc.request_post("/changeSinglepoepleNotice",boolsts=0)
                    except:
                        QMessageBox.warning(self, "个人警告信息推送功能失效", "个人警告信息推送功能失效功能失效请联系管理员！")
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")

    def server(self, q):
        if q.text() == '配置服务器IP':
            #self.setipinfo()
            self.child.findServerWidget.show()
        elif q.text() == '配置服务器IP电话':
            if int(self.child.authority) !=3:
                relay = QMessageBox.warning(self, "警告", "该操作将配置服务器自动呼叫中心（0332），请确认是否要配置服务器端IP电话！！频繁配置将导致0332IP电话死机，请不要多次点击！配置期间将出现程序无响应情况，期间建议不操作系统，等待配置结果！！",
                                            QMessageBox.StandardButton.Yes,
                                            QMessageBox.StandardButton.Close)
                if relay == QMessageBox.StandardButton.Yes:
                    self.child.setseverIpPhone()
                    #self.child.setIpPhone()
                    try:
                        api_fc.request_post("/static/insertLogs2",text="配置服务器IP电话操作",name=str(self.child.user),time=str(datetime.datetime.now()))
                    except :
                        QMessageBox.warning(self, "日志功能失效", "操作日志功能失效请联系管理员！")
                    #QMessageBox.warning(self, "提示", "功能正在开发，敬请期待！")
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
        elif q.text() == '服务器AMRO cookie登录':
            try:
                if int(self.child.authority) <= 1:
                    self.child.remoteCookielogin()
                else:
                    QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
            except:
                # print('登录服务器失败')
                res = ""
                QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
        elif q.text() == '服务器AMRO账号登录':
            try:
                if int(self.child.authority) <= 1:
                    self.child.remotelogin()
                else:
                    QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
            except:
                # print('登录服务器失败')
                res = ""
                QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
        elif q.text() == '服务器AMRO注销':
            try:
                if int(self.child.authority) <= 1:
                    relay = QMessageBox.warning(self, "警告", "注销AMRO账号登录将无法保持航班信息更新，慎重操作！！",
                                                QMessageBox.StandardButton.Yes,
                                                QMessageBox.StandardButton.Close)
                    if relay == QMessageBox.StandardButton.Yes:
                        try:
                            res = api_fc.request_get('/static/remotelogout')
                            if res['stauts'] == "ok":
                                try:
                                    api_fc.request_post("/static/insertLogs2", text="服务器AMRO注销操作", name=str(self.child.user),time=str(datetime.datetime.now()))
                                except:
                                    QMessageBox.warning(self, "日志功能失效", "操作日志功能失效请联系管理员！")
                                QMessageBox.warning(self, "提示", "服务器AMRO注销成功！")
                            else:
                                QMessageBox.warning(self, "提示", "服务器AMRO注销失败，请检查服务器端注销或联系管理员检查服务器！")
                        except:
                            QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
                else:
                    QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
            except:
                # print('登录服务器失败')
                res = ""
                QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
        elif q.text() == '配置本地IP电话':
            if int(self.child.authority) != 3:
                self.child.setLocalIpPhone()
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")

        elif q.text() == '测试呼出拨号':
            api_fc.request_get( "/static/calltry")
        elif q.text() == '测试通讯建立':
            api_fc.request_get( "/static/callon")
            api_fc.request_get( "/static/callon")
        elif q.text() == '测试通讯挂断':
            api_fc.request_get( "/static/calldown")
    def flight(self, q):
        if q.text() == '停场模板下载':
            if int(self.child.authority) != 3:
                self.child.saveCase()
                #QMessageBox.warning(self, "提示", "功能正在开发，敬请期待！")
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
        elif q.text() == '停场航班导入':
            if int(self.child.authority) != 3:
                self.child.updateStopFlight()
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
            #QMessageBox.warning(self, "提示", "功能正在开发，敬请期待！")
        elif q.text() == '全天航班导出(未含保障结束)':
            if int(self.child.authority) != 3:
                self.child.saveAllflight()
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
            #QMessageBox.warning(self, "提示", "功能正在开发，敬请期待！")
        elif q.text() == '航班安排备份导出':
            if int(self.child.authority) != 3:
                self.child.saveAllflight2()
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
            #QMessageBox.warning(self, "提示", "功能正在开发，敬请期待！")
        elif q.text() == '维修记录清点导出':
            self.child.saveAllflight3()

        elif q.text() == '交班航班导出':
            if int(self.child.authority) != 3:
                self.child.saveDealflight()
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
            #QMessageBox.warning(self, "提示", "功能正在开发，敬请期待！")
        elif q.text() == '人工添加航班':
            try:
                if int(self.child.authority) <= 1:
                    QMessageBox.warning(self, "提示", "注意：人工添加请确认航班信息准确性，人工添加航班无法进行派工操作！无法自动获取航班状态！")
                    self.child.AddFlight()
                else:
                    QMessageBox.warning(self,"提示", "权限不够，请联系管理员！")
            except:
                #print('登录服务器失败')
                res = ""
                QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
        elif q.text() == '航班交接':
            try:
                if int(self.child.authority) <= 2:

                    self.child.HandoverFlight()#需要关联
                else:
                    QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
            except:
                # print('登录服务器失败')
                res = ""
                QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
    def people(self, q):

        if q.text() == '关于':
            print('关于-----')
        elif q.text() == '白班考勤获取':
            if int(self.child.authority) != 3:
                res=api_fc.request_post("/static/getPglist",bcstr="A")
                if res["code"] == 200:
                    QMessageBox.warning(self, "提示", "白班考勤名单获取成功！")
                    #print('白班考勤获取成功-----')
                    self.child.QThreadSortNow("people")
                else:
                    QMessageBox.warning(self, "提示", "白班考勤名单获取失败，请确认服务器状态或者派工权限！")
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")

        elif q.text() == '夜班考勤获取':
            if int(self.child.authority) != 3:
                res=api_fc.request_post("/static/getPglist",bcstr="B")
                if res["code"] == 200:
                    QMessageBox.warning(self, "提示", "夜班考勤名单获取成功！")
                    self.child.QThreadSortNow("people")
                    #print('夜班考勤获取成功-----')
                else:
                    QMessageBox.warning(self, "提示", "夜班考勤名单获取失败，请确认服务器状态或者派工权限！")
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
        elif q.text() == '添加人员信息':
            if int(self.child.authority) == 0:
                self.child.openFile()
                try:
                    api_fc.request_post("/static/insertLogs2", text="添加人员信息操作",name=str(self.child.user),time=str(datetime.datetime.now()))
                except :
                    QMessageBox.warning(self, "日志功能失效", "操作日志功能失效请联系管理员！")
                #print(self.child.selectedtime)
                #print(self.child.selectedbc)
                #print(self.child.user)
                #print('添加人员信息-----')
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
    def logs(self, q):
        if q.text() == '清空待办':
            try:
                if int(self.child.authority) <= 1:
                    relay = QMessageBox.warning(self, "警告", "清空待办将清除除人工和准备组任务外所有待办信息，慎重操作！！",
                                                QMessageBox.StandardButton.Yes,
                                                QMessageBox.StandardButton.Close)
                    if relay == QMessageBox.StandardButton.Yes:
                        try:
                            res=api_fc.request_post("/static/mandealAllLogs",mode="norm",user=self.child.user)
                            if res["status"] == "ok":
                                try:
                                    api_fc.request_post("/static/insertLogs2", text="清空待办操作",name=str(self.child.user),time=str(datetime.datetime.now()))
                                except:
                                    QMessageBox.warning(self, "日志功能失效", "操作日志功能失效请联系管理员！")
                                QMessageBox.warning(self, "提示", "清空日志成功！")
                            else:
                                QMessageBox.warning(self, "提示", "清空日志失败，请检查服务器或联系管理员检查服务器！")
                        except:
                            QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")

                        self.child.QThreadSortNow("change")
                        self.child.QThreadSortNow("main")
                else:
                    QMessageBox.warning(self,"提示", "权限不够，请联系管理员！")
            except:
                #print('登录服务器失败')
                res = ""
                QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
        elif q.text() == '清空待办(超管)':
            try:
                if int(self.child.authority) <= 0:
                    relay = QMessageBox.warning(self, "警告", "清空待办将清除所有待办信息，慎重操作！！",
                                                QMessageBox.StandardButton.Yes,
                                                QMessageBox.StandardButton.Close)
                    if relay == QMessageBox.StandardButton.Yes:
                        try:
                            res=api_fc.request_post("/static/mandealAllLogs",mode="super",user=self.child.user)
                            if res["status"] == "ok":
                                try:
                                    api_fc.request_post("/static/insertLogs2", text="清空待办(超管)操作",name=str(self.child.user),time=str(datetime.datetime.now()))
                                except :
                                    QMessageBox.warning(self, "日志功能失效", "操作日志功能失效请联系管理员！")
                                QMessageBox.warning(self, "提示", "清空日志成功！")
                            else:
                                QMessageBox.warning(self, "提示", "清空日志失败，请检查配置文件或联系管理员检查服务器！")
                        except:
                            QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")

                        self.child.QThreadSortNow("change")
                        self.child.QThreadSortNow("main")
                else:
                    QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
            except:
                # print('登录服务器失败')
                res = ""
                QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
        elif q.text() == '查看日志':
            try:
                if int(self.child.authority) <= 2:
                    self.child.showcheckLogs()
                else:
                    QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
            except:
                # print('登录服务器失败')
                res = ""
                QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
        elif q.text() == '查看拨号进程':
            if int(self.child.authority) != 3:
                self.child.checkCalllistupdate()
                self.child.checkCalllist.show()
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
        elif q.text() == '更新准备组任务':
            if int(self.child.authority) <= 1:
                self.child.updateTaskAuto()
                try:
                    api_fc.request_post("/static/insertLogs2", text="更新准备组任务操作",name=str(self.child.user),time=str(datetime.datetime.now()))
                except :
                    QMessageBox.warning(self, "日志功能失效", "操作日志功能失效请联系管理员！")
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
            #print(self.child.selectedtime)
            #print(self.child.selectedbc)
            #print(self.child.user)
        elif q.text() == '更新风险数据库':
            if int(self.child.authority) <= 1:
                self.child.updateRiskdb()
                try:
                    api_fc.request_post("/static/insertLogs2", text="更新风险数据库操作",name=str(self.child.user),time=str(datetime.datetime.now()))
                except :
                    QMessageBox.warning(self, "日志功能失效", "操作日志功能失效请联系管理员！")
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
        elif q.text() == '导出风险数据库':
            if int(self.child.authority) <= 1:
                self.child.saveRisktable()
                try:
                    api_fc.request_post("/static/insertLogs2", text="导出风险数据库操作",name=str(self.child.user),time=str(datetime.datetime.now()))
                except :
                    QMessageBox.warning(self, "日志功能失效", "操作日志功能失效请联系管理员！")
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
        elif q.text() == '打开授权认证':
            if int(self.child.authority) != 3:
                self.child.openmaintainAuthCheck()
                try:
                    api_fc.request_post("/static/insertLogs2", text="打开授权认证操作",name=str(self.child.user),time=str(datetime.datetime.now()))
                except :
                    QMessageBox.warning(self, "日志功能失效", "操作日志功能失效请联系管理员！")
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
        elif q.text() == '关闭授权认证':
            if int(self.child.authority) != 3:
                self.child.closemaintainAuthCheck()
                try:
                    api_fc.request_post("/static/insertLogs2", text="关闭授权认证操作",name=str(self.child.user),time=str(datetime.datetime.now()))
                except :
                    QMessageBox.warning(self, "日志功能失效", "操作日志功能失效请联系管理员！")
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
    def account(self, q):

        if q.text() == '退出':
            app = QApplication.instance()
            app.quit()
        elif q.text() =='注册账号':
            if int(self.child.authority) == 0:
                self.child.showAdduser()
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
        elif q.text() =='修改密码':
            self.child.showChangePassword()
            #print('修改密码-----')
        elif q.text() =='变更权限':
            try:
                if int(self.child.authority) <= 0:
                    self.child.showchangeAuth()
                else:
                    QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
            except:
                # print('登录服务器失败')
                res = ""
                QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
    def analysis(self,q):
        if q.text() == '显示地图':
            if int(self.child.authority) != 3:
                ip = "http://" + str(self.child.ipinfo) + "/map/" + str(self.child.selectedtime)+"/"+api_fc.access_token

                self.child.mapWebPage.webEngineView.load(QUrl(ip))
                self.child.mapWebPage.show()
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")
        elif q.text() == '排班辅助':
            if int(self.child.authority) < 2:
                self.child.showWorkloadinfos()
            else:
                QMessageBox.warning(self, "提示", "权限不够，请联系管理员！")

    def showChild(self):
        if self.child.loginSts == True:
            #print('showChild')
            self.show()
            self.serchTimer = QTimer()
            self.serchTimer.start(3600 * 1000)
            self.serchTimer.timeout.connect(self.remoteCheck)
            #self.setipinfo()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = MenuDemo()
    #demo.show()
    sys.exit(app.exec())