# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'SignUp.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.
import sys
import pymysql
import random
import smtplib
from email.mime.text import MIMEText
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QLineEdit, QComboBox, QDialog, QMainWindow, QStackedWidget
from PyQt5.QtCore import QCoreApplication
from SignUp import signWindow



a = random.randint(0, 9)
b = random.randint(0, 9)
c = random.randint(0, 9)
d = random.randint(0, 9)
e = random.randint(0, 9)
f = random.randint(0, 9)
injung = str(a) + str(b) + str(c) + str(d) + str(e) + str(f)

class loginWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.resize(573, 343)
        self.setWindowTitle("로그인")
        self.setFont(QtGui.QFont("나눔스퀘어", 10))

        self.titleL = QLabel("로그인", self)
        self.titleL.setGeometry(240, 40, 96, 40)
        self.titleL.setFont(QtGui.QFont("나눔스퀘어", 18))
        self.titleL.setAlignment(QtCore.Qt.AlignCenter)

        self.idL = QLabel("아이디", self)
        self.idL.setGeometry(100, 130, 54, 22)
        self.idL.setAlignment(QtCore.Qt.AlignCenter)

        self.userid = QLineEdit(self)
        self.userid.setPlaceholderText("아이디")
        self.userid.setGeometry(200, 130, 151, 31)

        self.passWordL = QLabel("비밀번호", self)
        self.passWordL.setGeometry(100, 180, 72, 22)
        self.passWordL.setAlignment(QtCore.Qt.AlignCenter)

        self.passwd = QLineEdit(self)
        self.passwd.setGeometry(200, 180, 151, 31)
        self.passwd.setEchoMode(QLineEdit.Password)
        self.passwd.setPlaceholderText("비밀번호")

        self.loginButton = QPushButton("로그인", self)
        self.loginButton.setGeometry(390, 130, 81, 81)
        self.loginButton.clicked.connect(self.login)

        self.JoinButton = QPushButton("회원가입", self)
        self.JoinButton.setGeometry(80, 240, 93, 31)
        self.JoinButton.setFlat(True)
        self.JoinButton.clicked.connect(self.showLogin)

        self.FindID = QPushButton("아이디 찾기", self)
        self.FindID.setGeometry(210, 240, 122, 31)
        self.FindID.clicked.connect(self.findid)
        self.FindID.setFlat(True)

        self.FindPass = QPushButton("비밀번호 찾기", self)
        self.FindPass.setGeometry(370, 240, 122, 31)
        self.FindPass.clicked.connect(self.findpass)
        self.FindPass.setFlat(True)

    def showLogin(self):
        self.loginWindow = signWindow(self)
        self.loginWindow.show()
        self.close()

    def login(self):
        userid = self.userid.text().strip()
        passwd = self.passwd.text().strip()
        conn = pymysql.connect(host='localhost', user='bb', passwd='1201', db='boongboong')
        cursor = conn.cursor()
        sql = "select * from user where userid=%s and passwd=%s"
        data = cursor.execute(sql, (userid, passwd))
        if (len(cursor.fetchall()) > 0):
            self.messagebox("성공", "로그인되었습니다.")
        elif self.userid.text().strip() == "" or self.passwd.text().strip() == "":
            self.messagebox("실패", "입력되지 않은 정보가 있습니다.")
        else:
            self.messagebox("실패", "아이디 또는 비밀번호가 틀립니다.")

    def messagebox(self, title, message):
        mess = QtWidgets.QMessageBox()
        mess.setFont(QtGui.QFont("나눔스퀘어", 10))
        mess.setWindowTitle(title)
        mess.setText(message)
        if title == "성공":
            mess.setIcon(QtWidgets.QMessageBox.Information)
        elif title == "실패":
            mess.setIcon(QtWidgets.QMessageBox.Warning)

        mess.setStandardButtons(QtWidgets.QMessageBox.Ok)
        mess.exec_()

    def findid(self):
        self.fi = FindID(self)

    def findpass(self):
        self.fp = FindPass(self)


