import { useEffect, useState } from "react";
import { Button, Card, Col, Form, Modal, Row } from "react-bootstrap";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import * as Icons from "@fortawesome/free-solid-svg-icons";
import axios from "axios";
import { toast } from "react-toastify";
import styled from "@emotion/styled";
import ManagerAsignList from "./ManagerAsignList";
import EmpIDFetch from "./EmpIDFetch";

const Wrapper = styled.div`
  .card {
    border-radius: 0px;
    border: 0px;
    .card-text {
      font-size: 1rem;
      color: #686565;
    }
  }
`;
const ManagerForm = () => {
  const [show, setShow] = useState(false);
  const [empId, setEmpId] = useState();
  const [userName, setUserName] = useState();
  const [email, setEmail] = useState();
  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);

  const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
    try {
      let emp_id = empId;
      let name = userName;
      const response = await axios.post(
        `/api/user/manage_app_auth?&emp_id=${emp_id}&name=${name}&email=${email}`
      );
      console.log(response);
      toast.success("Manager created successfully");
    } catch (error:any) {
      console.log(error);
      toast.error(error.response.data.results)
    }
    event.preventDefault();
  };

  return (
    <>
    
      <Wrapper>
        <Card>
          <Card.Body>
            <Row>
              <Col>
                <Button
                  variant="outline-dark"
                  onClick={handleShow}
                  style={{
                    float: "right",
                  }}
                >
                  ADD &nbsp;
                  <FontAwesomeIcon
                    className="ms-2"
                    icon={Icons.faCirclePlus}
                    style={{
                      fontSize: "1em",
                    }}
                  />
                </Button>
                <Modal show={show} onHide={handleClose} centered>
                  <Modal.Header closeButton>
                    <Modal.Title style={{ fontSize: "1rem" }}>
                      Add Manager
                    </Modal.Title>
                  </Modal.Header>
                  <Modal.Body>
                    <Form onSubmit={handleSubmit}>
                      <Row className="mb-3">
                        <Form.Group
                          as={Col}
                          lg="6"
                          sm="12"
                          controlId="formGridEdu"
                        >
                          <Form.Label>Employee Id</Form.Label>
                          <Form.Select
                            name="emp_id"
                            onChange={(e: any) => setEmpId(e.target.value)}
                            placeholder=""
                            required
                          >
                            <EmpIDFetch/>
                          </Form.Select>
                        </Form.Group>
                        <Form.Group
                          as={Col}
                          lg="6"
                          sm="12"
                          className="mb-3"
                          controlId="formGridUniversity"
                        >
                          <Form.Label>Enter Name</Form.Label>
                          <Form.Control
                            placeholder=""
                            name="name"
                            onChange={(e: any) => setUserName(e.target.value)}
                            required
                          />
                        </Form.Group>

                        <Form.Group
                          as={Col}
                          lg="6"
                          sm="12"
                          className="mb-3"
                          controlId="formGridBoard"
                        >
                          <Form.Label>Enter Email Id</Form.Label>
                          <Form.Control
                            placeholder=""
                            name="email"
                            onChange={(e: any) => setEmail(e.target.value)}
                            required
                          />
                        </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>
          </Card.Body>
        </Card>
      </Wrapper>
     
    </>
  );
};
export default ManagerForm;
