import{a as s}from"./chunk-2E5Q76HC.js";import{d as u}from"./chunk-TY33ZR3E.js";import{a as e}from"./chunk-OC3NYLOT.js";import"./chunk-UNITEU6O.js";function p({refreshTrigger:i,API_BASE_URL:d}){let a=u(null),l=u(""),n=u(""),r=u("");async function c(o){if(o.preventDefault(),a.value=null,!l.value.trim()||!n.value.trim()||!r.value.trim()){a.value="All fields (Title, Author, ISBN) are required.";return}if(!s.value){a.value="You must be logged in to add a book.";return}try{let t=await fetch(`${d}/books/`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${s.value}`},body:JSON.stringify({title:l.value,author:n.value,isbn:r.value})});if(!t.ok){if(t.status===401)throw new Error("Unauthorized. Please log in again.");let b=await t.json().catch(()=>({detail:`HTTP error! status: ${t.status}`}));throw new Error(b.detail||`Failed to add book. Status: ${t.status}`)}l.value="",n.value="",r.value="",i.value++}catch(t){console.error("Error adding book:",t),a.value=t instanceof Error?t.message:"An unexpected error occurred while adding the book."}}return e("form",{onSubmit:c,class:"w-full bg-white p-6 rounded-lg shadow-md my-8",children:[e("h2",{class:"text-2xl font-semibold mb-4 text-gray-700",children:"Add New Book"}),a.value&&e("p",{class:"text-red-500 text-sm mb-3",children:a.value}),e("div",{class:"mb-4",children:[e("label",{for:"title",class:"block text-gray-700 text-sm font-bold mb-2",children:"Title:"}),e("input",{type:"text",id:"title",value:l.value,onInput:o=>l.value=o.target.value,class:"shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"})]}),e("div",{class:"mb-4",children:[e("label",{for:"author",class:"block text-gray-700 text-sm font-bold mb-2",children:"Author:"}),e("input",{type:"text",id:"author",value:n.value,onInput:o=>n.value=o.target.value,class:"shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"})]}),e("div",{class:"mb-6",children:[e("label",{for:"isbn",class:"block text-gray-700 text-sm font-bold mb-2",children:"ISBN:"}),e("input",{type:"text",id:"isbn",value:r.value,onInput:o=>r.value=o.target.value,class:"shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"})]}),e("button",{type:"submit",class:"bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline",children:"Add Book"})]})}export{p as default};
