import{r,c as C,a as R,b as _,j as a,i as A,n as d}from"./index-7c7a558c.js";import{B as z}from"./Breadcrumb-21e99ae5.js";import{i as E,j as F}from"./adminManageApi-69611741.js";import{R as I}from"./RoleSkeleton-7590a9f4.js";const q=()=>{const[n,j]=r.useState([]),i=C(),{isLoading:u}=R("getAdminUserCreateInfo",E,{onSuccess:e=>{var t,s,l,o;(s=(t=e==null?void 0:e.data)==null?void 0:t.data)!=null&&s.roles&&j((o=(l=e==null?void 0:e.data)==null?void 0:l.data)==null?void 0:o.permissions)}}),{isLoading:m,mutate:y}=_(F,{onSuccess:e=>{var t,s;d.success("Successfully Created"),i(`/admin/role/${(s=(t=e==null?void 0:e.data)==null?void 0:t.data)==null?void 0:s.id}`)},onError:e=>{var t,s,l,o,x,h,p,k;(s=(t=e==null?void 0:e.response)==null?void 0:t.data)!=null&&s.error?d.error((o=(l=e==null?void 0:e.response)==null?void 0:l.data)==null?void 0:o.error):(h=(x=e==null?void 0:e.response)==null?void 0:x.data)!=null&&h.message?d.error((k=(p=e==null?void 0:e.response)==null?void 0:p.data)==null?void 0:k.message):d.error("something went wrong!")}}),[g,v]=r.useState(""),[f,b]=r.useState([]),N=e=>{const t=e.target.value;e.target.checked?b(s=>[...s,t]):b(s=>s.filter(l=>l!==t))};r.useEffect(()=>{const e=localStorage.getItem("token"),t=localStorage.getItem("user");e&&t||i("/login")},[i]);const w=e=>{e.preventDefault(),m||y({formdata:{name:g,permissions:f}})},S=e=>e.split("_").map(t=>t.charAt(0).toUpperCase()+t.slice(1)).join(" ");let c=null;return u&&(c=a.jsx(I,{})),u||(c=a.jsxs("form",{className:"p-5",onSubmit:w,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:g,onChange:e=>v(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:(n==null?void 0:n.length)!=0&&n.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:N,checked:f.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:S(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:m?a.jsx(A,{color:"white",size:"lg",variant:"dots"}):"Submit"})]})),a.jsxs(a.Fragment,{children:[a.jsx(z,{title:"Add Role"}),a.jsx("div",{className:"my-12",children:a.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:c})})]})};export{q as default};
