import{r as s,j as e,M as m,V as L}from"./index-Cvh4tLHo.js";import{M as i}from"./Modal-CCihVZTY.js";import{A}from"./AdminAlert-Bt3L8_zJ.js";import{A as h}from"./AdminButton-BKglG8kI.js";import{A as M}from"./AdminTable-BQ5Lf7EC.js";import{E as V}from"./EmptyState-D6lCh4WN.js";import{P as w}from"./PageHeader-D6k34vvM.js";const J=[{label:"Every minute",value:"* * * * *"},{label:"Every 5 min",value:"*/5 * * * *"},{label:"Every hour",value:"0 * * * *"},{label:"Daily",value:"0 0 * * *"},{label:"Weekly",value:"0 0 * * 0"}];function K(){const[u,k]=s.useState([]),[D,j]=s.useState(!0),[f,x]=s.useState(""),[T,n]=s.useState(!1),[r,c]=s.useState(null),[l,o]=s.useState(null),[y,v]=s.useState(!1),[N,p]=s.useState(""),[b,g]=s.useState(!1),d=()=>{j(!0),m("/crontab/list").then(k).catch(t=>x(t.message)).finally(()=>j(!1))};s.useEffect(()=>{d()},[]);const F=t=>{t.preventDefault();const a=t.currentTarget,B=a.elements.namedItem("name").value.trim(),S=a.elements.namedItem("schedule").value.trim(),E=a.elements.namedItem("execstr").value.trim();if(!S||!E){p("Schedule and command are required");return}v(!0),p("");const C={name:B,type:"shell",schedule:S,execstr:E};(r?m(`/crontab/${r}`,{method:"PUT",body:JSON.stringify(C)}):m("/crontab/create",{method:"POST",body:JSON.stringify(C)})).then(()=>{n(!1),c(null),a.reset(),d()}).catch(H=>p(H.message)).finally(()=>v(!1))},I=t=>{c(t.id),o(t),n(!0)},P=t=>{confirm("Delete this cron job?")&&m(`/crontab/${t}`,{method:"DELETE"}).then(d).catch(a=>x(a.message))},q=()=>{g(!0),L().then(d).catch(t=>x(t.message)).finally(()=>g(!1))};return D?e.jsxs(e.Fragment,{children:[e.jsx(w,{title:"Cron"}),e.jsx("p",{className:"text-secondary",children:"Loading…"})]}):e.jsxs(e.Fragment,{children:[e.jsx(w,{title:"Cron",actions:e.jsxs("div",{className:"d-flex flex-wrap gap-2",children:[e.jsxs(h,{variant:"success",disabled:b||u.length===0,onClick:q,children:[b?e.jsx("span",{className:"spinner-border spinner-border-sm me-1",role:"status"}):e.jsx("i",{className:"ti ti-bolt me-1","aria-hidden":!0}),b?"Applying…":"Apply to System"]}),e.jsxs(h,{variant:"primary",onClick:()=>{c(null),o(null),n(!0)},children:[e.jsx("i",{className:"ti ti-plus me-1","aria-hidden":!0}),"Add Cron"]})]})}),f?e.jsx(A,{className:"mb-3",children:f}):null,e.jsxs(i,{show:T,onHide:()=>{n(!1),c(null),o(null)},centered:!0,size:"lg",children:[e.jsx(i.Header,{closeButton:!0,children:e.jsx(i.Title,{children:r?"Edit Cron Job":"Create Cron Job"})}),e.jsxs("form",{onSubmit:F,children:[e.jsxs(i.Body,{children:[N?e.jsx(A,{className:"mb-3",children:N}):null,e.jsxs("div",{className:"mb-3",children:[e.jsx("label",{className:"form-label",children:"Name (optional)"}),e.jsx("input",{id:"cron-name",name:"name",type:"text",placeholder:"My task",defaultValue:l==null?void 0:l.name,className:"form-control"})]}),e.jsxs("div",{className:"mb-3",children:[e.jsx("label",{className:"form-label",children:"Schedule (cron)"}),e.jsxs("select",{className:"form-select mb-2",onChange:t=>{const a=t.target.value;a&&(document.getElementById("cron-schedule").value=a)},children:[e.jsx("option",{value:"",children:"Select preset…"}),J.map(t=>e.jsxs("option",{value:t.value,children:[t.label," (",t.value,")"]},t.value))]}),e.jsx("input",{id:"cron-schedule",name:"schedule",type:"text",placeholder:"* * * * *",defaultValue:l==null?void 0:l.schedule,className:"form-control",required:!0})]}),e.jsxs("div",{className:"mb-0",children:[e.jsx("label",{className:"form-label",children:"Command"}),e.jsx("textarea",{id:"cron-execstr",name:"execstr",rows:3,placeholder:"/usr/bin/php /www/wwwroot/script.php",defaultValue:l==null?void 0:l.execstr,className:"form-control",required:!0})]})]}),e.jsxs(i.Footer,{children:[e.jsx(h,{type:"button",variant:"secondary",onClick:()=>{n(!1),c(null),o(null)},children:"Cancel"}),e.jsx(h,{type:"submit",variant:"primary",disabled:y,children:y?"Saving…":r?"Update":"Create"})]})]},r??"new")]}),e.jsx("div",{className:"alert alert-warning small mb-3",children:'Jobs are stored in the panel. Click "Apply to System" to sync them to the system crontab (root).'}),e.jsx("div",{className:"card",children:e.jsxs(M,{children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsx("th",{children:"Name"}),e.jsx("th",{children:"Schedule"}),e.jsx("th",{children:"Command"}),e.jsx("th",{className:"text-end",children:"Actions"})]})}),e.jsx("tbody",{children:u.length===0?e.jsx("tr",{children:e.jsx("td",{colSpan:4,className:"p-0",children:e.jsx(V,{title:"No cron jobs",description:'Click "Add Cron" to create one.'})})}):u.map(t=>e.jsxs("tr",{children:[e.jsx("td",{children:t.name||"—"}),e.jsx("td",{children:e.jsx("code",{className:"small",children:t.schedule})}),e.jsx("td",{className:"small font-monospace text-truncate",style:{maxWidth:280},title:t.execstr,children:t.execstr}),e.jsxs("td",{className:"text-end",children:[e.jsx("button",{type:"button",className:"btn btn-link btn-sm text-primary p-1",title:"Edit",onClick:()=>I(t),children:e.jsx("i",{className:"ti ti-edit","aria-hidden":!0})}),e.jsx("button",{type:"button",className:"btn btn-link btn-sm text-danger p-1",title:"Delete",onClick:()=>P(t.id),children:e.jsx("i",{className:"ti ti-trash","aria-hidden":!0})})]})]},t.id))})]})})]})}export{K as CrontabPage};