Files
MoneyFlix/public/assets/js/alpine/crm-calendar.js
T
1sonhador 8542395fbe Add public campaign analytics functionality
- Add PublicCampaignsController for public campaign analytics
- Add public routes for /public/analytics/{slug} and /api/campaigns/{slug}
- Add public campaign analytics view with charts and KPIs
- Support for campaign sharing via custom slugs
- Real-time analytics with ApexCharts integration
2025-10-15 00:30:00 +01:00

1 line
6.0 KiB
JavaScript

export default function crmCalendar(){return{currentYear:(new Date).getFullYear(),currentMonth:(new Date).getMonth()+1,events:[],loading:!1,showAddModal:!1,selectedDate:null,newEvent:{title:"",description:"",event_type:"meeting",start_date:"",start_time:"",end_date:"",end_time:"",all_day:!1,location:"",color:"#3788d8",priority:"medium",status:"scheduled"},init(){const t=new URLSearchParams(window.location.search);this.currentYear=parseInt(t.get("year"))||this.currentYear,this.currentMonth=parseInt(t.get("month"))||this.currentMonth,this.loadEvents()},get monthNames(){return["Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro"]},get currentMonthName(){return`${this.monthNames[this.currentMonth-1]} ${this.currentYear}`},get calendarDays(){const t=new Date(this.currentYear,this.currentMonth-1,1),e=(new Date(this.currentYear,this.currentMonth,0),new Date(t));e.setDate(e.getDate()-t.getDay());const r=[],n=new Date;for(let t=0;t<42;t++){const o=new Date(e);o.setDate(e.getDate()+t);const a=o.getDate(),s=o.getMonth()===this.currentMonth-1,i=o.toDateString()===n.toDateString(),c=o.toISOString().split("T")[0],h=this.getEventsForDate(c);r.push({date:o,dayNumber:a,isCurrentMonth:s,isToday:i,dateString:c,events:h})}return r},getEventsForDate(t){return this.events.filter(e=>e.start_date===t)},async loadEvents(){this.loading=!0;try{const t=new Date(this.currentYear,this.currentMonth-1,1).toISOString().split("T")[0],e=new Date(this.currentYear,this.currentMonth,0).toISOString().split("T")[0],r=await fetch(`/api/crm/calendar/events?start=${t}&end=${e}`);if(r.ok){const t=await r.json();this.events=t||[]}}catch(t){console.error("Erro ao carregar eventos:",t),this.showAlert("Erro ao carregar eventos","error")}finally{this.loading=!1}},previousMonth(){1===this.currentMonth?(this.currentMonth=12,this.currentYear--):this.currentMonth--,this.updateURL(),this.loadEvents()},nextMonth(){12===this.currentMonth?(this.currentMonth=1,this.currentYear++):this.currentMonth++,this.updateURL(),this.loadEvents()},goToToday(){const t=new Date;this.currentYear=t.getFullYear(),this.currentMonth=t.getMonth()+1,this.updateURL(),this.loadEvents()},changeView(t){let e;switch(t){case"month":default:e="/apps/crm/calendar";break;case"week":e="/apps/crm/calendar/week";break;case"day":e="/apps/crm/calendar/day"}window.location.href=e},updateURL(){const t=new URL(window.location);t.searchParams.set("year",this.currentYear),t.searchParams.set("month",this.currentMonth),window.history.pushState({},"",t)},showAddEventModal(t=null){this.selectedDate=t,t&&(this.newEvent.start_date=t),this.showAddModal=!0},async addEvent(){try{const t=await fetch("/api/crm/calendar/events",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(this.newEvent)});if(t.ok)this.showAlert("Evento criado com sucesso","success"),this.showAddModal=!1,this.resetNewEvent(),this.loadEvents();else{const e=await t.json();this.showAlert(e.message||"Erro ao criar evento","error")}}catch(t){console.error("Erro ao criar evento:",t),this.showAlert("Erro ao criar evento","error")}},resetNewEvent(){this.newEvent={title:"",description:"",event_type:"meeting",start_date:"",start_time:"",end_date:"",end_time:"",all_day:!1,location:"",color:"#3788d8",priority:"medium",status:"scheduled"}},showEventDetails(t){window.Swal?window.Swal.fire({title:t.title,html:`\n <div class="text-start">\n <p><strong>Descrição:</strong> ${t.description||"N/A"}</p>\n <p><strong>Tipo:</strong> ${t.event_type}</p>\n <p><strong>Data:</strong> ${t.start_date}</p>\n <p><strong>Hora:</strong> ${t.start_time||"Todo o dia"}</p>\n <p><strong>Local:</strong> ${t.location||"N/A"}</p>\n <p><strong>Prioridade:</strong> ${t.priority}</p>\n <p><strong>Status:</strong> ${t.status}</p>\n </div>\n `,icon:"info",confirmButtonText:"Editar",showCancelButton:!0,cancelButtonText:"Fechar",showDenyButton:!0,denyButtonText:"Eliminar"}).then(e=>{e.isConfirmed?this.editEvent(t.id):e.isDenied&&this.deleteEvent(t.id)}):alert(`Evento: ${t.title}\nDescrição: ${t.description||"N/A"}`)},showAlert(t,e="info"){window.Swal?window.Swal.fire({title:"success"===e?"Sucesso!":"Atenção",text:t,icon:e,confirmButtonText:"OK"}):alert(t)},getEventClass(t){const e=["calendar-event"];return"high"===t.priority?e.push("high-priority"):"medium"===t.priority?e.push("medium-priority"):"low"===t.priority&&e.push("low-priority"),e.join(" ")},async loadTodayEvents(){try{const t=await fetch("/api/crm/calendar/today");if(t.ok)return(await t.json()).events||[]}catch(t){console.error("Erro ao carregar eventos de hoje:",t)}return[]},async loadUpcomingEvents(){try{const t=await fetch("/api/crm/calendar/upcoming?limit=5");if(t.ok)return(await t.json()).events||[]}catch(t){console.error("Erro ao carregar eventos próximos:",t)}return[]},async deleteEvent(t){if((await Swal.fire({title:this.$t("common.confirm_title"),text:this.$t("common.confirm_text"),icon:"warning",showCancelButton:!0,confirmButtonColor:"#3085d6",cancelButtonColor:"#d33",confirmButtonText:this.$t("common.confirm_button"),cancelButtonText:this.$t("common.cancel"),customClass:{confirmButton:"btn btn-danger w-xs me-2 mt-2",cancelButton:"btn btn-secondary w-xs mt-2"},buttonsStyling:!1,showCloseButton:!0})).isConfirmed)try{const e=await fetch(`/api/crm/calendar/events/${t}/delete`,{method:"POST"});if(e.ok)this.showAlert("Evento eliminado com sucesso","success"),this.loadEvents();else{const t=await e.json();this.showAlert(t.message||"Erro ao eliminar evento","error")}}catch(t){console.error("Erro ao eliminar evento:",t),this.showAlert("Erro ao eliminar evento","error")}},async editEvent(t){try{const e=await fetch(`/api/crm/calendar/events/${t}`);if(e.ok){const t=await e.json();this.newEvent={...t},this.showAddModal=!0}}catch(t){console.error("Erro ao carregar evento:",t),this.showAlert("Erro ao carregar evento","error")}}}}