Hadeeratef91 commited on
Commit
ce0c71b
·
verified ·
1 Parent(s): aabc6f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -91
app.py CHANGED
@@ -1,112 +1,131 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import os
4
- import uuid
5
  from datetime import datetime
6
 
7
- st.set_page_config(page_title="نظام الشكاوى", layout="centered")
8
 
9
- # اسم الملف لتخزين الشكاوى
10
- csv_file = "complaints.csv"
11
-
12
- # التأكد من وجود الملف
13
- if not os.path.exists(csv_file):
14
- df_init = pd.DataFrame(columns=[
15
- "complaint_id", "username", "complaint_text", "status", "response", "timestamp"
16
- ])
17
- df_init.to_csv(csv_file, index=False)
18
-
19
- def load_data():
20
- return pd.read_csv(csv_file)
21
-
22
- def save_data(df):
23
- df.to_csv(csv_file, index=False)
24
-
25
- def generate_complaint_id(user_id, existing_ids):
26
- counter = 1
27
- while True:
28
- cid = f"SUE U{user_id}-{str(counter).zfill(6)}"
29
- if cid not in existing_ids:
30
- return cid
31
- counter += 1
32
 
33
- # جلسة المستخدم
34
- if "logged_in" not in st.session_state:
35
- st.session_state.logged_in = False
36
- if "role" not in st.session_state:
37
- st.session_state.role = None
 
38
  if "username" not in st.session_state:
39
  st.session_state.username = ""
 
 
40
 
 
41
  # تسجيل الدخول
42
- if not st.session_state.logged_in:
43
- st.title("تسجيل الدخول")
44
- username = st.text_input("اسم المستخدم")
45
- password = st.text_input("كلمة المرور", type="password")
46
- role = st.selectbox("الدور", ["مقدم الشكوى", "مستقبل الشكوى"])
47
-
48
- if st.button("دخول"):
49
  if username and password:
50
- st.session_state.logged_in = True
51
- st.session_state.role = role
 
 
 
52
  st.session_state.username = username
53
  st.rerun()
54
  else:
55
- st.error("من فضلك أدخل اسم المستخدم وكلمة المرور")
56
 
57
- else:
58
- st.sidebar.success(f"مرحبًا {st.session_state.username} ({st.session_state.role})")
59
- if st.sidebar.button("تسجيل الخروج"):
60
- st.session_state.logged_in = False
61
- st.session_state.role = None
62
- st.session_state.username = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  st.rerun()
64
 
65
- df = load_data()
 
 
 
 
 
 
 
66
 
67
- if st.session_state.role == "مقدم الشكوى":
68
- st.header("📨 إرسال شكوى جديدة")
69
- complaint_text = st.text_area("وصف الشكوى:")
70
- if st.button("إرسال الشكوى"):
71
- user_id = st.session_state.username[-1] if st.session_state.username[-1].isdigit() else "X"
72
- existing_ids = set(df["complaint_id"])
73
- complaint_id = generate_complaint_id(user_id, existing_ids)
74
- new_complaint = {
75
- "complaint_id": complaint_id,
76
- "username": st.session_state.username,
77
- "complaint_text": complaint_text,
78
- "status": "قيد الانتظار",
79
- "response": "",
80
- "timestamp": datetime.now().isoformat()
81
- }
82
- df = pd.concat([df, pd.DataFrame([new_complaint])], ignore_index=True)
83
- save_data(df)
84
- st.success(f"تم إرسال الشكوى بنجاح ✅\nرقم الشكوى الخاص بك: {complaint_id}")
85
 
86
- st.subheader("📋 الشكاوى الخاصة بك")
87
- user_df = df[df["username"] == st.session_state.username]
88
- if not user_df.empty:
89
- for _, row in user_df.iterrows():
90
- with st.expander(f"🔹 شكوى: {row['complaint_id']}"):
91
- st.write(f"**الحالة:** {row['status']}")
92
- st.write(f"**الرد:** {row['response'] if row['response'] else 'لم يتم الرد بعد'}")
93
- st.write(f"**الوصف:** {row['complaint_text']}")
94
- else:
95
- st.info("لا توجد شكاوى بعد.")
96
 
97
- elif st.session_state.role == "مستقبل الشكوى":
98
- st.header("📬 جميع الشكاوى")
99
- if df.empty:
100
- st.info("لا توجد شكاوى بعد")
101
- else:
102
- for i, row in df.iterrows():
103
- with st.expander(f"🧾 شكوى: {row['complaint_id']} (بواسطة {row['username']})"):
104
- st.write(f"**الوصف:** {row['complaint_text']}")
105
- st.write(f"**الحالة الحالية:** {row['status']}")
106
- new_status = st.selectbox("تغيير الحالة", ["قيد الانتظار", "جاري المعالجة", "تم الرد"], index=["قيد الانتظار", "جاري المعالجة", "تم الرد"].index(row['status']), key=f"status_{i}")
107
- response = st.text_area("الرد على الشكوى", value=row['response'], key=f"response_{i}")
108
- if st.button("تحديث", key=f"update_{i}"):
109
- df.at[i, 'status'] = new_status
110
- df.at[i, 'response'] = response
111
- save_data(df)
112
- st.success("تم تحديث الشكوى")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import os
 
4
  from datetime import datetime
5
 
6
+ st.set_page_config(page_title="نظام إدارة الشكاوى", layout="centered")
7
 
