import React, { useState } from 'react';
import axios from 'axios';

const FormPage = ({ table }) => {
  const [formData, setFormData] = useState({
    gross_total_income: '',
    net_taxable_income: '',
    tax_payable: '',
    total_tax_payable: '',
    refund: ''
  });

  const handleChange = e => {
    setFormData({ ...formData, [e.target.name]: e.target.value });
  };

  const handleSubmit = async e => {
    e.preventDefault();
    await axios.post(`http://localhost:5000/add/${table}`, formData);
    alert('Entry saved');
    setFormData({
      gross_total_income: '',
      net_taxable_income: '',
      tax_payable: '',
      total_tax_payable: '',
      refund: ''
    });
  };

  return (
    <form onSubmit={handleSubmit}>
      {Object.keys(formData).map(key => (
        <div key={key}>
          <label>{key.replaceAll('_', ' ')}:</label>
          <input
            type="number"
            step="0.01"
            name={key}
            value={formData[key]}
            onChange={handleChange}
            required
          />
        </div>
      ))}
      <button type="submit">Save</button>
    </form>
  );
};

export default FormPage;
