import axios from "axios";
import { useEffect, useRef, useState } from "react";
import Moment from "moment";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import * as Icons from "@fortawesome/free-solid-svg-icons";
import {
  Button,
  Card,
  Col,
  Form,
  InputGroup,
  Row,
  Table,
} from "react-bootstrap";
import { DownloadTableExcel } from "react-export-table-to-excel";
import styled from "@emotion/styled";
import YearSelectComp from "./YearSelectComp";

const DownloadProfileWrapper = styled.div`
  .form-select {
    border-bottom-right-radius: 0px !important;
    border-top-right-radius: 0px !important;
    box-shadow: none;
  }
  #button-addon2 {
    border-bottom-left-radius: 0px !important;
    border-top-left-radius: 0px !important;
  }
`;

const GetEmpProfile = () => {
  const [getempprofile, setGetEmpProfile] = useState([]);
  const[selectmonth, setMonth] = useState("");
  const[selectyear, setYear] = useState("");

  const monthProfileTableRef = useRef(null);

  const handleSubmit = (e: any) => {
    console.log(selectmonth, "month");
    console.log(selectyear, "year");
    fetchEmp(e);
    e.preventDefault();
  };
  const fetchEmp = async (e: any) => {
    try {
      const year = selectyear;
      console.log(selectyear, "year")
      const month = selectmonth;
      console.log(month, " month");
      const reponse = await axios.get( `/api/user/get_monthly_profile?&month=${month}&year=${year}`);
      setGetEmpProfile(reponse.data.results[0]);
      
    } catch (error) {
      console.log(error, "Fetching profile data error");
    }
  };
  useEffect(() => {}, []);

  return (
    <>
      <DownloadProfileWrapper>
        <Card className=" mb-3">
          <Card.Body>
            <Form as={Row} onClick={handleSubmit}>
              <Col lg="8"></Col>
              <Col lg="4">
                <InputGroup size="sm">

                  
                  <Form.Select name="month" onChange={(e:any)=>setMonth(e.target.value)} >

                    <option>Month</option>
                    <option value={1}>January</option>
                    <option value={2}>February</option>
                    <option value={3}>March</option>
                    <option value={4}>April</option>
                    <option value={5}>May</option>
                    <option value={6}>June</option>
                    <option value={7}>July</option>
                    <option value={8}>August</option>
                    <option value={9}>September</option>
                    <option value={10}>October</option>
                    <option value={11}>November</option>
                    <option value={12}>December</option>
                  </Form.Select>
                  <Form.Select name="year" placeholder="Year" onChange={(e:any)=>setYear(e.target.value)}>
                  <option>Year</option>
                    <YearSelectComp/>
                  </Form.Select>

                  <DownloadTableExcel
                    filename="Monthly Profile"
                    sheet="monthly profile"
                    currentTableRef={monthProfileTableRef.current}
                  >
                    <Button variant="primary" size="sm" id="button-addon2">
                      Download Profile
                      <FontAwesomeIcon
                        icon={Icons.faFileArrowDown}
                        className="ms-1"
                      />
                    </Button>
                  </DownloadTableExcel>
                </InputGroup>
              </Col>
            </Form>
          </Card.Body>
        </Card>
      </DownloadProfileWrapper>
      <Table ref={monthProfileTableRef} style={{ display: "none" }}>
        <thead>
          <tr>
            <th>Emp. ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Manager Assigned</th>
            <th>Employement</th>
            <th>D.O.B</th>
            <th>Organisation</th>
            <th>Department</th>
            <th>Designation</th>
            <th>Mobile No.</th>
            <th>Address</th>
            <th>City</th>
            <th>State</th>
            <th>Pin Code</th>
            <th>Date of Joining</th>
          </tr>
        </thead>
        <tbody>
          {getempprofile &&
            getempprofile.map((emp: any, index) => {
              return (
                <tr key={index}>
                  <td>{emp.emp_id}</td>
                  <td>{emp.name}</td>
                  <td>{emp.email}</td>
                  <td>{emp.manager_assign}</td>
                  <td>{emp.emp_type}</td>
                  <td>{emp.dob}</td>
                  <td>{emp.org_name}</td>
                  <td>{emp.department}</td>
                  <td>{emp.desig}</td>
                  <td>{emp.mob_number}</td>
                  <td>{emp.address}</td>
                  <td>{emp.city}</td>
                  <td>{emp.state}</td>
                  <td>{emp.pin_code}</td>
                  <td>{new Date(emp.join_date).toLocaleDateString()}</td>
                </tr>
              );
            })}
        </tbody>
      </Table>
    </>
  );
};

export default GetEmpProfile;
