import styled from "@emotion/styled";
import axios from "axios";
import { useEffect, useState } from "react";
import { Button, Card, Col, Container, Form, InputGroup, Row, Table } from "react-bootstrap"
import { RModalImages } from "react-modal-images";
import YearSelect from "./YearSelect";
import Pagination from "./Pagination";

const TDSCompWrapper = styled.div`
.table td,th{
    white-space:nowrap;
}
`
const EmpIncomeTaxDeclaration=()=>{
    const [empdata, setEmpData] = useState([]);
    const [fromyear, setFromYear] = useState("");
    const [payrolldata, setPayrollData] = useState<any>([]);
    const [filterData,setFilterData]=useState<any>([]); 
  const [currentPage, setCurrentPage] = useState(0);
  const [postsPerPage] = useState(15);
  

 

        const fetchEmp = async () => {
          try {
            const empData = JSON.parse(localStorage.getItem("session") || "");
            let from_year= fromyear;
            let to_year=parseInt(fromyear) + 1;
            const res = await
              axios.get(`/api/user/emp_itd_declared?&emp_id=${empData.emp_id}&from_year=${from_year}&to_year=${to_year}`);
                setEmpData(res.data.results[0]);
                setFilterData(res.data.results[0])
             
              
            console.log("Employee ITD Data", res.data.results[0]);
          } catch (error) {
            console.log(error, "Error Fetching Employee ITD Request Data");
          }
        };
        const itdData=async()=>{
            const empData = JSON.parse(localStorage.getItem("session") || "");
           const res=await axios.post(`/api/user/emp_itd_declared?&emp_id=${empData.emp_id}`)
           console.log(res.data.results[0],'jjjjjjjj');
           setFilterData(res.data.results[0]);
           setPayrollData(res.data.results[0]);
        }
        useEffect(()=>{
          itdData()
        },[])

        // Get current posts
  const paginateData=(arg:any)=>{
    const indexOfLastPost = arg * postsPerPage;
    const indexOfFirstPost = indexOfLastPost - postsPerPage;
    const currentPosts = empdata.slice(indexOfFirstPost, indexOfLastPost);
  
    setFilterData(currentPosts)
  }
  useEffect(()=>{
    paginateData(currentPage)
  },[empdata])
  // Change page
  const paginate = (pageNumber: any) => {
    setCurrentPage(pageNumber)
    paginateData(pageNumber)
  };
    return(
        <>
        <TDSCompWrapper>
        <Container>
            <Row>
            <Col lg="12" className="mb-2">
                <Card>
                    <Card.Body>
                <Form as={Row} className="d-flex justify-content-center">
                    <Col lg="6" sm="12">
                            <InputGroup>
                        
                        <Form.Control name="emp_id" defaultValue={payrolldata[0]?.emp_id} disabled/>
                      
                        <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"
                        onClick={fetchEmp}
                        id="button-addon2">
                        Search
                        </Button>
                    </InputGroup>
                </Col>
                </Form>
                </Card.Body>
                </Card>
            </Col>
                <Col>
                <h4>TDS Calculation Summary(OLD)</h4>
                <Table responsive bordered>
                    <thead>
                        <tr>
                            <th>Emp. ID</th>
                            <th>PAN Card</th>
                            <th>House Rent</th>
                            <th>Residence</th>
                            <th>House Rent Bill</th>
                            <th>Sec 80D - Medical Insurance Premium</th>
                            <th>Sec 80DD - Handicapped Dependent</th>
                            <th>Sec 80E - Repayment of Loan for higher education</th>
                            <th>Sec 80GG - Rent Paid</th>
                            <th>Sec 80U - Handicapped</th>
                            <th>Sec 80CCG - Rajeev Gandhi Equity Saving Scheme</th>
                            <th>Sec 80TTA / 80TTB - Interest on saving a/c</th>
                            <th>Other Deduction</th>
                            <th>Chapter VI A Bill</th>
                            <th>Sec 80C - Contribution to Pension Fund-Jeevan Suraksha</th>
                            <th>Sec 80C - Life Insurance Premium</th>
                            <th>Sec 80C - Deferred Annuity</th>
                            <th>Sec 80C - Public Provident Fund</th>
                            <th>Sec 80C - ULIP of UTI/LIC</th>
                            <th>Sec 80C - Repayment of Housing Loan</th>
                            <th>Sec 80C - Contribution to Pension Fund or UTI or Notified Mutual Fund</th>
                            <th>Sec 80C - Investment in ELSS made in units of Notified Mutual Fund</th>
                            <th>Sec 80C - Children Tuition Fee: Restricted to a max of 2 Children</th>
                            <th>Sec 80C - Deposit in home loan account scheme of NHB/HDFC</th>
                            <th>Sec 80C - 5 yrs. Term deposit in a Sch.Bank</th>
                            <th>Sec 80C - Other</th>
                            <th>Sec 80C - Others</th>
                            <th>Sec 80 C Bill Recipt</th>
                            <th>Sec 80CCC - Annuity/Pension Plan</th>
                            <th>Sec 80CCC Bill Recipt</th>
                            <th>Sec 80CC (1b)-	Employee Contribution</th>
                            <th>Sec 80CC Bill Recipt</th>
                            <th>Sec 80CCD -	Notified Pension Scheme</th>
                            <th>Sec 80CCD Bill Recipt</th>
                            <th>Sec 24 - Interest on Housing Loan on fully constructed accomodation only</th>
                            <th>Sec 24 - Interest if the loan is taken before 01/04/99 on fully constructed accomodation only.</th>
                            <th>Sec 24 Bill Recipt</th>
                          
                        </tr>
                    </thead>
                    {filterData &&
                    filterData?.map((emp: any, index: any) => {
                      return(
                    <tbody key={index}>
                        <tr>
                            <td>{emp.emp_id}</td>
                            <td>{emp?.emp_pan}</td>
                            <td>{emp.house_rent}</td>
                            <td>{emp.residence}</td>
                            <td>
                            <RModalImages
                                small={`/${emp.rent_file}`}
                                medium={`/${emp.rent_file}`}
                                large={`/${emp.rent_file}`}
                                alt={"80cc Bill"}
                                
                            />
                            </td>
                            <td>{emp.medic_insur_prem_80d}</td>
                            <td>{emp.handicap_depend_80dd}</td>
                            <td>{emp.high_edu_loan_80e}</td>
                            <td>{emp.paid_rent_80gg}</td>
                            <td>{emp.handicap_80u}</td>
                            <td>{emp.raj_gandhi_scheme_80ccg}</td>
                            <td>{emp.int_on_saving_80tta}</td>
                            <td>{emp.other_deduct}</td>
                            <td>
                                <RModalImages
                                small={`/${emp.chapter_vi_a_file}`}
                                medium={`/${emp.chapter_vi_a_file}`}
                                large={`/${emp.chapter_vi_a_file}`}
                                alt={"80cc Bill"}
                            />
                            </td>
                            <td>{emp.pf_jeevan_suraksha}</td>
                            <td>{emp.lic}</td>
                            <td>{emp.deferred_annuity}</td>
                            <td>{emp.public_pf}</td>
                            <td>{emp.ulip_uti_lic}</td>
                            <td>{emp.housing_loan_repay}</td>
                            <td>{emp.pf_uti_mutual_fund}</td>
                            <td>{emp.invest_elss}</td>
                            <td>{emp.child_tution_fee}</td>
                            <td>{emp.deposit_home_looan_sch}</td>
                            <td>{emp.five_yrs_bank_deposit}</td>
                            <td>{emp.other}</td>
                            <td>{emp.others}</td>
                            <td><RModalImages
                                small={`/${emp.deduct_80c_file}`}
                                medium={`/${emp.deduct_80c_file}`}
                                large={`/${emp.deduct_80c_file}`}
                                alt={"80cc Bill"}
                            /></td>
                            <td>{emp.annuity_pension_plan}</td>
                            <td><RModalImages
                                small={`/${emp.deduct_80ccc_file}`}
                                medium={`/${emp.deduct_80ccc_file}`}
                                large={`/${emp.deduct_80ccc_file}`}
                                alt={"80ccc Bill"}
                            /></td>
                            <td>{emp.emp_contri}</td>
                            <td><RModalImages
                                small={`/${emp.deduct_80cc_file}`}
                                medium={`/${emp.deduct_80cc_file}`}
                                large={`/${emp.deduct_80cc_file}`}
                                alt={"80cc Bill"}
                            /></td>
                            <td>{emp.notif_pens_sch}</td>
                            <td><RModalImages
                                small={`/${emp.deduct_80ccd_file}`}
                                medium={`/${emp.deduct_80ccd_file}`}
                                large={`/${emp.deduct_80ccd_file}`}
                                alt={"80ccd Bill"}
                            /></td>
                            <td>{emp.int_house_loaan}</td>
                            <td>{emp.int_loan_const_acco}</td>
                            <td><RModalImages
                                small={`/${emp.deduct_us24_file}`}
                                medium={`/${emp.deduct_us24_file}`}
                                large={`/${emp.deduct_us24_file}`}
                                alt={"US24 Bill"}
                            /></td>
                        </tr>
                    </tbody>
                     )})}
                </Table>
                <Pagination
                postsPerPage={postsPerPage}
                totalPosts={empdata.length}
                paginate={paginate}
                currentPage={currentPage}
                />
                </Col>
            </Row>
        </Container>
        </TDSCompWrapper>
        </>
    )
}
export default EmpIncomeTaxDeclaration;