import styled from "@emotion/styled";
import axios from "axios";
import { Card, Form, Button, Row, Col } from "react-bootstrap";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import * as Icons from "@fortawesome/free-solid-svg-icons";
import { toast } from "react-toastify";

const FormWrapper = styled.div`
  .hideForm {
    display: none;
  }
`;
const UpdateAttendance = ({ setIsApplied, empattendancedata }: any) => {
  const handleSubmitAttendanceLogout = async (
    event: React.FormEvent<HTMLFormElement>
  ) => {
    try {
      event.preventDefault();
      const data = new FormData(event.currentTarget);
      const empData = JSON.parse(localStorage.getItem("session") || "");
      let attendance_id = data.get("attendance_id");
      let clockout = data.get("clockout");
      console.log(clockout);
      setIsApplied(false);

      const response: any = await axios.put(
        `/api/user/attendance?&clockout=${clockout}&attendance_id=${attendance_id}&emp_id=${empData.emp_id}`
      );
      console.log("response: ", response.data);
      setIsApplied(true);
      toast.success("Clock Out Successfully");
    } catch (error: any) {
      setIsApplied(false);
      toast.error(error.response.data.results);
      console.log(error, "applied attendance clockout error");
    }
  };
  // console.log({ empattendancedata });
  return (
    <>
      <Card className="my-2">
        <Card.Body className="p-0">
          <Form onSubmit={handleSubmitAttendanceLogout}>
            <FormWrapper>
              <Row className="mb-3 hideForm">
                <Form.Group
                  as={Col}
                  sm={12}
                  md={12}
                  lg={6}
                  controlId="formGridPassword"
                  className="mb-3"
                >
                  <Form.Label>A. ID</Form.Label>
                  <Form.Control
                    type="text"
                    name="attendance_id"
                    defaultValue={
                      empattendancedata && empattendancedata[0]?.attendance_id
                    }
                  />
                </Form.Group>
                <Form.Group as={Col} sm={12} md={12} lg={6} className="mb-3">
                  <Form.Label>Clock Out</Form.Label>
                  <Form.Control
                    type="text"
                    name="clockout"
                    defaultValue={new Date().toLocaleTimeString("en-US", {
                      hour12:true,
                    })}
                  />
                </Form.Group>
              </Row>
            </FormWrapper>
            <Button variant="outline-danger" type="submit" className="w-100">
              Clock Out
              <FontAwesomeIcon
                className="ms-2"
                icon={Icons.faArrowRightFromBracket}
              />
            </Button>
          </Form>
        </Card.Body>
      </Card>
    </>
  );
};

export default UpdateAttendance;
