import{M as H}from"./bootstrap.esm-Bb7dPm-H.js";import{d as M}from"./datatables-CdFC5o1t.js";import{r as v,g as y}from"./api-DZ3cXm1m.js";import{i as z}from"./tooltip-manager-D9RaGGY4.js";import"jquery";import"./datatables-bbQliV7G.js";/* empty css                                  */let f,u={};document.addEventListener("DOMContentLoaded",function(){F(),N(),_()});function F(){const e=document.getElementById("basketsTable");if(!e)return;const t=e.getAttribute("data-records-url"),l=window.routes||{},i=window.translations||{};f=$(e).DataTable({...M.getDefaultConfig(),serverSide:!0,searching:!0,dom:'<"d-flex justify-content-between align-items-center mb-3"<"d-flex align-items-center"l><"d-flex align-items-center"f>>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:t,type:"GET",beforeSend:function(s){s.setRequestHeader("Authorization",`Bearer ${y()}`)},data:function(s){const n=document.getElementById("statusFilter"),a=document.getElementById("searchInput");n&&n.value&&(s.status=n.value),a&&a.value&&(s.search=a.value)},error:async function(s){s.status===401&&await v()&&f.ajax.reload()}},columns:[{data:"visitor_id",name:"visitor_id",render:function(s,n,a){return`<span class="font-monospace small" title="${a.visitor_id_full||s}">${s}</span>`}},{data:"ip_address",name:"ip_address",render:function(s){return`<span class="font-monospace small">${s}</span>`}},{data:"location",name:"location",render:function(s){return!s||s==="-"?'<span class="text-muted">-</span>':`<span class="small">${s}</span>`}},{data:"client_name",name:"client_name",render:function(s,n,a){return a.client_id?`<a href="${l.clients_overview?l.clients_overview.replace(":id",a.client_id):"#"}" class="text-decoration-none small">${s}</a>`:`<span class="text-muted small">${i.baskets_anonymous||"Anonymous"}</span>`}},{data:"item_count",name:"item_count",className:"text-center",render:function(s){return`<span class="badge ${s>0?"bg-primary":"bg-secondary"}">${s}</span>`}},{data:"total",name:"total",className:"text-end",render:function(s,n,a){const o=a.currency||"TRY";return`<span class="fw-medium">${s} ${o}</span>`}},{data:"status",name:"status",render:function(s,n,a){let o="";s==="active"?o=`<span class="badge bg-success">${i.baskets_status_active||"Active"}</span>`:o=`<span class="badge bg-secondary">${i.baskets_status_empty||"Empty"}</span>`;let r="";a.source==="redis"?r=`<span class="badge bg-info ms-1">${i.baskets_source_redis||"Redis"}</span>`:a.source==="mysql"&&(r=`<span class="badge bg-warning ms-1">${i.baskets_source_mysql||"MySQL"}</span>`);let c="";return a.coupon_code&&(c=`<span class="badge bg-purple ms-1" title="Coupon: ${a.coupon_code}"><i class="fas fa-tag"></i></span>`),o+r+c}},{data:"last_seen",name:"last_seen",render:function(s){return!s||s==="-"?'<span class="text-muted">-</span>':`<span class="small">${s}</span>`}},{data:null,name:"actions",orderable:!1,searchable:!1,className:"text-end",render:function(s,n,a){var c,m,p;const o=[],r=((c=window.permissions)==null?void 0:c.baskets)||{};return r.view&&o.push(`
                            <button type="button"
                                    class="btn btn-sm btn-outline-info d-inline-flex align-items-center justify-content-center view-basket"
                                    style="width: 32px; height: 32px;"
                                    data-visitor-id="${a.visitor_id_full||a.visitor_id}"
                                    data-source="${a.source}"
                                    data-bs-toggle="tooltip"
                                    title="${i.baskets_view||"View"}">
                                <i class="fas fa-eye"></i>
                            </button>`),(r.delete||(p=(m=window.pageConfig)==null?void 0:m.permissions)!=null&&p.canDelete)&&o.push(`
                            <button type="button"
                                    class="btn btn-sm btn-outline-danger d-inline-flex align-items-center justify-content-center ms-1 delete-basket"
                                    style="width: 32px; height: 32px;"
                                    data-visitor-id="${a.visitor_id_full||a.visitor_id}"
                                    data-source="${a.source}"
                                    data-mysql-id="${a.mysql_id||""}"
                                    data-bs-toggle="tooltip"
                                    title="${i.baskets_delete||"Delete"}">
                                <i class="fas fa-trash"></i>
                            </button>`),`<div class="d-inline-flex">${o.join("")}</div>`}}],order:[[7,"desc"]],drawCallback:function(){z()}})}function N(){var t,l,i;(t=document.getElementById("statusFilter"))==null||t.addEventListener("change",b);let e;(l=document.getElementById("searchInput"))==null||l.addEventListener("input",function(){clearTimeout(e),e=setTimeout(b,500)}),(i=document.getElementById("refreshBtn"))==null||i.addEventListener("click",function(){b(),_()}),document.addEventListener("click",function(s){if(s.target.closest(".view-basket")){const n=s.target.closest(".view-basket"),a=n.getAttribute("data-visitor-id"),o=n.getAttribute("data-source");C(a,o)}if(s.target.closest(".delete-basket")){const n=s.target.closest(".delete-basket"),a=n.getAttribute("data-visitor-id"),o=n.getAttribute("data-source"),r=n.getAttribute("data-mysql-id");j(a,o,r)}})}function b(){f&&f.ajax.reload()}async function _(){const e=window.routes||{};if(e.baskets_statistics)try{const t=await fetch(e.baskets_statistics,{headers:{Authorization:`Bearer ${y()}`}});if(t.status===401)return await v()?_():void 0;t.ok&&(u=await t.json(),R())}catch(t){console.error("Error loading statistics:",t)}}function R(){const e=document.getElementById("totalBaskets");e&&(e.textContent=u.total||0);const t=document.getElementById("activeBaskets");t&&(t.textContent=u.active||0);const l=document.getElementById("redisBaskets");l&&(l.textContent=u.redis_total||0);const i=document.getElementById("anonymousBaskets");i&&(i.textContent=u.anonymous||0);const s=document.getElementById("clientBaskets");s&&(s.textContent=u.client||0);const n=document.getElementById("mysqlBaskets");n&&(n.textContent=u.mysql_total||0)}async function C(e,t){const l=new H(document.getElementById("basketDetailModal")),i=document.getElementById("basketDetailContent"),s=window.routes||{},n=window.translations||{};i.innerHTML=`
        <div class="text-center py-4">
            <div class="spinner-border text-primary" role="status">
                <span class="visually-hidden">Loading...</span>
            </div>
        </div>
    `,l.show();try{const a=s.baskets_details.replace(":visitorId",e)+`?source=${t}`,o=await fetch(a,{headers:{Authorization:`Bearer ${y()}`,"Content-Type":"application/json","X-Requested-With":"XMLHttpRequest"}});if(o.status===401){if(await v())return C(e,t);i.innerHTML='<div class="alert alert-danger">Authentication error</div>';return}if(!o.ok){const c=await o.json();i.innerHTML=`<div class="alert alert-danger">${c.message||"Error loading basket details"}</div>`;return}const r=await o.json();r.success?i.innerHTML=S(r.data,n):i.innerHTML=`<div class="alert alert-warning">${r.message||"Basket not found"}</div>`}catch(a){console.error("Error loading basket details:",a),i.innerHTML='<div class="alert alert-danger">Error loading basket details</div>'}}function S(e,t){var o,r,c,m,p,h,k,x,E,T;const l=t.common_not_available||"-";let i=`
        <div class="card border-0 shadow-sm mb-3">
            <div class="card-header border-0 py-2">
                <h6 class="mb-0">
                    <i class="fas fa-user me-2 text-primary"></i>
                    ${t.baskets_detail_visitor_info||"Visitor Information"}
                </h6>
            </div>
            <div class="card-body py-2">
                <div class="row g-2 small">
                    <div class="col-md-6">
                        <div class="d-flex justify-content-between">
                            <span class="text-muted">${t.baskets_detail_ip_address||"IP Address"}:</span>
                            <span class="font-monospace">${((o=e.visitor)==null?void 0:o.ip_address)||l}</span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="d-flex justify-content-between">
                            <span class="text-muted">${t.baskets_detail_country||"Country"}:</span>
                            <span>${((r=e.visitor)==null?void 0:r.country)||l}</span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="d-flex justify-content-between">
                            <span class="text-muted">${t.baskets_detail_city||"City"}:</span>
                            <span>${((c=e.visitor)==null?void 0:c.city)||l}</span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="d-flex justify-content-between">
                            <span class="text-muted">${t.baskets_detail_timezone||"Timezone"}:</span>
                            <span>${((m=e.visitor)==null?void 0:m.timezone)||l}</span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="d-flex justify-content-between">
                            <span class="text-muted">${t.baskets_detail_browser_language||"Browser Language"}:</span>
                            <span>${((p=e.visitor)==null?void 0:p.browser_language)||l}</span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="d-flex justify-content-between">
                            <span class="text-muted">${t.baskets_detail_preferred_currency||"Currency"}:</span>
                            <span>${((h=e.visitor)==null?void 0:h.preferred_currency)||e.currency||l}</span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="d-flex justify-content-between">
                            <span class="text-muted">${t.baskets_detail_created_at||"Created"}:</span>
                            <span>${B((k=e.visitor)==null?void 0:k.created_at)}</span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="d-flex justify-content-between">
                            <span class="text-muted">${t.baskets_detail_last_seen||"Last Seen"}:</span>
                            <span>${B((x=e.visitor)==null?void 0:x.last_seen_at)}</span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="d-flex justify-content-between">
                            <span class="text-muted">${t.baskets_detail_source||"Source"}:</span>
                            <span class="badge ${e.source==="redis"?"bg-info":"bg-warning"}">${e.source.toUpperCase()}</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    `,s="";e.client?s=`
            <div class="card border-0 shadow-sm mb-3">
                <div class="card-header border-0 py-2">
                    <h6 class="mb-0">
                        <i class="fas fa-id-card me-2 text-success"></i>
                        ${t.baskets_detail_client_info||"Client Information"}
                    </h6>
                </div>
                <div class="card-body py-2">
                    <div class="row g-2 small">
                        <div class="col-md-6">
                            <div class="d-flex justify-content-between">
                                <span class="text-muted">${t.baskets_detail_name||"Name"}:</span>
                                <a href="${((T=(E=window.routes)==null?void 0:E.clients_overview)==null?void 0:T.replace(":id",e.client.id))||"#"}" class="text-decoration-none">${e.client.name}</a>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="d-flex justify-content-between">
                                <span class="text-muted">${t.baskets_detail_email||"Email"}:</span>
                                <span>${e.client.email}</span>
                            </div>
                        </div>
                        ${e.client.company?`
                        <div class="col-md-12">
                            <div class="d-flex justify-content-between">
                                <span class="text-muted">${t.baskets_detail_company||"Company"}:</span>
                                <span>${e.client.company}</span>
                            </div>
                        </div>
                        `:""}
                    </div>
                </div>
            </div>
        `:s=`
            <div class="card border-0 shadow-sm mb-3">
                <div class="card-header border-0 py-2">
                    <h6 class="mb-0">
                        <i class="fas fa-id-card me-2 text-muted"></i>
                        ${t.baskets_detail_client_info||"Client Information"}
                    </h6>
                </div>
                <div class="card-body py-2">
                    <p class="text-muted small mb-0">
                        <i class="fas fa-user-secret me-1"></i>
                        ${t.baskets_detail_no_client||"No client linked"}
                    </p>
                </div>
            </div>
        `;let n="";if(e.items&&e.items.length>0){const I=e.items.map((d,L)=>{const A=d.type==="domain"?"fa-globe":"fa-box",q=d.type==="domain"?`<span class="badge bg-primary">${t.baskets_detail_type_domain||"Domain"}</span>`:`<span class="badge bg-secondary">${t.baskets_detail_type_product||"Product"}</span>`;let g=d.name||"Unknown";d.domain_name&&(g+=`<br><small class="text-muted">${d.domain_name}</small>`,d.domain_action&&(g+=`<br><small class="text-info">${d.domain_action}</small>`));const D=(parseFloat(d.price)*parseInt(d.quantity||1)).toFixed(2);return`
                <tr>
                    <td>${L+1}</td>
                    <td>${q}</td>
                    <td>
                        <i class="fas ${A} me-1 text-muted"></i>
                        ${g}
                    </td>
                    <td class="text-end">${parseFloat(d.price).toFixed(2)} ${d.currency||e.currency}</td>
                    <td class="text-center">${d.quantity||1}</td>
                    <td class="text-center">${d.period_text||d.period||"-"}</td>
                    <td class="text-end fw-medium">${D} ${d.currency||e.currency}</td>
                </tr>
            `}).join("");n=`
            <div class="card border-0 shadow-sm mb-3">
                <div class="card-header border-0 py-2">
                    <h6 class="mb-0">
                        <i class="fas fa-shopping-basket me-2 text-warning"></i>
                        ${t.baskets_detail_basket_items||"Basket Items"}
                        <span class="badge bg-primary ms-2">${e.item_count}</span>
                    </h6>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-sm table-hover mb-0 small">
                            <thead>
                                <tr>
                                    <th style="width: 40px;">#</th>
                                    <th style="width: 80px;">Type</th>
                                    <th>${t.baskets_detail_product||"Product"}</th>
                                    <th class="text-end" style="width: 100px;">${t.baskets_detail_price||"Price"}</th>
                                    <th class="text-center" style="width: 60px;">${t.baskets_detail_quantity||"Qty"}</th>
                                    <th class="text-center" style="width: 80px;">${t.baskets_detail_period||"Period"}</th>
                                    <th class="text-end" style="width: 110px;">${t.baskets_detail_subtotal||"Subtotal"}</th>
                                </tr>
                            </thead>
                            <tbody>
                                ${I}
                            </tbody>
                            <tfoot>
                                <tr class="table-light">
                                    <td colspan="6" class="text-end fw-bold">${t.baskets_detail_total||"Total"}:</td>
                                    <td class="text-end fw-bold">${parseFloat(e.total).toFixed(2)} ${e.currency}</td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        `}else n=`
            <div class="card border-0 shadow-sm mb-3">
                <div class="card-header border-0 py-2">
                    <h6 class="mb-0">
                        <i class="fas fa-shopping-basket me-2 text-muted"></i>
                        ${t.baskets_detail_basket_items||"Basket Items"}
                    </h6>
                </div>
                <div class="card-body py-3 text-center">
                    <i class="fas fa-shopping-cart fa-2x text-muted mb-2"></i>
                    <p class="text-muted mb-0">${t.baskets_detail_no_items||"No items in basket"}</p>
                </div>
            </div>
        `;let a="";return e.coupon_code&&(a=`
            <div class="alert alert-success d-flex align-items-center py-2 mb-0" role="alert">
                <i class="fas fa-tag me-2"></i>
                <div>
                    <strong>${t.baskets_detail_coupon||"Coupon Code"}:</strong>
                    <span class="font-monospace ms-2">${e.coupon_code}</span>
                </div>
            </div>
        `),i+s+n+a}function B(e){if(!e)return"-";try{return new Date(e).toLocaleString()}catch{return e}}async function j(e,t,l){const i=window.translations||{},s=window.routes||{};if(await window.showConfirm(i.baskets_confirm_delete||"Are you sure you want to delete this basket?",{confirmClass:"btn-danger",icon:"warning"}))try{const a=t==="mysql"&&l?l:e,o=s.baskets_delete.replace(":id",a),r=await fetch(o,{method:"DELETE",headers:{Authorization:`Bearer ${y()}`,"Content-Type":"application/json","X-Requested-With":"XMLHttpRequest"},body:JSON.stringify({source:t,visitor_id:e})});if(r.status===401)return await v()?j(e,t,l):void 0;if(r.ok){const c=await r.json();w("success",c.message||"Basket deleted successfully"),b(),_()}else w("error","Error deleting basket")}catch(a){console.error("Error deleting basket:",a),w("error","Error deleting basket")}}function w(e,t){window.showToast?window.showToast(e,t):console.warn("Notification system not available:",e,t)}
