import styled from "@emotion/styled";
import axios from "axios";
import { useEffect, useState } from "react";
import { Card, Table, Image } from "react-bootstrap";

const PaySlipWrapper = styled.div`
  .table > :not(caption) > * > * {
    padding: 0rem;
    padding-left: 1rem;
  }
  th,
  td {
    white-space: nowrap;
    vertical-align: middle;
  }
  th {
    background-color: #2f5595;
    color: #ffff;
  }
`;

const PaySlip = ({salary_id}:any) => {
  const [empsalary, setEmpSalary] = useState<any>({});
  const [leavecount, setLeaveCreditCount]= useState<any>([]);

  useEffect(() => {
    const fetchEmp = async () => {
      try {
        const empData = JSON.parse(localStorage.getItem("session") || "");
        console.log(empData, "Employee Salary Slip");
        const res = axios.all([
          axios.get(`/api/user/get_salary?&salary_id=${salary_id}`, empData).then((res) => {
            setEmpSalary(res.data.results[0]);
          }),
          axios.post(`/api/user/get_salary?&emp_id=${empData.emp_id}`).then((res) => {
            setLeaveCreditCount(res.data.results[0]);
          }),
        ]);
        console.log("Salary Data", res);
      } catch (error) {
        console.log(error, "Error Fetching Salary Slip");
      }
    };
    fetchEmp();
  }, []);
  console.log({ empsalary });
  const basicpay =parseInt(empsalary[0]?.basic_pay || 0);
  const mhra =parseInt(empsalary[0]?.hra|| 0);
  const mlta =parseInt(empsalary[0]?.lta|| 0);
  const mpf =parseInt(empsalary[0]?.pf|| 0);
  const spAllow =parseInt(empsalary[0]?.spec_allow|| 0);
  const tallow =parseInt(empsalary[0]?.ta|| 0);
  const rallow =parseInt(empsalary[0]?.ra|| 0);
  const healthinsurance =parseInt(empsalary[0]?.health_insurance|| 0);
  const conveyallow =parseInt(empsalary[0]?.convey_allow|| 0);
  const anualbonus = parseInt(empsalary[0]?.yearly_bonus|| 0);
  const adhocEarn = parseInt(empsalary[0]?.adhoc_earning|| 0);
  const mealplus = parseInt(empsalary[0]?.mp_card|| 0);
  const adhoc_dedt = parseInt(empsalary[0]?.adhoc_deduct|| 0);
  const emppf = parseInt(empsalary[0]?.emp_pf|| 0);
  const proftax = parseInt(empsalary[0]?.prof_tax || 0);
  const mtds = parseInt(empsalary[0]?.tds || 0);
  const cpf = parseInt(empsalary[0]?.company_pf || 0);
  const monthlyDeduct = emppf + proftax + mealplus + adhoc_dedt + mtds
  const grossSal = basicpay + mhra + mlta + spAllow + tallow + rallow + mealplus + healthinsurance + conveyallow + anualbonus + adhocEarn;
  const netSalary = grossSal - monthlyDeduct;

  return (
    <>
      <PaySlipWrapper>
        <Card.Body className="p-1">
          <Table responsive bordered hover>
            <tbody>
              <tr>
                <td colSpan={2} className="p-1">
                  <Image src="/MN-Logo.png" width={200} height={50} alt="" />
                </td>
                <td colSpan={3}>
                <b>MetricsNumero Technology Pvt. Ltd.</b><br/> S/1- Second Floor,
                  Shradanajali Apartment
                 Nimta, Kolkata(West Bengal) - India-700049
                </td>
              </tr>

              <tr style={{ borderTop: "2px solid #6c757d" }}>
                <td>Employee Code</td>
                <td colSpan={2}>{empsalary[0]?.emp_id}</td>
                <td>PAN Number</td>
                <td>{empsalary[0]?.pan_number}</td>
              </tr>
              <tr>
                <td>Employee Name</td>
                <td colSpan={2}>{empsalary[0]?.name}</td>
                <td>UAN Number</td>
                <td>{empsalary[0]?.uan_number}</td>
              </tr>
              <tr>
                <td>ESI Number</td>
                <td colSpan={2}>{empsalary[0]?.esi_number}</td>
                <td>PF Number</td>
                <td>{empsalary[0]?.pf_number}</td>
              </tr>
              <tr>
                <td>Days Worked</td>
                <td colSpan={2}>{empsalary[0]?.work_days}</td>
                <td>LOP Days</td>
                <td>{empsalary[0]?.lop}</td>
              </tr>
              <tr>
                <td>Date Of Joining</td>
                <td colSpan={2}>
                  {new Date(empsalary[0]?.join_date).toLocaleDateString()}
                </td>
                <td>Month</td>
                <td>
                  {empsalary[0]?.month}&nbsp;{empsalary[0]?.year}
                </td>
              </tr>
              <tr>
                <td>Department</td>
                <td colSpan={2}>{empsalary[0]?.department}</td>
                <td>Designation</td>
                <td>{empsalary[0]?.desig}</td>
              </tr>
              <tr>
                <td>Location</td>
                <td colSpan={2}>{empsalary[0]?.state}</td>
                <td>Bank A/C Number</td>
                <td>{empsalary[0]?.bank_acc_number}</td>
              </tr>
              <tr>
                <th colSpan={3} className="text-center">
                  EARNINGS
                </th>
                <th colSpan={2} className="text-center">
                  DEDUCTIONS
                </th>
              </tr>
              <tr>
                <td>Basic</td>
                <td colSpan={2}>{Math.round(empsalary[0]?.basic_pay)}</td>
                <td>Professional Tax</td>
                <td> {empsalary[0]?.prof_tax}</td>
              </tr>
              <tr>
                <td>Bonus(Q/H/A)</td>
                <td colSpan={2}>{empsalary[0]?.yearly_bonus}</td>
                <td>MealPlus Card</td>
                <td colSpan={2}>{empsalary[0]?.mp_card}</td>
              </tr>
              <tr>
                <td>House Rent Allowance</td>
                <td colSpan={2}>{Math.round(empsalary[0]?.hra)}</td>
                <td>Provident Fund</td>
                <td>{empsalary[0]?.emp_pf}</td>
              </tr>
              <tr>
                <td>Conveyance Allowance</td>
                <td colSpan={2}>{empsalary[0]?.convey_allow}</td>
                <td>Adhoc Deduction</td>
                <td>{empsalary[0]?.adhoc_deduct}</td>
              </tr>
              <tr>
                <td>Medical Allowance</td>
                <td colSpan={2}>{empsalary[0]?.health_insurance}</td>
                <td>TDS</td>
                <td>{empsalary[0]?.tds}</td>
              </tr>
              <tr>
                <td>Telephone Allowances</td>
                <td colSpan={2}>{empsalary[0]?.ta}</td>
        
              </tr>
              <tr>
                <td>MealPlus Card</td>
                <td colSpan={2}>{empsalary[0]?.mp_card}</td>
                <td></td>
                <td></td>
              </tr>
              <tr>
                <td>Special Allowances</td>
                <td colSpan={2}>{Math.round(empsalary[0]?.spec_allow)}</td>
                <td></td>
                <td></td>
              </tr>
              <tr>
                <td>Relocation Allowances</td>
                <td colSpan={2}>{empsalary[0]?.ra}</td>
                <td></td>
                <td></td>
              </tr>
              <tr>
                <td>Leave Travel Allowance</td>
                <td colSpan={2}>{Math.round(empsalary[0]?.lta)}</td>
                <td></td>
                <td></td>
              </tr>
              <tr>
                <td>Adhoc Earning</td>
                <td colSpan={2}>{empsalary[0]?.adhoc_earning}</td>
                <td></td>
                <td></td>
              </tr>

              <tr
                style={{
                  borderTop: "2px solid #6c757d",
                  borderBottom: "2px solid #6c757d",
                }}
              >
                <td>
                  <b>Gross Salary</b>
                </td>
                <td colSpan={2}>{Math.round(grossSal)}</td>
                <td>
                  <b>Total Deductions</b>
                </td>
                <td>
                  {monthlyDeduct}
                </td>
              </tr>
              <tr>
                <td>Total Cumulative Deductions</td>
                <td colSpan={2}></td>
                <td>Pay Summary</td>
                <td></td>
              </tr>
              <tr>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;TDS</td>
                <td colSpan={2}>{empsalary[0]?.tds}</td>
                <td>Gross Salary</td>
                <td>{Math.round(grossSal)}</td>
              </tr>
              <tr>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;Provident Fund</td>
                <td colSpan={2}>{empsalary[0]?.emp_pf}</td>
                <td>Total Deductions</td>
                <td>
                  {monthlyDeduct}
                </td>
              </tr>
              <tr>
                <td></td>
                <td colSpan={2}></td>
                <td>Net Salary(Bank Transfer)</td>
                <td>{netSalary}</td>
              </tr>
              <tr>
                <th colSpan={5} className="text-center">
                  Leave Details
                </th>
              </tr>
              <tr>
                <td>Leave Type</td>
                <td>Opening Balance</td>
                <td>Entitlement</td>
                <td>Leaves Taken</td>
                <td>Current Balance</td>
              </tr>
              <tr>
                <td>Casual Leave</td>
                <td></td>
                <td>0</td>
                <td>{leavecount[0]?.casual || 0}</td>
                <td></td>
              </tr>
              <tr>
                <td>Compensatory Off</td>
                <td></td>
                <td>0</td>
                <td>0</td>
                <td></td>
              </tr>
              <tr>
                <td>Marriage leave</td>
                <td></td>
                <td>0</td>
                <td>{leavecount[0]?.marriage || 0}</td>
                <td></td>
              </tr>
              <tr>
                <td>Sick Leave</td>
                <td></td>
                <td>0</td>
                <td>{leavecount[0]?.sick || 0}</td>
                <td></td>
              </tr>
              <tr>
                <td>Work From Home</td>
                <td></td>
                <td>0</td>
                <td>{leavecount[0]?.work_from_home || 0}</td>
                <td></td>
              </tr>
              <tr style={{ borderTop: "2px solid #6c757d" }}>
                <td colSpan={5} className="text-center">
                  <b>This is computer generated Payslip needs no signature</b>
                </td>
              </tr>
            </tbody>
          </Table>
        </Card.Body>
      </PaySlipWrapper>
    </>
  );
};

export default PaySlip;