8
+ # -----------------------------------------
9
+ # تحميل أو إنشاء ملف CSV
10
+ file_path = "complaints.csv"
11
+ if os.path.exists(file_path):
12
+ df = pd.read_csv(file_path)
13
+ else:
14
+ df = pd.DataFrame(columns=["complaint_id", "username", "complaint_text", "status", "response", "timestamp"])
15
+ df.to_csv(file_path, index=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # -----------------------------------------
18
+ # تهيئة session state
19
+ if "is_logged_in" not in st.session_state:
20
+ st.session_state.is_logged_in = False
21
+ if "user_type" not in st.session_state:
22
+ st.session_state.user_type = None
23
  if "username" not in st.session_state:
24
  st.session_state.username = ""
25
+ if "last_seen_count" not in st.session_state:
26
+ st.session_state.last_seen_count = len(df)
27
 
28
+ # -----------------------------------------
29
  # تسجيل الدخول
30
+ if not st.session_state.is_logged_in:
31
+ st.title("🔐 Sign In")
32
+ username = st.text_input("User Name ")
33
+ password = st.text_input("Password", type="password")
34
+ if st.button("Sing in "):
 
 
35
  if username and password:
36
+ if username.startswith("admin"):
37
+ st.session_state.user_type = "receiver"
38
+ else:
39
+ st.session_state.user_type = "sender"
40
+ st.session_state.is_logged_in = True
41
  st.session_state.username = username
42
  st.rerun()
43
  else:
44
+ st.warning("يرجى إدخال اسم المستخدم وكلمة المرور")
45
 
46
+ # -----------------------------------------
47
+ # مقدم الشكوى
48
+ elif st.session_state.user_type == "sender":
49
+ st.title("📨 تقديم شكوى")
50
+ complaint_text = st.text_area("Description ")
51
+ if st.button("Send complaint"):
52
+ user_id = st.session_state.username.split("_")[-1].upper()
53
+ existing_ids = df[df["username"] == st.session_state.username]["complaint_id"]
54
+ next_id = len(existing_ids) + 1
55
+ complaint_id = f"SUE {user_id}-{next_id:06}"
56
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
57
+ new_complaint = {
58
+ "complaint_id": complaint_id,
59
+ "username": st.session_state.username,
60
+ "complaint_text": complaint_text,
61
+ "status": "Pending",
62
+ "response": "",
63
+ "timestamp": timestamp
64
+ }
65
+ df = pd.concat([df, pd.DataFrame([new_complaint])], ignore_index=True)
66
+ df.to_csv(file_path, index=False)
67
+ st.success("✅ تم تقديم الشكوى بنجاح")
68
  st.rerun()
69
 
70
+ # إشعار بتحديث حالة الشكوى أو الرد
71
+ user_complaints = df[df["username"] == st.session_state.username]
72
+ if not user_complaints.empty:
73
+ last_complaint = user_complaints.iloc[-1]
74
+ if last_complaint["status"].lower() != "pending" or pd.notna(last_complaint["response"]):
75
+ st.info(f"✅ تم تحديث حالة شكواك ({last_complaint['complaint_id']}) إلى: {last_complaint['status']}")
76
+ if pd.notna(last_complaint["response"]) and last_complaint["response"].strip() != "":
77
+ st.success(f"💬 تم الرد على شكوا��: {last_complaint['response']}")
78
 
79
+ # عرض الشكاوى الخاصة به
80
+ st.subheader("📋 last complaints")
81
+ st.dataframe(user_complaints[["complaint_id", "complaint_text", "status", "response", "timestamp"]], use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
+ # زر تسجيل الخروج
84
+ if st.button("🚪 Log out"):
85
+ st.session_state.is_logged_in = False
86
+ st.session_state.user_type = None
87
+ st.rerun()
 
 
 
 
 
88
 
89
+ # -----------------------------------------
90
+ # مستقبل الشكوى
91
+ elif st.session_state.user_type == "receiver":
92
+ st.title("📬 إدارة الشكاوى")
93
+
94
+ # تنبيه عن شكاوى جديدة
95
+ current_count = len(df)
96
+ new_complaints = current_count - st.session_state.last_seen_count
97
+ if new_complaints > 0:
98
+ st.success(f"📬 تم تقديم {new_complaints} شكوى جديدة منذ آخر زيارة.")
99
+ st.session_state.last_seen_count = current_count
100
+
101
+ # عرض الشكاوى
102
+ st.subheader("📋 جميع الشكاوى")
103
+ selected_index = st.selectbox("اختر شكوى للتعامل معها", df.index, format_func=lambda i: f"{df.loc[i, 'complaint_id']} - {df.loc[i, 'username']}")
104
+ selected_row = df.loc[selected_index]
105
+ st.write(f"### ✉️ شكوى: {selected_row['complaint_id']}")
106
+ st.write(f"**مقدم الشكوى:** {selected_row['username']}")
107
+ st.write(f"**الوصف:** {selected_row['complaint_text']}")
108
+
109
+ new_status = st.selectbox("🔄 تحديث الحالة", ["Pending", "In Progress", "Resolved"], index=["Pending", "In Progress", "Resolved"].index(selected_row["status"]))
110
+ new_response = st.text_area("📝 الرد على الشكوى", value=selected_row["response"] or "")
111
+
112
+ if st.button("💾 حفظ التعديلات"):
113
+ df.at[selected_index, "status"] = new_status
114
+ df.at[selected_index, "response"] = new_response
115
+ df.to_csv(file_path, index=False)
116
+ st.success("✅ تم حفظ التعديلات")
117
+ st.rerun()
118
+
119
+ # زر لتحميل الشكاوى
120
+ st.download_button(
121
+ label="📥 تحميل الشكاوى كـ CSV",
122
+ data=df.to_csv(index=False).encode('utf-8'),
123
+ file_name="complaints_export.csv",
124
+ mime="text/csv"
125
+ )
126
+
127
+ # زر تسجيل الخروج
128
+ if st.button("🚪Sign Out"):
129
+ st.session_state.is_logged_in = False
130
+ st.session_state.user_type = None
131
+ st.rerun()