import{j as e,s as I}from"./index-3808da28.js";import{a6 as P,u as D,F as f,d as L,r as w,C as N,y as R,B as j,a7 as _,J as q,I as v,P as O,T as A,a8 as B,m as p}from"./vendor-61f0f29a.js";import{v as o}from"./v4-82fce61c.js";const{Title:z}=B,H=()=>{const{id:i}=P(),[F]=D(),g=F.get("edit")==="true",m=i&&!g,[n]=f.useForm(),k=L(),[d,u]=w.useState(!1),[h,y]=w.useState([{id:o(),name:""}]),V=[{title:"名称",dataIndex:"name",render:(t,s,a)=>e.jsx(f.Item,{name:["items",a,"name"],rules:[{required:!0,message:"请输入名称"}],style:{margin:0},preserve:!0,children:e.jsx(v,{placeholder:"请输入名称",maxLength:100})})},{title:"操作",width:100,render:(t,s,a)=>e.jsx(j,{type:"link",danger:!0,disabled:h.length===1||m,onClick:()=>S(s.id,a),children:"删除"})}],S=(t,s)=>{const r=(n.getFieldsValue().items||[]).filter((x,b)=>b!==s),c=h.filter((x,b)=>b!==s);y(c),n.setFieldsValue({items:r})},T=()=>{const t={id:o(),name:""},s=n.getFieldValue("items")||[];y(a=>[...a,t]),n.setFieldsValue({items:[...s,t]})},C=async()=>{var t;try{u(!0);const{data:s,error:a}=await I.from("resources").select("*").eq("id",i).single();if(a)throw a;if(s!=null&&s.attributes){const l=((t=s.attributes.items)==null?void 0:t.map(r=>({...r,id:r.id||o(),name:r.name||""})))||[{id:o(),name:""}];n.setFieldsValue({taskName:s.attributes.taskName,items:l}),y(l)}}catch(s){p.error("获取任务详情失败，请稍后重试"),console.error("获取任务详情失败:",s)}finally{u(!1)}};w.useEffect(()=>{i&&C()},[i]);const E=async t=>{try{if(u(!0),t.items.some(r=>{var c;return!((c=r.name)!=null&&c.trim())})){p.error("请填写所有任务项的名称");return}const a={type:"task",attributes:{taskName:t.taskName.trim(),items:t.items.map((r,c)=>{var x;return{id:h[c].id,name:(x=r.name)==null?void 0:x.trim()}})}};i&&(a.id=i);const{error:l}=await I.from("resources").upsert(a).select();if(l)throw l;p.success("保存成功"),k("/resource/task")}catch(s){p.error("保存失败，请稍后重试"),console.error("保存失败:",s)}finally{u(!1)}};return e.jsx("div",{className:"bg-gradient-to-b from-gray-50 to-white min-h-screen p-6",children:e.jsx(N,{className:"shadow-lg rounded-lg border-0",title:e.jsxs("div",{className:"flex justify-between items-center py-2",children:[e.jsxs("div",{className:"flex items-center space-x-3",children:[e.jsx(z,{level:4,className:"mb-0 text-gray-800",children:i?g?"编辑任务":"查看任务":"新建任务"}),e.jsx("span",{className:"text-gray-400 text-sm",children:i?g?"请修改任务信息":"任务详情":"请填写任务信息"})]}),e.jsxs(R,{size:"middle",children:[e.jsx(j,{icon:e.jsx(_,{}),onClick:()=>k("/resource/task"),children:"返回"}),!m&&e.jsx(j,{type:"primary",icon:e.jsx(q,{}),onClick:()=>n.submit(),loading:d,children:"保存"})]})]}),children:e.jsxs(f,{form:n,onFinish:E,layout:"vertical",initialValues:{items:[{id:o()}]},disabled:m||d,children:[e.jsx(N,{className:"mb-4 shadow-sm rounded-lg",title:e.jsxs("div",{className:"flex items-center",children:[e.jsx("div",{className:"w-1 h-4 bg-blue-500 rounded-full mr-2"}),e.jsx("span",{children:"基本信息"})]}),bordered:!1,children:e.jsx(f.Item,{name:"taskName",label:"任务名称",rules:[{required:!0,message:"请输入任务名称"}],children:e.jsx(v,{placeholder:"请输入任务名称",maxLength:100})})}),e.jsx(N,{className:"shadow-sm rounded-lg",title:e.jsxs("div",{className:"flex items-center",children:[e.jsx("div",{className:"w-1 h-4 bg-green-500 rounded-full mr-2"}),e.jsx("span",{children:"任务明细"})]}),extra:!m&&e.jsx(j,{type:"dashed",icon:e.jsx(O,{}),onClick:T,disabled:d,children:"新增一栏"}),bordered:!1,children:e.jsx(A,{dataSource:h,columns:V,pagination:!1,rowKey:"id",loading:d})})]})})})};export{H as default};
//# sourceMappingURL=index-fd856c91.js.map