class id_Main(QtWidgets.QWidget):
    def setupUi(self, FindID):
        FindID.setObjectName("FindID")
        FindID.resize(454, 317)

        self.QtStack = QtWidgets.QStackedLayout()

        self.stack1 = QtWidgets.QWidget()
        self.stack2 = QtWidgets.QWidget()

        self.Window1UI()
        self.Window2UI()

        self.QtStack.addWidget(self.stack1)
        self.QtStack.addWidget(self.stack2)

    def Window1UI(self):
        self.stack1.resize(454, 317)
        self.stack1.setWindowTitle("아이디 찾기")
        self.stack1.setFont(QtGui.QFont("나눔스퀘어", 10))

        self.titleL = QLabel("아이디 찾기", self.stack1)
        self.titleL.setGeometry(120, 40, 213, 40)
        self.titleL.setFont(QtGui.QFont("나눔스퀘어", 18))
        self.titleL.setAlignment(QtCore.Qt.AlignCenter)

        self.emailL = QLabel("이메일", self.stack1)
        self.emailL.setGeometry(90, 120, 54, 22)
        self.emailL.setAlignment(QtCore.Qt.AlignCenter)

        self.email = QLineEdit(self.stack1)
        self.email.setPlaceholderText("이메일")
        self.email.setGeometry(170, 120, 151, 31)

        self.accButton = QtWidgets.QPushButton(self.stack1)
        self.accButton.setText("인증하기")
        self.accButton.setGeometry(330, 120, 71, 31)

        self.accL = QLabel("인증번호", self.stack1)
        self.accL.setGeometry(80, 180, 72, 22)
        self.accL.setAlignment(QtCore.Qt.AlignCenter)

        self.accNum = QLineEdit(self.stack1)
        self.accNum.setPlaceholderText("인증번호")
        self.accNum.setGeometry(170, 180, 151, 31)

        self.nextButton = QtWidgets.QPushButton(self.stack1)
        self.nextButton.setText("다음")
        self.nextButton.setGeometry(160, 240, 151, 41)

    def Window2UI(self):
        self.stack2.resize(454, 317)
        self.stack2.setWindowTitle("아이디 찾기")
        self.stack2.setFont(QtGui.QFont("나눔스퀘어", 10))

        self.titleL = QLabel("아이디 찾기", self.stack2)
        self.titleL.setGeometry(110, 40, 213, 40)
        self.titleL.setFont(QtGui.QFont("나눔스퀘어", 18))
        self.titleL.setAlignment(QtCore.Qt.AlignCenter)

        self.useridtext = QLabel("회원님의 아이디는 ", self.stack2)
        self.useridtext.setGeometry(100, 140, 151, 20)
        self.useridtext.setAlignment(QtCore.Qt.AlignCenter)

        self.idtext = QLabel("", self.stack2)
        self.idtext.setGeometry(250, 140, 116, 20)
        self.idtext.setAlignment(QtCore.Qt.AlignCenter)

        self.ok = QtWidgets.QPushButton(self.stack2)
        self.ok.setText("확인")
        self.ok.setGeometry(150, 230, 151, 41)


class FindID(QMainWindow, id_Main):
    def __init__(self, parent=None):
        super(FindID, self).__init__(parent)
        self.setupUi(self)

        self.accButton.clicked.connect(self.sendEmail)
        self.nextButton.clicked.connect(self.FindID1)
        self.ok.clicked.connect(self.oook)

    def oook(self):
        self.stack2.close()

    def sendEmail(self):
        email = self.email.text().strip()

        conn = pymysql.connect(host='localhost', user='bb', passwd='1201', db='boongboong')
        cursor = conn.cursor()
        sql = "select email from user where email=%s"
        data = cursor.execute(sql, email)

        if (len(cursor.fetchall()) > 0):
            s = smtplib.SMTP('smtp.gmail.com', 587)
            s.starttls()
            s.login('sonjiu00@gmail.com', 'wqutybgnhwdreglk')
            msg = MIMEText(str(injung))
            msg['Subject'] = '이메일 인증코드'
            s.sendmail("sonjiu00@gmail.com", email, msg.as_string())
            s.quit()
        elif self.email.text().strip() == "":
            self.messagebox("실패", "이메일을 입력하세요.")
        else:
            self.messagebox("실패", "존재하지 않는 이메일입니다.")

    def FindID1(self):
        if self.email.text().strip() == "" or self.accNum.text().strip() == "":
            self.messagebox("실패", "입력되지 않은 정보가 있습니다.")
        elif self.accNum.text().strip() != injung:
            self.messagebox("실패", "인증번호가 틀립니다.")
        else:
            self.QtStack.setCurrentIndex(1)
            self.getuserid()

    def messagebox(self, title, message):
        mess = QtWidgets.QMessageBox()
        mess.setFont(QtGui.QFont("나눔스퀘어", 10))
        mess.setWindowTitle(title)
        mess.setText(message)
        if title == "성공":
            mess.setIcon(QtWidgets.QMessageBox.Information)
        elif title == "실패":
            mess.setIcon(QtWidgets.QMessageBox.Warning)

        mess.setStandardButtons(QtWidgets.QMessageBox.Ok)
        mess.exec_()

    def getuserid(self):
        email = self.email.text().strip()

        conn = pymysql.connect(host='localhost', user='bb', passwd='1201', db='boongboong')
        cursor = conn.cursor()
        sql = "select userid from user where email=%s"
        data = cursor.execute(sql, email)
        result = cursor.fetchall()
        re=str(result[0][0])
        res = self.idtext.setText(str(re))
        return res



