from PyQt5 import QtCore, QtGui, QtWidgets
from tehrep_show import  Showtehrep
import MySQLdb   as mysql

class Tehreper(object):
    def show_Tehrep(self):
        self.window = QtWidgets.QMainWindow()
        self.ui =  Showtehrep()
        self.ui.setupUi(self.window)
        self.window.show()
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(632, 296)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(20, 50, 31, 21))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(60, 50, 131, 20))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_3.setGeometry(QtCore.QRect(200, 50, 81, 20))
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_4.setGeometry(QtCore.QRect(290, 50, 81, 20))
        self.lineEdit_4.setObjectName("lineEdit_4")
        self.lineEdit_5 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_5.setGeometry(QtCore.QRect(380, 50, 91, 20))
        self.lineEdit_5.setObjectName("lineEdit_5")
        self.lineEdit_6 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_6.setGeometry(QtCore.QRect(480, 50, 141, 20))
        self.lineEdit_6.setObjectName("lineEdit_6")
        self.lineEdit_8 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_8.setGeometry(QtCore.QRect(80, 90, 151, 21))
        self.lineEdit_8.setObjectName("lineEdit_8")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(10, 30, 51, 21))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(60, 30, 141, 21))
        font = QtGui.QFont()
        font.setPointSize(8)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(210, 30, 121, 20))
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(290, 30, 91, 20))
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(380, 30, 111, 20))
        self.label_5.setObjectName("label_5")
        self.label_6 = QtWidgets.QLabel(self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(480, 30, 191, 21))
        self.label_6.setObjectName("label_6")
        self.label_7 = QtWidgets.QLabel(self.centralwidget)
        self.label_7.setGeometry(QtCore.QRect(20, 70, 51, 21))
        self.label_7.setObjectName("label_7")
        self.label_8 = QtWidgets.QLabel(self.centralwidget)
        self.label_8.setGeometry(QtCore.QRect(240, 70, 61, 21))
        self.label_8.setObjectName("label_8")
        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(230, 190, 161, 51))
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_4.clicked.connect(self.show_Tehrep)
        self.lineEdit_7 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_7.setGeometry(QtCore.QRect(20, 90, 41, 20))
        self.lineEdit_7.setObjectName("lineEdit_7")
        self.lineEdit_9 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_9.setGeometry(QtCore.QRect(240, 90, 51, 20))
        self.lineEdit_9.setObjectName("lineEdit_9")
        self.label_9 = QtWidgets.QLabel(self.centralwidget)
        self.label_9.setGeometry(QtCore.QRect(80, 70, 161, 21))
        self.label_9.setObjectName("label_9")
        self.lineEdit_10 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_10.setGeometry(QtCore.QRect(300, 90, 131, 20))
        self.lineEdit_10.setObjectName("lineEdit_10")
        self.label_10 = QtWidgets.QLabel(self.centralwidget)
        self.label_10.setGeometry(QtCore.QRect(300, 70, 131, 21))
        self.label_10.setObjectName("label_10")
        self.lineEdit_11 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_11.setGeometry(QtCore.QRect(440, 90, 71, 20))
        self.lineEdit_11.setObjectName("lineEdit_11")
        self.label_11 = QtWidgets.QLabel(self.centralwidget)
        self.label_11.setGeometry(QtCore.QRect(440, 70, 71, 21))
        self.label_11.setObjectName("label_11")
        self.lineEdit_12 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_12.setGeometry(QtCore.QRect(520, 90, 101, 20))
        self.lineEdit_12.setObjectName("lineEdit_12")
        self.label_12 = QtWidgets.QLabel(self.centralwidget)
        self.label_12.setGeometry(QtCore.QRect(530, 70, 71, 21))
        self.label_12.setObjectName("label_12")
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(110, 120, 401, 41))
        self.widget.setObjectName("widget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushButton = QtWidgets.QPushButton(self.widget)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.change)
        self.horizontalLayout.addWidget(self.pushButton)
        self.pushButton_2 = QtWidgets.QPushButton(self.widget)
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_2.clicked.connect(self.add)
        self.horizontalLayout.addWidget(self.pushButton_2)
        self.pushButton_3 = QtWidgets.QPushButton(self.widget)
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_3.clicked.connect(self.dels)
        self.horizontalLayout.addWidget(self.pushButton_3)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 632, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "tr_num"))
        self.label_2.setText(_translate("MainWindow", "Date_of_delivery_for_repair"))
        self.label_3.setText(_translate("MainWindow", "Repair_period"))
        self.label_4.setText(_translate("MainWindow", "Type_of_repair"))
        self.label_5.setText(_translate("MainWindow", "Date_of_transfer"))
        self.label_6.setText(_translate("MainWindow", "FIO_Employee_passed_repair"))
        self.label_7.setText(_translate("MainWindow", "Num_EPR"))
        self.label_8.setText(_translate("MainWindow", "Num_EAR"))
        self.pushButton_4.setText(_translate("MainWindow", "Show_Teh_Repair"))
        self.label_9.setText(_translate("MainWindow", "FIO_Employee_accepted_repair"))
        self.label_10.setText(_translate("MainWindow", "Position_of_repair_person"))
        self.label_11.setText(_translate("MainWindow", "Teh_Inv_num"))
        self.label_12.setText(_translate("MainWindow", "Teh_Div_num"))
        self.pushButton.setText(_translate("MainWindow", "Change_Teh_Repair"))
        self.pushButton_2.setText(_translate("MainWindow", "Add_Teh_Repair"))
        self.pushButton_3.setText(_translate("MainWindow", "Delete_Teh_Repair"))

    def change(self):

        try:
            ids = int(self.lineEdit.text())
            Date_of_delivery_for_repair = self.lineEdit_2.text()
            Repair_period = self.lineEdit_3.text()
            Type_of_repair = self.lineEdit_4.text()
            Date_of_transfer = self.lineEdit_5.text()
            FIO_Employee_passed_repair = self.lineEdit_6.text()
            Number__Employee_passed_repair = int(self.lineEdit_7.text())
            FIO_Employee_accepted_repair = self.lineEdit_8.text()
            Number__Employee_accepted_repair = int(self.lineEdit_9.text())
            Position_of_repair_person = self.lineEdit_10.text()
            technics_technics_inventory_num = int(self.lineEdit_11.text())
            technics_Division_Division_num = int(self.lineEdit_12.text())
        except:
            return

        try:
            db = mysql.connect(host="localhost", user="root", passwd="1234", db="lab4")
            cur = db.cursor()
            ch1="""UPDATE technics_repair SET Date_of_delivery_for_repair = %s, Repair_period = %s, Type_of_repair = %s,Date_of_transfer = %s,  FIO_Employee_passed_repair = %s, Number__Employee_passed_repair = %s, FIO_Employee_accepted_repair = %s, Number__Employee_accepted_repair = %s, Position_of_repair_person = %s, technics_technics_inventory_num = %s, technics_Division_Division_num = %s WHERE technics_repair_num = %s"""
            ch2=( Date_of_delivery_for_repair, Repair_period, Type_of_repair, Date_of_transfer, FIO_Employee_passed_repair, int(Number__Employee_passed_repair),FIO_Employee_accepted_repair, int(Number__Employee_accepted_repair), Position_of_repair_person,int(technics_technics_inventory_num), int(technics_Division_Division_num), int(ids))
            cur.execute(ch1, ch2)
            db.commit()
            cur.close()
        finally:
            db.close()

    def add(self):

        try:
            ids = int(self.lineEdit.text())
            Date_of_delivery_for_repair = self.lineEdit_2.text()
            Repair_period = self.lineEdit_3.text()
            Type_of_repair = self.lineEdit_4.text()
            Date_of_transfer = self.lineEdit_5.text()
            FIO_Employee_passed_repair = self.lineEdit_6.text()
            Number__Employee_passed_repair = int(self.lineEdit_7.text())
            FIO_Employee_accepted_repair = self.lineEdit_8.text()
            Number__Employee_accepted_repair = int(self.lineEdit_9.text())
            Position_of_repair_person = self.lineEdit_10.text()
            technics_technics_inventory_num = int(self.lineEdit_11.text())
            technics_Division_Division_num = int(self.lineEdit_12.text())
        except:
            return
        db = mysql.connect(host="localhost", user="root", passwd="1234", db="lab4")
        cur = db.cursor()
        cur.execute('insert into technics_repair values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', \
                  (int(ids), Date_of_delivery_for_repair, Repair_period, Type_of_repair, Date_of_transfer, FIO_Employee_passed_repair, int(Number__Employee_passed_repair),FIO_Employee_accepted_repair, int(Number__Employee_accepted_repair), Position_of_repair_person,int(technics_technics_inventory_num), int(technics_Division_Division_num),))
        db.commit()

        cur.close()
        db.close()

    def dels(self):

        db = mysql.connect(host="localhost", user="root", passwd="1234", db="lab4")
        cur = db.cursor()
        try:
            ids = int(self.lineEdit.text())
        except:
            return
        cur.execute("DELETE FROM technics_repair WHERE technics_repair_num = %s",(int(ids),))
        db.commit()
        cur.close()
        db.close()



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Tehreper()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
