import axios from "axios";
import { useState } from "react";
import {
  Button,
  Card,
  Col,
  Container,
  Form,
  Row,
  Stack,
} from "react-bootstrap";
import { toast } from "react-toastify";

const SalaryBulkUpload = () => {
  const [file, setFile] = useState("");
  const [isdocuments, setIsDocuments] = useState(false);

  const handleFile = (event: any) => {
    setFile(event.target.files[0]);
    console.log(event.target.files[0], "new file");
  };
  const handleDocument = async (event: React.FormEvent<HTMLFormElement>) => {
    try {
      event.preventDefault();
      const data = new FormData();
      data.append("salary_file", file);
      setIsDocuments(false);

      const response = await axios.post(`/api/upload/bulksalaryupload`, data, {
        headers: { "content-type": "multipart/form-data" },
      });
      console.log(response, "upload response");
      setIsDocuments(true);
      toast.success("Bulk Salary Uploaded Successfully");
    } catch (error: any) {
      setIsDocuments(false);
      toast.error(error.response.data.results);
      console.log(error, "Error Fetching Upload  Data");
    }
  };
  return (
    <>
      <Container>
        <Row>
          <Col>
            <Card className="my-2 docCard">
              <Card.Header className="text-white">
                <b>Salary Bulk Upload</b>
              </Card.Header>
              <Card.Body>
                <Form onSubmit={handleDocument} encType="multipart/form-data">
                  <Row className="mb-3">
                    <Form.Group
                      className="mb-3"
                      as={Col}
                      controlId="formGridFile"
                    >
                      <Form.Label>File</Form.Label>
                      <Stack direction="horizontal" gap={3}>
                        <Form.Control
                          className="me-auto"
                          type="file"
                          name="salary_file"
                          placeholder="Add your item here..."
                          onChange={handleFile}
                          // multiple
                        />
                        <Button variant="secondary" type="submit">
                          Upload
                        </Button>
                      </Stack>
                    </Form.Group>
                  </Row>
                </Form>
              </Card.Body>
            </Card>
          </Col>
        </Row>
      </Container>
    </>
  );
};

export default SalaryBulkUpload;
