1import{j as e,bf as J,bg as G,bh as $,bi as W,bj as X,t as l,r as o,T as Se,u as De,I as Te,J as le,f as Ae,V as D,d as Fe,S as Pe,N as ne,l as T,E as oe,K as Ke,s as c,O as t}from"./index-hXw6ftrS.js";import{o as Be,s as _,b as ce,u as Ee,C as Ie,t as qe}from"./types-CbTG7o04.js";import{A as Me}from"./AdminLayout-wgA6AW0K.js";import{C as Ue,a as Le,b as Re,d as Oe,c as Qe}from"./card-DYGdilSS.js";import{I as F}from"./input-CGBE-TqA.js";import{T as de}from"./textarea-DAzT4tNc.js";import{B as x}from"./badge-COjR1-IC.js";import{S as ue}from"./switch-CFK0mtpR.js";import{T as ze,a as Ve,b as me,c as p,d as He,e as g}from"./table-BJLCkYQp.js";import{F as Je,a as k,b as j,c as f,d as w,e as H,f as he}from"./form-Clk_jMWN.js";import{S as Ge,a as $e,b as We,c as Xe,d as P}from"./select-BJxQeUKe.js";import{A as Ye,a as Ze,b as es,c as ss,d as as,e as rs,f as ts,g as is}from"./alert-dialog-CT4Ob312.js";import{R as ls}from"./RichTextEditor-CYWYJFBb.js";import{L as pe}from"./link-2-DnvJzfFh.js";import{A as ns}from"./arrow-left-D0Z-_GCd.js";import{P as os,a as cs}from"./plus-C_t73vWw.js";import{E as xe}from"./eye-off-DITD2fdb.js";import{S as ds}from"./star-CpUkRC9N.js";import{T as us}from"./trash-2-BJHzY9mo.js";import{U as ms}from"./upload-NqEQPqbN.js";import"./layout-dashboard-D2aZjlX_.js";import"./megaphone-BMNY5bbU.js";import"./images--YEsJ6xp.js";import"./image-gPMVwkW6.js";import"./stethoscope-CyZzpUvZ.js";import"./award-C78RWvFt.js";import"./shield-DAj5TFdV.js";import"./index-B-U8V-CR.js";import"./label-DPvcnWsc.js";import"./chevron-up-CHrIJPHc.js";const hs=({article:i,onClose:b,onUse:m})=>e.jsx(J,{open:!!i,onOpenChange:N=>!N&&b(),children:e.jsxs(G,{className:"max-w-2xl max-h-[85vh] overflow-y-auto",children:[e.jsxs($,{children:[e.jsxs(W,{className:"flex items-center gap-2",children:[e.jsx(pe,{className:"w-4 h-4 text-primary"}),"Pratinjau Konten"]}),e.jsx(X,{children:"Periksa hasil ambil dari link ini sebelum dipakai ke form berita. Anda masih bisa edit semuanya setelah ini."})]}),i&&e.jsxs("div",{className:"space-y-4",children:[i.image_url&&e.jsx("img",{src:i.image_url,alt:"",className:"w-full h-48 object-cover rounded-xl border border-border"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground mb-1",children:"Judul"}),e.jsx("p",{className:"font-serif font-bold text-lg leading-snug",children:i.title||e.jsx("span",{className:"italic text-muted-foreground",children:"(tidak terdeteksi)"})})]}),i.excerpt&&e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground mb-1",children:"Ringkasan"}),e.jsx("p",{className:"text-sm text-foreground/80",children:i.excerpt})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground mb-1",children:"Cuplikan Isi"}),e.jsx("div",{className:"text-sm text-foreground/70 max-h-40 overflow-y-auto border border-border rounded-lg p-3 prose prose-sm max-w-none",dangerouslySetInnerHTML:{__html:i.content}})]}),e.jsxs("p",{className:"text-xs text-muted-foreground truncate",children:["Sumber: ",i.source_url]}),e.jsxs("div",{className:"flex justify-end gap-3 pt-2",children:[e.jsx(l,{type:"button",variant:"outline",onClick:b,children:"Batal"}),e.jsx(l,{type:"button",onClick:()=>m(i),children:"Gunakan"})]})]})]})}),xs=Be({title:_().min(3,"Judul minimal 3 karakter"),excerpt:_().optional(),content:_().optional(),category:_().min(1,"Pilih kategori"),is_published:ce().default(!1),is_featured:ce().default(!1),image_url:_().optional(),source_url:_().optional()}),ps=i=>i instanceof Error?i.message:String(i),Hs=()=>{const[i,b]=o.useState(!1),[m,N]=o.useState(null),[K,B]=o.useState(null),[E,ge]=o.useState(""),[je,Y]=o.useState(!1),[I,q]=o.useState(""),[Z,ee]=o.useState(!1),[fe,M]=o.useState(null),u=Se(),{t:d,i18n:be}=De();be.language==="en"?Te:le;const r=Ee({resolver:qe(xs),defaultValues:{title:"",excerpt:"",content:"",category:"",is_published:!1,is_featured:!1,image_url:"",source_url:""}}),[C,ye]=o.useState("all"),[U,A]=o.useState(null),[L,S]=o.useState(""),{data:y,isLoading:ve}=Ae({queryKey:["admin-news"],queryFn:async()=>{const{data:s,error:a}=await c.from("news").select("*, author:profiles!news_author_id_fkey(full_name)").order("created_at",{ascending:!1});if(a)throw a;return s}}),se=D({mutationFn:async s=>{const{error:a}=await c.from("news").update({status:"published",is_published:!0,reviewed_at:new Date().toISOString()}).eq("id",s);if(a)throw a},onSuccess:()=>
1{u.invalidateQueries({queryKey:["admin-news"]}),u.invalidateQueries({queryKey:["news-public"]}),t.success("Artikel disetujui & dipublikasikan")},onError:s=>t.error(s.message||"Gagal approve")}),R=D({mutationFn:async({id:s,reason:a})=>{const{error:n}=await c.from("news").update({status:"rejected",is_published:!1,rejection_reason:a,reviewed_at:new Date().toISOString()}).eq("id",s);if(n)throw n},onSuccess:()=>{u.invalidateQueries({queryKey:["admin-news"]}),t.success("Artikel ditolak"),A(null),S("")},onError:s=>t.error(s.message||"Gagal menolak")}),O=D({mutationFn:async s=>{var h;const{data:a}=await c.from("profiles").select("id").eq("user_id",(h=(await c.auth.getUser()).data.user)==null?void 0:h.id).single(),{error:n}=await c.from("news").insert({title:s.title,excerpt:s.excerpt||null,content:s.content||null,category:s.category,is_published:s.is_published,is_featured:s.is_featured,image_url:s.image_url||null,source_url:s.source_url||null,status:s.is_published?"published":"draft",author_id:(a==null?void 0:a.id)??null});if(n)throw n},onSuccess:()=>{u.invalidateQueries({queryKey:["admin-news"]}
1),u.invalidateQueries({queryKey:["news-public"]}),t.success(d("admin_news.create_success")),z()},onError:()=>t.error(d("admin_news.create_error"))}),Q=D({mutationFn:async({id:s,data:a})=>{const{error:n}=await c.from("news").update({title:a.title,excerpt:a.excerpt||null,content:a.content||null,category:a.category,is_published:a.is_published,is_featured:a.is_featured,image_url:a.image_url||null,source_url:a.source_url||null,status:a.is_published?"published":"draft"}).eq("id",s);if(n)throw n},onSuccess:()=>{u.invalidateQueries({queryKey:["admin-news"]}),u.invalidateQueries({queryKey:["news-public"]}),t.success(d("admin_news.update_success")),z()},onError:()=>t.error(d("admin_news.update_error"))}),_e=D({mutationFn:async s=>{const{error:a}=await c.from("news").delete().eq("id",s);if(a)throw a},onSuccess:()=>{u.invalidateQueries({queryKey:["admin-news"]}),t.success(d("admin_news.delete_success")),B(null)},onError:()=>t.error(d("admin_news.delete_error"))}),ke=async s=>{var n;const a=(n=s.target.files)==null?void 0:n[0];if(a){if(!a.type.startsWith("image/")){t.error(d("member_profile.avatar_type_error"));return}if(a.size>5*1024*1024){t.error(d("member_profile.avatar_size_error"));return}Y(!0);try{const h=a.name.split(".").pop(),te=`news/${Date.now()}.${h}`,{error:ie}=await c.storage.from("news-images").upload(te,a);if(ie)throw ie;const{data:Ce}=c.storage.from("news-images").getPublicUrl(te);r.setValue("image_url",Ce.publicUrl),t.success(d("admin_news.update_success"))}catch{t.error(d("admin_news.upload_error"))}finally{Y(!1)}}},ae=s=>{s?(N(s),r.reset({title:s.title,excerpt:s.excerpt||"",content:s.content||"",category:s.category||"",is_published:s.is_published,is_featured:s.is_featured,image_url:s.image_url||"",source_url:s.source_url||""})):(N(null),r.reset()),q(""),b(!0)},z=()=>{b(!1),N(null),q(""),r.reset()},re=async()=>{if(!I.trim()){t.error("Masukkan link artikel terlebih dahulu");return}ee(!0);try{const{data:s,error:a}=await c.functions.invoke("fetch-article",{body:{url:I.trim()}});if(a)throw a;if(s!=null&&s.error)throw new Error(s.error);M(s)}catch(s){t.error("Gagal mengambil konten",{description:ps(s)})}finally{ee(!1)}},we=s=>{s.title&&r.setValue("title",s.title),s.excerpt&&r.setValue("excerpt",s.excerpt),r.setValue("content",s.content),s.image_url&&r.setValue("image_url",s.image_url),r.setValue("source_url",s.source_url),M(null),t.success("Konten diterapkan ke form â silakan periksa & edit sebelum menyimpan")},Ne=s=>{m?Q.mutate({id:m.id,data:s}):O.mutate(s)},v=y==null?void 0:y.filter(s=>{var h;const a=s.title.toLowerCase().includes(E.toLowerCase())||((h=s.category)==null?void 0:h.toLowerCase().includes(E.toLowerCase())),n=C==="all"?!0:C==="pending"?s.status==="pending":C==="published"?s.is_published:C==="draft"?!s.is_published&&s.status!=="pending":!0;return a&&n}),V=(y==null?void 0:y.filter(s=>s.status==="pending").length)||0;return e.jsx(Me,{title:"Kelola Berita",subtitle:"Tambah, edit, dan kelola berita PDPI",children:e.jsxs("div",{className:"max-w-7xl mx-auto",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-6",children:[e.jsx(Fe,{to:"/admin",children:e.jsxs(l,{variant:"ghost",size:"sm",children:[e.jsx(ns,{className:"w-4 h-4 mr-2"}),"Kembali"]})}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs("div",{className:"relative flex-1 sm:w-64",children:[e.jsx(Pe,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground"}),e.jsx(F,{placeholder:"Cari berita...",value:E,onChange:s=>ge(s.target.value),className:"pl-9"})]}),e.jsxs(l,{onClick:()=>ae(),children:[e.jsx(os,{className:"w-4 h-4 mr-2"}),"Tambah Berita"]})]})]}),e.jsxs(Ue,{children:[e.jsxs(Le,{children:[e.jsxs(Re,{className:"flex items-center gap-2",children:[e.jsx(ne,{className:"w-5 h-5 text-primary"}),"Daftar Berita",V>0&&e.jsxs(x,{className:"bg-yellow-100 text-yellow-700 ml-2",children:[V," menunggu approval"]})]}),e.jsxs(Oe,{children:[(v==null?void 0:v.length)||0," berita"]}),e.jsx("div",{className:"flex flex-wrap gap-2 pt-3",children:["all","pending","published","draft"].map(s=>e.jsx(l,{size:"sm",variant:C===s?"default":"outline",onClick:()=>ye(s),children:s==="all"?"Semua":s==="pending"?`Menunggu (${V})`:s==="published"?"Published":"Draft"},s))})]}),e.jsx(Qe,{children:ve?e.jsxs("div",{className:"text-center py-12 text-muted-foreground",children:[e.jsx(T,{className:"w-8 h-8 animate-spin mx-auto mb-4"}),"Memuat..."]}):v&&v.length>0?e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(ze,{children:[e.jsx(Ve,{children:e.jsxs(me,{children:[e.jsx(p,{children:"Judul"}),e.jsx(p,{children:"Penulis"}),e.jsx(p,{children:"Kategori"}),e.jsx(p,{children:"Status"}),e.jsx(p,{children:"Featured"}),e.jsx(p,{children:"Tanggal"}),e.jsx(p,{className:"text-right",children:"Aksi"})]})}),e.jsx(He,{children:v.map(s=>{var a;return e.jsxs(me,{children:[e.jsx(g,{children:e.jsxs("div",{className:"flex items-center gap-3",children:[s.image_url&&e.jsx("img",{src:s.image_url,alt:s.title,className:"w-10 h-10 rounded object-cover flex-shrink-0"}),e.jsx("span",{className:"font-medium line-clamp-1",children:s.title})]})}),e.jsx(g,{children:e.jsx("span",{className:"text-xs text-muted-foreground",children:((a=s.author)==null?void 0:a.full_name)||"Admin"})}),e.jsx(g,{children:e.jsx(x,{variant:"outline",children:s.category||"-"})}),e.jsx(g,{children:s.status==="pending"?e.jsx(x,{className:"bg-yellow-100 text-yellow-700",children:"Menunggu Review"}):s.status==="rejected"?e.jsx(x,{className:"bg-red-100 text-red-700",children:"Ditolak"}):s.is_published?e.jsxs(x,{className:"bg-accent/10 text-accent",children:[e.jsx(oe,{className:"w-3 h-3 mr-1"}),"Published"]}):e.jsxs(x,{variant:"secondary",children:[e.jsx(xe,{className:"w-3 h-3 mr-1"}),"Draft"]})}),e.jsx(g,{children:s.is_featured?e.jsxs(x,{className:"bg-primary/10 text-primary",children:[e.jsx(ds,{className:"w-3 h-3 mr-1"}),"Featured"]}):"-"}),e.jsx(g,{children:Ke(new Date(s.created_at),"dd MMM yyyy",{locale:le})}),e.jsx(g,{children:e.jsxs("div",{className:"flex justify-end gap-1",children:[s.status==="pending"&&e.jsxs(e.Fragment,{children:[e.jsx(l,{size:"sm",variant:"ghost",className:"text-green-600 hover:bg-green-50",onClick:()=>se.mutate(s.id),title:"Setujui & Publikasikan",disabled:se.isPending,children:e.jsx(oe,{className:"w-4 h-4"})}),e.jsx(l,{size:"sm",variant:"ghost",className:"text-orange-600 hover:bg-orange-50",onClick:()=>{A(s.id),S("")},title:"Tolak",children:e.jsx(xe,{className:"w-4 h-4"})})]}),e.jsx(l,{size:"sm",variant:"ghost",onClick:()=>ae(s),title:"Edit",children:e.jsx(cs,{className:"w-4 h-4"})}),e.jsx(l,{size:"sm",variant:"ghost",className:"text-destructive hover:text-destructive hover:bg-destructive/10",onClick:()=>B(s.id),title:"Hapus",children:e.jsx(us,{className:"w-4 h-4"})})]})})]},s.id)})})]})}):e.jsxs("div",{className:"text-center py-12 text-muted-foreground",children:[e.jsx(ne,{className:"w-12 h-12 mx-auto mb-4 opacity-50"}),e.jsx("p",{children:'Belum ada berita. Klik "Tambah Berita" untuk memulai.'})]})})]}),e.jsx(J,{open:i,onOpenChange:b,children:e.jsxs(G,{className:"max-w-4xl max-h-[92vh] overflow-y-auto",children:[e.jsxs($,{children:[e.jsx(W,{children:m?"Edit Berita":"Tambah Berita Baru"}),e.jsx(X,{children:m?"Perbarui informasi berita":"Isi form untuk menambahkan berita baru"})]}),e.jsx(Je,{...r,children:e.jsxs("form",{onSubmit:r.handleSubmit(Ne),className:"space-y-5",children:[e.jsxs("div",{className:"rounded-xl border border-dashed border-primary/40 bg-primary/5 p-4 space-y-2",children:[e.jsxs("label",{className:"text-sm font-medium flex items-center gap-1.5",children:[e.jsx(pe,{className:"w-3.5 h-3.5"}),"Ambil dari Link (opsional)"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Tempel link berita sumber, sistem akan coba ambil judul/ringkasan/isi/gambar-nya secara otomatis."}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(F,{placeholder:"https://...",value:I,onChange:s=>q(s.target.value),onKeyDown:s=>{s.key==="Enter"&&(s.preventDefault(),re())}}),e.jsx(l,{type:"button",variant:"secondary",onClick:re,disabled:Z,children:Z?e.jsx(T,{className:"w-4 h-4 animate-spin"}):"Ambil Konten"})]}),r.watch("source_url")&&e.jsxs("p",{className:"text-xs text-muted-foreground truncate",children:["Sumber tersimpan: ",r.watch("source_url")]})]}),e.jsx(k,{control:r.control,name:"title",render:({field:s})=>e.jsxs(j,{children:[e.jsx(f,{children:"Judul Berita *"}),e.jsx(w,{children:e.jsx(F,{placeholder:"Judul berita...",...s})}),e.jsx(H,{})]})}),e.jsx(k,{control:r.control,name:"category",render:({field:s})=>e.jsxs(j,{children:[e.jsx(f,{children:"Kategori *"}),e.jsxs(Ge,{onValueChange:s.onChange,value:s.value,children:[e.jsx(w,{children:e.jsx($e,{children:e.jsx(We,{placeholder:"Pilih kategori"})})}),e.jsxs(Xe,{children:[e.jsx(P,{value:"organisasi",children:"Organisasi"}),e.jsx(P,{value:"ilmiah",children:"Ilmiah"}),e.jsx(P,{value:"kesehatan",children:"Kesehatan Publik"}),e.jsx(P,{value:"kolaborasi",children:"Kolaborasi"})]})]}),e.jsx(H,{})]})}),e.jsx(k,{control:r.control,name:"excerpt",render:({field:s})=>e.jsxs(j,{children:[e.jsx(f,{children:"Ringkasan"}),e.jsx(w,{children:e.jsx(de,{placeholder:"Ringkasan singkat yang tampil di kartu berita...",className:"min-h-[70px] resize-none",...s})}),e.jsx(H,{})]})}),e.jsxs(j,{children:[e.jsx(f,{children:"Konten Lengkap"}),e.jsx(Ie,{control:r.control,name:"content",render:({field:s})=>e.jsx(ls,{content:s.value||"",onChange:s.onChange,placeholder:"Tulis konten berita lengkap di sini..."})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(f,{children:"Gambar Sampul"}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(k,{control:r.control,name:"image_url",render:({field:s})=>e.jsx(j,{className:"flex-1",children:e.jsx(w,{children:e.jsx(F,{placeholder:"URL gambar atau upload di bawah",...s})})})}),e.jsx("label",{htmlFor:"news-image-upload",children:e.jsx(l,{type:"button",variant:"outline",size:"sm",asChild:!0,children:e.jsxs("span",{className:"cursor-pointer",children:[je?e.jsx(T,{className:"w-4 h-4 animate-spin"}):e.jsx(ms,{className:"w-4 h-4 mr-1"}),"Upload"]})})}),e.jsx("input",{id:"news-image-upload",type:"file",accept:"image/*",className:"hidden",onChange:ke})]}),r.watch("image_url")&&e.jsx("img",{src:r.watch("image_url"),alt:"Preview",className:"w-full h-40 object-cover rounded-xl mt-2"})]}),e.jsxs("div",{className:"grid sm:grid-cols-2 gap-4",children:[e.jsx(k,{control:r.control,name:"is_published",render:({field:s})=>e.jsxs(j,{className:"flex items-center justify-between rounded-xl border p-4",children:[e.jsxs("div",{children:[e.jsx(f,{children:"Publikasikan"}),e.jsx(he,{children:"Tampilkan di halaman berita publik"})]}),e.jsx(w,{children:e.jsx(ue,{checked:s.value,onCheckedChange:s.onChange})})]})}),e.jsx(k,{control:r.control,name:"is_featured",render:({field:s})=>e.jsxs(j,{className:"flex items-center justify-between rounded-xl border p-4",children:[e.jsxs("div",{children:[e.jsx(f,{children:"Featured"}),e.jsx(he,{children:"Tampilkan sebagai berita unggulan"})]}),e.jsx(w,{children:e.jsx(ue,{checked:s.value,onCheckedChange:s.onChange})})]})})]}),e.jsxs("div",{className:"flex justify-end gap-3 pt-2",children:[e.jsx(l,{type:"button",variant:"outline",onClick:z,children:"Batal"}),e.jsxs(l,{type:"submit",disabled:O.isPending||Q.isPending,children:[(O.isPending||Q.isPending)&&e.jsx(T,{className:"w-4 h-4 mr-2 animate-spin"}),m?"Simpan Perubahan":"Tambah Berita"]})]})]})})]})}),e.jsx(Ye,{open:!!K,onOpenChange:s=>!s&&B(null),children:e.jsxs(Ze,{children:[e.jsxs(es,{children:[e.jsx(ss,{children:"Hapus Berita?"}),e.jsx(as,{children:"Berita ini akan dihapus permanen dan tidak dapat dikembalikan."})]}),e.jsxs(rs,{children:[e.jsx(ts,{children:"Batal"}),e.jsx(is,{className:"bg-destructive text-destructive-foreground hover:bg-destructive/90",onClick:()=>K&&_e.mutate(K),children:"Hapus"})]})]})}),e.jsx(J,{open:!!U,onOpenChange:s=>{s||(A(null),S(""))},children:e.jsxs(G,{children:[e.jsxs($,{children:[e.jsx(W,{children:"Tolak Artikel"}
1),e.jsx(X,{children:"Berikan alasan singkat agar penulis dapat memperbaiki."})]}),e.jsx(de,{value:L,onChange:s=>S(s.target.value),placeholder:"Alasan penolakan...",className:"min-h-[100px]"}),e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsx(l,{variant:"outline",onClick:()=>{A(null),S("")},children:"Batal"}),e.jsxs(l,{variant:"destructive",disabled:!L.trim()||R.isPending,onClick:()=>U&&R.mutate({id:U,reason:L.trim()}),children:[R.isPending&&e.jsx(T,{className:"w-4 h-4 animate-spin mr-2"}),"Tolak Artikel"]})]})]})}),e.jsx(hs,{article:fe,onClose:()=>M(null),onUse:we})]})})};export{Hs as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.