import axios from "axios";
import { useEffect, useState } from "react";
import { Col, Container, Row, Table } from "react-bootstrap";
import styled from "@emotion/styled";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import * as Icons from "@fortawesome/free-solid-svg-icons";
import TravellingReqForm from "./TravellingReqForm";
import EmpSearch from "./EmpSearch";
import Pagination from "./Pagination";
const AppliedTravelWrapper = styled.div`
  box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15) !important;
  border: 0px;
  border-radius: 0px;
  padding: 1%;
`;
const AppliedTravel = () => {
  const [emptravel, EmpTravel] = useState<any>([]);
  const [currentPage, setCurrentPage] = useState(0);
  const [postsPerPage] = useState(15);
  const [travel, setTravel] = useState(false);
  const [iscancel, setIsCancelled] = useState(false)
  const [filterData,setFilterData]=useState<any>()

  const handleStatusCancel = async (cancel: any) => {
    try {
      setIsCancelled(false);
      const response = await axios.put(
        `/api/user/get_travel_req_list?travell_id=${cancel}&type=cancel`
      );
      console.log("Cancelled");
      setIsCancelled(true);
    } catch (error) {
      setIsCancelled(false);
      console.log(error, "Status change error");
    }
  };
  useEffect(() => {
    const fetchEmp = async () => {
      try {
        const empData = JSON.parse(localStorage.getItem("session") || "");
        const response = await axios.get(
          `/api/user/travel_req?&emp_id=${empData.emp_id}`
        );
        EmpTravel(response.data.results[0]);
        setFilterData(response.data.results[0]);
        console.log("Employee reimbursement data", response);
      } catch (error) {
        console.log(error, "Fetching user reimbursement data error");
      }
    };
    fetchEmp();
  }, [travel, iscancel]);
  console.log(emptravel, "State");

  const paginateData=(arg:any)=>{
    const indexOfLastPost = arg * postsPerPage;
    const indexOfFirstPost = indexOfLastPost - postsPerPage;
    const currentPosts = emptravel.slice(indexOfFirstPost, indexOfLastPost);
  
    setFilterData(currentPosts)
  }
  useEffect(()=>{
    paginateData(currentPage)
  },[emptravel])

  // Change page
  const paginate = (pageNumber: any) => {
    setCurrentPage(pageNumber)
    paginateData(pageNumber)
  
  };
const sendData=(data:any)=>{
setFilterData(data)
}
  return (
    <>
      <TravellingReqForm setTravel={setTravel} />

      <Container>
        <Row className="my-2">
          <Col sm={12} md={12} lg={12}>
            <AppliedTravelWrapper>
            <EmpSearch empData={emptravel} sendData={sendData}/>
              <Table responsive bordered hover size="md">
                <thead>
                  <tr>
                    {/* <th>R.ID</th> */}
                    <th>Emp.ID</th>
                    <th>Travel Type</th>
                    <th>Mode Of Travel</th>
                    <th>Date Of Travel</th>
                    <th>Source</th>
                    <th>Destination</th>
                    <th>Description</th>
                    <th>Approving Authority</th>
                    <th>Status</th>
                    <th>Cancel</th>
                  </tr>
                </thead>
                <tbody>
                  {filterData &&
                    filterData.map((emp: any, i: any) => {
                      return (
                        <tr key={i}>
                          <td>{emp.emp_id}</td>
                          <td>{emp.travell_type}</td>
                          <td>{emp.mode_of_travell}</td>
                          <td>
                            {new Date(
                              emp.date_of_travel_from
                            ).toLocaleDateString()}
                          </td>
                          <td>{emp.source}</td>
                          <td>{emp.destination}</td>
                          <td>{emp.description}</td>
                          <td>{emp.request_approver}</td>
                          <td>{emp.status}</td>
                          <td> 
                            <FontAwesomeIcon
                                icon={Icons.faCircleXmark}
                                className="ms-1"
                                size="2x"
                                onClick={() => handleStatusCancel(emp.travell_id)}
                                style={{
                                  color: "crimson",
                                }}
                              />
                              </td>
                        </tr>
                      );
                    })}
                </tbody>
              </Table>
              <Pagination
                postsPerPage={postsPerPage}
                totalPosts={emptravel.length}
                paginate={paginate}
                currentPage={currentPage}
              />
            </AppliedTravelWrapper>
          </Col>
        </Row>
      </Container>
    </>
  );
};

export default AppliedTravel;
