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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import firebase_admin
|
| 3 |
+
from firebase_admin import credentials, firestore
|
| 4 |
|
| 5 |
|
| 6 |
+
# Initialize Firestore only once
|
| 7 |
+
# Check if Firebase is already initialized
|
| 8 |
+
if not firebase_admin._apps:
|
| 9 |
+
cred = credentials.Certificate("/home/swiftcomcdpl/key/firebase_key.json")
|
| 10 |
+
firebase_admin.initialize_app(cred)
|
| 11 |
+
|
| 12 |
+
db = firestore.client()
|
| 13 |
+
|
| 14 |
+
# Initialize page
|
| 15 |
+
if "selected_page" not in st.session_state:
|
| 16 |
+
st.session_state.selected_page = "Home"
|
| 17 |
+
|
| 18 |
+
# Inject custom CSS
|
| 19 |
+
st.markdown("""
|
| 20 |
+
<style>
|
| 21 |
+
div.stButton > button {
|
| 22 |
+
width: 100%;
|
| 23 |
+
height: 45px;
|
| 24 |
+
font-size: 16px;
|
| 25 |
+
margin-bottom: 8px;
|
| 26 |
+
background-color: #31333f;
|
| 27 |
+
color: white;
|
| 28 |
+
border: none;
|
| 29 |
+
border-radius: 6px;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
div.stButton > button:hover {
|
| 33 |
+
background-color: #505266;
|
| 34 |
+
}
|
| 35 |
+
</style>
|
| 36 |
+
""", unsafe_allow_html=True)
|
| 37 |
+
|
| 38 |
+
# Sidebar buttons
|
| 39 |
+
with st.sidebar:
|
| 40 |
+
st.title("π Navigation")
|
| 41 |
+
|
| 42 |
+
if st.button("π Home"):
|
| 43 |
+
st.session_state.selected_page = "Home"
|
| 44 |
+
if st.button("π User Form"):
|
| 45 |
+
st.session_state.selected_page = "User Form"
|
| 46 |
+
if st.button("π Distributors"):
|
| 47 |
+
st.session_state.selected_page = "Distributors"
|
| 48 |
+
|
| 49 |
+
# USER Form radio options
|
| 50 |
+
if st.session_state.selected_page == "User Form":
|
| 51 |
+
user_option = st.radio(
|
| 52 |
+
"Choose form",
|
| 53 |
+
("View User", "Add User", "Delete User", "Update User")
|
| 54 |
+
)
|
| 55 |
+
st.session_state.user_option = user_option
|
| 56 |
+
|
| 57 |
+
# DISTRIBUTORS radio options
|
| 58 |
+
if st.session_state.selected_page == "Distributors":
|
| 59 |
+
dist_option = st.radio(
|
| 60 |
+
"Choose form",
|
| 61 |
+
("View Distributor", "Add Distributor", "Delete Distributor", "Update Distributor")
|
| 62 |
+
)
|
| 63 |
+
st.session_state.dist_option = dist_option
|
| 64 |
+
|
| 65 |
+
# Render selected page
|
| 66 |
+
page = st.session_state.selected_page
|
| 67 |
+
|
| 68 |
+
if page == "Home":
|
| 69 |
+
st.title("π Home Page")
|
| 70 |
+
st.write("Welcome to the homepage.")
|
| 71 |
+
|
| 72 |
+
elif page == "User Form":
|
| 73 |
+
st.title("π User Form Page")
|
| 74 |
+
|
| 75 |
+
# Handle Add User
|
| 76 |
+
if st.session_state.get("user_option") == "Add User":
|
| 77 |
+
st.subheader("Add New User")
|
| 78 |
+
#border
|
| 79 |
+
st.markdown("""
|
| 80 |
+
<style>
|
| 81 |
+
.custom-form {
|
| 82 |
+
background-color: #497a78; /* Light blue */
|
| 83 |
+
padding: 12px;
|
| 84 |
+
border-radius: 10px;
|
| 85 |
+
box-shadow: 2px 2px 8px rgba(0,0,0,0.1);
|
| 86 |
+
}
|
| 87 |
+
</style>
|
| 88 |
+
""", unsafe_allow_html=True)
|
| 89 |
+
with st.container():
|
| 90 |
+
st.markdown('<div class="custom-form">', unsafe_allow_html=True)
|
| 91 |
+
#border closed
|
| 92 |
+
|
| 93 |
+
with st.form("add_user_form"):
|
| 94 |
+
name = st.text_input("Name")
|
| 95 |
+
user_type = st.selectbox("Type", ["Admin", "Standard", "Guest"])
|
| 96 |
+
password = st.text_input("Password", type="password")
|
| 97 |
+
submitted = st.form_submit_button("Submit")
|
| 98 |
+
|
| 99 |
+
if submitted:
|
| 100 |
+
# Get current max ID
|
| 101 |
+
users_ref = db.collection("users")
|
| 102 |
+
all_users = users_ref.stream()
|
| 103 |
+
max_id = 0
|
| 104 |
+
for doc in all_users:
|
| 105 |
+
doc_data = doc.to_dict()
|
| 106 |
+
if "id" in doc_data and isinstance(doc_data["id"], int):
|
| 107 |
+
max_id = max(max_id, doc_data["id"])
|
| 108 |
+
|
| 109 |
+
new_id = max_id + 1
|
| 110 |
+
|
| 111 |
+
# Save to Firestore
|
| 112 |
+
users_ref.add({
|
| 113 |
+
"id": new_id,
|
| 114 |
+
"name": name,
|
| 115 |
+
"type": user_type,
|
| 116 |
+
"pass": password
|
| 117 |
+
})
|
| 118 |
+
|
| 119 |
+
st.success(f"β
User '{name}' added with ID {new_id}.")
|
| 120 |
+
|
| 121 |
+
if st.session_state.get("user_option") == "View User":
|
| 122 |
+
st.subheader("π List of Users")
|
| 123 |
+
|
| 124 |
+
#border
|
| 125 |
+
st.markdown("""
|
| 126 |
+
<style>
|
| 127 |
+
.custom-form {
|
| 128 |
+
background-color: #497a78; /* Light blue */
|
| 129 |
+
padding: 12px;
|
| 130 |
+
border-radius: 10px;
|
| 131 |
+
box-shadow: 2px 2px 8px rgba(0,0,0,0.1);
|
| 132 |
+
}
|
| 133 |
+
</style>
|
| 134 |
+
""", unsafe_allow_html=True)
|
| 135 |
+
with st.container():
|
| 136 |
+
st.markdown('<div class="custom-form">', unsafe_allow_html=True)
|
| 137 |
+
#border closed
|
| 138 |
+
|
| 139 |
+
try:
|
| 140 |
+
users_ref = db.collection("users")
|
| 141 |
+
docs = db.collection("users").get()
|
| 142 |
+
|
| 143 |
+
user_data = []
|
| 144 |
+
for doc in docs:
|
| 145 |
+
user = doc.to_dict()
|
| 146 |
+
user["doc_id"] = doc.id
|
| 147 |
+
user_data.append(user)
|
| 148 |
+
|
| 149 |
+
if user_data:
|
| 150 |
+
st.dataframe(user_data)
|
| 151 |
+
else:
|
| 152 |
+
st.info("No users found.")
|
| 153 |
+
except Exception as e:
|
| 154 |
+
st.error(f"Error fetching users: {e}")
|
| 155 |
+
|
| 156 |
+
elif page == "Distributors":
|
| 157 |
+
st.title("π Distributors Page")
|
| 158 |
+
st.write("Reports section coming soon.")
|