import{r as x,c as T,u as V,a as G,b as H,j as t,i as J,n as h}from"./index-7c7a558c.js";import{B as K}from"./Breadcrumb-21e99ae5.js";import{k as O,l as W}from"./adminManageApi-69611741.js";import{R as X}from"./RoleSkeleton-7590a9f4.js";const ea=()=>{const[b,p]=x.useState([]),k=T(),j=V(),S=j==null?void 0:j.roleId,{isLoading:N}=G("getAdminUserCreateInfo",()=>O(S),{onSuccess:a=>{var e,l,i,c,s,n,o,u,I,A,z,E,F,L,P,B,U,$;if(R((i=(l=(e=a==null?void 0:a.data)==null?void 0:e.data)==null?void 0:l.role)==null?void 0:i.name),p((s=(c=a==null?void 0:a.data)==null?void 0:c.data)==null?void 0:s.permissions),(o=(n=a==null?void 0:a.data)==null?void 0:n.data)!=null&&o.selected_permissions&&((A=(I=(u=a==null?void 0:a.data)==null?void 0:u.data)==null?void 0:I.permissions)==null?void 0:A.length)!=0){let m=(L=(F=(E=(z=a==null?void 0:a.data)==null?void 0:z.data)==null?void 0:E.permissions)==null?void 0:F.filter(d=>{var r,g,f;return(f=(g=(r=a==null?void 0:a.data)==null?void 0:r.data)==null?void 0:g.selected_permissions)==null?void 0:f.includes(`${d==null?void 0:d.id}`)}))==null?void 0:L.map(d=>d.name);(m==null?void 0:m.length)===0&&(m=($=(U=(B=(P=a==null?void 0:a.data)==null?void 0:P.data)==null?void 0:B.permissions)==null?void 0:U.filter(d=>{var r,g,f;return(f=(g=(r=a==null?void 0:a.data)==null?void 0:r.data)==null?void 0:g.selected_permissions)==null?void 0:f.includes(d==null?void 0:d.id)}))==null?void 0:$.map(d=>d.name)),y(m)}}}),{isLoading:w,mutate:q}=H(W,{onSuccess:a=>{var e,l;h.success("Successfully saved!"),k(`/admin/role/${(l=(e=a==null?void 0:a.data)==null?void 0:e.data)==null?void 0:l.id}`)},onError:a=>{var e,l,i,c,s,n,o,u;(l=(e=a==null?void 0:a.response)==null?void 0:e.data)!=null&&l.error?h.error((c=(i=a==null?void 0:a.response)==null?void 0:i.data)==null?void 0:c.error):(n=(s=a==null?void 0:a.response)==null?void 0:s.data)!=null&&n.message?h.error((u=(o=a==null?void 0:a.response)==null?void 0:o.data)==null?void 0:u.message):h.error("something went wrong!")}}),[_,R]=x.useState(""),[C,y]=x.useState([]),D=a=>{const e=a.target.value;a.target.checked?y(l=>[...l,e]):y(l=>l.filter(i=>i!==e))};x.useEffect(()=>{const a=localStorage.getItem("token"),e=localStorage.getItem("user");a&&e||k("/login")},[k]);const M=a=>{a.preventDefault(),w||q({Id:S,formdata:{name:_,permissions:C}})},Q=a=>a.split("_").map(e=>e.charAt(0).toUpperCase()+e.slice(1)).join(" ");let v=null;return N&&(v=t.jsx(X,{})),N||(v=t.jsxs("form",{className:"p-5",onSubmit:M,children:[t.jsxs("div",{className:"relative z-0 w-full mb-6 group",children:[t.jsx("input",{type:"text",name:"floating_role_name",id:"floating_role_name",className:"block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600",placeholder:"",defaultValue:_,onChange:a=>R(a.target.value),required:!0}),t.jsx("label",{htmlFor:"floating_role_name",className:"peer-focus:font-medium absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6",children:"Role Name"})]}),t.jsxs("div",{className:"relative z-0 w-full mb-6 group",children:[t.jsx("h2",{className:"text-xl font-bold mb-4 dark:text-white text-black",children:"Select Permissions:"}),t.jsx("div",{className:"grid md:grid-cols-2 grid-cols-1",children:(b==null?void 0:b.length)!=0&&b.map((a,e)=>t.jsxs("div",{className:"flex items-center mb-4",children:[t.jsx("input",{id:a.name,type:"checkbox",className:"w-5 h-5 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600",value:a.name,onChange:D,checked:C.includes(a.name)}),t.jsx("label",{htmlFor:a.name,className:"ml-2 text-lg font-medium dark:text-white text-black",children:t.jsx("span",{className:"ml-2",children:Q(a.name)})})]},e))})]}),t.jsx("button",{type:"submit",className:"text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800",children:w?t.jsx(J,{color:"white",size:"lg",variant:"dots"}):"Submit"})]})),t.jsxs(t.Fragment,{children:[t.jsx(K,{title:"Single Role"}),t.jsx("div",{className:"my-12",children:t.jsx("div",{className:"relative overflow-x-auto p-2 bg-gradient-to-r from-main to-second shadow-md border-dashed border rounded-tr-xl rounded-bl-xl",children:v})})]})};export{ea as default};
