inogen/app/admin/pages/FinancialPage.tsx
2026-10-18 18:12:59 +03:30

26 lines
4.4 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Pencil, Plus } from "lucide-react";
import { useEffect,useState } from "react";
import { Link } from "react-router";
import { Card,EmptyState,ErrorBanner,PageTitle,Select } from "../components/Ui";
import { api } from "../lib/api";
import type { FinancialYearItem,PagedResult } from "../types";
import { ListToolbar, PaginationControls, useDebouncedValue } from "../components/ListControls";
import { ArchiveButton } from "../components/ArchiveButton";
import { BulkArchiveActions, SelectionCheckbox } from "../components/BulkArchive";
const money=(value?:number)=>value==null?"—":new Intl.NumberFormat("fa-IR",{maximumFractionDigits:8}).format(value);
export function FinancialPage(){
const[data,setData]=useState<PagedResult<FinancialYearItem>|null>(null);const[error,setError]=useState("");
const[page,setPage]=useState(1);const[pageSize,setPageSize]=useState(25);const[search,setSearch]=useState("");const[status,setStatus]=useState("");const[loading,setLoading]=useState(true);const debouncedSearch=useDebouncedValue(search);
const[selected,setSelected]=useState<Set<string>>(new Set());
useEffect(()=>{let active=true;setLoading(true);setError("");const query=new URLSearchParams({page:String(page),pageSize:String(pageSize)});if(debouncedSearch)query.set("search",debouncedSearch);if(status)query.set("status",status);void api<PagedResult<FinancialYearItem>>(`/api/v1/financial-years?${query}`).then(x=>{if(active)setData(x);}).catch(e=>{if(active)setError(e.message);}).finally(()=>{if(active)setLoading(false);});return()=>{active=false;};},[page,pageSize,debouncedSearch,status]);
const remove=(id:string)=>setData(old=>old?{...old,items:old.items.filter(x=>x.id!==id),totalCount:old.totalCount-1}:old);
return <><PageTitle title="اطلاعات مالی" description="سال مالی، چهار فصل و جمع‌های محاسبه‌شده با واحد میلیون ریال" action={<Link className="button link-button" to="/admin/financial/new"><Plus size={18}/>سال مالی جدید</Link>}/>{error&&<ErrorBanner message={error}/>}<Card>
<ListToolbar search={search} onSearch={value=>{setSearch(value);setPage(1);}} total={data?.totalCount??0} placeholder="سال مالی"><label className="list-filter">وضعیت<Select value={status} onChange={e=>{setStatus(e.target.value);setPage(1);}}><option value="">همه</option><option value="0">پیش‌نویس</option><option value="1">منتشرشده</option><option value="2">بایگانی</option></Select></label></ListToolbar>
{data?.items.length?<BulkArchiveActions entityType="FinancialYear" records={data.items} selected={selected} onSelectionChange={setSelected} onArchived={ids=>setData(old=>old?{...old,items:old.items.filter(x=>!ids.includes(x.id)),totalCount:old.totalCount-ids.length}:old)}/>:null}
{!data?<div className="loading-line">در حال دریافت اطلاعات…</div>:data.items.length===0?<EmptyState title="اطلاعات مالی یافت نشد" description="فیلتر را تغییر دهید یا اولین سال مالی را ثبت کنید."/>:<><div className={`table-wrap${loading?" is-loading":""}`}><table><thead><tr><th>انتخاب</th><th>سال</th><th>بودجه مصوب</th><th>هزینه نوآوری</th><th>درآمد کل</th><th>درآمد محصولات جدید</th><th>تحقیق و توسعه</th><th>سرمایه‌گذاری</th><th>عملیات</th></tr></thead><tbody>{data.items.map(x=><tr key={x.id}><td><SelectionCheckbox checked={selected.has(x.id)} onChange={checked=>setSelected(old=>{const next=new Set(old);checked?next.add(x.id):next.delete(x.id);return next;})} label={`انتخاب سال ${x.fiscalYear}`}/></td><td>{x.fiscalYear}</td><td>{money(x.totals.approvedInnovationBudget)}</td><td>{money(x.totals.innovationExpense)}</td><td>{money(x.totals.companyTotalRevenue)}</td><td>{money(x.totals.newProductsRevenue)}</td><td>{money(x.totals.researchDevelopmentExpense)}</td><td>{money(x.totals.investmentAmount)}</td><td><div className="row-actions"><Link className="row-action" to={`/admin/financial/${x.id}/edit`}><Pencil size={15}/>ویرایش</Link><ArchiveButton entityType="FinancialYear" id={x.id} rowVersion={x.rowVersion} onArchived={()=>remove(x.id)}/></div></td></tr>)}</tbody></table></div><PaginationControls page={page} pageSize={pageSize} total={data.totalCount} loading={loading} onPage={setPage} onPageSize={value=>{setPageSize(value);setPage(1);}}/></>}
</Card></>;
}