import{a as u}from"./chunk-2E5Q76HC.js";import{a as h,d as l}from"./chunk-TY33ZR3E.js";import{a as r}from"./chunk-OC3NYLOT.js";import{d as g}from"./chunk-UNITEU6O.js";var d="http://127.0.0.1:9000";function v(c){let i=l([]),s=l(!0),n=l(null),a=l(null);async function b(){s.value=!0,n.value=null;try{let e=await fetch(`${d}/books/`);if(!e.ok){let t=await e.json().catch(()=>({detail:`HTTP error! status: ${e.status}`}));throw new Error(t.detail||`HTTP error! status: ${e.status}`)}let o=await e.json();i.value=o}catch(e){console.error("Error fetching books:",e),n.value=e instanceof Error?e.message:"Failed to load books. Make sure the API server is running."}finally{s.value=!1}}h(()=>{b()},[c.refreshTrigger.value]);async function w(e,o){if(a.value=null,!u.value){a.value="You must be logged in to borrow a book.";return}try{let t=await fetch(`${d}/books/${e}/borrow`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${u.value}`},body:JSON.stringify({})});if(!t.ok){if(t.status===401)throw new Error("Unauthorized. Please log in again to borrow.");let p=await t.json().catch(()=>({detail:`HTTP error! status: ${t.status}`}));throw new Error(p.detail||`Failed to borrow book. Status: ${t.status}`)}c.refreshTrigger.value++}catch(t){console.error("Error borrowing book:",t),a.value=t instanceof Error?t.message:"An unexpected error occurred while borrowing the book."}}async function f(e){if(a.value=null,!u.value){a.value="You must be logged in to return a book.";return}try{let o=await fetch(`${d}/books/${e}/return`,{method:"POST",headers:{Authorization:`Bearer ${u.value}`}});if(!o.ok){if(o.status===401)throw new Error("Unauthorized. Please log in again to return.");let t=await o.json().catch(()=>({detail:`HTTP error! status: ${o.status}`}));throw new Error(t.detail||`Failed to return book. Status: ${o.status}`)}c.refreshTrigger.value++}catch(o){console.error("Error returning book:",o),a.value=o instanceof Error?o.message:"An unexpected error occurred while returning the book."}}return r(g,{children:[s.value&&r("p",{class:"text-gray-600",children:"Loading books..."}),n.value&&r("p",{class:"text-red-500 bg-red-100 p-3 rounded-md mb-4",children:["Error loading books: ",n.value]}),a.value&&r("p",{class:"text-red-500 bg-red-100 p-3 rounded-md mb-4",children:a.value}),!s.value&&!n.value&&i.value.length===0&&r("p",{class:"text-gray-600",children:"No books in the library yet."}),!s.value&&!n.value&&i.value.length>0&&r("ul",{class:"space-y-4",children:i.value.map(e=>r("li",{class:"bg-white p-4 rounded-lg shadow",children:[r("h3",{class:"text-xl font-semibold text-gray-800",children:e.title}),r("p",{class:"text-gray-600",children:["by ",e.author]}),r("p",{class:"text-sm text-gray-500",children:["ISBN: ",e.isbn]}),e.is_borrowed&&r("p",{class:"text-sm text-red-500 mt-1",children:["Borrowed by: ",e.borrower_username||"N/A"," (Due:"," ",e.due_date?new Date(e.due_date).toLocaleDateString():"N/A",")"]}),r("div",{class:"mt-2",children:e.is_borrowed?r("button",{type:"button",onClick:()=>f(e.id),class:"bg-yellow-500 hover:bg-yellow-700 text-white font-bold py-1 px-3 rounded text-sm focus:outline-none focus:shadow-outline",children:"Return Book"}):r("button",{type:"button",onClick:()=>w(e.id,e.title),class:"bg-green-500 hover:bg-green-700 text-white font-bold py-1 px-3 rounded text-sm focus:outline-none focus:shadow-outline",children:"Borrow Book"})})]},e.id))})]})}export{v as default};
