import * as React from "react"; import { Box, Typography, TextField, Checkbox, Button } from "@mui/material"; import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider"; import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs"; import { DesktopDatePicker } from "@mui/x-date-pickers/DesktopDatePicker"; import Table from "@mui/material/Table"; import TableBody from "@mui/material/TableBody"; import TableCell from "@mui/material/TableCell"; import TableRow from "@mui/material/TableRow"; import TableContainer from "@mui/material/TableContainer"; import Paper from "@mui/material/Paper"; import { DataGrid, GridColDef, GridSelectionModel, GridToolbar } from "@mui/x-data-grid"; import MenuItem from "@mui/material/MenuItem"; import Select, { SelectChangeEvent } from "@mui/material/Select"; import { PrivilegesProps, DiscountProps } from "./types"; import useStore, { StoreState } from "../../../../store"; import theme from "../../../../theme"; const columns: GridColDef[] = [ { field: "id", headerName: "ID", width: 30, sortable: false, }, { field: "name", headerName: "Название скидки", width: 200, sortable: false, }, { field: "endless", headerName: "Бесконечная", width: 120, sortable: false, }, { field: "from", headerName: "От", width: 120, sortable: false, }, { field: "dueTo", headerName: "До", width: 120, sortable: false, }, { field: "privileges", headerName: "Привилегии", width: 210, sortable: false, }, { field: "active", headerName: "Активна", width: 100, sortable: false, }, { field: "basketMore", headerName: "Корзина больше", width: 140, sortable: false, } , { field: "toTime", headerName: "На время", width: 140, sortable: false, } , { field: "toCapacity", headerName: "На объем", width: 140, sortable: false, } ]; const rows:Array = [ { id: 1, name: "Скидка 1", endless: false, from: "", dueTo: "", privileges: [ { good: "Товар 1", discount: 0.3 }, { good: "Товар 2", discount: 0.2 } ], active: false,incomeMore:1, basketMore: 10, toTime: 20, toCapacity: 30 }, { id: 2, name: "Скидка 2", endless: false, from: "", dueTo: "", privileges: [ { good: "Товар 3", discount: 0.3 }, { good: "Товар 4", discount: 0.2 } ], active: true,incomeMore:1, basketMore: 10, toTime: 20, toCapacity: 30 }, { id: 3, name: "Скидка 3", endless: false, from: "", dueTo: "", privileges: [ { good: "Товар 5", discount: 0.3 }, { good: "Товар 6", discount: 0.2 } ], active: false, incomeMore: 1, basketMore: 10, toTime: 20, toCapacity: 30 }, ]; const Discounts: React.FC = () => { const [checkboxState, setCheckboxState] = React.useState(false); const toggleCheckbox = () => { setCheckboxState( !checkboxState ); } const [value1, setValue1] = React.useState( new Date() ); const [value2, setValue2] = React.useState( new Date() ); const [service, setService] = React.useState("Шаблонизатор"); const handleChange = (event: SelectChangeEvent) => { setService(event.target.value as string); }; const { discountsArray, discountsArraySet } = useStore((state) => state); //discountsArraySet( rows ); const { discountsActiveArray, discountsActiveArraySet } = useStore((state) => state); let discountsActiveArrayUpdated:Array; const findActiveDiscounts = () => { const actives:Array = []; discountsArray.forEach( (item, i) => { if( item.active == true ) { actives.push( i ); } } ); discountsActiveArrayUpdated = [ ...actives ]; if( JSON.stringify(discountsActiveArray) != JSON.stringify(discountsActiveArrayUpdated) ) { discountsActiveArraySet( discountsActiveArrayUpdated ); } } findActiveDiscounts(); const discountsArrayConverted = discountsArray.map( (item) => { const basketMorePerc = Math.round(Number(item.basketMore) * 100) + "%"; const toTimePerc = Math.round(Number(item.toTime) * 100) + "%"; const toCapacityPerc = Math.round(Number(item.toCapacity) * 100) + "%"; const dateFrom = item.from ? new Date( Number(item.from) ) : ""; const dateDueTo = item.from ? new Date( Number(item.dueTo) ) : ""; const strFrom = dateFrom ? `${dateFrom.getDate()}.${dateFrom.getMonth()}.${dateFrom.getFullYear()}` : "-" const strDueTo = dateDueTo ? `${dateDueTo.getDate()}.${dateDueTo.getMonth()}.${dateDueTo.getFullYear()}` : "-" if( item.privileges.length ) { const result = item.privileges.reduce( (acc, privilege) => { acc = acc ? `${acc}, ${privilege.good} - ${privilege.discount}%` : `${privilege.good} - ${ Math.round(privilege.discount * 100) }%`; return acc; }, "" ); return { ...item, privileges: result, from: strFrom, dueTo: strDueTo, basketMore: basketMorePerc, toTime: toTimePerc, toCapacity: toCapacityPerc } } else { return { ...item, from: strFrom, dueTo: strDueTo, basketMore: basketMorePerc, toTime: toTimePerc, toCapacity: toCapacityPerc } } } ); const createDiscount = ( name:string, discount: number, addedMore: number, basketMore: number, toTime: number, toCapacity: number, ) => { const newDiscount = { id: new Date().getTime(), name, endless: checkboxState, incomeMore: addedMore, from: checkboxState ? "" : new Date( value1 ).getTime() +"", dueTo: checkboxState ? "" : new Date( value2 ).getTime() +"", privileges: [{ good: service, discount: discount / 100 }], active: false, basketMore: basketMore / 100, toTime: toTime / 100, toCapacity: toCapacity / 100 } const discountsArrayUpdated = [ ...discountsArray, newDiscount ]; discountsArraySet( discountsArrayUpdated ); } const fieldName = React.useRef(null); const fieldDiscount = React.useRef(null); const fieldAddedMore = React.useRef(null); const basketMore = React.useRef(null); const toTime = React.useRef(null); const toCapacity = React.useRef(null); const cleraAddedMore = () => { if (fieldAddedMore.current) { fieldAddedMore.current.value = ""; } } const checkFields = () => { if( fieldName.current != null && fieldDiscount.current != null && fieldAddedMore.current != null && basketMore.current != null && toTime.current != null && toCapacity.current != null ) { createDiscount( fieldName.current.value, Number(fieldDiscount.current.value), Number(fieldAddedMore.current.value), Number(basketMore.current.value), Number(toTime.current.value), Number(toCapacity.current.value) ); } } const { discountsSelectedRowsData, discountsSelectedRowsDataSet } = useStore((state) => state); const onRowsSelectionHandler = ( ids:GridSelectionModel ) => { const result:Array = []; ids.forEach((id) => discountsArray.forEach( (row) => { if(row.id === id) result.push(row); } ) ); discountsSelectedRowsDataSet( [ ...result ] ); }; const activation = ( value:boolean ) => { discountsArray.forEach( (item, i) => { discountsSelectedRowsData.forEach( (selected) => { if( item.id == selected.id ) { if( value ) { discountsArray[ i ].active = true; } else { discountsArray[ i ].active = false; } } } ); } ); discountsArraySet( discountsArray ); } return ( СКИДКИ Название: Условия: cleraAddedMore() } /> Работает, если заплатите 100500 денег Вы должны будете продать душу дьяволу
Дата действия: С { if(e) { setValue1(e) } } } renderInput={(params) => } InputProps={{ sx: { height: "40px", color: theme.palette.secondary.main, border: "1px solid", borderColor: theme.palette.secondary.main, "& .MuiSvgIcon-root": { color: theme.palette.secondary.main } } }} /> по { if(e) { setValue2(e) } } } renderInput={(params) => } InputProps={{ sx: { height: "40px", color: theme.palette.secondary.main, border: "1px solid", borderColor: theme.palette.secondary.main, "& .MuiSvgIcon-root": { color: theme.palette.secondary.main } } }} /> toggleCheckbox() } /> Бессрочно
onRowsSelectionHandler( ids ) } />
); } export default Discounts;