import{e as w,i as S,r as l,f as _,j as e}from"./index-sga-FecS.js";import{F as E,f as U}from"./index-qo5Z666n.js";import{b as k}from"./index.esm-7CdJvro0.js";import{C as D,a as i}from"./CRow-BaMSc2yS.js";import{C as P,a as I}from"./CCardBody-e8lVonr1.js";import{C as R}from"./CCardHeader-B7SoQ3bq.js";import{C as $}from"./CForm-C4v7yst3.js";function O(){const{t:s,i18n:o}=w();S();const[a,f]=l.useState(null),[b,p]=l.useState(!0),[h,g]=l.useState(null),[c,m]=l.useState({type:"",text:""}),[x,j]=l.useState(null),[y,v]=l.useState(!1),d=l.useCallback(async()=>{p(!0);try{const t=o.language==="ar"?"/ar":"/en",r=await _.get(`${t}/api/users/me/`);f(r.data),console.warn(r.data),g(null)}catch(t){console.error("Error fetching user profile:",t.response?.data||t.message),g(s("error_fetching_data",{message:t.response?.data||t.message}))}finally{p(!1)}},[o.language,s]);l.useEffect(()=>{d()},[d]);const N=t=>{j(t.target.files[0])},u=t=>{const{name:r,value:n}=t.target;f(C=>({...C,[r]:n}))},F=async t=>{t.preventDefault(),m({type:"",text:""}),v(!0);const r=new FormData;a?.first_name!==void 0&&r.append("first_name",a.first_name),a?.last_name!==void 0&&r.append("last_name",a.last_name),a?.email!==void 0&&r.append("email",a.email),x&&r.append("avatar",x);try{const n=o.language==="ar"?"/ar":"/en",C=await _.patch(`${n}/api/users/${a?.id}/`,r,{headers:{"Content-Type":"multipart/form-data"}});m({type:"success",text:s("profile_updated_successfully")}),await d()}catch(n){console.error("Error updating profile:",n.response?.data||n.message),m({type:"error",text:s("error_updating_profile",{message:n.response?.data||n.message})})}finally{v(!1),j(null)}};return b?e.jsx("div",{className:"text-center py-4",children:s("loading_data")}):h?e.jsx("div",{className:"alert alert-danger",children:h}):e.jsx("section",{className:"user-profile-edit-page container py-4",children:e.jsx(k,{children:e.jsx(D,{className:"justify-content-center",children:e.jsx(i,{lg:10,children:e.jsxs(P,{className:"mb-4",children:[e.jsxs(R,{className:"d-flex align-items-center justify-content-between",children:[e.jsx("strong",{children:s("edit_profile")}),e.jsx("span",{className:"text-muted",children:a?.username||""})]}),e.jsx(I,{children:e.jsxs($,{onSubmit:F,className:"row g-3 align-items-center",children:[e.jsxs(i,{md:4,className:"d-flex flex-column align-items-center text-center",children:[e.jsxs("div",{className:"profile-avatar-container",style:{display:"inline-block"},children:[a?.avatar_url?e.jsx("img",{src:a.avatar_url,alt:a.username||"avatar",style:{width:32,height:32,borderRadius:"50%",objectFit:"cover",display:"block"}}):e.jsx("div",{"aria-label":"avatar placeholder",style:{width:32,height:32,borderRadius:"50%",background:"#ccc",display:"inline-flex",alignItems:"center",justifyContent:"center",color:"#fff",fontSize:12},children:a?.username?.charAt(0).toUpperCase()}),e.jsx("label",{htmlFor:"avatar-upload",style:{display:"inline-block",marginLeft:6,cursor:"pointer"},children:e.jsx(E,{icon:U})}),e.jsx("input",{id:"avatar-upload",type:"file",accept:"image/*",onChange:N,style:{display:"none"}})]}),e.jsx("span",{className:"text-muted small",children:s("avatar_tip",{defaultValue:""})})]}),e.jsxs(i,{md:8,children:[e.jsxs("div",{className:"row g-3",children:[e.jsxs(i,{md:6,children:[e.jsxs("label",{className:"form-label",htmlFor:"first_name",children:[s("first_name"),"*"]}),e.jsx("input",{className:"form-control",disabled:!0,readOnly:!0,id:"first_name",name:"first_name",value:a?.first_name||"",onChange:u})]}),e.jsxs(i,{md:6,children:[e.jsxs("label",{className:"form-label",htmlFor:"last_name",children:[s("last_name"),"*"]}),e.jsx("input",{className:"form-control",id:"last_name",name:"last_name",value:a?.last_name||"",onChange:u})]}),e.jsxs(i,{md:6,children:[e.jsx("label",{className:"form-label",htmlFor:"email",children:s("email")}),e.jsx("input",{className:"form-control",id:"email",name:"email",value:a?.email||"",onChange:u})]})]}),e.jsx("div",{className:"mt-3 d-flex justify-content-end",children:e.jsx("button",{type:"submit",className:"btn btn-primary",disabled:y,children:s(y?"saving":"save_changes")})}),c.text&&e.jsx("div",{className:`mt-2 alert ${c.type==="success"?"alert-success":"alert-danger"}`,role:"status",children:c.text})]})]})})]})})})})})}export{O as default};
