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(`

Scheduling

`)) 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) } w.Write([]byte(``)) } w.Write([]byte(`
ClientCustomerServiceDateHourStatusActions
` + clientName + `` + customerName + `` + serviceName + `` + s.PlanDate + `` + s.Time + `` + s.Status + `View
`)) } 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 } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write([]byte(`

Schedule

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`)) } 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 kept for existing tests --- 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) }