Files
workspace/apps/go-crm/internal/handlers/scheduling.go
gabspereira 9c3bfd131d feat(go-crm): industrial terminal-core UI redesign
Complete visual overhaul of the go-crm web interface:

- New shared layout system (internal/templates/ui.go) with dark zinc
  industrial theme, JetBrains Mono typography, grid/noise textures
- Redesigned all pages: Dashboard, Login/Signup, Clients, Customers,
  Services, Scheduling, Payments, Questions, Answers, Leads,
  Review Queue, Report, Keyword Mapping
- Tailwind CSS via CDN with custom color palette (amber/emerald/rose/sky)
- HTMX-powered interactions with CSS swap animations
- Status pills, KPI cards, data tables, empty states, inline forms
- Mobile-responsive sidebar with collapsible navigation
- All existing tests updated and passing
- Zero new build dependencies — works with existing go run/air workflow
2026-05-23 18:00:32 -03:00

289 lines
12 KiB
Go

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(`<option value="%d">%s</option>`, c.ClientID, htmlEscape(c.Name))
clientNames[c.ClientID] = c.Name
}
for _, cu := range customers {
customerOptions += fmt.Sprintf(`<option value="%d">%s</option>`, cu.CustomerID, htmlEscape(cu.Name))
customerNames[cu.CustomerID] = cu.Name
}
for _, s := range services {
serviceOptions += fmt.Sprintf(`<option value="%d">%s</option>`, s.ServiceID, htmlEscape(s.Name))
serviceNames[s.ServiceID] = s.Name
}
buf := templates.BufRender()
actions := `<button type="button" onclick="document.getElementById('scheduleForm').classList.toggle('hidden')" class="btn-primary"><svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg> Add Schedule</button>`
fmt.Fprint(buf, templates.PageHeader("Scheduling", "Appointments and calendar", actions))
fmt.Fprintf(buf, `
<div id="scheduleForm" class="hidden mb-6 animate-slide-up">
<div class="card-industrial p-5">
<h3 class="text-sm font-semibold text-zinc-200 mb-4">New Appointment</h3>
<form hx-post="/scheduling" hx-target="#scheduleList" hx-swap="innerHTML" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
<select name="client_id" required class="input-industrial"><option value="">Select Client</option>%s</select>
<select name="customer_id" required class="input-industrial"><option value="">Select Customer</option>%s</select>
<select name="service_id" required class="input-industrial"><option value="">Select Service</option>%s</select>
<input type="date" name="plan_date" class="input-industrial">
<input type="time" name="time" class="input-industrial">
<select name="status" class="input-industrial"><option value="pending">Pending</option><option value="confirmed">Confirmed</option><option value="cancelled">Cancelled</option></select>
<div class="flex items-end"><button type="submit" class="btn-primary">Save</button></div>
</form>
</div>
</div>`, clientOptions, customerOptions, serviceOptions)
if len(schedules) == 0 {
fmt.Fprint(buf, templates.EmptyState(`<path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"/>`, "No appointments scheduled yet."))
} else {
fmt.Fprintf(buf, `<div class="card-industrial overflow-hidden animate-slide-up stagger-1"><div class="overflow-x-auto">`)
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, `<tr>
<td class="text-zinc-300 text-sm">%s</td>
<td class="text-zinc-300 text-sm">%s</td>
<td class="text-zinc-400 text-xs">%s</td>
<td class="font-mono text-xs text-zinc-300">%s</td>
<td class="font-mono text-xs text-zinc-300">%s</td>
<td>%s</td>
<td>
<div class="flex items-center gap-2">
<a href="/scheduling/%d" class="btn-ghost btn-sm">View</a>
<button type="button" onclick="document.getElementById('editSched%d').classList.toggle('hidden')" class="btn-ghost btn-sm">Edit</button>
<form hx-delete="/scheduling/%d" hx-target="closest tr" hx-swap="outerHTML" style="display:inline">
<button type="submit" class="btn-danger btn-sm" onclick="return confirm('Delete?')">Delete</button>
</form>
</div>
</td>
</tr>`, 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, `</div></div>`)
for _, s := range schedules {
clientName := clientNames[s.ClientID]
customerName := customerNames[s.CustomerID]
serviceName := serviceNames[s.ServiceID]
fmt.Fprintf(buf, `
<div id="editSched%d" class="hidden mt-4 animate-slide-up">
<div class="card-industrial p-5 border-amber-400/10">
<h3 class="text-sm font-semibold text-zinc-200 mb-3">Edit Appointment #%d</h3>
<form hx-put="/scheduling/%d" hx-target="#scheduleList" hx-swap="innerHTML" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-3">
<select name="client_id" class="input-industrial text-xs"><option value="%d">%s</option>%s</select>
<select name="customer_id" class="input-industrial text-xs"><option value="%d">%s</option>%s</select>
<select name="service_id" class="input-industrial text-xs"><option value="%d">%s</option>%s</select>
<input type="date" name="plan_date" value="%s" class="input-industrial text-xs">
<input type="time" name="time" value="%s" class="input-industrial text-xs">
<select name="status" class="input-industrial text-xs"><option value="%s">%s</option><option value="pending">Pending</option><option value="confirmed">Confirmed</option><option value="cancelled">Cancelled</option></select>
<div class="flex items-end gap-2">
<button type="submit" class="btn-primary btn-sm">Save</button>
<button type="button" onclick="document.getElementById('editSched%d').classList.add('hidden')" class="btn-ghost btn-sm">Cancel</button>
</div>
</form>
</div>
</div>`, 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 `<span class="pill pill-emerald">confirmed</span>`
case "cancelled":
return `<span class="pill pill-rose">cancelled</span>`
default:
return `<span class="pill pill-amber">pending</span>`
}
}
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, `
<div class="max-w-lg animate-slide-up">
<div class="card-industrial p-6">
<div class="flex items-center gap-2 mb-6">%s</div>
<div class="space-y-3">
<div class="flex justify-between py-3 border-b border-white/[0.04]">
<span class="text-xs font-mono uppercase text-zinc-500">Date</span>
<span class="text-sm font-mono text-zinc-300">%s</span>
</div>
<div class="flex justify-between py-3 border-b border-white/[0.04]">
<span class="text-xs font-mono uppercase text-zinc-500">Time</span>
<span class="text-sm font-mono text-zinc-300">%s</span>
</div>
<div class="flex justify-between py-3">
<span class="text-xs font-mono uppercase text-zinc-500">Client / Customer / Service</span>
<span class="text-sm text-zinc-300 text-right font-mono">%d / %d / %d</span>
</div>
</div>
<div class="mt-6">
<a href="/scheduling" class="btn-ghost btn-sm">Back</a>
</div>
</div>
</div>`, 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)
}