import axios from "axios";
import { useEffect, useState } from "react";
import { Button, Card, Col, Container, Form, Row } from "react-bootstrap";
import AssignManagerComponent from "./AssignManagerComponent";
import DatePicker from "react-datepicker";
import styled from "@emotion/styled";
import { toast } from "react-toastify";
const TitleCardWrapper = styled.div`
  .card {
    background: #55cefeed;
    border: 0px;
    box-shadow: 0 0.2rem 0.2rem rgba(0, 0, 0, 0.15) !important;
    .title {
      @media (max-width: 992px) {
        text-align: center;
      }
    }
  }
`;
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 TravellingReqForm = ({ setTravel }: any) => {
  const [travelltype, setTravellType] = useState("");
  const [reqapprover, setReqApprover] = useState("");
  const [modeoftravell, setModeOfTravell] = useState("");
  const [enddate, setEndDate] = useState(new Date());
  const [startdate, setStartDate] = useState(new Date());
  const [emp, setEmp] = useState<any>([]);
  const [sourcefrom, setSource] = useState("");
  const [destinationto, setDestination] = useState("");
  const [description1,setDescription]=useState()
  const handleEndDate = (e: any) => {
    setEndDate(e.target.value);
  };
  const handleTravellinReq = async (
    event: React.FormEvent<HTMLFormElement>
  ) => {
    try {
      event.preventDefault();
      const data = new FormData();
      const empData = JSON.parse(localStorage.getItem("session") || "");
      let travell_type = travelltype;
      let date_of_travel_from = startdate.toISOString().substring(0, 10);
      // let date_of_travel_to = enddate.toISOString().substring(0, 10);
      // &date_of_travel_to=${date_of_travel_to}
      let mode_of_travell = modeoftravell;
      let source = sourcefrom;
      let destination = destinationto;
      let request_approver = reqapprover;
      let description=description1;
      setTravel(false);
      const response: any = await axios.post(
        `/api/user/travel_req?&travell_type=${travell_type}&date_of_travel_from=${date_of_travel_from}&mode_of_travell=${mode_of_travell}&source=${source}&destination=${destination}&request_approver=${request_approver}&emp_id=${empData.emp_id}&description=${description}`,
        data,
        {
          headers: { "content-type": "multipart/form-data" },
        }
      );
      setTravel(true);
      toast.success("Travel Request Submitted Successfully");
      console.log("response", response.data);
    } catch (error: any) {
      setTravel(true);
      toast.error(error.response.data.results);
      console.log(error);
    }
  };

  useEffect(() => {
    const fetchEmp = async () => {
      try {
        const empData = JSON.parse(localStorage.getItem("session") || "");

        const response = await axios.post(`/api/user/emp_profile`, { empData });
        setEmp(response.data.results[0]);
        console.log("Emp Data", response);
      } catch (error) {
        console.log(error, "Error Fetching Emp Data");
      }
    };
    fetchEmp();
  }, []);
  console.log({ emp });
  return (
    <>
      <Container>
        <Row>
          <Col>
            <TitleCardWrapper>
              <Card className="mb-3">
                <Card.Body className="p-1">
                  <Row className="title">
                    <Col lg="1" className="text-center">
                      <Card.Img
                        variant="start"
                        src="https://img.icons8.com/cute-clipart/64/null/airplane-mode-on.png"
                        width="50px"
                        height="50px"
                        alt=""
                      />
                    </Col>
                    <Col lg="11">
                      <Card.Title className="my-3 text-dark">Travel</Card.Title>
                    </Col>
                  </Row>
                </Card.Body>
              </Card>
            </TitleCardWrapper>
            <Card>
              <Card.Body>
                <Form
                  onSubmit={handleTravellinReq}
                  encType="multipart/form-data"
                >
                  <Row className="align-items-center mb-3">
                    <Col sm={4} className="mb-3">
                      <Form.Label htmlFor="inlineFormInputEID">
                        Employee ID
                      </Form.Label>
                      <Form.Control
                        type="text"
                        id="inlineFormInputEID"
                        name="emp_id"
                        placeholder=""
                        required
                        defaultValue={emp[0]?.emp_id}
                        disabled
                      />
                    </Col>
                    <Col sm={4} className="mb-3">
                      <Form.Label htmlFor="inlineFormInputName">
                        Travel Request
                      </Form.Label>
                      <Form.Select
                        name="travell_type"
                        required
                        onChange={(e) => {
                          setTravellType(e.target.value);
                        }}
                      >
                        <option>Select Travel Type</option>
                        <option value="Local Travel">Local Travel</option>
                        <option value="Domestic">Domestic</option>
                        <option value="International">International</option>
                      </Form.Select>
                    </Col>
                    <Col sm={4} className="mb-3">
                      <Form.Label htmlFor="inlineFormInputDiscription">
                        Mode Of Travel
                      </Form.Label>
                      <Form.Select
                        name="mode_of_travell"
                        required
                        onChange={(e) => {
                          setModeOfTravell(e.target.value);
                        }}
                      >
                        <option>Select Mode of travel</option>
                        <option value="Cab">Cab</option>
                        <option value="Bus">Bus</option>
                        <option value="Rail">Rail</option>
                        <option value="Airline"> Airline</option>
                        <option value="Hybrid"> Hybrid</option>
                      </Form.Select>
                    </Col>
                    <Col sm={4} className="mb-3">
                      <Form.Label htmlFor="inlineFormInputName">
                        Date Of Travel
                      </Form.Label>
                      <DateInputWrapper>
                        <DatePicker
                          className="form-control"
                          name="date_of_travel_from"
                          dateFormat="yyyy-MM-dd"
                          selected={startdate}
                          onChange={(startdate: any) => setStartDate(startdate)}
                          showYearDropdown
                          scrollableMonthYearDropdown
                          required
                          minDate={new Date()}
                        />
                      </DateInputWrapper>
                    </Col>
                    {/* <Col sm={4} className="mb-3">
                      <Form.Label htmlFor="inlineFormInputBillDate">
                        Date Of Travel To
                      </Form.Label>
                      <DateInputWrapper>
                        <DatePicker
                          className="form-control"
                          name="date_of_travel_to"
                          dateFormat="yyyy-MM-dd"
                          selected={enddate}
                          onChange={(enddate: any) => setEndDate(enddate)}
                          showYearDropdown
                          scrollableMonthYearDropdown
                          required
                          minDate={new Date()}
                        />
                      </DateInputWrapper>
                    </Col> */}

                    <Col sm={4} className="mb-3">
                      <Form.Label htmlFor="inlineFormInputSource">
                        Source
                      </Form.Label>
                      <Form.Control
                        id="inlineFormInputSource"
                        name="source"
                        required
                        placeholder="Enter Source"
                        onChange={(e) => setSource(e.target.value)}
                      />
                    </Col>
                    <Col sm={4} className="mb-3">
                      <Form.Label htmlFor="inlineFormInputDestination">
                        Destination
                      </Form.Label>
                      <Form.Control
                        name="destination"
                        required
                        id="inlineFormInputDestination"
                        placeholder="Enter Destination"
                        onChange={(e) => setDestination(e.target.value)}
                      />
                    </Col>
                    <Col sm={4} className="mb-3">
                      <Form.Label htmlFor="inlineFormInputEID">
                        Description
                      </Form.Label>
                      <Form.Control as="textarea"
                      style={{ height: '30px' }}
                        type="text"
                        id="inlineFormInputEID"
                        name="description"
                        
                        onChange={(e:any) => setDescription(e.target.value)}
                      />
                    </Col>
                    <Col sm={4} className="mb-3">
                      <Form.Label htmlFor="inlineFormInputApproval">
                        Approving Authority
                      </Form.Label>
                      <Form.Select
                        name="request_approver"
                        required
                        onChange={(e) => setReqApprover(e.target.value)}
                      >
                        <option>Select Approving Authority</option>
                        <AssignManagerComponent />
                      </Form.Select>
                    </Col>
                   
                  </Row>
                  <Row className="align-items-center mb-3">
                    <Col xs="auto" className="my-1">
                      <Button type="submit">Submit</Button>
                    </Col>
                  </Row>
                </Form>
              </Card.Body>
            </Card>
          </Col>
        </Row>
      </Container>
    </>
  );
};

export default TravellingReqForm;
