import axios from "axios";
import { useEffect, useState } from "react";
import { Button, Form } from "react-bootstrap";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import * as Icons from "@fortawesome/free-solid-svg-icons";

const ProfilePic = ({ setProfileUpdated }: any) => {
  const handleFile = (event: any) => {
    console.log(event.target.files[0], "new file");
    handleDocument(event);
  };
  const handleDocument = async (event: any) => {
    try {
      event.preventDefault();
      const data = new FormData();
      const empData = JSON.parse(localStorage.getItem("session") || "");

      let profile_pic = event.target.files[0];
      data.append("profile_pic", profile_pic);
      setProfileUpdated(false);

      const response = await axios.put(
        `/api/upload/update_doc?=${profile_pic}&emp_id=${empData.emp_id}`,
        data,
        {
          headers: { "content-type": "multipart/form-data" },
        }
      );
      console.log(response, "upload response");
      setProfileUpdated(true);
    } catch (error) {
      setProfileUpdated(false);
      console.log(error, "Error Fetching Upload  Data");
    }
  };

  return (
    <>
      <Form onSubmit={handleDocument} encType="multipart/form-data">
        <Form.Control
          type="file"
          name="profile_pic"
          className="icon"
          style={{ opacity: "0" }}
          onChange={handleFile}
        />
      </Form>
    </>
  );
};

export default ProfilePic;
