package handlers import ( "net/http" "strconv" "time" "go-crm/internal/db" "github.com/go-chi/chi/v5" ) func (a *App) ListClients(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")) if limit == 0 { limit = 20 } clients, err := db.ListClients(a.DB, accountID, limit, offset) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write([]byte(` Clients

Clients

`)) for _, c := range clients { var whatsappCell string connected := false if c.WhatsAppNumber != "" && a.WAConnector != nil { connected, _ = a.WAConnector.IsConnected(r.Context(), c.ClientID) } if connected { whatsappCell = c.WhatsAppNumber + ` ` } else if c.WhatsAppConnected == 1 && c.WhatsAppNumber != "" { whatsappCell = c.WhatsAppNumber + ` Reconnect` } else { whatsappCell = `Connect` } w.Write([]byte(``)) } w.Write([]byte(`
NamePhoneWhatsAppActions
` + c.Name + ` ` + c.Phone + ` ` + whatsappCell + ` View

Back to Home

`)) } func (a *App) CreateClient(w http.ResponseWriter, r *http.Request) { accountID, ok := a.requireAuth(w, r) if !ok { return } r.ParseForm() client := db.Client{ AccountID: accountID, Name: r.FormValue("name"), Phone: r.FormValue("phone"), Email: r.FormValue("email"), Address: r.FormValue("address"), Notes: r.FormValue("notes"), CreatedAt: time.Now().Unix(), } if err := client.Create(a.DB); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("HX-Refresh", "true") } func (a *App) ViewClient(w http.ResponseWriter, r *http.Request) { accountID, ok := a.requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) client, err := db.GetClientByID(a.DB, accountID, id) if err != nil { http.Error(w, "Client not found", http.StatusNotFound) return } w.Header().Set("Content-Type", "text/html; charset=utf-8") var whatsappSection string if client.WhatsAppNumber != "" { connected := false if a.WAConnector != nil { connected, _ = a.WAConnector.IsConnected(r.Context(), client.ClientID) } status := "Not connected" style := "color:#999" if connected { status = "Connected" style = "color:#28a745" } else if client.WhatsAppConnected == 1 { status = "Disconnected (was connected)" style = "color:#dc3545" } whatsappSection = `

WhatsApp: ` + client.WhatsAppNumber + ` (` + status + `)

Reconnect WhatsApp

` } else { whatsappSection = `

WhatsApp: Not configured Connect

` } w.Write([]byte(`

` + client.Name + `

Client ID: ` + strconv.FormatInt(client.ClientID, 10) + `

Phone: ` + client.Phone + `

Email: ` + client.Email + `

Address: ` + client.Address + `

Notes: ` + client.Notes + `

` + whatsappSection + ` Back `)) } func (a *App) UpdateClient(w http.ResponseWriter, r *http.Request) { accountID, ok := a.requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) r.ParseForm() client := db.Client{ ClientID: id, AccountID: accountID, Name: r.FormValue("name"), Phone: r.FormValue("phone"), Email: r.FormValue("email"), Address: r.FormValue("address"), Notes: r.FormValue("notes"), } if err := client.Update(a.DB); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Header().Set("HX-Refresh", "true") } func (a *App) DeleteClient(w http.ResponseWriter, r *http.Request) { accountID, ok := a.requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) client := &db.Client{ClientID: id, AccountID: accountID} if err := client.Delete(a.DB); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write([]byte("OK")) } // --- package-level shims kept for existing tests --- func ListClients(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).ListClients(w, r) } func CreateClient(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).CreateClient(w, r) } func ViewClient(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).ViewClient(w, r) } func UpdateClient(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).UpdateClient(w, r) } func DeleteClient(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).DeleteClient(w, r) }