import{r as e}from"./rolldown-runtime-QTnfLwEv.js";import{p as t,t as n}from"./vendor-react-iIHq6SOl.js";import{o as r}from"./main-DgxOmW_I.js";import{t as i}from"./AdminLayout-Cj-1ee3V.js";var a=e(t(),1),o=n(),s={username:``,nama:``,password:``,dinas_id:``};function c(){let[e,t]=(0,a.useState)([]),[n,c]=(0,a.useState)([]),[l,u]=(0,a.useState)(!0),[d,f]=(0,a.useState)(``),[p,m]=(0,a.useState)(!1),[h,g]=(0,a.useState)(null),[_,v]=(0,a.useState)(s),[y,b]=(0,a.useState)({}),[x,S]=(0,a.useState)(!1),[C,w]=(0,a.useState)(null),T=localStorage.getItem(`admin_token`),E=(e,t=`success`)=>{w({msg:e,type:t})};(0,a.useEffect)(()=>{if(C){let e=setTimeout(()=>w(null),3e3);return()=>clearTimeout(e)}},[C]);let D=async()=>{try{u(!0);let e=await(await fetch(r(`/api/admin/users`),{headers:{Authorization:`Bearer ${T}`,Accept:`application/json`}})).json();e.success?t(e.data):E(e.message||`Gagal memuat data admin.`,`error`)}catch{E(`Terjadi kesalahan jaringan saat memuat data admin.`,`error`)}finally{u(!1)}},O=async()=>{try{let e=await(await fetch(r(`/api/dinas`),{headers:{Accept:`application/json`}})).json();e.success&&c(e.data)}catch{console.error(`Gagal mengambil pilihan dinas.`)}};(0,a.useEffect)(()=>{D(),O()},[]);let k=(0,a.useMemo)(()=>{let t=d.toLowerCase();return e.filter(e=>e.username.toLowerCase().includes(t)||e.nama.toLowerCase().includes(t)||e.dinas?.nama&&e.dinas.nama.toLowerCase().includes(t)||e.dinas?.singkatan&&e.dinas.singkatan.toLowerCase().includes(t))},[e,d]),A=()=>{g(null),v(s),b({}),m(!0)},j=e=>{g(e.id),v({username:e.username,nama:e.nama,password:``,dinas_id:e.dinas_id===null?``:e.dinas_id.toString()}),b({}),m(!0)},M=e=>t=>{v(n=>({...n,[e]:t.target.value}))},N=async e=>{e.preventDefault(),b({}),S(!0);try{let e=h?`/api/admin/users/${h}`:`/api/admin/users`,t=h?`PUT`:`POST`,n=await fetch(r(e),{method:t,headers:{Authorization:`Bearer ${T}`,"Content-Type":`application/json`,Accept:`application/json`},body:JSON.stringify({username:_.username,nama:_.nama,password:_.password||null,dinas_id:_.dinas_id?parseInt(_.dinas_id):null})}),i=await n.json();n.ok&&i.success?(E(i.message||`Akun admin berhasil disimpan.`,`success`),m(!1),v(s),D()):i.errors?b(i.errors):E(i.message||`Gagal menyimpan akun admin.`,`error`)}catch{E(`Terjadi kesalahan jaringan saat menyimpan.`,`error`)}finally{S(!1)}},P=async e=>{if(window.confirm(`Apakah Anda yakin ingin menghapus akun admin ini?`))try{let t=await(await fetch(r(`/api/admin/users/${e}`),{method:`DELETE`,headers:{Authorization:`Bearer ${T}`,Accept:`application/json`}})).json();t.success?(E(`Akun admin berhasil dihapus.`,`success`),D()):E(t.message||`Gagal menghapus akun admin.`,`error`)}catch{E(`Gagal menghapus data karena kesalahan jaringan.`,`error`)}};return(0,o.jsxs)(i,{children:[(0,o.jsx)(`style`,{children:`
        .user-form-grid {
          display: grid;
          grid-template-columns: 1fr 1fr;
          gap: 16px;
          align-items: start;
        }
        @media (max-width: 600px) {
          .user-form-grid { grid-template-columns: 1fr; }
        }
      `}),C&&(0,o.jsx)(`div`,{style:{position:`fixed`,bottom:`24px`,right:`24px`,padding:`12px 20px`,borderRadius:`10px`,color:`white`,fontSize:`13.5px`,fontWeight:`600`,zIndex:9999,boxShadow:`0 4px 12px rgba(0,0,0,0.1)`,background:C.type===`error`?`#B91C1C`:`#0028B3`},children:C.msg}),(0,o.jsxs)(`div`,{style:{marginBottom:`24px`,display:`flex`,alignItems:`flex-start`,justifyContent:`space-between`,flexWrap:`wrap`,gap:`12px`},children:[(0,o.jsxs)(`div`,{children:[(0,o.jsx)(`h2`,{style:{fontSize:`22px`,fontWeight:`800`,color:`#001178`,letterSpacing:`-0.3px`},children:`Manajemen Akun Admin`}),(0,o.jsx)(`p`,{style:{fontSize:`13.5px`,color:`#64748B`,marginTop:`3px`},children:`Kelola akun admin untuk masing-masing instansi dinas Kabupaten Bogor`})]}),(0,o.jsx)(`button`,{onClick:A,style:{display:`flex`,alignItems:`center`,gap:`8px`,padding:`10px 18px`,minHeight:`42px`,border:`none`,borderRadius:`8px`,background:`#0028B3`,color:`white`,cursor:`pointer`,fontSize:`13px`,fontWeight:`700`,fontFamily:`inherit`,boxShadow:`0 2px 8px rgba(0,40,179,0.2)`},children:`+ Buat Admin Baru`})]}),(0,o.jsxs)(`div`,{style:{background:`white`,padding:`16px 20px`,borderRadius:`16px`,boxShadow:`0 1px 3px rgba(0,0,0,0.05)`,border:`1px solid rgba(228, 231, 237, 0.8)`,marginBottom:`20px`,display:`flex`,alignItems:`center`,gap:`12px`},children:[(0,o.jsxs)(`svg`,{viewBox:`0 0 24 24`,width:`18`,height:`18`,fill:`none`,stroke:`#64748B`,strokeWidth:`2`,strokeLinecap:`round`,strokeLinejoin:`round`,style:{flexShrink:0},children:[(0,o.jsx)(`circle`,{cx:`11`,cy:`11`,r:`8`}),(0,o.jsx)(`line`,{x1:`21`,y1:`21`,x2:`16.65`,y2:`16.65`})]}),(0,o.jsx)(`input`,{type:`text`,placeholder:`Cari akun berdasarkan nama, username, atau dinas...`,value:d,onChange:e=>(0,a.startTransition)(()=>f(e.target.value)),style:{flex:1,border:`none`,outline:`none`,fontSize:`14px`,color:`#1E293B`,fontFamily:`inherit`}})]}),p&&(0,o.jsxs)(`div`,{style:{background:`white`,borderRadius:`16px`,boxShadow:`0 4px 20px rgba(0,0,0,0.08)`,border:`1.5px solid #0028B3`,overflow:`hidden`,marginBottom:`20px`},children:[(0,o.jsxs)(`div`,{style:{padding:`16px 20px`,background:`#0028B3`,color:`white`,display:`flex`,alignItems:`center`,justifyContent:`space-between`},children:[(0,o.jsx)(`div`,{style:{fontSize:`15px`,fontWeight:`700`},children:h?`Edit Akun Admin`:`Buat Akun Admin Baru`}),(0,o.jsx)(`button`,{onClick:()=>m(!1),style:{background:`rgba(255,255,255,0.15)`,border:`none`,color:`white`,width:`30px`,height:`30px`,borderRadius:`8px`,cursor:`pointer`,fontSize:`16px`,display:`flex`,alignItems:`center`,justifyContent:`center`},children:`✕`})]}),(0,o.jsxs)(`form`,{onSubmit:N,className:`user-form-grid`,style:{padding:`20px`},children:[(0,o.jsxs)(`div`,{style:{display:`flex`,flexDirection:`column`,gap:`6px`},children:[(0,o.jsx)(`label`,{style:{fontSize:`12.5px`,fontWeight:`700`,color:`#475569`},children:`Nama Lengkap`}),(0,o.jsx)(`input`,{type:`text`,value:_.nama,onChange:M(`nama`),placeholder:`Nama lengkap admin...`,required:!0,style:{padding:`10px 14px`,borderRadius:`8px`,border:`1px solid #CBD5E1`,fontSize:`13.5px`,fontFamily:`inherit`,outline:`none`}}),y.nama&&(0,o.jsx)(`span`,{style:{fontSize:`12px`,color:`#B91C1C`},children:y.nama[0]})]}),(0,o.jsxs)(`div`,{style:{display:`flex`,flexDirection:`column`,gap:`6px`},children:[(0,o.jsx)(`label`,{style:{fontSize:`12.5px`,fontWeight:`700`,color:`#475569`},children:`Username`}),(0,o.jsx)(`input`,{type:`text`,value:_.username,onChange:M(`username`),placeholder:`Username untuk login...`,required:!0,style:{padding:`10px 14px`,borderRadius:`8px`,border:`1px solid #CBD5E1`,fontSize:`13.5px`,fontFamily:`inherit`,outline:`none`}}),y.username&&(0,o.jsx)(`span`,{style:{fontSize:`12px`,color:`#B91C1C`},children:y.username[0]})]}),(0,o.jsxs)(`div`,{style:{display:`flex`,flexDirection:`column`,gap:`6px`},children:[(0,o.jsxs)(`label`,{style:{fontSize:`12.5px`,fontWeight:`700`,color:`#475569`},children:[`Password `,h&&(0,o.jsx)(`span`,{style:{fontWeight:`normal`,color:`#94A3B8`},children:`(Biarkan kosong jika tidak diubah)`})]}),(0,o.jsx)(`input`,{type:`password`,value:_.password,onChange:M(`password`),placeholder:`Min. 6 karakter...`,required:!h,style:{padding:`10px 14px`,borderRadius:`8px`,border:`1px solid #CBD5E1`,fontSize:`13.5px`,fontFamily:`inherit`,outline:`none`}}),y.password&&(0,o.jsx)(`span`,{style:{fontSize:`12px`,color:`#B91C1C`},children:y.password[0]})]}),(0,o.jsxs)(`div`,{style:{display:`flex`,flexDirection:`column`,gap:`6px`},children:[(0,o.jsx)(`label`,{style:{fontSize:`12.5px`,fontWeight:`700`,color:`#475569`},children:`Hak Akses Instansi (Dinas)`}),(0,o.jsxs)(`select`,{value:_.dinas_id,onChange:M(`dinas_id`),style:{padding:`10px 14px`,borderRadius:`8px`,border:`1px solid #CBD5E1`,fontSize:`13.5px`,fontFamily:`inherit`,outline:`none`,background:`white`},children:[(0,o.jsx)(`option`,{value:``,children:`Super Admin (Akses Semua Instansi)`}),n.map(e=>(0,o.jsxs)(`option`,{value:e.id,children:[e.nama,` (`,e.singkatan,`)`]},e.id))]}),y.dinas_id&&(0,o.jsx)(`span`,{style:{fontSize:`12px`,color:`#B91C1C`},children:y.dinas_id[0]})]}),(0,o.jsxs)(`div`,{style:{gridColumn:`1/-1`,display:`flex`,gap:`10px`,justifyContent:`flex-end`,marginTop:`4px`},children:[(0,o.jsx)(`button`,{type:`button`,onClick:()=>m(!1),style:{background:`white`,border:`1px solid #CBD5E1`,borderRadius:`8px`,padding:`10px 18px`,minHeight:`42px`,fontSize:`13px`,fontWeight:`600`,cursor:`pointer`,fontFamily:`inherit`},children:`Batal`}),(0,o.jsx)(`button`,{type:`submit`,disabled:x,style:{background:x?`#94A3B8`:`#0028B3`,color:`white`,border:`none`,borderRadius:`8px`,padding:`10px 24px`,minHeight:`42px`,fontSize:`13px`,fontWeight:`700`,cursor:`pointer`,fontFamily:`inherit`,boxShadow:`0 2px 8px rgba(0,40,179,0.2)`},children:x?`Menyimpan...`:h?`Simpan Perubahan`:`Buat Akun`})]})]})]}),(0,o.jsxs)(`div`,{style:{background:`white`,borderRadius:`16px`,boxShadow:`0 1px 3px rgba(0,0,0,0.05)`,border:`1px solid rgba(228, 231, 237, 0.8)`,overflow:`hidden`},children:[(0,o.jsxs)(`div`,{style:{padding:`16px 20px`,borderBottom:`1px solid #E4E7ED`,display:`flex`,alignItems:`center`,justifyContent:`space-between`},children:[(0,o.jsx)(`h3`,{style:{fontSize:`15px`,fontWeight:`700`,color:`#0F172A`},children:`Daftar Akun Admin`}),(0,o.jsxs)(`span`,{style:{fontSize:`12.5px`,color:`#64748B`,fontWeight:`500`},children:[k.length,` akun`]})]}),l?(0,o.jsx)(`div`,{style:{padding:`36px`,textAlign:`center`,color:`#64748B`,fontSize:`13.5px`},children:`Memuat data admin...`}):(0,o.jsx)(`div`,{style:{overflowX:`auto`},children:(0,o.jsxs)(`table`,{style:{width:`100%`,borderCollapse:`collapse`},children:[(0,o.jsx)(`thead`,{children:(0,o.jsx)(`tr`,{children:[`Nama Admin`,`Username`,`Hak Akses / Instansi`,`Status Akun`,`Aksi`].map(e=>(0,o.jsx)(`th`,{style:{background:`#F8FAFC`,padding:`12px 16px`,fontSize:`11px`,fontWeight:`700`,color:`#64748B`,textTransform:`uppercase`,letterSpacing:`0.6px`,textAlign:`left`,whiteSpace:`nowrap`,borderBottom:`1px solid #E2E8F0`},children:e},e))})}),(0,o.jsx)(`tbody`,{children:k.length===0?(0,o.jsx)(`tr`,{children:(0,o.jsx)(`td`,{colSpan:5,style:{padding:`32px`,textAlign:`center`,color:`#64748B`,fontSize:`13.5px`},children:`Tidak ada akun admin ditemukan.`})}):k.map(e=>(0,o.jsxs)(`tr`,{style:{borderBottom:`1px solid #F1F5F9`,transition:`background 0.2s`},children:[(0,o.jsx)(`td`,{style:{padding:`14px 16px`,fontSize:`13.5px`,fontWeight:`600`,color:`#0F172A`},children:e.nama}),(0,o.jsx)(`td`,{style:{padding:`14px 16px`,fontSize:`13px`,color:`#64748B`},children:e.username}),(0,o.jsx)(`td`,{style:{padding:`14px 16px`,fontSize:`13px`,color:e.dinas_id?`#0F172A`:`#0028B3`,fontWeight:e.dinas_id?`normal`:`700`},children:e.dinas?`${e.dinas.nama} (${e.dinas.singkatan})`:`Super Admin (Akses Global)`}),(0,o.jsx)(`td`,{style:{padding:`14px 16px`,fontSize:`12px`,whiteSpace:`nowrap`},children:(0,o.jsx)(`span`,{style:{background:`#ECFDF5`,color:`#059669`,padding:`4px 8px`,borderRadius:`100px`,fontWeight:`700`},children:`Aktif`})}),(0,o.jsx)(`td`,{style:{padding:`14px 16px`,whiteSpace:`nowrap`},children:(0,o.jsxs)(`div`,{style:{display:`flex`,gap:`8px`},children:[(0,o.jsx)(`button`,{onClick:()=>j(e),style:{background:`#EFF6FF`,border:`none`,borderRadius:`6px`,color:`#0028B3`,padding:`6px 12px`,fontSize:`12.5px`,fontWeight:`700`,cursor:`pointer`},children:`Edit`}),(0,o.jsx)(`button`,{onClick:()=>P(e.id),style:{background:`#FEF2F2`,border:`none`,borderRadius:`6px`,color:`#B91C1C`,padding:`6px 12px`,fontSize:`12.5px`,fontWeight:`700`,cursor:`pointer`},children:`Hapus`})]})})]},e.id))})]})})]})]})}export{c as default};