File size: 4,234 Bytes
44e7320
 
106b612
 
44e7320
106b612
d685333
d975ba4
cf4a816
 
 
7074721
 
106b612
44e7320
d5bcf89
dfbe477
d5bcf89
f68916e
d5bcf89
 
 
f68916e
 
d5bcf89
f68916e
d5bcf89
 
 
ed3b18b
d5bcf89
f68916e
d5bcf89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44e7320
ed3b18b
d685333
 
106b612
 
d685333
 
 
 
 
 
d5bcf89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19384fe
106b612
 
d685333
6cca50e
106b612
6cca50e
106b612
6cca50e
d5bcf89
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pandas as pd
import gspread
import gradio as gr
from oauth2client.service_account import ServiceAccountCredentials

# ------------------ AUTH ------------------
VALID_USERS = {
    "[email protected]": "Pass.123",
    "[email protected]": "Pass.123",
    "[email protected]": "Pass.123",
    "[email protected]": "Pass.123"
}

# ------------------ GOOGLE SHEET SETUP ------------------
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("deep-mile-461309-t8-0e90103411e0.json", scope)
client = gspread.authorize(creds)
sheet_url = "https://docs.google.com/spreadsheets/d/1if4KoVQvw5ZbhknfdZbzMkcTiPfsD6bz9V3a1th-bwQ"

# ------------------ SHEET REFRESH FUNCTIONS ------------------
def load_sheet(sheet_name):
    sheet = client.open_by_url(sheet_url).worksheet(sheet_name)
    data = sheet.get_all_records()
    df = pd.DataFrame(data)
    return df

# ------------------ REPORTS TAB ------------------
def filter_calls_by_date(date):
    df = load_sheet("Calls")
    df['Date'] = pd.to_datetime(df['Date'], errors='coerce').dt.date.astype(str)
    return df[df['Date'] == date]

def filter_appointments_by_date(date):
    df = load_sheet("Appointments")
    df['Date'] = pd.to_datetime(df['Date'], errors='coerce').dt.date.astype(str)
    return df[df['Date'] == date]

# ------------------ APPOINTED LEADS ------------------
def appointed_leads_table():
    df = load_sheet("Appointed Leads")
    grouped = df.groupby('Rep')['Customer Name'].apply(list).reset_index()
    return grouped

# ------------------ INTERACTIVE QUERY VIEW ------------------
def search_table(sheet_name, field, keyword):
    df = load_sheet(sheet_name)
    if field not in df.columns:
        return pd.DataFrame(), "Field not found."
    results = df[df[field].astype(str).str.contains(keyword, case=False, na=False)]
    return results, f"Found {len(results)} results."

# ------------------ GRADIO UI ------------------
with gr.Blocks() as app:
    with gr.Row():
        with gr.Column(visible=True) as login_ui:
            gr.Markdown("## πŸ” Login Required")
            email = gr.Textbox(label="Email")
            password = gr.Textbox(label="Password", type="password")
            login_btn = gr.Button("Login")
            login_msg = gr.Markdown("")

        with gr.Column(visible=False) as main_ui:
            gr.Markdown("## πŸ—‚οΈ Graffiti Admin Dashboard")

            with gr.Tab("πŸ“… Calls Report"):
                calls_date = gr.Textbox(label="Enter Date (YYYY-MM-DD)")
                calls_table = gr.Dataframe()
                calls_date.change(lambda d: filter_calls_by_date(d), inputs=calls_date, outputs=calls_table)

            with gr.Tab("πŸ“… Appointments Report"):
                appt_date = gr.Textbox(label="Enter Date (YYYY-MM-DD)")
                appt_table = gr.Dataframe()
                appt_date.change(lambda d: filter_appointments_by_date(d), inputs=appt_date, outputs=appt_table)

            with gr.Tab("πŸ§‘ Appointed Leads"):
                leads_btn = gr.Button("View Appointed Leads")
                leads_output = gr.Dataframe()
                leads_btn.click(fn=appointed_leads_table, outputs=leads_output)

            with gr.Tab("πŸ” Query Live Sheets"):
                sheet_choice = gr.Dropdown(choices=["LiveQuotes", "LiveCustomer", "LiveJobBags"], label="Select Sheet")
                field_input = gr.Textbox(label="Field (column name)")
                keyword_input = gr.Textbox(label="Keyword to search")
                query_btn = gr.Button("Search")
                query_table = gr.Dataframe()
                query_info = gr.Markdown()
                query_btn.click(fn=search_table, inputs=[sheet_choice, field_input, keyword_input], outputs=[query_table, query_info])

    def do_login(user, pw):
        if VALID_USERS.get(user) == pw:
            return gr.update(visible=False), gr.update(visible=True), ""
        else:
            return gr.update(visible=True), gr.update(visible=False), "❌ Invalid email or password."

    login_btn.click(fn=do_login, inputs=[email, password], outputs=[login_ui, main_ui, login_msg])

app.launch()