import axios from "axios";
import { useState } from "react";
import { Button, Card, Col, Container, Form, Row, Stack } from "react-bootstrap";
import { toast } from "react-toastify";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import * as Icons from "@fortawesome/free-solid-svg-icons";
import Link from "next/link";
import MonthAttendList from "./MonthAttendList";

const AttendanceMonthBulkUpload = ()=>{
    const [file, setFile] = useState("");
    const [isuploaded, setIsUploaded] = useState(false);

  
    const handleFile = (event: any) => {
      setFile(event.target.files[0]);
      console.log(event.target.files[0], "new file");
    };
    const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
      try {
        event.preventDefault();
        const data = new FormData();
        data.append("attendance_file", file);
        setIsUploaded(false);
  
        const response = await axios.post(`/api/upload/monthattendanceupload`, data, {
          headers: { "content-type": "multipart/form-data" },
        });
        console.log(response, "upload response");
        setIsUploaded(true);
        toast.success("Bulk Attendance Uploaded Successfully");
      } catch (error: any) {
        setIsUploaded(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">Monthly Attendance Bulk Upload</b>
                <Link
              href="/monthly_attendance_template.csv"
              className="btn btn-light btn-sm ms-2 "
              style={{ float: "right" }}
            >
              Download Template
              <FontAwesomeIcon icon={Icons.faFileArrowDown} className="ms-1" />
            </Link>
              </Card.Header>
              <Card.Body>
                <Form onSubmit={handleSubmit} 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="attendace_file"
                          placeholder="Add your item here..."
                          onChange={handleFile}
                          // multiple
                        />
                        <Button variant="secondary" type="submit">
                          Upload
                        </Button>
                      </Stack>
                    </Form.Group>
                  </Row>
                </Form>
              </Card.Body>
            </Card>

            <MonthAttendList isuploaded={isuploaded}/>
                </Col>
            </Row>
        </Container>
        
        </>
    )
}

export default AttendanceMonthBulkUpload;