import{u as M,f as N,g as O,r as a,d as u,j as e,L as U,b as f,S as G,H as K,M as _,l as V,B as Y,n as Q}from"./index-Cn0iwDxV.js";import X from"./LocationPage-CJsPwtKT.js";import{C as Z}from"./chevron-left-CS5gTWBq.js";import{H as ee}from"./house-xtq0GOBd.js";function b(i){const n=i.trim().split(/\s+/);return n.length<=1?n[0]??"":`${n[0]} ${n[n.length-1][0]}.`}function w(i){return i.toLowerCase().split("_").map(n=>n.charAt(0).toUpperCase()+n.slice(1)).join(" ")}function re(i){return i?new Date(i).toLocaleDateString("en-GB",{day:"numeric",month:"short",year:"numeric"}):"Rolling"}function oe(){const{id:i}=M(),n=!!(i&&N(i)),{user:j,token:d}=O(),[t,B]=a.useState(null),[P,R]=a.useState(!0),[S,W]=a.useState(""),[c,v]=a.useState(!1),[D,z]=a.useState(!1),[o,m]=a.useState({name:"",email:"",phone:"",message:""}),[l,p]=a.useState({}),[y,k]=a.useState(!1),[E,C]=a.useState(""),[I,A]=a.useState(!1),[T,q]=a.useState(null);if(a.useEffect(()=>{if(!i||n)return;let s=!1;return u.get(`/jobs/${i}`).then(r=>{s||(B(r),m(g=>({...g,name:j?.name??g.name,email:j?.email??g.email})))}).catch(r=>{s||W(r instanceof Error?r.message:"Failed to load job.")}).finally(()=>{s||R(!1)}),()=>{s=!0}},[i,j,n]),a.useEffect(()=>{!i||!d||n||u.get("/users/me/saved-jobs",d).then(s=>v(s.some(r=>r.id===i))).catch(()=>{})},[i,d,n]),n)return e.jsx(X,{categorySlugProp:"jobs",countySlugProp:i});const H=async()=>{if(!(!i||!d||D)){z(!0);try{c?(await u.delete(`/users/me/saved-jobs/${i}`,d),v(!1)):(await u.post(`/users/me/saved-jobs/${i}`,{},d),v(!0))}catch{}finally{z(!1)}}},L=async s=>{if(s.preventDefault(),!i)return;const r={};o.name.trim()||(r.name="Please enter your name."),o.email.trim()?/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(o.email)||(r.email="Please enter a valid email address."):r.email="Please enter your email address.",o.message.trim()||(r.message="Please enter a message.");const g=/^(?:\+44|0)\d{9,10}$/;if(o.phone&&!g.test(o.phone.replace(/\s/g,""))&&(r.phone="Please enter a valid UK phone number."),Object.keys(r).length>0){p(r);return}p({}),C(""),k(!0);try{const x=await u.post(`/jobs/${i}/enquiry`,{senderName:o.name,senderEmail:o.email,senderPhone:o.phone||void 0,message:o.message},d??void 0);q(x.enquiryId??null),A(!0),m(J=>({...J,message:""}))}catch(x){C(x instanceof Error?x.message:"Failed to send enquiry.")}finally{k(!1)}};if(P)return e.jsxs("main",{style:{backgroundColor:"var(--cream)",minHeight:"80vh",display:"flex",justifyContent:"center",alignItems:"center"},children:[e.jsx(U,{size:34,color:"var(--green-800)",style:{animation:"spin 1s linear infinite"}}),e.jsx("style",{children:"@keyframes spin { to { transform: rotate(360deg); } }"})]});if(S||!t)return e.jsx("main",{style:{backgroundColor:"var(--cream)",minHeight:"80vh",display:"flex",justifyContent:"center",alignItems:"center"},children:e.jsxs("div",{style:{textAlign:"center",padding:"40px 24px"},children:[e.jsx("p",{style:{color:"#dc2626",fontWeight:600,marginBottom:14},children:S||"Job not found."}),e.jsx(f,{to:"/jobs",style:{color:"var(--green-800)",fontWeight:700},children:"Back to Jobs"})]})});const $=t.description.slice(0,155).replace(/\n/g," ").trim(),F={"@context":"https://schema.org","@type":"JobPosting",title:t.title,description:t.description,datePosted:t.createdAt,validThrough:t.closingDate??void 0,employmentType:t.jobType==="PERMANENT"?"FULL_TIME":"TEMPORARY",hiringOrganization:{"@type":"Organization",name:t.user.farmName??b(t.user.name)},jobLocation:t.county?{"@type":"Place",address:{"@type":"PostalAddress",addressRegion:t.county,addressCountry:"GB"}}:void 0,baseSalary:t.wageSalary?{"@type":"MonetaryAmount",currency:"GBP",value:{"@type":"QuantitativeValue",description:t.wageSalary}}:void 0},h=s=>({border:`1.5px solid ${s?"#dc2626":"var(--border)"}`,borderRadius:8,padding:"12px 14px",fontSize:"0.92rem",outline:"none",minHeight:48,width:"100%",boxSizing:"border-box"});return e.jsxs("main",{style:{backgroundColor:"var(--cream)",minHeight:"80vh",padding:"30px 24px 56px"},children:[e.jsx(G,{title:t.title,description:$,url:`/jobs/${t.id}`,jsonLd:F}),e.jsxs("div",{style:{maxWidth:980,margin:"0 auto"},children:[e.jsxs(f,{to:"/jobs",style:{display:"inline-flex",alignItems:"center",gap:6,color:"var(--green-700)",fontWeight:700,marginBottom:16},children:[e.jsx(Z,{size:16})," Back to Jobs"]}),e.jsxs("div",{className:"listing-detail-grid",style:{display:"grid",gridTemplateColumns:"1fr 340px",gap:20,alignItems:"start"},children:[e.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:16},children:[e.jsxs("section",{style:{backgroundColor:"white",border:"1.5px solid var(--border)",borderRadius:14,padding:"24px 24px 22px"},children:[e.jsxs("div",{style:{display:"flex",justifyContent:"space-between",gap:12,flexWrap:"wrap",marginBottom:10},children:[e.jsxs("div",{children:[e.jsxs("div",{style:{display:"flex",gap:8,flexWrap:"wrap",marginBottom:8},children:[e.jsx("span",{style:{backgroundColor:"#e8f0fa",color:"#1a4d7c",fontSize:"0.72rem",fontWeight:700,padding:"3px 9px",borderRadius:20},children:w(t.farmType)}),e.jsx("span",{style:{color:"var(--green-700)",border:"1.5px solid var(--green-700)",fontSize:"0.72rem",fontWeight:700,padding:"3px 9px",borderRadius:20},children:w(t.jobType)}),t.accommodationIncluded&&e.jsxs("span",{style:{backgroundColor:"#e8f5ec",color:"#1a4a2a",fontSize:"0.72rem",fontWeight:700,padding:"3px 9px",borderRadius:20,display:"inline-flex",alignItems:"center",gap:4},children:[e.jsx(ee,{size:11})," Accommodation"]})]}),e.jsx("h1",{style:{fontSize:"clamp(1.35rem, 2.7vw, 1.9rem)",fontWeight:900,color:"var(--ink)",lineHeight:1.25},children:t.title}),e.jsx("p",{style:{fontSize:"0.9rem",color:"var(--ink-muted)",marginTop:6},children:t.user.farmName??b(t.user.name)})]}),e.jsxs("div",{style:{textAlign:"right",display:"flex",flexDirection:"column",alignItems:"flex-end",gap:10},children:[e.jsx("div",{style:{fontSize:"1.18rem",fontWeight:900,color:"var(--green-800)"},children:t.wageSalary??"Salary/rate not specified"}),d&&e.jsxs("button",{onClick:()=>{H()},"aria-label":c?"Remove from saved":"Save job",style:{display:"flex",alignItems:"center",gap:6,padding:"8px 14px",borderRadius:8,border:"1.5px solid var(--border)",backgroundColor:c?"#fff0f3":"white",cursor:"pointer",fontSize:"0.82rem",fontWeight:600,color:c?"#e53e3e":"var(--ink-muted)"},children:[e.jsx(K,{size:15,fill:c?"#e53e3e":"none",color:c?"#e53e3e":"var(--ink-muted)"}),c?"Saved":"Save Job"]})]})]}),e.jsxs("div",{style:{display:"flex",gap:18,flexWrap:"wrap",fontSize:"0.84rem",color:"var(--ink-muted)"},children:[e.jsxs("span",{style:{display:"inline-flex",alignItems:"center",gap:5},children:[e.jsx(_,{size:13})," ",t.locationText??t.county??"UK"]}),e.jsxs("span",{style:{display:"inline-flex",alignItems:"center",gap:5},children:[e.jsx(V,{size:13})," Closing date: ",re(t.closingDate)]})]})]}),e.jsxs("section",{style:{backgroundColor:"white",border:"1.5px solid var(--border)",borderRadius:14,padding:"24px"},children:[e.jsx("h2",{style:{fontSize:"1.02rem",fontWeight:800,marginBottom:14,color:"var(--ink)"},children:"Job Description"}),e.jsx("p",{style:{color:"var(--ink-light)",lineHeight:1.75,whiteSpace:"pre-line"},children:t.description}),e.jsxs("div",{style:{marginTop:22,borderTop:"1px solid var(--border)",paddingTop:16},children:[e.jsxs("div",{style:{display:"flex",alignItems:"center",gap:8,marginBottom:10},children:[e.jsx(Y,{size:16,color:"var(--green-700)"}),e.jsx("span",{style:{fontWeight:700,color:"var(--ink)"},children:"Employer"})]}),e.jsxs("p",{style:{color:"var(--ink-light)",fontSize:"0.92rem"},children:[b(t.user.name),t.user.farmName?` (${t.user.farmName})`:""]})]})]})]}),e.jsx("div",{children:e.jsxs("div",{style:{backgroundColor:"white",borderRadius:14,border:"1.5px solid var(--border)",padding:"24px",position:"sticky",top:88},children:[e.jsx("h3",{style:{fontWeight:800,fontSize:"1rem",color:"var(--ink)",marginBottom:4},children:"Apply / Enquire"}),e.jsx("p",{style:{fontSize:"0.82rem",color:"var(--ink-muted)",marginBottom:18},children:d?"Message the employer directly. Replies appear in your Messages.":"Send a message to the employer. They'll reply to your email address."}),I?e.jsxs("div",{style:{background:"#D1FAE5",border:"1px solid #10B981",borderRadius:8,padding:"20px",textAlign:"center"},children:[e.jsx("p",{style:{color:"#065F46",fontWeight:600,marginBottom:4},children:"Application sent successfully!"}),e.jsx("p",{style:{color:"#065F46",fontSize:"0.9rem",marginBottom:10},children:"The employer will be notified and can reply via their dashboard."}),d?e.jsx(f,{to:`/dashboard?tab=enquiries&enquiry=${T??""}`,style:{display:"inline-flex",alignItems:"center",gap:6,backgroundColor:"var(--green-800)",color:"white",fontWeight:700,fontSize:"0.88rem",padding:"10px 18px",borderRadius:8,textDecoration:"none"},children:"View in Messages →"}):e.jsx("p",{style:{fontSize:"0.84rem",color:"#065F46"},children:"Keep an eye on your email for their reply."})]}):e.jsxs("form",{onSubmit:L,style:{display:"flex",flexDirection:"column",gap:12},children:[e.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:4},children:[e.jsx("input",{type:"text",placeholder:"Your name *",value:o.name,onChange:s=>{m(r=>({...r,name:s.target.value})),p(r=>({...r,name:""}))},style:h(!!l.name)}),l.name&&e.jsx("p",{style:{fontSize:"0.78rem",color:"#dc2626",margin:0},children:l.name})]}),e.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:4},children:[e.jsx("input",{type:"email",placeholder:"Email address *",value:o.email,onChange:s=>{m(r=>({...r,email:s.target.value})),p(r=>({...r,email:""}))},style:h(!!l.email)}),l.email&&e.jsx("p",{style:{fontSize:"0.78rem",color:"#dc2626",margin:0},children:l.email})]}),e.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:4},children:[e.jsx("input",{type:"tel",placeholder:"Phone number (optional)",value:o.phone,onChange:s=>{m(r=>({...r,phone:s.target.value})),p(r=>({...r,phone:""}))},style:h(!!l.phone)}),l.phone&&e.jsx("p",{style:{fontSize:"0.78rem",color:"#dc2626",margin:0},children:l.phone})]}),e.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:4},children:[e.jsx("textarea",{placeholder:"Introduce yourself and why you're interested in this role…",value:o.message,onChange:s=>{m(r=>({...r,message:s.target.value})),p(r=>({...r,message:""}))},rows:5,style:{...h(!!l.message),resize:"vertical",lineHeight:1.55,minHeight:"unset"}}),l.message&&e.jsx("p",{style:{fontSize:"0.78rem",color:"#dc2626",margin:0},children:l.message})]}),e.jsxs("button",{type:"submit",disabled:y,style:{backgroundColor:"var(--ochre-500)",color:"white",fontWeight:700,fontSize:"0.95rem",padding:"14px",borderRadius:10,border:"1.5px solid var(--ochre-400)",cursor:y?"not-allowed":"pointer",minHeight:50,display:"flex",alignItems:"center",justifyContent:"center",gap:8,opacity:y?.75:1},children:[e.jsx(Q,{size:17}),y?"Sending…":"Send Application"]}),E&&e.jsx("p",{style:{fontSize:"0.78rem",color:"#dc2626",textAlign:"center"},children:E}),e.jsxs("p",{style:{fontSize:"0.75rem",color:"var(--ink-muted)",textAlign:"center"},children:["By applying you agree to our"," ",e.jsx(f,{to:"/terms",style:{color:"var(--green-700)"},children:"Terms of Service"}),"."]})]})]})})]})]})]})}export{oe as default};