import{M as p}from"./bootstrap.esm-Bb7dPm-H.js";import{d as g}from"./datatables-I6jthWPb.js";import{i as h}from"./tooltip-manager-D9RaGGY4.js";import"./datatables-Bo82YvhT.js";/* empty css                                  */const{permissions:f,routes:d,translations:m}=window;let a;document.addEventListener("DOMContentLoaded",function(){y(),b(),h()});function y(){a=$("#serviceCostsTable").DataTable({...g.getDefaultConfig(),serverSide:!0,searching:!1,ajax:{url:d.serviceCosts.records,type:"GET",headers:{Authorization:"Bearer "+document.querySelector('meta[name="api-token"]').getAttribute("content")},data:function(e){e.product_id=$("#productFilter").val()}},columns:[{data:"product",name:"product",render:function(e,n,i){return n==="display"?e?`<strong>${e}</strong>`:"-":e}},{data:"group",name:"group",render:function(e,n,i){return n==="display"?e?`<span class="badge bg-secondary">${e}</span>`:'<span class="text-muted">-</span>':e}},{data:"periods",name:"periods",render:function(e,n,i){return n==="display"?!Array.isArray(e)||e.length===0?'<span class="text-muted">-</span>':e.map(t=>`<span class="badge bg-info me-1">${t} ay</span>`).join(""):e}},{data:"id",name:"actions",orderable:!1,searchable:!1,render:function(e,n,i){var t,c,l,o;if(n==="display"){const r=[];if((t=f.serviceCosts)!=null&&t.update){const s=d.serviceCosts.edit.replace(":id",e);r.push(`
                                <a href="${s}"
                                   class="btn btn-sm btn-outline-primary d-inline-flex align-items-center justify-content-center"
                                   style="width: 32px; height: 32px;"
                                   data-bs-toggle="tooltip"
                                   title="${((c=m.buttons)==null?void 0:c.edit)||"Edit"}">
                                    <i class="fas fa-edit"></i>
                                </a>`)}return(l=f.serviceCosts)!=null&&l.delete&&r.push(`
                                <button type="button"
                                        class="btn btn-sm btn-outline-danger d-inline-flex align-items-center justify-content-center ms-1 delete-service-cost"
                                        style="width: 32px; height: 32px;"
                                        data-id="${e}"
                                        data-bs-toggle="tooltip"
                                        title="${((o=m.buttons)==null?void 0:o.delete)||"Delete"}">
                                    <i class="fas fa-trash"></i>
                                </button>`),r.length===0?'<span class="text-muted">-</span>':`<div class="d-inline-flex">${r.join("")}</div>`}return e}}],drawCallback:function(){h()}})}function b(){$("#productFilter").on("change",function(){a.draw()}),$("#clearFilters").on("click",function(){$("#productFilter").val(""),a.draw()}),$("#createServiceCostForm").on("submit",function(e){e.preventDefault(),v()}),$(document).on("click",".delete-service-cost",function(){const e=$(this).data("id");w(e)})}function v(){var c,l;const e=document.getElementById("createServiceCostForm"),n={};[1,3,6,12,24,36].forEach(o=>{var s,u;const r=(u=(s=e.querySelector(`[name="periods[${o}]"]`))==null?void 0:s.value)==null?void 0:u.trim();r&&(n[o]=parseFloat(r.replace(",",".")))});const i={product_id:parseInt(e.querySelector('[name="product_id"]').value,10),currency_id:parseInt(e.querySelector('[name="currency_id"]').value,10),is_active:e.querySelector('[name="is_active"]').checked?1:0,periods:n},t=(c=document.querySelector('meta[name="api-token"]'))==null?void 0:c.getAttribute("content");fetch(d.serviceCosts.store,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json","X-CSRF-TOKEN":((l=document.querySelector('meta[name="csrf-token"]'))==null?void 0:l.getAttribute("content"))||"",...t?{Authorization:`Bearer ${t}`}:{}},credentials:"same-origin",body:JSON.stringify(i)}).then(async o=>{const r=await o.json().catch(()=>({}));if(o.ok&&r.success){showSuccess(r.message);const s=document.getElementById("createServiceCostModal");(p.getInstance(s)||new p(s)).hide(),e.reset(),a&&a.draw()}else{const s=r.message||(r.errors?Object.values(r.errors).flat().join(`
`):"An error occurred");showError(s)}}).catch(o=>{console.error("Error:",o),showError("An error occurred. Please try again.")})}async function w(e){var i;await window.showConfirm(((i=m.confirmations)==null?void 0:i.delete_message)||"Are you sure you want to delete this cost?",{confirmClass:"btn-danger",icon:"warning"})&&fetch(d.serviceCosts.delete.replace(":id",e),{method:"DELETE",headers:{Authorization:"Bearer "+document.querySelector('meta[name="api-token"]').getAttribute("content")}}).then(t=>t.json()).then(t=>{t.success?(showSuccess(t.message),a.draw()):showError(t.message)}).catch(t=>{console.error("Error:",t),showError("An error occurred. Please try again.")})}
