import{d as x}from"./datatables-CdFC5o1t.js";import{r as g,g as h}from"./api-DZ3cXm1m.js";import{i as w}from"./tooltip-manager-D9RaGGY4.js";import"./bootstrap.esm-Bb7dPm-H.js";import"jquery";import"./datatables-bbQliV7G.js";/* empty css                                  */document.addEventListener("DOMContentLoaded",function(){if(!document.getElementById("quotesTable"))return;const a=window.quoteIndexConfig;if(!a)return;const y={draft:"bg-secondary",sent:"bg-info",accepted:"bg-success",rejected:"bg-danger",expired:"bg-warning",converted:"bg-primary"},r=$("#quotesTable").DataTable({...x.getDefaultConfig(),serverSide:!0,processing:!0,searching:!1,dom:'<"d-flex align-items-center mb-3"<"d-flex align-items-center"l>>t<"d-flex justify-content-between align-items-center mt-3"<"d-flex align-items-center"i><"d-flex align-items-center"p>>',ajax:{url:a.routes.records,type:"GET",data:function(e){var s;const n=(s=document.getElementById("statusFilter"))==null?void 0:s.value;n&&n!=="all"&&(e.status=n);const t=document.getElementById("searchInput");t&&t.value&&(e.search={value:t.value})},beforeSend:function(e){e.setRequestHeader("Authorization",`Bearer ${h()}`)},error:async function(e){e.status===401&&await g()&&r.ajax.reload()}},columns:[{data:"quote_number",name:"quote_number",render:function(e,n,t){return`<a href="${a.routes.edit.replace(":id",t.id)}" class="text-decoration-none fw-bold">${e}</a>`}},{data:"subject",name:"subject"},{data:"client_name",name:"client_name",render:function(e){return e||"-"}},{data:"admin_name",name:"admin_name",render:function(e){return e||"-"}},{data:"status",name:"status",render:function(e){const n=y[e]||"bg-secondary",t=a.translations["status_"+e]||e;return`<span class="badge ${n}">${t}</span>`}},{data:"total_amount",name:"total_amount",render:function(e,n,t){return`${e} ${t.currency||""}`.trim()}},{data:"valid_until",name:"valid_until",render:function(e){return e||"-"}},{data:"created_at",name:"created_at",render:function(e){return e?`<small>${e}</small>`:"-"}},{data:"id",name:"actions",orderable:!1,searchable:!1,className:"text-end",render:function(e,n,t){var m,p,b;const s=a.routes.edit.replace(":id",e),o=a.routes.downloadPdf.replace(":id",e),f=a.routes.destroy.replace(":id",e),l=((m=window.pageConfig)==null?void 0:m.permissions)||{},i=[];return l.canEdit&&i.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="${((b=(p=window.translations)==null?void 0:p.buttons)==null?void 0:b.edit)||"Edit"}">
                            <i class="fas fa-edit"></i>
                        </a>`),l.canDownloadPdf&&i.push(`<a href="${o}"
                           class="btn btn-sm btn-outline-secondary d-inline-flex align-items-center justify-content-center ms-1"
                           style="width: 32px; height: 32px;"
                           target="_blank"
                           data-bs-toggle="tooltip"
                           title="${a.translations.btn_download_pdf||"Download PDF"}">
                            <i class="fas fa-file-pdf"></i>
                        </a>`),l.canDelete&&t.status==="draft"&&i.push(`<button type="button"
                                class="btn btn-sm btn-outline-danger d-inline-flex align-items-center justify-content-center ms-1 delete-quote"
                                style="width: 32px; height: 32px;"
                                data-id="${e}"
                                data-url="${f}"
                                data-bs-toggle="tooltip"
                                title="${a.translations.btn_delete||"Delete"}">
                            <i class="fas fa-trash"></i>
                        </button>`),i.length===0?'<span class="text-muted">-</span>':`<div class="d-inline-flex justify-content-end">${i.join("")}</div>`}}],drawCallback:function(){w()}}),d=document.getElementById("statusFilter");d&&d.addEventListener("change",function(){r.ajax.reload()});let c=null;const u=document.getElementById("searchInput");u&&u.addEventListener("keyup",function(){clearTimeout(c),c=setTimeout(()=>{r.ajax.reload()},500)}),document.body.addEventListener("click",async function(e){const n=e.target.closest(".delete-quote");if(!n||!confirm(a.translations.confirm_delete))return;const t=n.getAttribute("data-url");try{const s=await fetch(t,{method:"DELETE",headers:{"Content-Type":"application/json",Authorization:`Bearer ${h()}`}});if(s.status===401){const o=await g();o&&(await fetch(t,{method:"DELETE",headers:{"Content-Type":"application/json",Authorization:`Bearer ${o}`}})).ok&&r.ajax.reload(null,!1);return}s.ok&&r.ajax.reload(null,!1)}catch(s){console.error("Delete error:",s)}})});