class Ui_Main(QtWidgets.QWidget):
    def setupUi(self, FindPass):
        FindPass.setObjectName("FindPass")
        FindPass.resize(454, 317)

        self.QtStack = QtWidgets.QStackedLayout()

        self.stack1 = QtWidgets.QWidget()
        self.stack2 = QtWidgets.QWidget()
        self.stack3 = QtWidgets.QWidget()

        self.Window1UI()
        self.Window2UI()
        self.Window3UI()

        self.QtStack.addWidget(self.stack1)
        self.QtStack.addWidget(self.stack2)
        self.QtStack.addWidget(self.stack3)

    def Window1UI(self):
        self.stack1.resize(454, 317)
        self.stack1.setWindowTitle("비밀번호 찾기")
        self.stack1.setFont(QtGui.QFont("나눔스퀘어", 10))

        self.titleL = QLabel("비밀번호 찾기", self.stack1)
        self.titleL.setGeometry(120, 40, 213, 40)
        self.titleL.setFont(QtGui.QFont("나눔스퀘어", 18))
        self.titleL.setAlignment(QtCore.Qt.AlignCenter)

        self.idL = QLabel("아이디", self.stack1)
        self.idL.setGeometry(110, 130, 48, 20)
        self.idL.setAlignment(QtCore.Qt.AlignCenter)

        self.userid = QLineEdit(self.stack1)
        self.userid.setPlaceholderText("아이디를 입력하세요.")
        self.userid.setGeometry(190, 120, 151, 31)

        self.nextButton = QtWidgets.QPushButton(self.stack1)
        self.nextButton.setText("다음")
        self.nextButton.setGeometry(160, 240, 151, 41)

    def Window2UI(self):
        self.stack2.resize(454, 317)
        self.stack2.setWindowTitle("비밀번호 찾기")
        self.stack2.setFont(QtGui.QFont("나눔스퀘어", 10))

        self.titleL = QLabel("비밀번호 찾기", self.stack2)
        self.titleL.setGeometry(130, 40, 213, 40)
        self.titleL.setFont(QtGui.QFont("나눔스퀘어", 18))
        self.titleL.setAlignment(QtCore.Qt.AlignCenter)

        self.emailL = QLabel("이메일", self.stack2)
        self.emailL.setGeometry(90, 120, 54, 22)
        self.emailL.setAlignment(QtCore.Qt.AlignCenter)

        self.email = QLineEdit(self.stack2)
        self.email.setPlaceholderText("이메일")
        self.email.setGeometry(170, 120, 151, 31)

        self.accButton = QtWidgets.QPushButton(self.stack2)
        self.accButton.setText("인증하기")
        self.accButton.setGeometry(330, 120, 71, 31)

        self.accL = QLabel("인증번호", self.stack2)
        self.accL.setGeometry(80, 180, 72, 22)
        self.accL.setAlignment(QtCore.Qt.AlignCenter)

        self.accNum = QLineEdit(self.stack2)
        self.accNum.setPlaceholderText("인증번호")
        self.accNum.setGeometry(170, 180, 151, 31)

        self.nextButton2 = QtWidgets.QPushButton(self.stack2)
        self.nextButton2.setText("다음")
        self.nextButton2.setGeometry(160, 240, 151, 41)

    def Window3UI(self):
        self.stack3.resize(454, 317)
        self.stack3.setWindowTitle("비밀번호 찾기")
        self.stack3.setFont(QtGui.QFont("나눔스퀘어", 10))

        self.titleL = QLabel("비밀번호 찾기", self.stack3)
        self.titleL.setGeometry(120, 40, 213, 40)
        self.titleL.setFont(QtGui.QFont("나눔스퀘어", 18))
        self.titleL.setAlignment(QtCore.Qt.AlignCenter)

        self.emailL = QLabel("변경할 비밀번호", self.stack3)
        self.emailL.setGeometry(70, 120, 117, 20)
        self.emailL.setAlignment(QtCore.Qt.AlignCenter)


        self.sp = QLineEdit(self.stack3)
        self.sp.setPlaceholderText("비밀번호")
        self.sp.setGeometry(210, 110, 151, 31)
        self.sp.setEchoMode(QLineEdit.Password)

        self.accL = QLabel("비밀번호 확인", self.stack3)
        self.accL.setGeometry(70, 170, 101, 20)
        self.accL.setAlignment(QtCore.Qt.AlignCenter)


        self.sp1 = QLineEdit(self.stack3)
        self.sp1.setPlaceholderText("비밀번호 확인")
        self.sp1.setGeometry(210, 170, 151, 31)
        self.sp1.setEchoMode(QLineEdit.Password)


        self.ok = QtWidgets.QPushButton(self.stack3)
        self.ok.setText("확인")
        self.ok.setGeometry(160, 240, 151, 41)


