from PyQt5 import QtCore, QtGui, QtWidgets
import pyqrcode
import time
import mysql.connector as mc


class Ui_joel_MainWindow(object):
    def setupUi(self, joel_MainWindow):
        joel_MainWindow.setObjectName("joel_MainWindow")
        joel_MainWindow.resize(800, 600)
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        joel_MainWindow.setFont(font)
        self.centralwidget = QtWidgets.QWidget(joel_MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.inviter_label = QtWidgets.QLabel(self.centralwidget)
        self.inviter_label.setGeometry(QtCore.QRect(0, 80, 111, 41))
        font = QtGui.QFont()
        font.setFamily("MS Sans Serif")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.inviter_label.setFont(font)
        self.inviter_label.setObjectName("inviter_label")
        self.guest_label = QtWidgets.QLabel(self.centralwidget)
        self.guest_label.setGeometry(QtCore.QRect(0, 120, 111, 31))
        font = QtGui.QFont()
        font.setFamily("MS Sans Serif")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.guest_label.setFont(font)
        self.guest_label.setObjectName("guest_label")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(90, 30, 421, 31))
        font = QtGui.QFont()
        font.setFamily("MS Sans Serif")
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(75)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.gid_label = QtWidgets.QLabel(self.centralwidget)
        self.gid_label.setGeometry(QtCore.QRect(0, 170, 111, 21))
        font = QtGui.QFont()
        font.setFamily("MS Sans Serif")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.gid_label.setFont(font)
        self.gid_label.setObjectName("gid_label")
        self.setdate = QtWidgets.QCalendarWidget(self.centralwidget)
        self.setdate.setGeometry(QtCore.QRect(130, 210, 321, 211))
        self.setdate.setObjectName("setdate")
        self.settime = QtWidgets.QTimeEdit(self.centralwidget)
        self.settime.setGeometry(QtCore.QRect(140, 450, 118, 22))
        self.settime.setObjectName("settime")
        self.date_label = QtWidgets.QLabel(self.centralwidget)
        self.date_label.setGeometry(QtCore.QRect(0, 220, 111, 16))
        font = QtGui.QFont()
        font.setFamily("MS Sans Serif")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.date_label.setFont(font)
        self.date_label.setObjectName("date_label")
        self.time_label = QtWidgets.QLabel(self.centralwidget)
        self.time_label.setGeometry(QtCore.QRect(10, 450, 101, 16))
        self.time_label.setObjectName("time_label")
        self.submit_pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.submit_pushButton.setGeometry(QtCore.QRect(250, 500, 75, 23))
        self.submit_pushButton.setObjectName("submit_pushButton")
        self.invite_lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.invite_lineEdit.setGeometry(QtCore.QRect(130, 90, 321, 20))
        self.invite_lineEdit.setObjectName("invite_lineEdit")
        self.name_lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.name_lineEdit.setGeometry(QtCore.QRect(130, 130, 321, 20))
        self.name_lineEdit.setObjectName("name_lineEdit")
        self.id_lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.id_lineEdit.setGeometry(QtCore.QRect(130, 170, 321, 20))
        self.id_lineEdit.setObjectName("id_lineEdit")
        joel_MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(joel_MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
        self.menubar.setObjectName("menubar")
        joel_MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(joel_MainWindow)
        self.statusbar.setObjectName("statusbar")
        joel_MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(joel_MainWindow)
        QtCore.QMetaObject.connectSlotsByName(joel_MainWindow)
    
    def insert(self,s):
        db=mc.connect(host="localhost",user="root",passwd="12345678",database="vms")
        cur=db.cursor()
        # cur.execute("create table info(id int auto_increment,inviter varchar(50) not null,gname varchar(100) not null,gid varchar(50) not null,day varchar(30) not null,meetingid varchar(50) not null,in_time varchar(50),out_time varchar(50),primary key (id,meetingid))")
        # db.commit()
        val=s.split(",")
        cur.execute("insert into info(inviter,gname,gid,day,meetingid) values (%s,%s,%s,%s,%s)",val)
        db.commit()


    def disp(self):
        inviter=self.invite_lineEdit.text()
        name=self.name_lineEdit.text()
        id=self.id_lineEdit.text()
        day=self.setdate.selectedDate().toString()
        time1=self.settime.text()
        time2=time.time()
        s=inviter+","+name+","+id+","+day+","+id+str(int(time2))
        qr= pyqrcode.create(str(s))
        qr.svg(name+".svg", scale = 8)
        self.insert(s)

    def retranslateUi(self, joel_MainWindow):
        _translate = QtCore.QCoreApplication.translate
        joel_MainWindow.setWindowTitle(_translate("joel_MainWindow", "MainWindow"))
        self.inviter_label.setText(_translate("joel_MainWindow", "Inviter ID      :"))
        self.guest_label.setText(_translate("joel_MainWindow", "Guest Name :"))
        self.label_3.setText(_translate("joel_MainWindow", "Visitor Management System"))
        self.gid_label.setText(_translate("joel_MainWindow", "Guest ID      :"))
        self.date_label.setText(_translate("joel_MainWindow", "Set Date      :"))
        self.time_label.setText(_translate("joel_MainWindow", "Set Time     :"))
        self.submit_pushButton.setText(_translate("joel_MainWindow", "SUBMIT"))
        self.submit_pushButton.clicked.connect(self.disp)

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