import streamlit as st import pandas as pd from datetime import datetime, timedelta from persiantools.jdatetime import JalaliDate # Load schedule data from CSV def load_schedule(): try: schedule_data = pd.read_csv('schedule.csv') except FileNotFoundError: schedule_data = pd.DataFrame(columns=['Class', 'Day', 'Start Time', 'Username', 'System']) return schedule_data # Save schedule data to CSV def save_schedule(schedule_data): schedule_data.to_csv('schedule.csv', index=False) # Load user details from CSV def load_user_details(): try: user_details = pd.read_csv('details.csv') except FileNotFoundError: user_details = pd.DataFrame(columns=['username', 'password']) return user_details # Check for system availability def has_system_availability(schedule_data, class_selected, day_selected, start_time_selected, system_selected): bookings = schedule_data[ (schedule_data['Class'] == class_selected) & (schedule_data['Day'] == day_selected) & (schedule_data['Start Time'] == start_time_selected) ] if len(bookings) >= 6: return False elif system_selected in bookings['System'].values: return False else: return True # Convert string day and time to datetime def get_class_datetime(day_selected, start_time_selected): days_of_week = ["شنبه", "یکشنبه", "دوشنبه", "سه‌شنبه", "چهارشنبه", "پنج‌شنبه", "جمعه"] today = JalaliDate.today().to_gregorian() current_day_index = today.weekday() # This returns the current day of the week as an integer (0=Monday, 6=Sunday) # Adjusting days of the week to start from Saturday to match the Persian calendar target_day_index = days_of_week.index(day_selected) adjusted_day_index = (target_day_index + 1) % 7 # Mapping Saturday to 0, ..., Friday to 6 delta_days = (adjusted_day_index - current_day_index) % 7 # If the class day is today but time has passed, move to the next week class_time = datetime.strptime(start_time_selected, "%H:%M").time() if delta_days == 0 and class_time < datetime.now().time(): delta_days = 7 class_date = today + timedelta(days=delta_days) class_datetime = datetime.combine(class_date, class_time) return class_datetime # Initialize session state variables if 'username' not in st.session_state: st.session_state.username = "" if 'password' not in st.session_state: st.session_state.password = "" if 'schedule_data' not in st.session_state: st.session_state.schedule_data = load_schedule() if 'authenticated' not in st.session_state: st.session_state.authenticated = False if 'is_admin' not in st.session_state: st.session_state.is_admin = False if 'page' not in st.session_state: st.session_state.page = "login" if 'user_display_name' not in st.session_state: st.session_state.user_display_name = "" # Login Page def login_page(): st.title("ورود") st.session_state.username = st.text_input("نام کاربری") st.session_state.password = st.text_input("رمز عبور", type='password') user_details = load_user_details() if st.button("ورود"): if ( (st.session_state.username in user_details['username'].values) and (st.session_state.password in user_details[user_details['username'] == st.session_state.username]['password'].values) ) or (st.session_state.username == "armin" and st.session_state.password == "12345679"): st.session_state.authenticated = True st.session_state.is_admin = (st.session_state.username == "armin") st.session_state.page = "enter_name" st.experimental_rerun() else: st.error("نام کاربری یا رمز عبور اشتباه است. لطفا دوباره تلاش کنید.") # Name Entry Page def name_entry_page(): st.title("ورود نام کاربر") st.session_state.user_display_name = st.text_input("لطفاً نام خود را وارد کنید") if st.button("تایید"): if st.session_state.user_display_name: st.session_state.page = "schedule" st.experimental_rerun() else: st.error("لطفاً نام خود را وارد کنید.") # Class Schedule Page def schedule_page(): st.title("برنامه کلاسی") classes = ["تعمیرات موبایل"] days = ["شنبه", "یکشنبه", "دوشنبه", "سه‌شنبه", "چهارشنبه", "پنج‌شنبه", "جمعه"] start_times = ["10:30","12:00","13:30","15:00","16:30","18:00","19:30"] systems = ["ست 1", "ست 2", "ست 3", "ست 4", "ست 5", "ست 6","ست 7","ست 8"] st.header("مدیریت کلاس‌ها") col1, col2 = st.columns(2) with col1: st.subheader("اضافه کردن کلاس") class_selected = st.selectbox("کلاس", classes, key='add_class_select') day_selected = st.selectbox("روز", days, key='add_day_select') start_time_selected = st.selectbox("زمان شروع", start_times, key='add_time_select') system_selected = st.selectbox("سیستم", systems, key='add_system_select') if st.button("اضافه کردن کلاس"): class_datetime = get_class_datetime(day_selected, start_time_selected) if class_datetime - datetime.now() < timedelta(hours=24): st.error("کلاس‌ها باید حداقل ۳۰ ساعت قبل رزرو شوند.") elif not has_system_availability(st.session_state.schedule_data, class_selected, day_selected, start_time_selected, system_selected): st.error(f"سیستم '{system_selected}' در زمان '{start_time_selected}' در روز '{day_selected}' برای '{class_selected}' موجود نیست.") else: new_row = pd.DataFrame([[class_selected, day_selected, start_time_selected, st.session_state.user_display_name, system_selected]], columns=['Class', 'Day', 'Start Time', 'Username', 'System']) st.session_state.schedule_data = pd.concat([st.session_state.schedule_data, new_row], ignore_index=True) save_schedule(st.session_state.schedule_data) st.success(f"کلاس {class_selected} با موفقیت اضافه شد به {system_selected}.") with col2: st.subheader("حذف کلاس") if not st.session_state.schedule_data.empty and 'Username' in st.session_state.schedule_data.columns: user_classes = st.session_state.schedule_data[ (st.session_state.schedule_data['Username'] == st.session_state.user_display_name) | st.session_state.is_admin ] if not user_classes.empty: class_to_remove = st.selectbox("کلاس", user_classes['Class'].unique(), key='remove_class_select') day_to_remove = st.selectbox("روز", user_classes[user_classes['Class'] == class_to_remove]['Day'].unique(), key='remove_day_select') time_to_remove = st.selectbox("زمان شروع", user_classes[ (user_classes['Class'] == class_to_remove) & (user_classes['Day'] == day_to_remove) ]['Start Time'].unique(), key='remove_time_select') system_to_remove = st.selectbox("سیستم", user_classes[ (user_classes['Class'] == class_to_remove) & (user_classes['Day'] == day_to_remove) & (user_classes['Start Time'] == time_to_remove) ]['System'].unique(), key='remove_system_select') if st.button("حذف کلاس"): st.session_state.schedule_data = st.session_state.schedule_data[~( (st.session_state.schedule_data['Class'] == class_to_remove) & (st.session_state.schedule_data['Day'] == day_to_remove) & (st.session_state.schedule_data['Start Time'] == time_to_remove) & (st.session_state.schedule_data['System'] == system_to_remove) & ((st.session_state.is_admin) or (st.session_state.schedule_data['Username'] == st.session_state.user_display_name)) )] save_schedule(st.session_state.schedule_data) st.success(f"کلاس {class_to_remove} در روز {day_to_remove} ساعت {time_to_remove} با موفقیت حذف شد.") else: st.error("شما هیچ کلاسی برای حذف ندارید.") else: st.error("هیچ کلاسی برای حذف وجود ندارد یا ستون 'Username' موجود نیست.") st.header("برنامه شما") st.table(st.session_state.schedule_data) if st.button("خروج"): st.session_state['authenticated'] = False st.session_state['username'] = "" st.session_state['password'] = "" st.session_state['is_admin'] = False st.session_state['user_display_name'] = "" st.session_state.page = "login" st.experimental_rerun() # Page navigation if st.session_state.page == "login": login_page() elif st.session_state.page == "enter_name": name_entry_page() elif st.session_state.page == "schedule": if st.session_state['authenticated']: schedule_page() else: st.session_state.page = "login" login_page()