package handlers

import (
	"html/template"
	"net/http"
	"strconv"

	"git.espin.casa/albert/cml04-falcon-printer/storage"
	"github.com/julienschmidt/httprouter"
)

func ListCustomerOrderHandler(storage storage.Storager) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		// get po
		po := ps.ByName("po")
		iPo, _ := strconv.Atoi(po)
		// get production orders
		cos, err := storage.ListCostumerOrders(r.Context(), iPo)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		// create view
		view := &PageView{
			CustomerOrders: cos,
		}
		t, _ := template.ParseFiles("templates/base.html", "templates/index.html")
		if err := t.Execute(w, view); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}
}
