import { Button, Card, Col, Container, Form, Modal, Row } from "react-bootstrap";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import * as Icons from "@fortawesome/free-solid-svg-icons";
import { useState } from "react";
import { toast } from "react-toastify";
import axios from "axios";
import styled from "@emotion/styled";
import DatePicker from "react-datepicker";
import EmpIDFetch from "./EmpIDFetch";


const DateInputWrapper = styled.div`
  .react-datepicker__navigation--years-upcoming {
    background-image: url(https://img.icons8.com/plumpy/24/null/collapse-arrow.png);
    background-repeat: no-repeat;
    background-position: center;
    background-size: inherit;
  }
  .react-datepicker__navigation--years-previous {
    background-image: url(https://img.icons8.com/plumpy/24/null/chevron-down.png);
    background-repeat: no-repeat;
    background-position: center;
    background-size: inherit;
  }
`;
const AttendanceModal=({setIsAdded}:any)=>{
  const [show, setShow] = useState(false);
  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);
  const [attdate, setAttDate] = useState(new Date());

  const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
    try {
      event.preventDefault();
      const empData = JSON.parse(localStorage.getItem("session") || "");
      console.log(empData)
      const data = new FormData(event.currentTarget);
      let emp_id = data.get("emp_id");
      let total_work_days = data.get("total_work_days");
      let att_date = attdate.toISOString().substring(0, 10);
      setIsAdded(false);
      const response: any = await axios.post(
        `/api/user/get_attendance_days?&emp_id=${emp_id}&total_work_days=${total_work_days}&att_date=${att_date}`
      );
      console.log("response: ", response.data);
      setIsAdded(true);
      toast.success("Attendance added successfully");
    } catch (error: any) {
        setIsAdded(false);
      toast.error(error.response.data.results);
      console.log(error, "error while adding");
    }
  };

    return(
        <>
        <Container>
            <Row>
                <Col className="my-3">
                <Button variant="outline-dark" className="ms-2" onClick={handleShow} style={{
                      float:"right"
                    }}>
                        ADD&nbsp;
            <FontAwesomeIcon
                    icon={Icons.faCirclePlus}
                    style={{
                      fontSize: "1em",
                    }}
                    
                  />
                
                  </Button>
                  
                  <Modal show={show} onHide={handleClose} centered>
                    <Modal.Header closeButton>
                      <Modal.Title style={{fontSize:"1rem"}}>Add Attendance</Modal.Title>
                    </Modal.Header>
                    <Modal.Body >
                    <Form  onSubmit={handleSubmit}>

                    <Row className="mb-3">
                      <Form.Group as={Col} sm={12} md={12} lg={4} controlId="formGridBName">
                        <Form.Label>Employee ID</Form.Label>
                        <Form.Select name="emp_id">
                          <EmpIDFetch/>
                        </Form.Select>
                      </Form.Group>
                    <Form.Group as={Col} sm={12} md={12} lg={4} className="mb-3" controlId="formGridBranch">
                      <Form.Label>Total Work Days</Form.Label>
                      <Form.Control type="text" name="total_work_days" placeholder="" />
                    </Form.Group>
                    <Form.Group as={Col} sm={12} md={12} lg={4} className="mb-3">
                <Form.Label>Attendance Date</Form.Label>
                <DateInputWrapper>
                  <DatePicker
                    className="form-control"
                    name="att_date"
                    dateFormat="yyyy-MM-dd"
                    selected={attdate}
                    onChange={(date: any) => setAttDate(date)}
                  />
                </DateInputWrapper>
              </Form.Group>
                     
                    </Row>
                    <Row className="d-flex justify-content-between p-3">
                    <Button variant="primary" type="submit" size="sm" className="w-25" onClick={handleClose}>
                        Submit
                      </Button>
                      <Button variant="secondary" size="sm" className="w-25" onClick={handleClose}>
            Cancel
          </Button>
          </Row>
                    </Form>

                    </Modal.Body>
                  

                  </Modal>
                </Col>
            </Row>
        </Container>
        
        </>
    )
}

export default AttendanceModal;