import{j as e,al as W,ak as M,B as k,T as H,d as J,I as K,b as Q}from"./mui-vendor-CkIVaoQP.js";import{r as n}from"./react-vendor-CmFgShSM.js";import{L as m,j as A,h as f,A as V,G as i,C as Z,v as ee,O as te,X as N,S as w}from"./index-Vwb3Bce5.js";import{a as Y,u as z}from"./redux-vendor-B7uoacJt.js";import{R as ue}from"./index.esm-Bu7XMDZU.js";import{R as se}from"./ResponsiveTable-DugzNG82.js";import{d as ae}from"./Add-C-nZhvlM.js";import"./iconBase-BEJYyZsk.js";import"./FilterAlt-CTDmPS36.js";import"./styled-components.browser.esm-gS9mh05b.js";const ne=Y("PROVINCE_POULTRY_SCIENCE_EXPERTS",async(l,{dispatch:c})=>{c(m());const{data:s,status:d}=await A.get("get-all-poultry-for-poultry-science/");return c(f()),{data:s,status:d}}),re=Y("PROVINCE_POLICY_UPDATE_LIMITATIONS_EDIT",async(l,{dispatch:c})=>{c(m());try{const{data:s,status:d}=await A.put("poultry_science/0/",l);return c(f()),{data:s,status:d}}catch(s){return c(f()),{error:s.response.data.result}}}),oe=({item:l,updateTable:c,poultryExperts:s})=>{const[d,x]=n.useState(),h=z(),[p]=n.useContext(V),S=s?s.map(t=>{var r,o;return{id:t.id,label:`${t.unitName||"-"} (${((r=t.user)==null?void 0:r.fullname)||"-"} - ${((o=t.user)==null?void 0:o.mobile)||"-"})`}}):[],y=l!=null&&l.poultry?l.poultry.map(t=>{var r,o;return{id:t.id,label:`${t.unitName||"-"} (${((r=t.user)==null?void 0:r.fullname)||"-"} - ${((o=t.user)==null?void 0:o.mobile)||"-"})`}}):[],g=[...y,...S.filter(t=>!y.some(r=>r.id===t.id))],C=y,E=()=>{h(re({poultry:d,poultry_science_id:l.id})).then(t=>{t.payload.error?p({vertical:"top",horizontal:"center",msg:t.payload.error,severity:"error"}):(c(),h(Z()),p({vertical:"top",horizontal:"center",msg:"عملیات با موفقیت انجام شد.",severity:"success"}))})};return e.jsxs(i,{container:!0,xs:12,justifyContent:"center",direction:"column",gap:2,children:[e.jsx(i,{children:e.jsx(W,{disablePortal:!0,multiple:!0,defaultValue:C,id:"province",options:g,getOptionLabel:t=>t.label,isOptionEqualToValue:(t,r)=>t.id===r.id,onChange:(t,r)=>{x(r.map(o=>o.id))},renderInput:t=>e.jsx(M,{...t,label:"مرغداری را انتخاب  کنید"})})}),e.jsx(k,{fullWidth:!0,variant:"contained",onClick:E,children:"ثبت"})]})},le=()=>{const[,,,l,,c]=n.useContext(V),s=z();n.useEffect(()=>{const a=ee(new Date).format("YYYY-MM-DD");l(a),c(a)},[]);const d=a=>{r(a.target.value)},[x,h]=n.useState([]),[p,S]=n.useState([]),[y,g]=n.useState(0),[C,E]=n.useState(10),[t,r]=n.useState(""),[o,I]=n.useState(1),[B,G]=n.useState([]),P=async a=>{let u;s(m()),u=await A.get(`poultry_science/?search=filter&value=${t}&role=${N()}&page=${a}&page_size=${C}`),s(f()),h(u.data.results),g(u.data.count)},U=a=>{P(a),I(a)},X=a=>{E(a),I(1)},q=()=>{s(ne()).then(a=>{S(a.payload.data)})},v=()=>{q(),P(o!==0?o:1)};n.useEffect(()=>{v()},[s]),n.useEffect(()=>{const a=x==null?void 0:x.map((u,j)=>{var T,_,O,$,R;return[o===1?j+1:j+C*(o-1)+1,(T=u==null?void 0:u.user)==null?void 0:T.fullname,(_=u==null?void 0:u.user)==null?void 0:_.mobile,e.jsx(H,{variant:"caption",children:(O=u==null?void 0:u.poultry)!=null&&O.length?(R=($=u==null?void 0:u.poultry)==null?void 0:$.map((b,ce)=>{var F,L;return`${b.unitName} (${(F=b.user)==null?void 0:F.fullname} - ${(L=b.user)==null?void 0:L.mobile})`}))==null?void 0:R.join(" - "):"-"},j),e.jsx(J,{title:"مدیریت مرغدار زیر مجموعه",placement:"right",children:e.jsx(K,{disabled:!(p!=null&&p.length),color:"primary",onClick:()=>{s(te({title:"افزودن مرغدار زیرمجموعه",content:e.jsx(oe,{updateTable:v,item:u,poultryExperts:p})}))},children:e.jsx(ae,{})})},j)]});G(a)},[x]),n.useEffect(()=>{P(1)},[s,C]);const D=async a=>{a.preventDefault(),s(m());try{const u=await A.get(`poultry_science/?role=${N()}&search=filter&value=${t}&page=1&page_size=${C}`);h(u.data.results),g(u.data.count),s(f())}catch(u){console.error("Error fetching data:",u)}};return e.jsxs(i,{container:!0,xs:12,justifyContent:"center",alignItems:"center",gap:2,children:[e.jsx(i,{container:!0,xs:12,justifyContent:"start",alignItems:"center",gap:2,children:e.jsx(i,{children:e.jsxs("form",{onSubmit:D,children:[e.jsx(M,{id:"outlined-basic",size:"small",label:"جستجو",variant:"outlined",style:{width:250},onChange:d}),e.jsx(k,{type:"submit",onClick:D,endIcon:e.jsx(ue,{}),children:"جستجو"})]})})}),e.jsx(se,{data:B,columns:["ردیف","نام کامل","موبایل","مرغداران زیر مجموعه","عملیات"],handlePageChange:U,totalRows:y,page:o,perPage:C,handlePerRowsChange:X,title:"کارشناسان علوم دامی"})]})},me=()=>e.jsx(e.Fragment,{children:e.jsx(Q,{display:"flex",justifyContent:"center",children:e.jsx(i,{container:!0,direction:"column",justifyContent:"center",xs:12,sm:12,md:10,lg:10,children:e.jsx(i,{container:!0,justifyContent:"space-between",mt:w.MEDIUM,gap:w.SMALL,children:e.jsx(i,{xs:12,children:e.jsx(le,{})})})})})});export{me as default};
