package handlers import ( "net/http" "strconv" "time" "go-crm/internal/db" "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 += `` clientNames[c.ClientID] = c.Name } for _, cu := range customers { customerOptions += `` customerNames[cu.CustomerID] = cu.Name } for _, s := range services { serviceOptions += `` serviceNames[s.ServiceID] = s.Name } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write([]byte(`
| Client | Customer | Service | Date | Hour | Status | Actions |
|---|---|---|---|---|---|---|
| ` + clientName + ` | ` + customerName + ` | ` + serviceName + ` | ` + s.PlanDate + ` | ` + s.Time + ` | ` + s.Status + ` | View |
Client ID: ` + strconv.FormatInt(sch.ClientID, 10) + `
Customer ID: ` + strconv.FormatInt(sch.CustomerID, 10) + `
Service ID: ` + strconv.FormatInt(sch.ServiceID, 10) + `
Date: ` + sch.PlanDate + `
Time: ` + sch.Time + `
Status: ` + sch.Status + `
Back