class FindPass(QMainWindow, Ui_Main):
    def __init__(self, parent=None):
        super(FindPass, self).__init__(parent)
        self.setupUi(self)

        self.nextButton.clicked.connect(self.FindPass1)
        self.accButton.clicked.connect(self.sendEmail)
        self.nextButton2.clicked.connect(self.FindPass2)
        self.ok.clicked.connect(self.setPass)

    def FindPass1(self):
        userid = self.userid.text().strip()
        conn = pymysql.connect(host='localhost', user='bb', passwd='1201', db='boongboong')
        cursor = conn.cursor()
        sql = "select * from user where userid=%s"
        data = cursor.execute(sql, (userid))
        if (len(cursor.fetchall()) == 0):
            self.messagebox("실패", "존재하지 않는 아이디입니다.")
        else:
            self.QtStack.setCurrentIndex(1)

    def messagebox(self, title, message):
        mess = QtWidgets.QMessageBox()
        mess.setFont(QtGui.QFont("나눔스퀘어", 10))
        mess.setWindowTitle(title)
        mess.setText(message)
        if title == "성공":
            mess.setIcon(QtWidgets.QMessageBox.Information)
        elif title == "실패":
            mess.setIcon(QtWidgets.QMessageBox.Warning)

        mess.setStandardButtons(QtWidgets.QMessageBox.Ok)
        mess.exec_()

    def sendEmail(self):
        email = self.email.text().strip()

        conn = pymysql.connect(host='localhost', user='bb', passwd='1201', db='boongboong')
        cursor = conn.cursor()
        sql = "select email from user where email=%s"
        data = cursor.execute(sql, email)

        if (len(cursor.fetchall()) > 0):
            s = smtplib.SMTP('smtp.gmail.com', 587)
            s.starttls()
            s.login('sonjiu00@gmail.com', 'wqutybgnhwdreglk')
            msg = MIMEText(str(injung))
            msg['Subject'] = '이메일 인증코드'
            s.sendmail("sonjiu00@gmail.com", email, msg.as_string())
            s.quit()
        elif self.email.text().strip() == "":
            self.messagebox("실패", "이메일을 입력하세요.")
        else:
            self.messagebox("실패", "존재하지 않는 이메일입니다.")

    def FindPass2(self):
        if self.email.text().strip() == "" or self.accNum.text().strip() == "":
            self.messagebox("실패", "입력되지 않은 정보가 있습니다.")
        elif self.accNum.text().strip() != injung:
            self.messagebox("실패", "인증번호가 틀립니다.")
        else:
            self.QtStack.setCurrentIndex(2)

    def setPass(self):
        sp = self.sp.text().strip()
        userid = self.userid.text().strip()
        if self.sp.text().strip() != self.sp1.text().strip():
            self.messagebox("실패", "비밀번호가 일치하지 않습니다.")
        elif self.sp.text().strip() == "" or self.sp1.text().strip() == "":
            self.messagebox("실패", "입력되지 않은 정보가 있습니다.")
        else:
            conn = pymysql.connect(host='localhost', user='bb', passwd='1201', db='boongboong')
            cursor = conn.cursor()
            sql = "update user set passwd=%s where userid=%s"
            data = cursor.execute(sql, (sp, userid))
            conn.commit()
            self.messagebox("성공", "비밀번호가 변경되었습니다.")
            self.stack3.close()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = loginWindow()
    w.show()
    sys.exit(app.exec_())