"use client" import type React from "react" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Alert, AlertDescription } from "@/components/ui/alert" import { Textarea } from "@/components/ui/textarea" import { Loader2, Save, RefreshCw } from "lucide-react" const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://127.0.0.1:5000/api/v1" interface ModelPrice { _id: { $oid: string } provider: string product: string currency: string price: string billingBy: string billingUnit: number type: string createdAt: { $date: string } updatedAt: { $date: string } __v: number } interface ModelPricesResponse { success: boolean data: ModelPrice[] } interface CostUpdateResponse { success: boolean docs_updated: number } interface CostUpdatePayload { product: string start_date: string end_date: string price: string billing_unit: number company_ids?: string[] } export default function CostUpdateForm() { const [isLoading, setIsLoading] = useState(false) const [isLoadingData, setIsLoadingData] = useState(false) const [error, setError] = useState("") const [success, setSuccess] = useState("") // Estados para os dados dos combos const [products, setProducts] = useState([]) const [billingOptions, setBillingOptions] = useState<{ billingBy: string; billingUnit: number }[]>([]) const [formData, setFormData] = useState({ product: "", start_date: "", end_date: "", price: "", billing_unit: "", company_ids: "", }) const getAuthHeaders = () => { const token = localStorage.getItem("access_token") return { Authorization: `Bearer ${token}`, "Content-Type": "application/json", } } // Função para buscar dados dos modelos const fetchModelPrices = async () => { setIsLoadingData(true) setError("") try { const response = await fetch(`${API_BASE_URL}/usage/model/prices?type=stt`, { headers: getAuthHeaders(), }) if (response.ok) { const result: ModelPricesResponse = await response.json() if (result.success && Array.isArray(result.data)) { // Extrair produtos únicos const uniqueProducts = [...new Set(result.data.map((item) => item.product))] setProducts(uniqueProducts) // Extrair opções de billing únicas const uniqueBillingOptions = result.data.reduce( (acc, item) => { const existing = acc.find( (option) => option.billingBy === item.billingBy && option.billingUnit === item.billingUnit, ) if (!existing) { acc.push({ billingBy: item.billingBy, billingUnit: item.billingUnit, }) } return acc }, [] as { billingBy: string; billingUnit: number }[], ) setBillingOptions(uniqueBillingOptions) } } else { const errorData = await response.json() setError(errorData.message || "Erro ao buscar dados dos modelos") } } catch (err) { console.log("Erro de conexão com o servidor: ",err) setError("Erro de conexão com o servidor") } finally { setIsLoadingData(false) } } // Carregar dados ao montar o componente useEffect(() => { fetchModelPrices() }, []) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setIsLoading(true) setError("") setSuccess("") try { // Converte company_ids de string para array (se fornecido) let company_ids: string[] | undefined if (formData.company_ids.trim()) { company_ids = formData.company_ids .split(",") .map((id) => id.trim()) .filter((id) => id.length > 0) } const payload: CostUpdatePayload = { product: formData.product, start_date: formData.start_date, end_date: formData.end_date, price: formData.price, billing_unit: Number(formData.billing_unit), } // Só adiciona company_ids se foi fornecido if (company_ids && company_ids.length > 0) { payload.company_ids = company_ids } const response = await fetch(`${API_BASE_URL}/usage/cost`, { method: "PATCH", headers: getAuthHeaders(), body: JSON.stringify(payload), }) if (response.ok) { const result: CostUpdateResponse = await response.json() if (result.success) { setSuccess(`Custos atualizados com sucesso! ${result.docs_updated} registros foram atualizados.`) setFormData({ product: "", start_date: "", end_date: "", price: "", billing_unit: "", company_ids: "", }) } else { setError("Erro na resposta do servidor") } } else { const errorData = await response.json() setError(errorData.message || "Erro ao atualizar custos") } } catch (err) { console.log("====> Erro de conexão com o servidor: ", err) setError("Erro de conexão com o servidor") } finally { setIsLoading(false) } } const handleInputChange = (field: string, value: string) => { setFormData((prev) => ({ ...prev, [field]: value })) } const handleBillingChange = (billingBy: string) => { // Encontrar o billingUnit correspondente const selectedOption = billingOptions.find((option) => option.billingBy === billingBy) if (selectedOption) { setFormData((prev) => ({ ...prev, billing_unit: selectedOption.billingUnit.toString(), })) } } return ( Atualizar Custos de Uso
handleInputChange("start_date", e.target.value)} required />
handleInputChange("end_date", e.target.value)} required />
handleInputChange("price", e.target.value)} placeholder="0.024" required />