|
import streamlit as st |
|
import pandas as pd |
|
import requests |
|
from datetime import datetime, timedelta |
|
|
|
|
|
st.set_page_config(layout="wide") |
|
|
|
|
|
if 'bookings' not in st.session_state: |
|
st.session_state.bookings = [] |
|
|
|
|
|
def get_available_slots(location_id): |
|
service_id = 36561 |
|
|
|
all_slots = [f"{hour}:00" for hour in range(9, 21)] |
|
booked_slots = [booking['time'] for booking in st.session_state.bookings if booking['service_id'] == service_id and booking['location_id'] == location_id] |
|
available_slots = [slot for slot in all_slots if slot not in booked_slots] |
|
return available_slots |
|
|
|
|
|
def add_booking(location_id, time): |
|
service_id = 36561 |
|
st.session_state.bookings.append({ |
|
'id': len(st.session_state.bookings) + 1, |
|
'service_id': service_id, |
|
'location_id': location_id, |
|
'time': time, |
|
'created_at': datetime.now() |
|
}) |
|
|
|
|
|
def get_booking_by_id(booking_id): |
|
for booking in st.session_state.bookings: |
|
if booking['id'] == booking_id: |
|
return booking |
|
return None |
|
|
|
|
|
def update_booking(booking_id, location_id, time): |
|
service_id = 36561 |
|
for booking in st.session_state.bookings: |
|
if booking['id'] == booking_id: |
|
booking['service_id'] = service_id |
|
booking['location_id'] = location_id |
|
booking['time'] = time |
|
return booking |
|
return None |
|
|
|
|
|
def delete_booking(booking_id): |
|
st.session_state.bookings = [booking for booking in st.session_state.bookings if booking['id'] != booking_id] |
|
|
|
|
|
def get_current_month_bookings(): |
|
api_key = "AmeTa7RskgvDqF3VGLBJ6PrWS9NnUtCz2w48QyXu" |
|
now = datetime.now() |
|
start_time = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0) |
|
end_time = (start_time + timedelta(days=32)).replace(day=1) - timedelta(seconds=1) |
|
url = f"https://app.booklux.com/api/v1/bookings?api_key={api_key}" |
|
|
|
|
|
response = requests.get(url) |
|
if response.status_code == 200: |
|
return response.json()['data'] |
|
else: |
|
st.error(f"Error fetching bookings: {response.status_code}") |
|
return [] |
|
|
|
|
|
st.title("YOLO Management - Squash Club") |
|
|
|
|
|
st.header("Add New Booking") |
|
location_id = st.selectbox("Court Number", list(range(1, 8))) |
|
time = st.selectbox("Time Slot", get_available_slots(location_id)) |
|
if st.button("Add Booking"): |
|
add_booking(location_id, time) |
|
st.success(f"Booking added successfully for Court {location_id} at {time}!") |
|
|
|
|
|
st.header("Bookings List") |
|
current_month_bookings = get_current_month_bookings() |
|
bookings_df = pd.DataFrame(current_month_bookings) |
|
st.dataframe(bookings_df) |
|
|
|
|
|
st.header("Update Booking") |
|
booking_id = st.number_input("Booking ID", min_value=1, step=1) |
|
new_location_id = st.selectbox("New Court Number", list(range(1, 8))) |
|
new_time = st.selectbox("New Time Slot", get_available_slots(new_location_id)) |
|
if st.button("Update Booking"): |
|
updated_booking = update_booking(booking_id, new_location_id, new_time) |
|
if updated_booking: |
|
st.success(f"Booking {booking_id} updated successfully!") |
|
else: |
|
st.error(f"Booking {booking_id} not found!") |
|
|
|
|
|
st.header("Delete Booking") |
|
delete_booking_id = st.number_input("Booking ID to Delete", min_value=1, step=1) |
|
if st.button("Delete Booking"): |
|
delete_booking(delete_booking_id) |
|
st.success(f"Booking {delete_booking_id} deleted successfully!") |
|
|
|
|
|
st.header("Available Slots") |
|
court_numbers = list(range(1, 8)) |
|
tabs = st.tabs([f"Court {i}" for i in court_numbers]) |
|
|
|
for i, tab in enumerate(tabs): |
|
with tab: |
|
available_slots = get_available_slots(court_numbers[i]) |
|
st.write(f"Available slots for Court {court_numbers[i]}:") |
|
st.write(available_slots) |
|
|