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

# Form implementation generated from reading ui file 'cart.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.


from PyQt5 import QtCore, QtGui, QtWidgets
import os
import mysql.connector as conn
from Delete1 import DeleteView
mydb = conn.connect(host="localhost",user="root",password="root",database="shop")
mycur=mydb.cursor(buffered=True)


class Ui_MainWindow(object):
    def __init__(self,cname,cid):
        super().__init__()
        self.cname=cname
        self.cid=cid
        
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(12, 197, 200))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(12, 197, 200))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(12, 197, 200))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(12, 197, 200))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        MainWindow.setPalette(palette)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.delete_item = QtWidgets.QPushButton(self.centralwidget)
        self.delete_item.setGeometry(QtCore.QRect(60, 430, 681, 34))
        self.delete_item.setObjectName("delete_item")
        self.checkout_item = QtWidgets.QPushButton(self.centralwidget)
        self.checkout_item.setGeometry(QtCore.QRect(60, 510, 681, 34))
        self.checkout_item.setObjectName("checkout_item")
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(60, 100, 671, 291))
        self.widget.setObjectName("widget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.order_table = QtWidgets.QTableWidget(self.widget)
       
        self.order_table.setObjectName("order_table")
        self.order_table.setColumnCount(4)
        item = QtWidgets.QTableWidgetItem()
        self.order_table.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.order_table.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.order_table.setHorizontalHeaderItem(2, item)
        item = QtWidgets.QTableWidgetItem()
        self.order_table.setHorizontalHeaderItem(3, item)
        self.verticalLayout.addWidget(self.order_table)
        self.load_item = QtWidgets.QPushButton(self.widget)
        self.load_item.setObjectName("load_item")
        self.verticalLayout.addWidget(self.load_item)
        MainWindow.setCentralWidget(self.centralwidget)
        self.load_item.clicked.connect(self.sh)
        self.delete_item.clicked.connect(self.cm)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.delete_item.setText(_translate("MainWindow", "Delete"))
        self.checkout_item.setText(_translate("MainWindow", "CheckOut"))
        item = self.order_table.horizontalHeaderItem(0)
        item.setText(_translate("MainWindow", "Name"))
        item = self.order_table.horizontalHeaderItem(1)
        item.setText(_translate("MainWindow", "Product_Name"))
        item = self.order_table.horizontalHeaderItem(2)
        item.setText(_translate("MainWindow", "Quauntity"))
        item = self.order_table.horizontalHeaderItem(3)
        item.setText(_translate("MainWindow", "Total"))
        self.load_item.setText(_translate("MainWindow", "Load"))




    def sh(self):
        
        self.order_table.setRowCount(15)
        self.order_table.clear()
        
        
        mycur.execute("select pid,quantity from orders where customerid={}".format(self.cid))
        za=mycur.fetchall()
        productid=[]
        pquantity=[]
        
        for l in za:
            productid.append(l[0])
            pquantity.append(l[1])
        dt={}    
        mycur.execute("select productid,productname,price from product")  
        lmo=mycur.fetchall()
        for x in lmo:
            dt[x[0]]=[x[1],x[2]]
            
        print(dt)  
        
        
            
        
            
        
        mycur.execute("select cname,quantity,pid from orders where customerid={}".format(self.cid))
        
        row=0
        
        
        
        while True:
            sqlrow=mycur.fetchone()
            
            if sqlrow == None:
                break
            
            
            mnb=sqlrow[2]
            pln=dt[mnb][0]
            
            
            pc=sqlrow[1]
            tp=int(pc)*int(dt[mnb][1])
            
            i=0
            z=1
            k=3
            
            
            
            
            for col in [0,1]:
                self.order_table.setItem(row,i,QtWidgets.QTableWidgetItem(str(sqlrow[col])))
                
                self.order_table.setItem(row,z,QtWidgets.QTableWidgetItem(str(pln)))
                
                self.order_table.setItem(row,k,QtWidgets.QTableWidgetItem(str(tp)))
                i+=2
            row+=1 
            
        
        
            
                       
        

        
    def cm(self):
        self.b=DeleteView(self.cname,self.cid)
        self.b.show()
        


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow('a',4)
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
