package handlers import ( "fmt" "net/http" "strconv" "time" "go-crm/internal/db" "go-crm/internal/templates" "github.com/go-chi/chi/v5" ) func (a *App) ListSchedules(w http.ResponseWriter, r *http.Request) { accountID, ok := a.requireAuth(w, r) if !ok { return } limit, _ := strconv.Atoi(r.URL.Query().Get("limit")) offset, _ := strconv.Atoi(r.URL.Query().Get("offset")) clientID, _ := strconv.ParseInt(r.URL.Query().Get("client_id"), 10, 64) customerID, _ := strconv.ParseInt(r.URL.Query().Get("customer_id"), 10, 64) if limit == 0 { limit = 20 } schedules, err := db.ListSchedules(a.DB, clientID, customerID, limit, offset) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } clients, err := db.ListClients(a.DB, accountID, limit, offset) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } customers, err := db.ListCustomers(a.DB, accountID, clientID, limit, offset) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } services, err := db.ListServices(a.DB, accountID, clientID, limit, offset) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var clientOptions, customerOptions, serviceOptions string clientNames := make(map[int64]string) customerNames := make(map[int64]string) serviceNames := make(map[int64]string) for _, c := range clients { clientOptions += fmt.Sprintf(``, c.ClientID, htmlEscape(c.Name)) clientNames[c.ClientID] = c.Name } for _, cu := range customers { customerOptions += fmt.Sprintf(``, cu.CustomerID, htmlEscape(cu.Name)) customerNames[cu.CustomerID] = cu.Name } for _, s := range services { serviceOptions += fmt.Sprintf(``, s.ServiceID, htmlEscape(s.Name)) serviceNames[s.ServiceID] = s.Name } buf := templates.BufRender() actions := `` fmt.Fprint(buf, templates.PageHeader("Scheduling", "Appointments and calendar", actions)) fmt.Fprintf(buf, ` `, clientOptions, customerOptions, serviceOptions) if len(schedules) == 0 { fmt.Fprint(buf, templates.EmptyState(``, "No appointments scheduled yet.")) } else { fmt.Fprintf(buf, `
`) fmt.Fprint(buf, templates.TableStart([]string{"Client", "Customer", "Service", "Date", "Time", "Status", "Actions"})) for _, s := range schedules { clientName := clientNames[s.ClientID] if clientName == "" { clientName = strconv.FormatInt(s.ClientID, 10) } customerName := customerNames[s.CustomerID] if customerName == "" { customerName = strconv.FormatInt(s.CustomerID, 10) } serviceName := serviceNames[s.ServiceID] if serviceName == "" { serviceName = strconv.FormatInt(s.ServiceID, 10) } statusPill := statusPillForSchedule(s.Status) fmt.Fprintf(buf, ` %s %s %s %s %s %s
View
`, htmlEscape(clientName), htmlEscape(customerName), htmlEscape(serviceName), htmlEscape(s.PlanDate), htmlEscape(s.Time), statusPill, s.ScheduleID, s.ScheduleID, s.ScheduleID) } fmt.Fprint(buf, templates.TableEnd()) fmt.Fprintf(buf, `
`) for _, s := range schedules { clientName := clientNames[s.ClientID] customerName := customerNames[s.CustomerID] serviceName := serviceNames[s.ServiceID] fmt.Fprintf(buf, ` `, s.ScheduleID, s.ScheduleID, s.ScheduleID, s.ClientID, htmlEscape(clientName), clientOptions, s.CustomerID, htmlEscape(customerName), customerOptions, s.ServiceID, htmlEscape(serviceName), serviceOptions, htmlEscape(s.PlanDate), htmlEscape(s.Time), s.Status, s.Status, s.ScheduleID) } } w.Header().Set("Content-Type", "text/html; charset=utf-8") templates.WritePage(w, buf, "Scheduling", "scheduling") } func statusPillForSchedule(status string) string { switch status { case "confirmed": return `confirmed` case "cancelled": return `cancelled` default: return `pending` } } func (a *App) CreateSchedule(w http.ResponseWriter, r *http.Request) { _, ok := a.requireAuth(w, r) if !ok { return } r.ParseForm() clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64) customerID, _ := strconv.ParseInt(r.FormValue("customer_id"), 10, 64) serviceID, _ := strconv.ParseInt(r.FormValue("service_id"), 10, 64) schedule := db.Schedule{ ClientID: clientID, CustomerID: customerID, ServiceID: serviceID, PlanDate: r.FormValue("plan_date"), Time: r.FormValue("time"), Status: r.FormValue("status"), CreatedAt: time.Now().Unix(), } if err := schedule.Create(a.DB); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Header().Set("HX-Refresh", "true") } func (a *App) ViewSchedule(w http.ResponseWriter, r *http.Request) { _, ok := a.requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) var sch db.Schedule err := a.DB.QueryRow("SELECT schedule_id, client_id, customer_id, service_id, plan_date, time, status, created_at FROM scheduling WHERE schedule_id = ?", id).Scan(&sch.ScheduleID, &sch.ClientID, &sch.CustomerID, &sch.ServiceID, &sch.PlanDate, &sch.Time, &sch.Status, &sch.CreatedAt) if err != nil { http.Error(w, "Schedule not found", http.StatusNotFound) return } buf := templates.BufRender() fmt.Fprint(buf, templates.PageHeader("Appointment", "Schedule details")) statusPill := statusPillForSchedule(sch.Status) fmt.Fprintf(buf, `
%s
Date %s
Time %s
Client / Customer / Service %d / %d / %d
`, statusPill, htmlEscape(sch.PlanDate), htmlEscape(sch.Time), sch.ClientID, sch.CustomerID, sch.ServiceID) w.Header().Set("Content-Type", "text/html; charset=utf-8") templates.WritePage(w, buf, "Appointment", "scheduling") } func (a *App) UpdateSchedule(w http.ResponseWriter, r *http.Request) { _, ok := a.requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) r.ParseForm() clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64) customerID, _ := strconv.ParseInt(r.FormValue("customer_id"), 10, 64) serviceID, _ := strconv.ParseInt(r.FormValue("service_id"), 10, 64) _, err := a.DB.Exec("UPDATE scheduling SET client_id=?, customer_id=?, service_id=?, plan_date=?, time=?, status=? WHERE schedule_id=?", clientID, customerID, serviceID, r.FormValue("plan_date"), r.FormValue("time"), r.FormValue("status"), id) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Header().Set("HX-Refresh", "true") } func (a *App) DeleteSchedule(w http.ResponseWriter, r *http.Request) { _, ok := a.requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) a.DB.Exec("DELETE FROM scheduling WHERE schedule_id = ?", id) } // --- package-level shims --- func ListSchedules(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).ListSchedules(w, r) } func CreateSchedule(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).CreateSchedule(w, r) } func ViewSchedule(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).ViewSchedule(w, r) } func UpdateSchedule(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).UpdateSchedule(w, r) } func DeleteSchedule(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).DeleteSchedule(w, r) }