import {
  Button,
  Card,
  Form,
  InputNumber,
  notification,
  Popover,
  Radio,
} from "antd";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";
import { GenerateDocument } from "../../utils/RenderTemplate";
import { TemplateList } from "../../utils/TemplateConstants";

const mapStateToProps = createStructuredSelector({
  bodyshop: selectBodyshop,
});
const mapDispatchToProps = (dispatch) => ({
  //setUserLanguage: language => dispatch(setUserLanguage(language))
});
export default connect(
  mapStateToProps,
  mapDispatchToProps
)(PrintCenterJobsLabels);

export function PrintCenterJobsLabels({ bodyshop, jobId }) {
  const [isModalVisible, setIsModalVisible] = useState(false);
  const [loading, setLoading] = useState(false);
  const { t } = useTranslation();
  const [form] = Form.useForm();

  const handleOk = (e) => {
    e.stopPropagation();
    form.submit();
  };

  const handleCancel = () => {
    setIsModalVisible(false);
    setLoading(false);
  };
  const handleFinish = async ({ template, ...values }) => {
    const { sendtype, ...restVals } = values;
    setLoading(true);
    try {
      await GenerateDocument(
        {
          name: TemplateList("job_special")[template].key,
          variables: { id: jobId },
          context: restVals,
        },
        {},
        "p",
        jobId
      );
      setIsModalVisible(false);
    } catch (error) {
      notification.open({ type: "error", message: JSON.stringify(error) });
    } finally {
      setLoading(false);
    }

    form.resetFields();
  };

  const content = (
    <Card>
      <Form
        onFinish={handleFinish}
        autoComplete={"off"}
        layout="vertical"
        form={form}
      >
        <Form.Item
          rules={[
            {
              required: true,
              //message: t("general.validation.required"),
            },
          ]}
          name="template"
        >
          <Radio.Group>
            <Radio.Button value="parts_label_multiple">
              {t("printcenter.jobs.parts_label_multiple")}
            </Radio.Button>
            <Radio.Button value="folder_label_multiple">
              {t("printcenter.jobs.folder_label_multiple")}
            </Radio.Button>
          </Radio.Group>
        </Form.Item>
        <Form.Item
          rules={[
            {
              required: true,
              //message: t("general.validation.required"),
            },
          ]}
          label={t("printcenter.jobs.labels.position")}
          name="position"
        >
          <InputNumber min={1} precision={0} />
        </Form.Item>
        <Form.Item
          rules={[
            {
              required: true,
              //message: t("general.validation.required"),
            },
          ]}
          label={t("printcenter.jobs.labels.count")}
          name="count"
        >
          <InputNumber min={1} precision={0} max={99} />
        </Form.Item>
        <Button type="primary" loading={loading} onClick={handleOk}>
          {t("general.actions.print")}
        </Button>
        <Button onClick={handleCancel}>{t("general.actions.cancel")}</Button>
      </Form>
    </Card>
  );
  return (
    <Popover content={content} visible={isModalVisible}>
      <Button onClick={() => setIsModalVisible(true)}>
        {t("printcenter.jobs.labels.labels")}
      </Button>
    </Popover>
  );
}
