import axios from "axios";
import { useState } from "react";
import {
  Button,
  Card,
  Col,
  Container,
  Form,
  Row,
  Stack,
} from "react-bootstrap";
import Link from "next/link";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import * as Icons from "@fortawesome/free-solid-svg-icons";
import { toast } from "react-toastify";

const AddHolidaysList = () => {
  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();
      const empData = JSON.parse(localStorage.getItem("session") || "");

      data.append("holidays_file", file);
      setIsDocuments(false);

      const response = await axios.post(`/api/upload/holidays`, data, {
        headers: { "content-type": "multipart/form-data" },
      });
      console.log(response, "upload response");
      setIsDocuments(true);
      toast.success("Holiday File 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 className="text-start">Holidays List Upload</b>
                <Link
                  className="btn btn-light btn-sm"
                  style={{ float: "right" }}
                  href="/holidays_template.csv"
                >
                  Download Template
                  <FontAwesomeIcon
                    icon={Icons.faFileArrowDown}
                    className="ms-1"
                  />
                </Link>
              </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="holidays_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 AddHolidaysList;
