import React, { useCallback } from "react";
import styled from "styled-components";
import FormPage from "../jobForm/FormPage";
import Loading from "../util/Loading";
import { FullScreen } from "../util/Layout";
import { updateJobInfo } from "../data/submitData";
import { getJobPlain } from "../data/getJobInfo";
import { useLoaded } from "../util/hooks";
import { notification } from "antd";

const Backgroud = styled(FullScreen)`
  background: rgba(245, 246, 249, 1);
`;

export default ({ jobId, history }) => {
  const submit = useCallback((v) => {
    const f = async () => {
      const changed = (await updateJobInfo(jobId, v)).changed;
      if (changed) {
        notification.open({
          message: "成功",
          description: "编辑职位成功，请耐心等待审核",
        });
        const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
        await wait(1000);
      }
      history.push("/recruitment?tab=1&jobstate=drafted");
    };
    f();
  }, []);
  const { isLoaded, data } = useLoaded(getJobPlain, [jobId]);
  if (!isLoaded) {
    return (
      <Backgroud>
        <Loading />
      </Backgroud>
    );
  }
  return (
    <FormPage
      submit={submit}
      data={data}
      back={() => {
        history.push("/recruitment?tab=1&jobstate=drafted");
      }}
    ></FormPage>
  );
};
