2 lines
32 KiB
JavaScript
2 lines
32 KiB
JavaScript
import{u as e,j as a}from"./vendor-query-a3e439f2.js";import{r,u as t,f as s}from"./vendor-react-ac1483bd.js";import{c as d,a as i,e as l,d as n,b as c,u as o}from"./vendor-forms-f89aa741.js";import{o as m}from"./yup-bff05cf1.js";import{b as g,c as x,d as u}from"./_hooks-05707864.js";import{u as p}from"./_hooks-9d916060.js";import{u as h}from"./_hooks-8b9f7cf5.js";import{c as y}from"./vendor-toast-598db4db.js";import{h as b,A as j,b as f,d as v}from"./_requests-35c9d4c3.js";import{P as k}from"./_models-0008c1da.js";import{M as N}from"./MultiSelectAutocomplete-a5a00ba6.js";import{B as _,L as w,P as C,F as S}from"./index-590deac5.js";import{I as V}from"./Input-dc2009a3.js";import{V as F,W as D,Q as O,Y as E,Z as I,X as M,m as $,x as z,y as L,P,z as A}from"./vendor-ui-8a3c5c7d.js";const B=()=>e({mutationFn:async e=>{var a;const r=new FormData;r.append("file",e),r.append("name","uploaded-file");const t=await b(j(v.UPLOAD_FILE),r,{headers:{"Content-Type":"multipart/form-data"}});if(!(null==(a=t.data)?void 0:a.file))throw new Error("Invalid upload response");return{id:t.data.file.id.toString(),url:t.data.file.url}},onError:e=>{y.error((null==e?void 0:e.message)||"خطا در آپلود فایل")}}),R=()=>e({mutationFn:async e=>(await f(j(v.DELETE_FILE(e)))).data,onSuccess:()=>{y.success("فایل با موفقیت حذف شد")},onError:e=>{y.error((null==e?void 0:e.message)||"خطا در حذف فایل")}}),T=({onUpload:e,onRemove:t,acceptedTypes:s=["image/*","video/*"],maxFileSize:d=10485760,maxFiles:i=10,label:l="فایلها",description:n="تصاویر و ویدیوها را اینجا بکشید یا کلیک کنید",error:c,disabled:o=!1,className:m=""})=>{const[g,x]=r.useState([]),[u,p]=r.useState(!1),h=r.useRef(null),y=e=>e.startsWith("image/"),b=e=>{if(0===e)return"0 Bytes";const a=Math.floor(Math.log(e)/Math.log(1024));return parseFloat((e/Math.pow(1024,a)).toFixed(2))+" "+["Bytes","KB","MB","GB"][a]},j=r.useCallback(async a=>{const r=(e=>{if(d&&e.size>d)return`حجم فایل نباید بیشتر از ${b(d)} باشد`;if(s.length>0&&!s.some(a=>"image/*"===a?e.type.startsWith("image/"):"video/*"===a?e.type.startsWith("video/"):e.type===a))return"نوع فایل پشتیبانی نمیشود";return i&&g.length>=i?`حداکثر ${i} فایل مجاز است`:null})(a);if(r){const e={id:Math.random().toString(36).substr(2,9),name:a.name,size:a.size,type:a.type,progress:0,status:"error",error:r};return void x(a=>[...a,e])}const t=Math.random().toString(36).substr(2,9),l=await(e=>new Promise(a=>{if(y(e.type)){const r=new FileReader;r.onload=e=>{var r;return a(null==(r=e.target)?void 0:r.result)},r.readAsDataURL(e)}else a("")}))(a),n={id:t,name:a.name,size:a.size,type:a.type,preview:l,progress:0,status:"uploading"};x(e=>[...e,n]);try{const r=setInterval(()=>{x(e=>e.map(e=>e.id===t&&e.progress<90?{...e,progress:e.progress+10}:e))},200),s=await e(a);clearInterval(r),x(e=>e.map(e=>e.id===t?{...e,progress:100,status:"completed",url:s.url,id:s.id}:e))}catch(c){x(e=>e.map(e=>e.id===t?{...e,status:"error",error:c.message||"خطا در آپلود فایل"}:e))}},[e,i,d,s]),f=r.useCallback(e=>{Array.from(e).forEach(e=>{j(e)})},[j]),v=r.useCallback(e=>{if(e.preventDefault(),p(!1),o)return;const a=e.dataTransfer.files;f(a)},[o,f]),k=r.useCallback(e=>{e.preventDefault(),o||p(!0)},[o]),N=r.useCallback(e=>{e.preventDefault(),p(!1)},[]);return a.jsxs("div",{className:`space-y-4 ${m}`,children:[l&&a.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300",children:l}),a.jsxs("div",{className:`\n relative border-2 border-dashed rounded-lg p-6 transition-colors cursor-pointer\n ${u?"border-primary-400 bg-primary-50 dark:bg-primary-900/20":"border-gray-300 dark:border-gray-600"}\n ${o?"opacity-50 cursor-not-allowed":"hover:border-primary-400 hover:bg-gray-50 dark:hover:bg-gray-700"}\n ${c?"border-red-300 bg-red-50 dark:bg-red-900/20":""}\n `,onDrop:v,onDragOver:k,onDragLeave:N,onClick:()=>{var e;o||null==(e=h.current)||e.click()},children:[a.jsx("input",{ref:h,type:"file",multiple:!0,accept:s.join(","),className:"hidden",onChange:e=>e.target.files&&f(e.target.files),disabled:o}),a.jsxs("div",{className:"text-center",children:[a.jsx(F,{className:"mx-auto h-12 w-12 text-gray-400"}),a.jsxs("div",{className:"mt-4",children:[a.jsx("p",{className:"text-sm text-gray-600 dark:text-gray-400",children:n}),a.jsxs("p",{className:"text-xs text-gray-500 dark:text-gray-500 mt-1",children:["حداکثر ",b(d)," • ",s.join(", ")]})]})]})]}),c&&a.jsxs("p",{className:"text-sm text-red-600 dark:text-red-400 flex items-center gap-1",children:[a.jsx(D,{className:"h-4 w-4"}),c]}),g.length>0&&a.jsxs("div",{className:"space-y-3",children:[a.jsxs("h4",{className:"text-sm font-medium text-gray-700 dark:text-gray-300",children:["فایلهای آپلود شده (",g.length,")"]}),a.jsx("div",{className:"space-y-2",children:g.map(e=>a.jsxs("div",{className:"flex items-center gap-3 p-3 bg-gray-50 dark:bg-gray-700 rounded-lg",children:[a.jsx("div",{className:"flex-shrink-0",children:e.preview?a.jsx("img",{src:e.preview,alt:e.name,className:"w-10 h-10 object-cover rounded"}):a.jsx("div",{className:"w-10 h-10 bg-gray-200 dark:bg-gray-600 rounded flex items-center justify-center",children:y(e.type)?a.jsx(O,{className:"h-5 w-5 text-gray-500"}):a.jsx(E,{className:"h-5 w-5 text-gray-500"})})}),a.jsxs("div",{className:"flex-1 min-w-0",children:[a.jsx("p",{className:"text-sm font-medium text-gray-900 dark:text-gray-100 truncate",children:e.name}),a.jsx("p",{className:"text-xs text-gray-500 dark:text-gray-400",children:b(e.size)}),"uploading"===e.status&&a.jsxs("div",{className:"mt-1",children:[a.jsx("div",{className:"w-full bg-gray-200 dark:bg-gray-600 rounded-full h-1.5",children:a.jsx("div",{className:"bg-primary-600 h-1.5 rounded-full transition-all duration-300",style:{width:`${e.progress}%`}})}),a.jsxs("p",{className:"text-xs text-gray-500 mt-1",children:[e.progress,"%"]})]}),"error"===e.status&&e.error&&a.jsxs("p",{className:"text-xs text-red-600 dark:text-red-400 mt-1 flex items-center gap-1",children:[a.jsx(D,{className:"h-3 w-3"}),e.error]})]}),a.jsxs("div",{className:"flex items-center gap-2",children:["completed"===e.status&&a.jsx(I,{className:"h-5 w-5 text-green-500"}),"error"===e.status&&a.jsx(D,{className:"h-5 w-5 text-red-500"}),a.jsx(_,{variant:"secondary",size:"sm",onClick:a=>{var r;null==a||a.stopPropagation(),r=e.id,x(e=>e.filter(e=>e.id!==r)),null==t||t(r)},className:"p-1 h-8 w-8",children:a.jsx(M,{className:"h-4 w-4"})})]})]},e.id))})]})]})},U=({variant:e,onSave:t,onCancel:s,isEdit:d=!1})=>{const[i,l]=r.useState(e||{enabled:!0,fee_percentage:0,profit_percentage:0,stock_limit:0,stock_managed:!0,stock_number:0,weight:0,attributes:{},meta:{},images:[]}),[n,c]=r.useState((null==e?void 0:e.images)||[]),[o,m]=r.useState((null==e?void 0:e.attributes)||{}),[g,x]=r.useState((null==e?void 0:e.meta)||{}),[u,p]=r.useState(""),[h,y]=r.useState(""),[b,j]=r.useState(""),[f,v]=r.useState(""),{mutateAsync:k}=B(),{mutate:N}=R(),w=(e,a)=>{l(r=>({...r,[e]:a}))},C=e=>{const a=n.filter(a=>a.id!==e);c(a),N(e)};return a.jsxs("div",{className:"space-y-6 bg-gray-50 dark:bg-gray-700 p-6 rounded-lg border",children:[a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsx("h4",{className:"text-lg font-medium text-gray-900 dark:text-gray-100",children:d?"ویرایش Variant":"افزودن Variant جدید"}),a.jsxs("div",{className:"flex gap-2",children:[a.jsx(_,{variant:"secondary",onClick:s,children:"انصراف"}),a.jsx(_,{onClick:()=>{const e={...i,images:n,attributes:o,meta:g};t(e)},children:d?"بهروزرسانی":"افزودن"})]})]}),a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4",children:[a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"درصد کارمزد"}),a.jsx("input",{type:"number",value:i.fee_percentage,onChange:e=>w("fee_percentage",parseFloat(e.target.value)||0),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-primary-500 dark:bg-gray-700 dark:text-gray-100",placeholder:"0",min:"0",max:"100",step:"0.1"})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"درصد سود"}),a.jsx("input",{type:"number",value:i.profit_percentage,onChange:e=>w("profit_percentage",parseFloat(e.target.value)||0),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-primary-500 dark:bg-gray-700 dark:text-gray-100",placeholder:"0",min:"0",max:"100",step:"0.1"})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"وزن (گرم)"}),a.jsx("input",{type:"number",value:i.weight,onChange:e=>w("weight",parseFloat(e.target.value)||0),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-primary-500 dark:bg-gray-700 dark:text-gray-100",placeholder:"0",min:"0",step:"0.1"})]})]}),a.jsxs("div",{children:[a.jsx("h5",{className:"text-md font-medium text-gray-900 dark:text-gray-100 mb-3",children:"مدیریت موجودی"}),a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:[a.jsxs("div",{className:"flex items-center space-x-3 space-x-reverse",children:[a.jsx("input",{type:"checkbox",checked:i.stock_managed,onChange:e=>w("stock_managed",e.target.checked),className:"w-4 h-4 text-primary-600 bg-gray-100 border-gray-300 rounded focus:ring-primary-500"}),a.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-300",children:"مدیریت موجودی فعال باشد"})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"تعداد موجودی"}),a.jsx("input",{type:"number",value:i.stock_number,onChange:e=>w("stock_number",parseInt(e.target.value)||0),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-primary-500 dark:bg-gray-700 dark:text-gray-100",placeholder:"0",min:"0",disabled:!i.stock_managed})]}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"حد کمینه موجودی"}),a.jsx("input",{type:"number",value:i.stock_limit,onChange:e=>w("stock_limit",parseInt(e.target.value)||0),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-primary-500 dark:bg-gray-700 dark:text-gray-100",placeholder:"0",min:"0",disabled:!i.stock_managed})]})]})]}),a.jsxs("div",{children:[a.jsx("h5",{className:"text-md font-medium text-gray-900 dark:text-gray-100 mb-3",children:"تصاویر Variant"}),a.jsx(T,{onUpload:async e=>{try{const a=await k(e),r={id:a.id,url:a.url,alt:e.name,order:n.length},t=[...n,r];return c(t),a}catch(a){throw a}},onRemove:C,acceptedTypes:["image/*"],maxFileSize:5242880,maxFiles:5,label:"",description:"تصاویر مخصوص این variant را آپلود کنید"}),n.length>0&&a.jsx("div",{className:"mt-4",children:a.jsx("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-3",children:n.map((e,r)=>a.jsxs("div",{className:"relative group",children:[a.jsx("img",{src:e.url,alt:e.alt||`تصویر ${r+1}`,className:"w-full h-20 object-cover rounded-lg border"}),a.jsx("button",{type:"button",onClick:()=>C(e.id),className:"absolute -top-1 -right-1 w-5 h-5 bg-red-500 text-white rounded-full flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity",children:"×"})]},e.id))})})]}),a.jsxs("div",{children:[a.jsx("h5",{className:"text-md font-medium text-gray-900 dark:text-gray-100 mb-3",children:"ویژگیهای Variant"}),a.jsxs("div",{className:"flex gap-3 mb-3",children:[a.jsx("input",{type:"text",value:u,onChange:e=>p(e.target.value),placeholder:"نام ویژگی (مثل: رنگ، سایز)",className:"flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-primary-500 dark:bg-gray-700 dark:text-gray-100"}),a.jsx("input",{type:"text",value:h,onChange:e=>y(e.target.value),placeholder:"مقدار (مثل: قرمز، بزرگ)",className:"flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-primary-500 dark:bg-gray-700 dark:text-gray-100"}),a.jsxs(_,{type:"button",variant:"secondary",onClick:()=>{if(u.trim()&&h.trim()){const e={...o,[u.trim()]:h.trim()};m(e),p(""),y("")}},className:"flex items-center gap-2",children:[a.jsx($,{className:"h-4 w-4"}),"افزودن"]})]}),Object.keys(o).length>0&&a.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-2",children:Object.entries(o).map(([e,r])=>a.jsxs("div",{className:"flex items-center justify-between bg-white dark:bg-gray-600 px-3 py-2 rounded-md border",children:[a.jsxs("span",{className:"text-sm",children:[a.jsxs("strong",{children:[e,":"]})," ",String(r)]}),a.jsx("button",{type:"button",onClick:()=>(e=>{const a={...o};delete a[e],m(a)})(e),className:"text-red-500 hover:text-red-700",children:a.jsx(L,{className:"h-3 w-3"})})]},e))})]}),a.jsxs("div",{children:[a.jsx("h5",{className:"text-md font-medium text-gray-900 dark:text-gray-100 mb-3",children:"Meta Data"}),a.jsxs("div",{className:"flex gap-3 mb-3",children:[a.jsx("input",{type:"text",value:b,onChange:e=>j(e.target.value),placeholder:"کلید Meta",className:"flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-primary-500 dark:bg-gray-700 dark:text-gray-100"}),a.jsx("input",{type:"text",value:f,onChange:e=>v(e.target.value),placeholder:"مقدار Meta",className:"flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-primary-500 dark:bg-gray-700 dark:text-gray-100"}),a.jsxs(_,{type:"button",variant:"secondary",onClick:()=>{if(b.trim()&&f.trim()){const e={...g,[b.trim()]:f.trim()};x(e),j(""),v("")}},className:"flex items-center gap-2",children:[a.jsx($,{className:"h-4 w-4"}),"افزودن"]})]}),Object.keys(g).length>0&&a.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-2",children:Object.entries(g).map(([e,r])=>a.jsxs("div",{className:"flex items-center justify-between bg-white dark:bg-gray-600 px-3 py-2 rounded-md border",children:[a.jsxs("span",{className:"text-sm",children:[a.jsxs("strong",{children:[e,":"]})," ",String(r)]}),a.jsx("button",{type:"button",onClick:()=>(e=>{const a={...g};delete a[e],x(a)})(e),className:"text-red-500 hover:text-red-700",children:a.jsx(L,{className:"h-3 w-3"})})]},e))})]}),a.jsxs("div",{className:"flex items-center space-x-3 space-x-reverse",children:[a.jsx("input",{type:"checkbox",checked:i.enabled,onChange:e=>w("enabled",e.target.checked),className:"w-4 h-4 text-primary-600 bg-gray-100 border-gray-300 rounded focus:ring-primary-500"}),a.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-300",children:"Variant فعال باشد"})]})]})},W=({variants:e,onChange:t,disabled:s=!1})=>{const[d,i]=r.useState(!1),[l,n]=r.useState(null),c=()=>{n(null),i(!0)};return a.jsxs("div",{className:"space-y-4",children:[a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsxs("h3",{className:"text-lg font-medium text-gray-900 dark:text-gray-100",children:["Variants محصول (",e.length,")"]}),!s&&!d&&a.jsxs(_,{onClick:c,className:"flex items-center gap-2",children:[a.jsx($,{className:"h-4 w-4"}),"افزودن Variant"]})]}),d&&a.jsx(U,{variant:null!==l?e[l]:void 0,onSave:a=>{if(null!==l){const r=[...e];r[l]=a,t(r)}else t([...e,a]);i(!1),n(null)},onCancel:()=>{i(!1),n(null)},isEdit:null!==l}),e.length>0&&a.jsx("div",{className:"space-y-3",children:e.map((r,d)=>a.jsx("div",{className:"bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-lg p-4",children:a.jsxs("div",{className:"flex items-center justify-between",children:[a.jsxs("div",{className:"flex-1",children:[a.jsxs("div",{className:"flex items-center gap-4 mb-2",children:[a.jsxs("h4",{className:"font-medium text-gray-900 dark:text-gray-100",children:["Variant ",d+1]}),a.jsx("span",{className:"inline-flex items-center px-2 py-1 rounded-full text-xs font-medium "+(r.enabled?"bg-green-100 text-green-800":"bg-red-100 text-red-800"),children:r.enabled?"فعال":"غیرفعال"})]}),a.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4 text-sm text-gray-600 dark:text-gray-400",children:[a.jsxs("div",{children:[a.jsx("strong",{children:"درصد کارمزد:"})," ",r.fee_percentage,"%"]}),a.jsxs("div",{children:[a.jsx("strong",{children:"درصد سود:"})," ",r.profit_percentage,"%"]}),a.jsxs("div",{children:[a.jsx("strong",{children:"موجودی:"})," ",r.stock_managed?`${r.stock_number} عدد`:"بدون محدودیت"]}),a.jsxs("div",{children:[a.jsx("strong",{children:"وزن:"})," ",r.weight," گرم"]})]}),r.images&&r.images.length>0&&a.jsxs("div",{className:"flex gap-2 mt-3",children:[r.images.slice(0,3).map((e,r)=>a.jsx("img",{src:e.url,alt:e.alt||`تصویر ${r+1}`,className:"w-12 h-12 object-cover rounded border"},e.id)),r.images.length>3&&a.jsxs("div",{className:"w-12 h-12 bg-gray-100 dark:bg-gray-600 rounded border flex items-center justify-center text-xs",children:["+",r.images.length-3]})]}),Object.keys(r.attributes).length>0&&a.jsxs("div",{className:"mt-2",children:[a.jsx("div",{className:"text-xs text-gray-500 dark:text-gray-400 mb-1",children:"ویژگیها:"}),a.jsx("div",{className:"flex flex-wrap gap-1",children:Object.entries(r.attributes).map(([e,r])=>a.jsxs("span",{className:"inline-flex items-center px-2 py-1 rounded-full text-xs bg-blue-100 text-blue-800",children:[e,": ",String(r)]},e))})]})]}),!s&&a.jsxs("div",{className:"flex gap-2",children:[a.jsx("button",{onClick:()=>(e=>{n(e),i(!0)})(d),className:"p-2 text-blue-600 hover:bg-blue-50 dark:hover:bg-blue-900/20 rounded-md",title:"ویرایش",children:a.jsx(z,{className:"h-4 w-4"})}),a.jsx("button",{onClick:()=>(a=>{const r=e.filter((e,r)=>r!==a);t(r)})(d),className:"p-2 text-red-600 hover:bg-red-50 dark:hover:bg-red-900/20 rounded-md",title:"حذف",children:a.jsx(L,{className:"h-4 w-4"})})]})]})},d))}),0===e.length&&!d&&a.jsxs("div",{className:"text-center py-8 bg-gray-50 dark:bg-gray-700 rounded-lg border-2 border-dashed border-gray-300 dark:border-gray-600",children:[a.jsx(P,{className:"h-12 w-12 text-gray-400 mx-auto mb-4"}),a.jsx("p",{className:"text-gray-500 dark:text-gray-400 mb-4",children:"هنوز هیچ Variant ای اضافه نشده"}),!s&&a.jsxs(_,{onClick:c,className:"flex items-center gap-2 mx-auto",children:[a.jsx($,{className:"h-4 w-4"}),"افزودن اولین Variant"]})]})]})},q=d({name:i().required("نام محصول الزامی است").min(2,"نام محصول باید حداقل 2 کاراکتر باشد"),description:i().default(""),design_style:i().default(""),enabled:l().default(!0),total_sold:n().min(0).default(0),type:n().oneOf([0,1,2,3]).default(0),category_ids:c().of(n()).default([]),product_option_id:n().optional().nullable(),attributes:d().default({}),images:c().of(d()).default([]),variants:c().default([])}),G=()=>{var e,d,i;const l=t(),{id:n}=s(),c=!!n,[y,b]=r.useState([]),[j,f]=r.useState({}),[v,F]=r.useState(""),[D,O]=r.useState(""),{data:E,isLoading:I}=g(n||"",c),{data:z,isLoading:P}=p(),{data:U,isLoading:G}=h(),{mutate:H,isPending:J}=x(),{mutate:K,isPending:Q}=u(),{mutateAsync:Y}=B(),{mutate:Z}=R(),X=J||Q,{register:ee,handleSubmit:ae,formState:{errors:re,isValid:te,isDirty:se},setValue:de,watch:ie,reset:le,control:ne}=o({resolver:m(q),mode:"onChange",defaultValues:{name:"",description:"",design_style:"",enabled:!0,total_sold:0,type:1,category_ids:[],product_option_id:void 0,attributes:{},images:[],variants:[]}}),ce=ie();r.useEffect(()=>{var e;if(c&&E){const a=(null==(e=E.variants)?void 0:e.map(e=>({id:e.id,enabled:e.enabled,fee_percentage:e.fee_percentage,profit_percentage:e.profit_percentage,stock_limit:e.stock_limit,stock_managed:e.stock_managed,stock_number:e.stock_number,weight:e.weight,attributes:e.attributes||{},meta:e.meta||{},images:e.images||[]})))||[];le({name:E.name,description:E.description||"",design_style:E.design_style||"",enabled:E.enabled,total_sold:E.total_sold||0,type:1,category_ids:E.category_ids||[],product_option_id:E.product_option_id||void 0,attributes:E.attributes||{},images:E.images||[],variants:a}),b(E.images||[]),f(E.attributes||{})}},[c,E,le]);const oe=e=>{const a=y.filter(a=>a.id!==e);b(a),de("images",a,{shouldValidate:!0,shouldDirty:!0}),Z(e)},me=()=>{l("/products")};if(c&&I)return a.jsx("div",{className:"flex justify-center items-center h-64",children:a.jsx(w,{})});const ge=(z||[]).map(e=>({id:e.id,title:e.name,description:e.description}));(U||[]).map(e=>({id:e.id,title:e.title,description:`تعداد گزینهها: ${(e.options||[]).length}`}));const xe=a.jsxs(_,{variant:"secondary",onClick:me,className:"flex items-center gap-2",children:[a.jsx(A,{className:"h-4 w-4"}),"بازگشت"]});return a.jsxs(C,{className:"max-w-6xl mx-auto",children:[a.jsx(S,{title:c?"ویرایش محصول":"ایجاد محصول جدید",subtitle:c?"ویرایش اطلاعات محصول":"اطلاعات محصول جدید را وارد کنید",backButton:xe}),a.jsx("div",{className:"bg-white dark:bg-gray-800 shadow-sm border border-gray-200 dark:border-gray-700 rounded-lg p-6",children:a.jsxs("form",{onSubmit:ae(e=>{var a,r;const t={name:e.name,description:e.description,design_style:e.design_style,enabled:e.enabled,total_sold:e.total_sold,type:1,attributes:j,category_ids:e.category_ids.length>0?e.category_ids:[],product_option_id:e.product_option_id||void 0,images:y.map(e=>parseInt(e.id))};if(c&&n){const r=(null==(a=e.variants)?void 0:a.map(e=>({id:e.id||0,enabled:e.enabled,fee_percentage:e.fee_percentage,profit_percentage:e.profit_percentage,stock_limit:e.stock_limit,stock_managed:e.stock_managed,stock_number:e.stock_number,weight:e.weight,attributes:e.attributes&&Object.keys(e.attributes).length>0?e.attributes:{},meta:e.meta&&Object.keys(e.meta).length>0?e.meta:{}})))||[];K({id:parseInt(n),...t,variants:r},{onSuccess:()=>{l("/products")}})}else{const a=(null==(r=e.variants)?void 0:r.map(e=>({enabled:e.enabled,fee_percentage:e.fee_percentage,profit_percentage:e.profit_percentage,stock_limit:e.stock_limit,stock_managed:e.stock_managed,stock_number:e.stock_number,weight:e.weight,attributes:e.attributes&&Object.keys(e.attributes).length>0?e.attributes:{},meta:e.meta&&Object.keys(e.meta).length>0?e.meta:{}})))||[];H({...t,variants:a},{onSuccess:()=>{l("/products")}})}}),className:"space-y-8",children:[a.jsxs("div",{children:[a.jsx("h3",{className:"text-lg font-medium text-gray-900 dark:text-gray-100 mb-4",children:"اطلاعات پایه"}),a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6",children:[a.jsx("div",{className:"md:col-span-2",children:a.jsx(V,{label:"نام محصول",...ee("name"),error:null==(e=re.name)?void 0:e.message,placeholder:"نام محصول را وارد کنید"})}),a.jsxs("div",{className:"flex items-center space-x-3 space-x-reverse",children:[a.jsx("input",{type:"checkbox",...ee("enabled"),className:"w-4 h-4 text-primary-600 bg-gray-100 border-gray-300 rounded focus:ring-primary-500 dark:focus:ring-primary-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600"}),a.jsx("label",{className:"text-sm font-medium text-gray-700 dark:text-gray-300",children:"محصول فعال باشد"})]}),a.jsx(V,{label:"تعداد فروخته شده",type:"number",...ee("total_sold"),error:null==(d=re.total_sold)?void 0:d.message,placeholder:"0",min:"0"}),a.jsx(V,{label:"استایل طراحی",...ee("design_style"),error:null==(i=re.design_style)?void 0:i.message,placeholder:"مدرن، کلاسیک، مینیمال..."}),a.jsxs("div",{className:"md:col-span-2",children:[a.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"توضیحات"}),a.jsx("textarea",{...ee("description"),rows:4,className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-primary-500 dark:bg-gray-700 dark:text-gray-100",placeholder:"توضیحات کامل محصول را وارد کنید..."}),re.description&&a.jsx("p",{className:"text-red-500 text-sm mt-1",children:re.description.message})]})]})]}),a.jsxs("div",{children:[a.jsx("h3",{className:"text-lg font-medium text-gray-900 dark:text-gray-100 mb-4",children:"دستهبندی و گزینهها"}),a.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-6",children:[a.jsx(N,{label:"دستهبندیها",options:ge,selectedValues:ie("category_ids")||[],onChange:e=>de("category_ids",e,{shouldValidate:!0,shouldDirty:!0}),placeholder:"دستهبندیها را انتخاب کنید...",isLoading:P}),a.jsxs("div",{children:[a.jsx("label",{className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2",children:"گزینه محصول"}),a.jsxs("select",{...ee("product_option_id"),className:"w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-primary-500 dark:bg-gray-700 dark:text-gray-100",children:[a.jsx("option",{value:"",children:"بدون گزینه"}),(U||[]).map(e=>a.jsxs("option",{value:e.id,children:[e.title," (",(e.options||[]).length," گزینه)"]},e.id))]}),re.product_option_id&&a.jsx("p",{className:"text-red-500 text-sm mt-1",children:re.product_option_id.message})]})]})]}),a.jsxs("div",{children:[a.jsx("h3",{className:"text-lg font-medium text-gray-900 dark:text-gray-100 mb-4",children:"تصاویر محصول"}),a.jsx(T,{onUpload:async e=>{try{const a=await Y(e),r={id:a.id,url:a.url,alt:e.name,order:y.length},t=[...y,r];return b(t),de("images",t,{shouldValidate:!0,shouldDirty:!0}),a}catch(a){throw a}},onRemove:oe,acceptedTypes:["image/*"],maxFileSize:5242880,maxFiles:10,label:"",description:"تصاویر محصول را اینجا بکشید یا کلیک کنید"}),y.length>0&&a.jsxs("div",{className:"mt-6",children:[a.jsxs("h4",{className:"text-sm font-medium text-gray-700 dark:text-gray-300 mb-3",children:["تصاویر آپلود شده (",y.length,")"]}),a.jsx("div",{className:"grid grid-cols-2 md:grid-cols-4 lg:grid-cols-6 gap-4",children:y.map((e,r)=>a.jsxs("div",{className:"relative group",children:[a.jsx("img",{src:e.url,alt:e.alt||`تصویر ${r+1}`,className:"w-full h-24 object-cover rounded-lg border border-gray-200 dark:border-gray-600"}),a.jsx("button",{type:"button",onClick:()=>oe(e.id),className:"absolute -top-2 -right-2 w-6 h-6 bg-red-500 text-white rounded-full flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity",children:a.jsx(M,{className:"h-3 w-3"})}),0===r&&a.jsx("div",{className:"absolute bottom-1 left-1 bg-primary-500 text-white text-xs px-1 py-0.5 rounded",children:"اصلی"})]},e.id))})]})]}),a.jsx("div",{children:a.jsx(W,{variants:ie("variants")||[],onChange:e=>de("variants",e,{shouldValidate:!0,shouldDirty:!0})})}),a.jsxs("div",{children:[a.jsx("h3",{className:"text-lg font-medium text-gray-900 dark:text-gray-100 mb-4",children:"ویژگیهای سفارشی"}),a.jsxs("div",{className:"flex gap-3 mb-4",children:[a.jsx("input",{type:"text",value:v,onChange:e=>F(e.target.value),placeholder:"نام ویژگی (مثل: رنگ، سایز)",className:"flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-primary-500 dark:bg-gray-700 dark:text-gray-100"}),a.jsx("input",{type:"text",value:D,onChange:e=>O(e.target.value),placeholder:"مقدار (مثل: قرمز، بزرگ)",className:"flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-primary-500 dark:bg-gray-700 dark:text-gray-100"}),a.jsxs(_,{type:"button",variant:"secondary",onClick:()=>{if(v.trim()&&D.trim()){const e={...j,[v.trim()]:D.trim()};f(e),de("attributes",e,{shouldValidate:!0,shouldDirty:!0}),F(""),O("")}},className:"flex items-center gap-2",children:[a.jsx($,{className:"h-4 w-4"}),"افزودن"]})]}),Object.keys(j).length>0&&a.jsxs("div",{className:"space-y-2",children:[a.jsx("h4",{className:"text-sm font-medium text-gray-700 dark:text-gray-300",children:"ویژگیهای فعلی:"}),a.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-3",children:Object.entries(j).map(([e,r])=>a.jsxs("div",{className:"flex items-center justify-between bg-gray-50 dark:bg-gray-700 px-3 py-2 rounded-md",children:[a.jsxs("span",{className:"text-sm",children:[a.jsxs("strong",{children:[e,":"]})," ",String(r)]}),a.jsx("button",{type:"button",onClick:()=>(e=>{const a={...j};delete a[e],f(a),de("attributes",a,{shouldValidate:!0,shouldDirty:!0})})(e),className:"text-red-500 hover:text-red-700",children:a.jsx(L,{className:"h-4 w-4"})})]},e))})]})]}),ce.name&&a.jsxs("div",{className:"border border-gray-200 dark:border-gray-600 rounded-lg p-4 bg-gray-50 dark:bg-gray-700",children:[a.jsx("h3",{className:"text-sm font-medium text-gray-900 dark:text-gray-100 mb-3",children:"پیشنمایش محصول"}),a.jsxs("div",{className:"flex gap-4",children:[y.length>0&&a.jsx("img",{src:y[0].url,alt:ce.name,className:"w-20 h-20 object-cover rounded-lg border"}),a.jsxs("div",{className:"flex-1 space-y-2",children:[a.jsxs("div",{className:"text-sm text-gray-600 dark:text-gray-400",children:[a.jsx("strong",{children:"نام:"})," ",ce.name]}),a.jsxs("div",{className:"text-sm text-gray-600 dark:text-gray-400",children:[a.jsx("strong",{children:"نوع:"})," ",k[ce.type]]}),a.jsxs("div",{className:"text-sm text-gray-600 dark:text-gray-400",children:[a.jsx("strong",{children:"تعداد فروخته شده:"})," ",ce.total_sold]}),ce.design_style&&a.jsxs("div",{className:"text-sm text-gray-600 dark:text-gray-400",children:[a.jsx("strong",{children:"استایل:"})," ",ce.design_style]}),ce.category_ids&&ce.category_ids.length>0&&a.jsxs("div",{className:"text-sm text-gray-600 dark:text-gray-400",children:[a.jsx("strong",{children:"دستهبندیها:"})," ",null==z?void 0:z.filter(e=>ce.category_ids.includes(e.id)).map(e=>e.name).join(", ")]}),ce.variants&&ce.variants.length>0&&a.jsxs("div",{className:"text-sm text-gray-600 dark:text-gray-400",children:[a.jsx("strong",{children:"تعداد Variants:"})," ",ce.variants.length," نوع"]}),Object.keys(j).length>0&&a.jsxs("div",{className:"text-sm text-gray-600 dark:text-gray-400",children:[a.jsx("strong",{children:"ویژگیها:"})," ",Object.keys(j).length," مورد"]}),a.jsxs("div",{className:"flex items-center gap-2",children:[ce.enabled&&a.jsx("span",{className:"inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-green-100 text-green-800",children:"فعال"}),!ce.enabled&&a.jsx("span",{className:"inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-red-100 text-red-800",children:"غیرفعال"})]})]})]})]}),a.jsxs("div",{className:"flex justify-end space-x-4 space-x-reverse pt-6 border-t border-gray-200 dark:border-gray-600",children:[a.jsx(_,{type:"button",variant:"secondary",onClick:me,disabled:X,children:"انصراف"}),a.jsx(_,{type:"submit",loading:X,disabled:!te||X,children:c?"بهروزرسانی":"ایجاد محصول"})]})]})}),a.jsxs("div",{className:"bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg p-4",children:[a.jsx("h3",{className:"text-sm font-medium text-blue-900 dark:text-blue-100 mb-2",children:"راهنما"}),a.jsxs("ul",{className:"text-sm text-blue-700 dark:text-blue-300 space-y-1",children:[a.jsx("li",{children:"• نام محصول باید واضح و جذاب باشد"}),a.jsx("li",{children:"• میتوانید چندین دستهبندی برای محصول انتخاب کنید"}),a.jsx("li",{children:"• گزینه محصول برای محصولات متغیر (با رنگ، سایز و...) استفاده میشود"}),a.jsx("li",{children:"• ویژگیهای سفارشی برای اطلاعات اضافی محصول مفید هستند"}),a.jsx("li",{children:"• Variants برای انواع مختلف محصول استفاده میشود"}),a.jsx("li",{children:"• اولین تصویر به عنوان تصویر اصلی محصول استفاده میشود"})]})]})]})};export{G as default};
|