var e=[],t=null;window.fetchPendingApprovals=async function(){console.log(`fetchPendingApprovals called`);try{let t=await fetch(`/salary/pending-approvals`,{headers:{"X-CSRF-TOKEN":document.querySelector(`meta[name="csrf-token"]`).content,Accept:`application/json`}});if(!t.ok)throw Error(`Network response was not ok`);let n=await t.json();console.log(`Fetched pending approvals:`,n),e=n,window.renderApprovalList(),window.updatePendingCount()}catch(t){console.error(`Error fetching pending approvals:`,t),e=[],window.renderApprovalList()}},window.renderApprovalList=function(){let t=document.getElementById(`approvalListContent`);if(!t){console.error(`approvalListContent not found`);return}if(!e||e.length===0){t.innerHTML=`
            <div class="text-center py-8">
                <div class="w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-3">
                    <i class="fas fa-check-circle text-green-500 text-2xl"></i>
                </div>
                <p class="text-gray-500">No pending approvals</p>
                <p class="text-xs text-gray-400">All requests have been processed</p>
                <button onclick="window.fetchPendingApprovals()" class="mt-3 px-3 py-1 bg-purple-600 text-white rounded-lg text-sm">
                    Refresh
                </button>
            </div>
        `;return}t.innerHTML=e.map(e=>`
        <div id="approval-item-${e.id}" class="bg-amber-50 border-l-4 border-amber-500 p-4 rounded-lg transition-all duration-300">
            <div class="flex justify-between items-start">
                <div>
                    <p class="font-semibold text-gray-800">${e.type} Request #${e.reference}</p>
                    <p class="text-sm text-gray-600">Employee: ${e.employee_name}</p>
                    <p class="text-sm text-gray-600">Amount: KES ${Number(e.amount).toLocaleString()} ${e.reason?`(${e.reason})`:``}</p>
                    <p class="text-xs text-gray-500 mt-1">Submitted: ${new Date(e.created_at).toLocaleDateString()}</p>
                </div>
                <div class="flex gap-2">
                    <button onclick="window.approveRequest(${e.id}, '${e.type}')" class="px-3 py-1 bg-purple-600 text-white rounded-lg text-sm hover:bg-purple-700 transition">
                        <i class="fas fa-check mr-1"></i> Approve
                    </button>
                    <button onclick="window.rejectRequest(${e.id}, '${e.type}')" class="px-3 py-1 bg-red-600 text-white rounded-lg text-sm hover:bg-red-700 transition">
                        <i class="fas fa-times mr-1"></i> Reject
                    </button>
                </div>
            </div>
        </div>
    `).join(``)},window.updatePendingCount=function(){let t=document.getElementById(`pendingCount`),n=document.getElementById(`metricsPendingCount`);t&&(t.textContent=e.length),n&&(n.textContent=e.length)},window.approveRequest=async function(t,n){let r=document.getElementById(`approval-item-${t}`);if(r){r.classList.add(`fade-out`);try{let i=``;n===`Payment`?i=`/salary/payments/${t}/approve`:n===`Deduction`?i=`/salary/deductions/${t}/approve`:n===`Schedule`&&(i=`/salary/schedules/${t}/approve`);let a=await(await fetch(i,{method:`POST`,headers:{"X-CSRF-TOKEN":document.querySelector(`meta[name="csrf-token"]`).content,"Content-Type":`application/json`},body:JSON.stringify({})})).json();a.success?setTimeout(()=>{window.fetchPendingApprovals(),window.refreshDashboardStats(),window.showToast(`Request approved successfully!`,`success`),e.length===1&&setTimeout(()=>window.closeApprovalModal(),1500)},300):(window.showToast(`Error: `+(a.message||`Failed to approve`),`error`),r.classList.remove(`fade-out`))}catch(e){console.error(`Error approving request:`,e),window.showToast(`Network error. Please try again.`,`error`),r.classList.remove(`fade-out`)}}},window.rejectRequest=async function(t,n){let r=document.getElementById(`approval-item-${t}`);if(r){r.classList.add(`fade-out`);try{let i=``;n===`Payment`?i=`/salary/payments/${t}/reject`:n===`Deduction`?i=`/salary/deductions/${t}/reject`:n===`Schedule`&&(i=`/salary/schedules/${t}/reject`);let a=await(await fetch(i,{method:`POST`,headers:{"X-CSRF-TOKEN":document.querySelector(`meta[name="csrf-token"]`).content,"Content-Type":`application/json`},body:JSON.stringify({})})).json();a.success?setTimeout(()=>{window.fetchPendingApprovals(),window.refreshDashboardStats(),window.showToast(`Request rejected!`,`error`),e.length===1&&setTimeout(()=>window.closeApprovalModal(),1500)},300):(window.showToast(`Error: `+(a.message||`Failed to reject`),`error`),r.classList.remove(`fade-out`))}catch(e){console.error(`Error rejecting request:`,e),window.showToast(`Network error. Please try again.`,`error`),r.classList.remove(`fade-out`)}}},window.refreshDashboardStats=async function(){try{let e=await(await fetch(`/salary/pending-count`,{headers:{"X-CSRF-TOKEN":document.querySelector(`meta[name="csrf-token"]`).content}})).json(),t=document.getElementById(`pendingCount`);t&&(t.textContent=e.pending_count||0)}catch(e){console.error(`Error refreshing stats:`,e)}},window.showApprovalModal=function(){console.log(`showApprovalModal called`),window.fetchPendingApprovals();let e=document.getElementById(`approvalModal`);e&&e.classList.remove(`hidden`)},window.closeApprovalModal=function(){let e=document.getElementById(`approvalModal`);e&&e.classList.add(`hidden`)},window.showToast=function(e,t){let n=document.createElement(`div`);n.className=`fixed top-20 right-4 px-5 py-3 rounded-xl shadow-lg z-50 animate-slide-in ${t===`success`?`bg-purple-600`:`bg-red-600`} text-white`,n.innerHTML=`
        <div class="flex items-center gap-3">
            <i class="fas fa-${t===`success`?`check-circle`:`times-circle`} text-lg"></i>
            <div>
                <p class="font-semibold">${t===`success`?`Approved`:`Rejected`}</p>
                <p class="text-sm opacity-90">${e}</p>
            </div>
        </div>
    `,document.body.appendChild(n),setTimeout(()=>{n.style.opacity=`0`,n.style.transition=`opacity 0.3s`,setTimeout(()=>n.remove(),300)},3e3)};var n=[{icon:`fa-plus-circle`,text:`Create New Payment`,onclick:`window.location.href='/salary/payments/create'`,color:`bg-white/10 hover:bg-white/20`},{icon:`fa-minus-circle`,text:`Add Deduction`,onclick:`window.location.href='/salary/deductions'`,color:`bg-white/10 hover:bg-white/20`},{icon:`fa-calendar-plus`,text:`Schedule Payment`,onclick:`window.location.href='/salary/schedules'`,color:`bg-white/10 hover:bg-white/20`},{icon:`fa-chart-line`,text:`View Analytics`,onclick:`window.showAnalytics()`,color:`bg-white/10 hover:bg-white/20`},{icon:`fa-database`,text:`System Backup`,onclick:`window.showBackup()`,color:`bg-white/10 hover:bg-white/20`}],r=[{icon:`fa-chart-line`,text:`View Reports`,onclick:`window.location.href='/salary/history'`,color:`bg-white/10 hover:bg-white/20`},{icon:`fa-check-circle`,text:`Review Approvals`,onclick:`window.showApprovalModal()`,color:`bg-white/10 hover:bg-white/20`},{icon:`fa-clock`,text:`Pending Payments`,onclick:`window.location.href='/salary/payments?status=pending_approval'`,color:`bg-white/10 hover:bg-white/20`},{icon:`fa-download`,text:`Export Reports`,onclick:`window.exportReports()`,color:`bg-white/10 hover:bg-white/20`},{icon:`fa-chart-pie`,text:`Performance Metrics`,onclick:`window.showMetrics()`,color:`bg-white/10 hover:bg-white/20`}];window.renderQuickActions=function(e){let t=document.getElementById(`quickActionsContent`);t&&(t.innerHTML=(e===`Admin`?n:r).map(e=>`
        <button onclick="${e.onclick}" class="w-full text-left text-xs ${e.color} rounded-lg px-2 py-1.5 transition flex items-center gap-2">
            <i class="fas ${e.icon}"></i><span>${e.text}</span>
        </button>
    `).join(``),t.classList.add(`fade-in`),setTimeout(()=>t.classList.remove(`fade-in`),300))},window.refreshQuickActions=function(){let e=localStorage.getItem(`user_role`)||`Admin`;window.renderQuickActions(e)},window.showAnalytics=function(){alert(`Analytics dashboard with detailed payment statistics.`)},window.showBackup=function(){alert(`System backup initiated.`)},window.exportReports=function(){alert(`Exporting reports. Download will start shortly.`)},window.showMetrics=function(){let e=document.getElementById(`metricsModal`);e&&(e.classList.remove(`hidden`),setTimeout(()=>{let e=document.getElementById(`performanceChart`);e&&typeof Chart<`u`&&(t&&t.destroy(),t=new Chart(e,{type:`line`,data:{labels:[`Jan`,`Feb`,`Mar`,`Apr`,`May`,`Jun`],datasets:[{label:`Payments Processed (KES)`,data:[45e3,52e3,48e3,61e3,58e3,75e3],borderColor:`#8b5cf6`,backgroundColor:`rgba(139, 92, 246, 0.1)`,tension:.4,fill:!0}]},options:{responsive:!0,maintainAspectRatio:!0}}))},100))},window.closeMetricsModal=function(){let e=document.getElementById(`metricsModal`);e&&e.classList.add(`hidden`)},window.hasRole=function(e){return(localStorage.getItem(`user_role`)||`Admin`)===e},window.isAdmin=function(){return window.hasRole(`Admin`)},window.isDirector=function(){return window.hasRole(`Director`)},document.addEventListener(`DOMContentLoaded`,function(){if(console.log(`Salary App JS loaded successfully`),document.getElementById(`quickActionsContent`)){let e=localStorage.getItem(`user_role`)||`Admin`;window.renderQuickActions(e)}});