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

# Form implementation generated from reading ui file 'upset.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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.


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QWidget, QFileDialog, QGridLayout, QMainWindow ,QSizePolicy
from PyQt5.QtGui import QPixmap
from PyQt5.QtGui import QCursor
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from bs4 import BeautifulSoup
import requests
from PIL import Image
from io import BytesIO
import os
from zipfile import ZipFile
from os.path import basename
from PyQt5 import QtCore, QtGui, QtWidgets
import jdatetime
import json
import sys


class Ui_MainWindow(QtWidgets.QWidget):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setFixedSize(1300,820)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
    # sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
        # set clock
        #        
        layout = QVBoxLayout()
        self.label_9 = QtWidgets.QLabel(self.centralwidget)
        self.label_9.setGeometry(QtCore.QRect(10, 10, 191, 61))
        self.label_9.setObjectName("label_9")
        self.label_9.setStyleSheet('''*{
            color: 'black';
            font-family: 'Arial';
            font-weight: 900;
            font-size: 22px;
            border-radius: 12px;
            background: transparent;
            background-color: rgba(100, 100, 100, 0);


        }''')
        self.timer = QTimer(self)
        layout.addWidget(self.label_9)

        self.timer.timeout.connect(self.timerr)
        self.timer.start(1000)
        # end set clock

        self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(800, 40, 511, 581))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label.setObjectName("label")
        self.label.setStyleSheet(
                        '''*{
                    color: 'black';
                    border: 2px solid '#808080';
                    font-family: 'Arial';
                    font-size: 32px;
                    border-radius: 12px;
                    margin: 2px 2px;
                    padding: 6px;
                        }''')
        self.verticalLayout.addWidget(self.label)
        self.verticalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(750, 350, 491, 271))
        self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
        self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        self.label_2.setObjectName("label_2")
        self.label_2.setStyleSheet(
                        '''*{
                    color: 'black';
                    border: 2px solid '#808080';
                    font-family: 'Arial';
                    font-size: 32px;
                    border-radius: 12px;
                    margin: 2px 2px;
                    padding: 6px; 
                        }''')

        self.verticalLayout_2.addWidget(self.label_2)
        self.verticalLayoutWidget_3 = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget_3.setGeometry(QtCore.QRect(750, 40, 481, 111))
        self.verticalLayoutWidget_3.setObjectName("verticalLayoutWidget_3")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_3)
        self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.label_3 = QtWidgets.QLabel(self.verticalLayoutWidget_3)
        self.label_3.setObjectName("label_3")
        self.label_3.setStyleSheet(
                        '''*{
                    color: 'black';
                    border: 2px solid '#808080';
                    font-family: 'Arial';
                    font-size: 32px;
                    border-radius: 12px;
                    margin: 2px 2px;
                    padding: 6px;
                        }''')
        self.verticalLayout_3.addWidget(self.label_3)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(750, 170, 481, 161))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setStyleSheet(
                        '''*{
                    color: 'black';
                    border: 2px solid '#808080';
                    font-family: 'Arial';
                    font-size: 32px;
                    border-radius: 12px;
                    margin: 2px 2px;
                    padding: 6px;
                    background: transparent;
                    background-color: rgba(100, 100, 100, 0.4);
                        }
                        *:hover{
                    color: 'black';
                    border: 2px solid '#808080';
                    font-family: 'Arial';
                    font-size: 32px;
                    border-radius: 12px;
                    margin: 2px 2px;
                    padding: 6px;
                    background: transparent;
                    background-color: rgba(100, 100, 100, 0.9);
                        }''')
        self.line = QLineEdit(self.centralwidget)
        # pybutton = QPushButton('OK', self)
        # pybutton.clicked.connect(self.clickMethod)
        # pybutton.resize(200,32)
        # pybutton.move(80, 60)  
        self.line.move(80, 20)
        self.line.resize(200, 32)
        # self.nameLabel.move(20, 20)
        self.line.hide()
        self.line.returnPressed.connect(self.pushButton.click)
        
        
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1087, 21))
        self.menubar.setObjectName("menubar")
        self.menumenu = QtWidgets.QMenu(self.menubar)
        self.menumenu.setObjectName("menumenu")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionexit = QtWidgets.QAction(MainWindow)
        self.actionexit.setObjectName("actionexit")
        self.menumenu.addAction(self.actionexit)
        self.menubar.addAction(self.menumenu.menuAction())

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "حضور و غیاب"))
        self.label.setText(_translate("MainWindow", "                       تصویر"))
        self.label_2.setText(_translate("MainWindow", "                     اطلاعات"))
        self.label_3.setText(_translate("MainWindow", " "))
        self.pushButton.setText(_translate("MainWindow", "شروع حضور و غیاب"))
        self.pushButton.clicked.connect(self.upset)
        # self.menumenu.setTitle(_translate("MainWindow", "تاریخچه"))
        # self.actionexit.setText(_translate("MainWindow", "تاریخچه"))

    def timerr(self):
        time = jdatetime.datetime.now()
        self.label_9.setText(f'{time.hour} : {time.minute} : {time.second}')

    def upset(self):
            self.line.show()
            self.pushButton.hide()
            self.verticalLayoutWidget_3.setGeometry(QtCore.QRect(750, 40, 481, 281))
            time = jdatetime.datetime.now()
            inday = time.day
            inmonth = time.month
            inyear = time.year
            try:
                with open('datadaily.json') as json_filedaily:
                    datadaily = json.load(json_filedaily)
                with open('data.json') as outfile:
                    data = json.load(outfile)
                datadaily['0'] = [{"ID":data[index]['ID'],"time":0,"today":0,"repeat":0} for index in range(len(data))]
                with open('datadaily.json', 'w+') as outfiledaily:
                    json.dump(datadaily, outfiledaily)
                with open('daily.json') as dailys:
                    daily = json.load(dailys)
            except:
                daily={}
                with open('daily.json','w+') as dailys:
                    json.dump(daily,dailys)
                with open('data.json') as outfile:
                    data = json.load(outfile)
                datadaily = {}
                with open('datadaily.json', 'w+') as outfiledaily:
                    json.dump(datadaily, outfiledaily)
                datadaily['0'] = [{"ID":data[index]['ID'],"time":0,"today":0,"repeat":0} for index in range(len(data))]
                with open('datadaily.json', 'w+') as outfiledaily:
                    json.dump(datadaily, outfiledaily)
                # print(datadaily[0])
            true = 1
            o=0
            try:
                if datadaily[f'{inyear}-{inmonth}-{inday}'] != []:
                    pass
            except:
                datadaily[f'{inyear}-{inmonth}-{inday}'] = datadaily['0']


            with open('data.json') as outfile:
                data = json.load(outfile)

            # while true==1:
            # in_pass, done = QtWidgets.QInputDialog.getText(
            #     self, ' ', 'لطفا کارت را در مقابل دستگاه قرار دهید.')
            in_pass = self.line.text()
            self.line.clear()
            if True:
                for student in range(len(datadaily[f'{inyear}-{inmonth}-{inday}'])):
                    if datadaily['0'][student]['ID']==in_pass:
                        datadaily[f'{inyear}-{inmonth}-{inday}'][student]["today"]=1
                        datadaily[f'{inyear}-{inmonth}-{inday}'][student]["time"]=f'{time.hour}.{time.minute}'
                        datadaily[f'{inyear}-{inmonth}-{inday}'][student]["repeat"]+=1
                        self.label.setPixmap(QPixmap(data[student]['filename']))
                        self.label.setStyleSheet(
                                '''*{
                        border: 2px solid '#BC006C';
                        border-radius: 12px;
                        margin: 2px 2px;
                        padding: 6px;
                            }''')
                        self.label_2.setText('نام و نام خانوادگی :'
                                +str(data[student]['name'])+' '+str(data[student]['family'])+'\n ساعت ورود:  '+f'{time.hour}:{time.minute}'+'\n'+ 'پایه ی '+str(data[student]['class']))
                        # if int(data[student]['class']) == 1:
                            # number=number+1
                            # print(number)
                        cl = data[student]['class']
                        try:
                            # for i in range(len(daily["er"])):
                            if data[student]['ID'] not in daily["er"]:
                                if daily[f'{cl}'] != []:
                                    daily[f'{cl}']+=1
                                    daily["er"].append(data[student]['ID'])
                                else:
                                    daily[f'{cl}']=1
                                    daily['er'] = []
                                    daily["er"].append(data[student]['ID'])
                            else:
                                pass
                        except:
                            daily[f'{cl}']=1
                            daily['er'] = []
                            daily["er"].append(data[student]['ID'])

                        # try:
                            # print(f'{cl}')
                            # if daily[f'{cl}'] != []:
                                # print("ddddd")
                                # daily[f'{cl}']+=1
                                # pass
                            # else:print("OOOOOOOO")
                        # except:
                            # daily[f'{cl}'] = 1
                            # pass
                        # self.label_3.setText(daily[f'{cl}'])
                        # print(daily[f'{cl}'])
                        
                        # if int(data[student]['class']) == 4:
                        #     number1+=1
                        #     self.label_3.setText(f'{number1} student of class 4')
                        # if int(data[student]['class']) == 9:
                        #     number2+=1
                        #     self.label_3.setText(f'{number2} student of class 9')
                        




                        self.label_3.setStyleSheet('''*{
                                color: 'black';
                                border: 2px solid '#BC006C';
                                font-family: 'Arial';
                                font-size: 32px;
                                border-radius: 12px;
                                margin: 2px 2px;
                                padding: 6px;
                                background-color:'green';
                            }''')
                        # self.label_3.setText(" ")
                        self.label_2.setStyleSheet(
                            '''*{
                        color: 'black';
                        border: 2px solid '#BC006C';
                        font-family: 'Arial';
                        font-size: 32px;
                        border-radius: 12px;
                        margin: 2px 2px;
                        padding: 6px; 
                            }''')
            else:
                self.pushButton.show()
                self.verticalLayoutWidget_3.setGeometry(QtCore.QRect(750, 40, 481, 111))
                # o+=1
                # if o == 3:
                true=0
            with open('daily.json', 'w+') as dailys:
                json.dump(daily, dailys)

            with open('datadaily.json', 'w+') as outfile:
                json.dump(datadaily, outfile)

            try:
                with open('datadailys.json') as json_filedailys:
                    datadailys = json.load(json_filedailys)
                datadailys = [{"ID":data[index]['ID'],"today":[]} for index in range(len(data))]

            except:
                datadailys = []
                with open('datadailys.json', 'w+') as outfiledailys:
                    json.dump(datadailys, outfiledailys)
                datadailys = [{"ID":data[index]['ID'],"today":[]} for index in range(len(data))]
            for ins in range(len(datadaily[f'{inyear}-{inmonth}-{inday}'])):
            	if datadaily[f'{inyear}-{inmonth}-{inday}'][ins]["today"]==0:
            		datadailys[ins]['today'].append(f'{inyear}-{inmonth}-{inday}')





            with open('datadailys.json', 'w+') as outfiledailys:
                json.dump(datadailys, outfiledailys)
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()

    sys.exit(app.exec_())