'use client' import React from 'react' import { Button, Typography, Box, ToggleButton, ToggleButtonGroup, InputAdornment, IconButton } from '@mui/material' import Grid from '@mui/material/Grid2' import CustomTextField from '@/@core/components/mui/TextField' import { PurchaseOrderFormData, TransactionCost } from '@/types/apps/purchaseOrderTypes' import CustomAutocomplete from '@/@core/components/mui/Autocomplete' import ImageUpload from '@/components/ImageUpload' interface PurchaseSummaryProps { formData: PurchaseOrderFormData handleInputChange: (field: keyof PurchaseOrderFormData, value: any) => void } const PurchaseSummary: React.FC = ({ formData, handleInputChange }) => { // Initialize transaction costs if not exist const transactionCosts = formData.transactionCosts || [] // Options for transaction cost types const transactionCostOptions = [ { label: 'Biaya Admin', value: 'admin' }, { label: 'Pajak', value: 'pajak' }, { label: 'Materai', value: 'materai' }, { label: 'Lainnya', value: 'lainnya' } ] // Add new transaction cost const addTransactionCost = () => { const newCost: TransactionCost = { id: Date.now().toString(), type: '', name: '', amount: '' } handleInputChange('transactionCosts', [...transactionCosts, newCost]) } // Remove transaction cost const removeTransactionCost = (id: string) => { const filtered = transactionCosts.filter((cost: TransactionCost) => cost.id !== id) handleInputChange('transactionCosts', filtered) } // Update transaction cost const updateTransactionCost = (id: string, field: keyof TransactionCost, value: string) => { const updated = transactionCosts.map((cost: TransactionCost) => cost.id === id ? { ...cost, [field]: value } : cost ) handleInputChange('transactionCosts', updated) } // Calculate discount amount based on percentage or fixed amount const calculateDiscount = () => { if (!formData.discountValue) return 0 const subtotal = formData.subtotal || 0 if (formData.discountType === 'percentage') { return (subtotal * parseFloat(formData.discountValue)) / 100 } return parseFloat(formData.discountValue) } const discountAmount = calculateDiscount() const shippingCost = parseFloat(formData.shippingCost || '0') // Calculate total transaction costs const totalTransactionCost = transactionCosts.reduce((sum: number, cost: TransactionCost) => { return sum + parseFloat(cost.amount || '0') }, 0) const downPayment = parseFloat(formData.downPayment || '0') // Calculate total (subtotal - discount + shipping + transaction costs) const total = (formData.subtotal || 0) - discountAmount + shippingCost + totalTransactionCost // Calculate remaining balance (total - down payment) const remainingBalance = total - downPayment const handleUpload = async (file: File): Promise => { // Simulate upload return new Promise(resolve => { setTimeout(() => { resolve(URL.createObjectURL(file)) }, 1000) }) } return ( {/* Left Side - Pesan and Attachment */} {/* Pesan Section */} {formData.showPesan && ( ) => handleInputChange('pesan', e.target.value)} /> )} {/* Attachment Section */} {formData.showAttachment && ( )} {/* Right Side - Totals */} {/* Sub Total */} Sub Total {new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR', minimumFractionDigits: 0 }).format(formData.subtotal || 0)} {/* Additional Options */} {/* Tambah Diskon */} {/* Show input form when showTambahDiskon is true */} {formData.showTambahDiskon && ( ) => handleInputChange('discountValue', e.target.value) } sx={{ flex: 1 }} InputProps={{ endAdornment: formData.discountType === 'percentage' ? ( % ) : undefined }} /> { if (newValue) handleInputChange('discountType', newValue) }} size='small' > % Rp )} {/* Biaya Pengiriman */} {/* Show input form when showBiayaPengiriman is true */} {formData.showBiayaPengiriman && ( Biaya pengiriman ) => handleInputChange('shippingCost', e.target.value) } sx={{ flex: 1 }} InputProps={{ startAdornment: Rp }} /> )} {/* Biaya Transaksi - Multiple */} {/* Show multiple transaction cost inputs */} {formData.showBiayaTransaksi && ( {transactionCosts.map((cost: TransactionCost, index: number) => ( {/* Remove button */} removeTransactionCost(cost.id)} sx={{ color: 'error.main', border: '1px solid', borderColor: 'error.main', borderRadius: '50%', width: 28, height: 28, '&:hover': { backgroundColor: 'error.lighter' } }} > {/* Type AutoComplete */} (typeof option === 'string' ? option : option.label)} value={transactionCostOptions.find(option => option.value === cost.type) || null} onChange={(_, newValue) => { updateTransactionCost(cost.id, 'type', newValue ? newValue.value : '') }} renderInput={params => ( )} sx={{ minWidth: 180 }} noOptionsText='Tidak ada pilihan' /> {/* Name input */} ) => updateTransactionCost(cost.id, 'name', e.target.value) } sx={{ flex: 1 }} /> {/* Amount input */} ) => updateTransactionCost(cost.id, 'amount', e.target.value) } sx={{ width: 120 }} InputProps={{ startAdornment: Rp }} /> ))} {/* Add more button */} )} {/* Total */} Total {new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR', minimumFractionDigits: 0 }).format(total)} {/* Uang Muka */} {/* Dropdown */} (typeof option === 'string' ? option : option.label)} value={{ label: '1-10003 Gi...', value: '1-10003' }} onChange={(_, newValue) => { // Handle change if needed }} renderInput={params => } sx={{ minWidth: 120 }} /> {/* Amount input */} ) => handleInputChange('downPayment', e.target.value) } sx={{ width: '80px' }} inputProps={{ style: { textAlign: 'center' } }} /> {/* Percentage/Fixed toggle */} { if (newValue) handleInputChange('downPaymentType', newValue) }} size='small' > % Rp {/* Right side text */} Uang muka {downPayment > 0 ? downPayment.toLocaleString('id-ID') : '0'} {/* Sisa Tagihan */} Sisa Tagihan {new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR', minimumFractionDigits: 0 }).format(remainingBalance)} {/* Save Button */} ) } export default PurchaseSummary