import { DeleteFilled } from "@ant-design/icons";
import { Button, Form, Input, InputNumber, Select, Space, Switch } from "antd";
import React from "react";
import { useTranslation } from "react-i18next";
import styled from "styled-components";
import { TemplateList } from "../../utils/TemplateConstants";
import ConfigFormTypes from "../config-form-components/config-form-types";
import FormListMoveArrows from "../form-list-move-arrows/form-list-move-arrows.component";
import LayoutFormRow from "../layout-form-row/layout-form-row.component";

const SelectorDiv = styled.div`
  .ant-form-item .ant-select {
    width: 200px;
  }
`;

export default function ShopInfoIntakeChecklistComponent({ form }) {
  const { t } = useTranslation();

  const TemplateListGenerated = TemplateList();
  return (
    <div>
      <LayoutFormRow
        header={t("bodyshop.labels.intakechecklist")}
        id="intakechecklist"
      >
        <Form.List name={["intakechecklist", "form"]}>
          {(fields, { add, remove, move }) => {
            return (
              <div>
                {fields.map((field, index) => (
                  <Form.Item key={field.key}>
                    <LayoutFormRow noDivider>
                      <Form.Item
                        label={t("jobs.fields.intake.name")}
                        key={`${index}name`}
                        name={[field.name, "name"]}
                        rules={[
                          {
                            required: true,
                            //message: t("general.validation.required"),
                          },
                        ]}
                      >
                        <Input />
                      </Form.Item>
                      <Form.Item
                        label={t("jobs.fields.intake.type")}
                        key={`${index}type`}
                        name={[field.name, "type"]}
                        rules={[
                          {
                            required: true,
                            //message: t("general.validation.required"),
                          },
                        ]}
                      >
                        <Select>
                          {Object.keys(ConfigFormTypes).map((i, idx) => (
                            <Select.Option key={i} value={i}>
                              {i}
                            </Select.Option>
                          ))}
                        </Select>
                      </Form.Item>
                      <Form.Item
                        label={t("jobs.fields.intake.label")}
                        key={`${index}label`}
                        name={[field.name, "label"]}
                        rules={[
                          {
                            required: true,
                            //message: t("general.validation.required"),
                          },
                        ]}
                      >
                        <Input />
                      </Form.Item>

                      <Form.Item shouldUpdate>
                        {() => {
                          if (
                            form.getFieldValue([
                              "intakechecklist",
                              "form",
                              index,
                              "type",
                            ]) !== "slider"
                          )
                            return null;
                          return (
                            <>
                              <Form.Item
                                label={t("jobs.fields.intake.min")}
                                key={`${index}min`}
                                name={[field.name, "min"]}
                                dependencies={[[field.name, "type"]]}
                                rules={[
                                  {
                                    required: true,
                                    //message: t("general.validation.required"),
                                  },
                                ]}
                              >
                                <InputNumber />
                              </Form.Item>
                              <Form.Item
                                label={t("jobs.fields.intake.max")}
                                key={`${index}max`}
                                name={[field.name, "max"]}
                                rules={[
                                  {
                                    required: true,
                                    //message: t("general.validation.required"),
                                  },
                                ]}
                              >
                                <InputNumber />
                              </Form.Item>
                            </>
                          );
                        }}
                      </Form.Item>
                      <Form.Item
                        label={t("jobs.fields.intake.required")}
                        key={`${index}required`}
                        name={[field.name, "required"]}
                        valuePropName="checked"
                      >
                        <Switch />
                      </Form.Item>
                      <Space wrap>
                        <DeleteFilled
                          onClick={() => {
                            remove(field.name);
                          }}
                        />
                        <FormListMoveArrows
                          move={move}
                          index={index}
                          total={fields.length}
                        />
                      </Space>
                    </LayoutFormRow>
                  </Form.Item>
                ))}
                <Form.Item>
                  <Button
                    type="dashed"
                    onClick={() => {
                      add();
                    }}
                    style={{ width: "100%" }}
                  >
                    {t("general.actions.add")}
                  </Button>
                </Form.Item>
              </div>
            );
          }}
        </Form.List>
      </LayoutFormRow>
      <SelectorDiv>
        <Form.Item
          name={["intakechecklist", "templates"]}
          label={t("bodyshop.fields.intake.templates")}
          rules={[
            {
              required: true,
              //message: t("general.validation.required"),
              type: "array",
            },
          ]}
        >
          <Select mode="multiple">
            {Object.keys(TemplateListGenerated).map((i) => (
              <Select.Option
                key={TemplateListGenerated[i].key}
                value={TemplateListGenerated[i].key}
              >
                {TemplateListGenerated[i].title}
              </Select.Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item
          name={["intakechecklist", "next_contact_hours"]}
          label={t("bodyshop.fields.intake.next_contact_hours")}
        >
          <InputNumber min={0} precision={0} />
        </Form.Item>
      </SelectorDiv>

      <LayoutFormRow
        header={t("bodyshop.labels.deliverchecklist")}
        id="deliverchecklist"
      >
        <Form.List name={["deliverchecklist", "form"]}>
          {(fields, { add, remove, move }) => {
            return (
              <div>
                {fields.map((field, index) => (
                  <Form.Item key={field.key}>
                    <LayoutFormRow noDivider>
                      <Form.Item
                        label={t("jobs.fields.intake.name")}
                        key={`${index}named`}
                        name={[field.name, "name"]}
                        rules={[
                          {
                            required: true,
                            //message: t("general.validation.required"),
                          },
                        ]}
                      >
                        <Input />
                      </Form.Item>

                      <Form.Item
                        label={t("jobs.fields.intake.type")}
                        key={`${index}typed`}
                        name={[field.name, "type"]}
                        rules={[
                          {
                            required: true,
                            //message: t("general.validation.required"),
                          },
                        ]}
                      >
                        <Select>
                          {Object.keys(ConfigFormTypes).map((i, idx) => (
                            <Select.Option key={i} value={i}>
                              {i}
                            </Select.Option>
                          ))}
                        </Select>
                      </Form.Item>

                      <Form.Item
                        label={t("jobs.fields.intake.label")}
                        key={`${index}labeld`}
                        name={[field.name, "label"]}
                        rules={[
                          {
                            required: true,
                            //message: t("general.validation.required"),
                          },
                        ]}
                      >
                        <Input />
                      </Form.Item>

                      <Form.Item shouldUpdate>
                        {() => {
                          if (
                            form.getFieldValue([
                              "deliverchecklist",
                              "form",
                              index,
                              "type",
                            ]) !== "slider"
                          )
                            return null;
                          return (
                            <>
                              <Form.Item
                                label={t("jobs.fields.intake.min")}
                                key={`${index}mind`}
                                name={[field.name, "min"]}
                                dependencies={[[field.name, "type"]]}
                                rules={[
                                  {
                                    required:
                                      form.getFieldValue([
                                        field.name,
                                        "type",
                                      ]) === "slider",
                                    //message: t("general.validation.required"),
                                  },
                                ]}
                              >
                                <InputNumber />
                              </Form.Item>
                              <Form.Item
                                label={t("jobs.fields.intake.max")}
                                key={`${index}maxd`}
                                name={[field.name, "max"]}
                                dependencies={[[field.name, "type"]]}
                                rules={[
                                  {
                                    required:
                                      form.getFieldValue([
                                        field.name,
                                        "type",
                                      ]) === "slider",
                                    //message: t("general.validation.required"),
                                  },
                                ]}
                              >
                                <InputNumber />
                              </Form.Item>
                            </>
                          );
                        }}
                      </Form.Item>
                      <Form.Item
                        label={t("jobs.fields.intake.required")}
                        key={`${index}requiredd`}
                        name={[field.name, "required"]}
                        valuePropName="checked"
                      >
                        <Switch />
                      </Form.Item>
                      <DeleteFilled
                        onClick={() => {
                          remove(field.name);
                        }}
                      />
                      <FormListMoveArrows
                        move={move}
                        index={index}
                        total={fields.length}
                      />
                    </LayoutFormRow>
                  </Form.Item>
                ))}
                <Form.Item>
                  <Button
                    type="dashed"
                    onClick={() => {
                      add();
                    }}
                    style={{ width: "100%" }}
                  >
                    {t("general.actions.add")}
                  </Button>
                </Form.Item>
              </div>
            );
          }}
        </Form.List>
      </LayoutFormRow>
      <SelectorDiv>
        <Form.Item
          name={["deliverchecklist", "templates"]}
          label={t("bodyshop.fields.deliver.templates")}
          rules={[
            {
              required: true,
              //message: t("general.validation.required"),
              type: "array",
            },
          ]}
        >
          <Select mode="multiple">
            {Object.keys(TemplateListGenerated).map((i) => (
              <Select.Option
                key={TemplateListGenerated[i].key}
                value={TemplateListGenerated[i].key}
              >
                {TemplateListGenerated[i].title}
              </Select.Option>
            ))}
          </Select>
        </Form.Item>
      </SelectorDiv>
    </div>
  );
}
