import React, { useEffect, useState } from 'react' import { isEmpty } from 'lodash' import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout' import FormInput from '@/Components/FormInput' import Button from '@/Components/Button' import { Head, useForm } from '@inertiajs/react' import FormInputWith from '@/Components/FormInputWith' import LocationSelectionInput from '../Location/SelectionInput' import Checkbox from '@/Components/Checkbox' import { DEFAULT_EXPIRED_UNIT } from '@/constant' import { toFixed } from '@/utils' import FormInputNumeric from '@/Components/FormInputNumeric' export default function Form(props) { const { profile, levels, expireds } = props const [use_level, setUseLevel] = useState(false) const { data, setData, post, processing, errors } = useForm({ location_id: null, name: '', quota: '', display_note: '', expired: '', expired_unit: DEFAULT_EXPIRED_UNIT, description: '', min_stock: 0, display_price: 0, discount: 0, price_poin: 0, bonus_poin: 0, prices: null, }) const handleOnChange = (event) => { setData( event.target.name, event.target.type === 'checkbox' ? event.target.checked ? 1 : 0 : event.target.value ) } const handleUseLevel = () => { setUseLevel(!use_level) if (!use_level === true) { const prices = levels.map((level) => { return { level: level, customer_level_id: level.id, display_price: 0, discount: 0, price_poin: 0, bonus_poin: 0, } }) setData('prices', prices) return } setData('prices', null) } const handleSetLevelPrice = (id, event) => { setData( 'prices', data.prices.map((price) => { if (price.customer_level_id === id) { price[event.target.name] = event.target.value } return price }) ) } const handleSubmit = () => { if (isEmpty(profile) === false) { post(route('location-profile.update', profile)) return } post(route('location-profile.store')) } useEffect(() => { if (isEmpty(profile) === false) { if (profile.prices.length > 0) { setUseLevel(true) } setData({ name: profile.name, quota: profile.quota, display_note: profile.display_note, description: profile.description, min_stock: profile.min_stock, display_price: profile.display_price, discount: profile.discount, price_poin: profile.price_poin, bonus_poin: profile.bonus_poin, expired: profile.expired, expired_unit: profile.expired_unit, location_id: profile.location_id, prices: profile.prices.map((price) => { return { ...price, display_price: price.display_price, discount: price.discount, price_poin: price.price_poin, bonus_poin: price.bonus_poin, } }), }) } }, [profile]) return (
Profile Lokasi
setData('location_id', id)} error={errors.location_id} />
%
} name="discount" value={data.discount} onChange={handleOnChange} error={errors.discount} formClassName={'pr-10'} label="Discount" />
handleUseLevel(e.target.value)} />
{errors.prices && (

{errors.prices}

)} {data.prices?.map((price) => ( ))}
Level Harga Diskon Harga Poin Bonus Poin
{price.level.name} handleSetLevelPrice( price.customer_level_id, e ) } /> handleSetLevelPrice( price.customer_level_id, e ) } /> handleSetLevelPrice( price.customer_level_id, e ) } /> handleSetLevelPrice( price.customer_level_id, e ) } />
) }