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

# Form implementation generated from reading ui file 'StockSearching.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
from PyQt5.QtWidgets import QTableWidgetItem
from PyQt5.QtWidgets import QMessageBox
import pymongo
#from home import Ui_Home
import dns
server = "mongodb+srv://iBobby:1234@cluster0.1npcu.mongodb.net/<dbname>?retryWrites=true&w=majority"


class Ui_StockSearching(object):
    def setupUi(self, StockSearching):
        StockSearching.setObjectName("StockSearching")
        StockSearching.resize(837, 531)
        self.label_5 = QtWidgets.QLabel(StockSearching)
        self.label_5.setGeometry(QtCore.QRect(730, 20, 31, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_5.setFont(font)
        self.label_5.setObjectName("label_5")
        self.lblRealPrice = QtWidgets.QLabel(StockSearching)
        self.lblRealPrice.setGeometry(QtCore.QRect(190, 510, 131, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.lblRealPrice.setFont(font)
        self.lblRealPrice.setText("")
        self.lblRealPrice.setObjectName("lblRealPrice")
        self.cboCategory = QtWidgets.QComboBox(StockSearching)
        self.cboCategory.setGeometry(QtCore.QRect(130, 20, 151, 31))
        self.cboCategory.setObjectName("cboCategory")
        self.btnUpdate = QtWidgets.QPushButton(StockSearching)
        self.btnUpdate.setGeometry(QtCore.QRect(590, 60, 111, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.btnUpdate.setFont(font)
        self.btnUpdate.setObjectName("btnUpdate")
        self.btnDelete = QtWidgets.QPushButton(StockSearching)
        self.btnDelete.setGeometry(QtCore.QRect(590, 100, 111, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.btnDelete.setFont(font)
        self.btnDelete.setObjectName("btnDelete")
        self.label_4 = QtWidgets.QLabel(StockSearching)
        self.label_4.setGeometry(QtCore.QRect(30, 20, 91, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label_4.setFont(font)
        self.label_4.setObjectName("label_4")
        self.label_6 = QtWidgets.QLabel(StockSearching)
        self.label_6.setGeometry(QtCore.QRect(350, 20, 61, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label_6.setFont(font)
        self.label_6.setObjectName("label_6")
        self.txtCurrentPrice = QtWidgets.QTextEdit(StockSearching)
        self.txtCurrentPrice.setGeometry(QtCore.QRect(130, 100, 151, 31))
        self.txtCurrentPrice.setObjectName("txtCurrentPrice")
        self.label_3 = QtWidgets.QLabel(StockSearching)
        self.label_3.setGeometry(QtCore.QRect(30, 100, 91, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.label_8 = QtWidgets.QLabel(StockSearching)
        self.label_8.setGeometry(QtCore.QRect(300, 100, 121, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label_8.setFont(font)
        self.label_8.setObjectName("label_8")
        self.txtDividend = QtWidgets.QTextEdit(StockSearching)
        self.txtDividend.setGeometry(QtCore.QRect(420, 100, 151, 31))
        self.txtDividend.setObjectName("txtDividend")
        self.lblUserName = QtWidgets.QLabel(StockSearching)
        self.lblUserName.setGeometry(QtCore.QRect(770, 20, 101, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lblUserName.setFont(font)
        self.lblUserName.setObjectName("lblUserName")
        self.btnLogout = QtWidgets.QPushButton(StockSearching)
        self.btnLogout.setGeometry(QtCore.QRect(760, 500, 75, 23))
        self.btnLogout.setCheckable(False)
        self.btnLogout.setAutoRepeat(False)
        self.btnLogout.setAutoExclusive(False)
        self.btnLogout.setDefault(False)
        self.btnLogout.setFlat(True)
        self.btnLogout.setObjectName("btnLogout")
        self.btnSearch = QtWidgets.QPushButton(StockSearching)
        self.btnSearch.setGeometry(QtCore.QRect(590, 20, 111, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.btnSearch.setFont(font)
        self.btnSearch.setObjectName("btnSearch")
        self.lblSearch = QtWidgets.QTableWidget(StockSearching)
        self.lblSearch.setGeometry(QtCore.QRect(10, 160, 711, 351))
        self.lblSearch.setObjectName("lblSearch")
        self.lblSearch.setColumnCount(0)
        self.lblSearch.setRowCount(0)
        self.txtStockName = QtWidgets.QTextEdit(StockSearching)
        self.txtStockName.setGeometry(QtCore.QRect(420, 20, 151, 31))
        self.txtStockName.setObjectName("txtStockName")

        self.cboCategory.setObjectName("cboCategory")
        self.cboCategory.addItem("ดูทั้งหมด")
        self.cboCategory.addItem("เกษตรและอุตสาหกรรมอาหาร")
        self.cboCategory.addItem("สินค้าอุปโภคบริโภค")
        self.cboCategory.addItem("ธุรกิจการเงิน")
        self.cboCategory.addItem("สินค้าอุตสาหกรรม")
        self.cboCategory.addItem("อสังหาริมทรัพย์และก่อสร้าง")
        self.cboCategory.addItem("ทรัพยากร")
        self.cboCategory.addItem("บริการ")
        self.cboCategory.addItem("เทคโนโลยี")

        self.btnUpdate.clicked.connect(self.showUpdate)
        self.btnSearch.clicked.connect(self.showSearch)
        self.btnDelete.clicked.connect(self.delete)
       # self.btnLogout.clicked.connect(self.back)

        self.thiswindow = StockSearching
        self.retranslateUi(StockSearching)
        QtCore.QMetaObject.connectSlotsByName(StockSearching)


    def showUpdate(self):
        msg = self.MessageBox()
        stock_name = self.txtStockName.toPlainText()
        curr_price = self.txtCurrentPrice.toPlainText()
        dividend = self.txtDividend.toPlainText()
        with pymongo.MongoClient(server) as conn:
            db = conn.get_database("StockDividend")
            if stock_name == "" and curr_price == "" and dividend == "":
                msg.setText("กรุณาระบุชื่อหุ้นและข้อมูลที่ท่านต้องการจะอัพเดท")
                msg.exec_()
            elif stock_name != "" and curr_price == "" and dividend == "":
                msg.setText("กรุณาระบบข้อมูลที่ท่านต้องการจะอัพเดท")
                msg.exec_()
            elif stock_name != "" and curr_price != "" and dividend != "":
                where = {'stock_name': {'$eq': stock_name.upper()}}
                setTo = {'$set': {'current_price': float(curr_price),'dividend':float(dividend)}}
                db.sample_stock.update_one(where, setTo)
                msg.setText("ข้อมูลของท่านได้ทำการอัพเดทเรียบร้อยแล้ว")
                msg.exec_()
            elif stock_name != "" and curr_price != "":
                where = {'stock_name': {'$eq': stock_name.upper()}}
                setTo = {'$set': {'current_price': float(curr_price)}}
                db.sample_stock.update_one(where, setTo)
                msg.setText("ข้อมูลของท่านได้ทำการอัพเดทเรียบร้อยแล้ว")
                msg.exec_()
            elif stock_name != "" and dividend != "":
                where = {'stock_name': {'$eq': stock_name.upper()}}
                setTo = {'$set': {'dividend': float(dividend)}}
                db.sample_stock.update_one(where, setTo)
                msg.setText("ข้อมูลของท่านได้ทำการอัพเดทเรียบร้อยแล้ว")
                msg.exec_()


    def delete(self):
        msg = self.MessageBox()
        with pymongo.MongoClient(server) as conn:
            stock_name = self.txtStockName.toPlainText()
            db = conn.get_database("StockDividend")
            #cursor = db.sample_stock
            if stock_name == "":
                msg.setText("กรุณาป้อนชื่อหุ้นที่ต้องการจะลบ")
                msg.exec_()
                self.txtStockName.clear()
            else:
                where = {'stock_name' : {'$eq': stock_name}}
                db.sample_stock.delete_one(where)
                msg.setText("ข้อมูลของคุณได้ทำการลบออกเป็นที่เรียบร้อย")
                msg.exec_()

    def showSearch(self):
        with pymongo.MongoClient(server) as conn:
            db = conn.get_database("StockDividend")
            category = self.cboCategory.currentText()
            stock_name = self.txtStockName.toPlainText()
            if stock_name != "" and category == "ดูทั้งหมด":
                where = {'stock_name': {'$eq': stock_name.upper()}}
            elif stock_name == "" and category != "ดูทั้งหมด":
                where = {'category': {'$eq': category}}
            elif category == "ดูทั้งหมด":
                where = {}
            elif stock_name != "" and category != "":
                where = {'$and': [{'category': {'$eq': category}}, {'stock_name': {'$eq': stock_name.upper()}}]}
            elif stock_name != "":
                where = {'stock_name': {'$eq': stock_name.upper()}}
            count = db.sample_stock.count_documents(where)
            cursor = db.sample_stock.find(where)
            self.lblSearch.setRowCount(count)
            self.lblSearch.setColumnCount(7)

            header1 = QtWidgets.QTableWidgetItem("ID")
            header2 = QtWidgets.QTableWidgetItem("NAME")
            header3 = QtWidgets.QTableWidgetItem("CATEGORY")
            header4 = QtWidgets.QTableWidgetItem("FLEEFLOT")
            header5 = QtWidgets.QTableWidgetItem("PRICE")
            header6 = QtWidgets.QTableWidgetItem("DIVIDEND")
            header7 = QtWidgets.QTableWidgetItem("DATE")

            self.lblSearch.setHorizontalHeaderItem(0, header1)
            self.lblSearch.setHorizontalHeaderItem(1, header2)
            self.lblSearch.setHorizontalHeaderItem(2, header3)
            self.lblSearch.setHorizontalHeaderItem(3, header4)
            self.lblSearch.setHorizontalHeaderItem(4, header5)
            self.lblSearch.setHorizontalHeaderItem(5, header6)
            self.lblSearch.setHorizontalHeaderItem(6, header7)

            row = 0
            for i in cursor:
                self.lblSearch.setItem(row, 0, QTableWidgetItem("{}".format(i['_id'])))
                self.lblSearch.setItem(row, 1, QTableWidgetItem("{}".format(i['stock_name'])))
                self.lblSearch.setItem(row, 2, QTableWidgetItem("{}".format(i['category'])))
                self.lblSearch.setItem(row, 3, QTableWidgetItem("{}".format(i['fleefloat'])))
                self.lblSearch.setItem(row, 4, QTableWidgetItem("{}".format(i['current_price'])))
                self.lblSearch.setItem(row, 5, QTableWidgetItem("{}".format(i['dividend'])))
                self.lblSearch.setItem(row, 6, QTableWidgetItem("{}".format(i['date_time'])))
                row += 1

    def MessageBox(self):
        msg = QMessageBox()
        msg.setWindowTitle("Information")
        msg.setIcon(QMessageBox.Information)
        msg.setStandardButtons(QMessageBox.Ok)
        return msg

    def retranslateUi(self, StockSearching):
        _translate = QtCore.QCoreApplication.translate
        StockSearching.setWindowTitle(_translate("StockSearching", "Dialog"))
        self.label_5.setText(_translate("StockSearching", "ผู้ใช้ :"))
        self.btnUpdate.setText(_translate("StockSearching", "อัพเดทข้อมูล"))
        self.btnDelete.setText(_translate("StockSearching", "ลบ"))
        self.label_4.setText(_translate("StockSearching", "หมวดธุรกิจ"))
        self.label_6.setText(_translate("StockSearching", "ชื่อหุ้น"))
        self.label_3.setText(_translate("StockSearching", "ราคาปัจจุบัน"))
        self.label_8.setText(_translate("StockSearching", "ปันผลปัจจุบัน %"))
        self.lblUserName.setText(_translate("StockSearching", "ชื่อ"))
        self.btnLogout.setText(_translate("StockSearching", "Logout"))
        self.btnSearch.setText(_translate("StockSearching", "ค้นหาข้อมูล"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    StockSearching = QtWidgets.QDialog()
    ui = Ui_StockSearching()
    ui.setupUi(StockSearching)
    StockSearching.show()
    sys.exit(app.exec_())
