import { Button, Card, Form, InputNumber, Popover, Radio, Space } from "antd";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { GenerateDocument } from "../../utils/RenderTemplate";
import { TemplateList } from "../../utils/TemplateConstants";
import { useNotification } from "../../contexts/Notifications/notificationContext.jsx";

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

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

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

  const handleCancel = () => {
    setIsModalVisible(false);
    setLoading(false);
  };
  const handleFinish = async ({ template, ...values }) => {
    // eslint-disable-next-line no-unused-vars
    const { sendtype, ...restVals } = values;
    setLoading(true);
    try {
      await GenerateDocument(
        {
          name: TemplateList("job_special")[template].key,
          variables: { id: jobId },
          context: restVals
        },
        {},
        "p",
        jobId,
        notification
      );
      setIsModalVisible(false);
    } catch (error) {
      notification.error({ title: 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>
        <div style={{ display: "flex", justifyContent: "flex-end" }}>
          <Space>
            <Button type="primary" loading={loading} onClick={handleOk}>
              {t("general.actions.print")}
            </Button>
            <Button onClick={handleCancel}>{t("general.actions.cancel")}</Button>
          </Space>
        </div>
      </Form>
    </Card>
  );
  return (
    <Popover content={content} open={isModalVisible} getPopupContainer={(trigger) => trigger.parentElement}>
      <Button onClick={() => setIsModalVisible(true)}>{t("printcenter.jobs.labels.labels")}</Button>
    </Popover>
  );
}
