import axios from "axios";
import React, { useState, useEffect, useRef } from "react";
import { Button, Card, Col, Container, Form, InputGroup, Row, Table } from "react-bootstrap";
import { DownloadTableExcel } from "react-export-table-to-excel";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import * as Icons from "@fortawesome/free-solid-svg-icons";
import YearSelect from "./YearSelect";

const FYSalCal = () => {
  const [salaryList, setSalaryList] = useState<any>([]);
  const [empid, setEmpID] = useState("");
  const [filterData,setFilterData]=useState<any>([]); 
  const [fromyear, setFromYear] = useState("");
  const [salary,setSalaryData2]=useState([])

  const fySalaryTableRef = useRef(null);
  const fetchEmp = async () => {
    try {
      let emp_id = empid;
      let from_year= fromyear;
      let to_year=parseInt(fromyear) + 1;
      const response = await axios.get(
        `/api/user/get_fy_salary_data?&emp_id=${emp_id}&from_year=${from_year}&to_year=${to_year}`
      );
      setSalaryList(response.data.results[0]);
      setFilterData(response.data.results[0]);
      console.log("Employee payroll data1", response.data.results[0]);
    } catch (error) {
      console.log(error, "Fetching payroll data error");
    }
  };

  const salaryData=async()=>{
    const res=await axios.get(`/api/user/get_salary_all`)
    console.log(res.data.results[0],'jjjjjjjj');
    setFilterData(res.data.results[0]);
    setSalaryData2(res.data.results[0]);
    
 }

  useEffect(() => {
    salaryData();
  }, []);
  return (
    <>
     <Container>
            <Row>
            <Col lg="12" className="mb-3">
                <Card>
                    <Card.Body>
                <Form as={Row} className="d-flex justify-content-center">
                    <Col lg="6" sm="12">
                            <InputGroup>
                        
                        <Form.Select name="emp_id"  onChange={(e)=>setEmpID(e.target.value)}>
                          <option>Select Emp. ID</option>
                          {Array.from(new Set(salary.map((emp: any) => emp?.emp_id))).sort((a, b) => a - b)
.map((uniqueEmpID: any, i: any) => {
    return <option key={i} value={uniqueEmpID}>{uniqueEmpID}</option>
  })}

                        </Form.Select>
                      
                        <Form.Select  name="from_year" placeholder="Starting Year" onChange={(e)=>setFromYear(e.target.value)}>
                          <option>Select Year</option>
                  <YearSelect/>
                        </Form.Select>
                       
                       
                        <Button variant="outline-secondary"
                        id="button-addon2" onClick={fetchEmp}>
                        Search
                        </Button>
                    </InputGroup>
                </Col>
                </Form>
                </Card.Body>
                </Card>
            </Col>
            <Col sm={12} md={12} className="text-end my-1">
                <DownloadTableExcel
                    filename="FY Salary"
                    sheet="fy salary"
                    currentTableRef={fySalaryTableRef.current}
                  >
                    <Button variant="primary" size="sm" id="button-addon2">
                      Download
                      <FontAwesomeIcon
                        icon={Icons.faFileArrowDown}
                        className="ms-1"
                      />
                    </Button>
                  </DownloadTableExcel>
                </Col>
             
            <Col >
      <Table responsive bordered hover ref={fySalaryTableRef}>
        <thead>
          <tr>
            
            <th>Emp. ID</th>
             <th>Yearly salary</th> 
             <th>Basic salary</th> 
             <th>Professional tax</th> 
             <th>HRA</th>
            <th>LTA</th> 
            <th>Health Insurance</th> 
            <th>Special allowance</th> 
            <th>PF</th> 
            <th>Yearly Bonus</th> 
            <th>RA</th> 
            <th>Meal Plus Card</th> 
            <th>TA</th> 
            <th>convey_allow</th> 
            <th>Net Pay</th> 
            <th>Gratuity</th> 
            <th>Gross salary</th> 
            <th>Total deduction</th> 
            <th>Adhoc earning</th> 
            <th>Yearly TDS</th> 
          </tr>
        </thead>
        <tbody>
          {salaryList?.map((salaryData: any, i:any) => {
            return(
              <tr key={i}>
              <td>{salaryData.emp_id}</td>
             <td>{Math.round(salaryData.monthly_pay)}</td> 
             <td>{Math.round(salaryData.basic_pay)}</td> 
             <td>{Math.round(salaryData.prof_tax)}</td> 
             <td>{Math.round(salaryData.hra)}</td>
            <td>{Math.round(salaryData.lta)}</td> 
            <td>{Math.round(salaryData.health_insurance)}</td> 
            <td>{Math.round(salaryData.spec_allow)}</td> 
            <td>{Math.round(salaryData.pf)}</td>
            <td>{Math.round(salaryData.yearly_bonus)}</td> 
            <td>{Math.round(salaryData.ra)}</td> 
            <td>{Math.round(salaryData.mp_card)}</td> 
            <td>{Math.round(salaryData.ta)}</td> 
            <td>{Math.round(salaryData.convey_allow)}</td> 
            <td>{Math.round(salaryData.net_pay)}</td> 
            <td>{Math.round(salaryData.gratuity)}</td> 
            <td>{Math.round(salaryData.gross_sal)}</td> 
            <td>{Math.round(salaryData.total_deduction)}</td> 
            <td>{Math.round(salaryData.adhoc_earning)}</td> 
            <td>{Math.round(salaryData.tds)}</td> 
            </tr>)
            })}

        </tbody>
      </Table>
      </Col>
      </Row>
      </Container>
    </>
  );
};

export default FYSalCal;
