
import datetime

from PyQt6.QtWidgets import QWidget , QMessageBox

from UI.AddPG import Ui_FormAddPG
from .LogerinTxt import app_logger
from Functions.apiFunction import api_fc


class AddPGWidget(Ui_FormAddPG, QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        try:
            self.setupUi(self)
            nowDay = datetime.datetime.now()
            self.dateTimeEdit.setDateTime(nowDay)
            self.pushButton.clicked.connect(self.btn)
        except Exception as e:
            app_logger.log_error(e)

    def getname(self, name, date, ip):
        try:
            self.getname = name
            self.getdate = date
            self.getip = ip
            # print(self.getname)
            # print(self.getdate)
        except Exception as e:
            app_logger.log_error(e)

    # def severpost_meassge(self, postname, postdata, postdata2, postdata3):
    #     # print(ip)
    #     try:
    #         ip = "http://" + str(self.getip) + "/static/" + str(postname) + str(postdata) + str(postdata2) + str(
    #             postdata3)
    #         res = requests.get(url=ip, timeout=30).json()
    #         if res["返回值"] == "ok":
    #             QMessageBox.warning(self, "提示", "已创建待办")
    #         else:
    #             QMessageBox.warning(self, "提示", "创建待办失败，请检查配置文件或联系管理员检查服务器！")
    #     except Exception as e:
    #         QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")
    #         app_logger.log_error(e)

    def severpost_meassge(self, postname, **kwargs):
        try:
            # ip = "http://" + str(self.ipinfo) + "/static/" + str(postname) + str(postdata) + str(postdata2) + str(postdata3) + str(postdata4)
            # res = requests.get(url=ip, timeout=30).json()
            res = api_fc.request_post('/static/{}'.format(postname), **kwargs)
            #print('postname:{}, msg:{}'.format(postname, res['msg']))
            if res["返回值"] == "ok":
                QMessageBox.warning(self, "提示", "已创建待办")
            else:
                QMessageBox.warning(self, "提示", "创建待办失败，请检查配置文件或联系管理员检查服务器！")
        except Exception as e:
            app_logger.log_error(e)
            QMessageBox.warning(self, "提示", "服务器连接超时，请联系管理员检查服务器！")

    def btn(self):
        try:
            dealtext = self.textEdit.toPlainText()
            time = self.dateTimeEdit.dateTime().toString('yyyy-M-d H:mm')
            # print(time)
            self.severpost_meassge("insertLogs",
                                   text="/%s" % dealtext,
                                   name="/%s" % self.getname,
                                   time="/%s" % time)
            self.close()
        except Exception as e:
            app_logger.log_error(e)