import{r,c as q,u as D,a as M,b as Q,j as a,i as T,n as g}from"./index-7c7a558c.js";import{B as V}from"./Breadcrumb-f4cb0f42.js";import{k as G,l as H}from"./adminManageApi-69611741.js";import{R as J}from"./RoleSkeleton-7590a9f4.js";const Y=()=>{const[m,E]=r.useState([]),f=q(),b=D(),k=b==null?void 0:b.roleId,{isLoading:j}=M("getAdminUserCreateInfo",()=>G(k),{onSuccess:e=>{var t,l,s,d,i,c,n,o,w,p,_,R,C,I;if(S((s=(l=(t=e==null?void 0:e.data)==null?void 0:t.data)==null?void 0:l.role)==null?void 0:s.name),E((i=(d=e==null?void 0:e.data)==null?void 0:d.data)==null?void 0:i.permissions),(n=(c=e==null?void 0:e.data)==null?void 0:c.data)!=null&&n.selected_permissions&&((p=(w=(o=e==null?void 0:e.data)==null?void 0:o.data)==null?void 0:w.permissions)==null?void 0:p.length)!=0){const $=(I=(C=(R=(_=e==null?void 0:e.data)==null?void 0:_.data)==null?void 0:R.permissions)==null?void 0:C.filter(u=>{var P,A,z;return(z=(A=(P=e==null?void 0:e.data)==null?void 0:P.data)==null?void 0:A.selected_permissions)==null?void 0:z.includes(`${u==null?void 0:u.id}`)}))==null?void 0:I.map(u=>u.name);x($)}}}),{isLoading:y,mutate:F}=Q(H,{onSuccess:e=>{var t,l;g.success("Successfully saved!"),f(`/admin/role/${(l=(t=e==null?void 0:e.data)==null?void 0:t.data)==null?void 0:l.id}`)},onError:e=>{var t,l,s,d,i,c,n,o;(l=(t=e==null?void 0:e.response)==null?void 0:t.data)!=null&&l.error?g.error((d=(s=e==null?void 0:e.response)==null?void 0:s.data)==null?void 0:d.error):(c=(i=e==null?void 0:e.response)==null?void 0:i.data)!=null&&c.message?g.error((o=(n=e==null?void 0:e.response)==null?void 0:n.data)==null?void 0:o.message):g.error("something went wrong!")}}),[v,S]=r.useState(""),[N,x]=r.useState([]),L=e=>{const t=e.target.value;e.target.checked?x(l=>[...l,t]):x(l=>l.filter(s=>s!==t))};r.useEffect(()=>{const e=localStorage.getItem("token"),t=localStorage.getItem("user");e&&t||f("/login")},[f]);const B=e=>{e.preventDefault(),y||F({Id:k,formdata:{name:v,permissions:N}})},U=e=>e.split("_").map(t=>t.charAt(0).toUpperCase()+t.slice(1)).join(" ");let h=null;return j&&(h=a.jsx(J,{})),j||(h=a.jsxs("form",{className:"p-5",onSubmit:B,children:[a.jsxs("div",{className:"relative z-0 w-full mb-6 group",children:[a.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:v,onChange:e=>S(e.target.value),required:!0}),a.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"})]}),a.jsxs("div",{className:"relative z-0 w-full mb-6 group",children:[a.jsx("h2",{className:"text-xl font-bold mb-4 dark:text-white text-black",children:"Select Permissions:"}),a.jsx("div",{className:"grid md:grid-cols-2 grid-cols-1",children:(m==null?void 0:m.length)!=0&&m.map((e,t)=>a.jsxs("div",{className:"flex items-center mb-4",children:[a.jsx("input",{id:e.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:e.name,onChange:L,checked:N.includes(e.name)}),a.jsx("label",{htmlFor:e.name,className:"ml-2 text-lg font-medium dark:text-white text-black",children:a.jsx("span",{className:"ml-2",children:U(e.name)})})]},t))})]}),a.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:y?a.jsx(T,{color:"white",size:"lg",variant:"dots"}):"Submit"})]})),a.jsxs(a.Fragment,{children:[a.jsx(V,{title:"Single Role"}),a.jsx("div",{className:"my-12",children:a.jsx("div",{className:"relative overflow-x-auto shadow-md sm:rounded-lg section-bg p-2",children:h})})]})};export{Y as default};
