import { Form, Switch, InputNumber } from "antd";
import { useConfigStore } from "..";
import { EditExamineEnable, EditExamineStandardTime } from "@/api/node";
import { useModel } from "umi";

export default function Assessment() {
  const nodeData = useConfigStore((state) => state?.nodeData);
  const updateState = useConfigStore((state) => state.updateState);
  const [form] = Form.useForm();
  const { mode } = useModel("flowModel");

  const handleChangeIsExamineEnable = (checked: boolean) => {
    EditExamineEnable({
      ...nodeData,
      isExamineEnable: checked,
    });
    updateState({
      nodeData: {
        ...nodeData,
        isExamineEnable: checked,
      },
    });
  };

  const handleChangeExamineStandardTime = () => {
    const values = form.getFieldsValue();
    EditExamineStandardTime({
      ...nodeData,
      ...values,
    });
    updateState({
      nodeData: {
        ...nodeData,
        ...values,
      },
    });
  };

  return (
    <Form
      form={form}
      initialValues={{
        isExamineEnable: nodeData?.isExamineEnable,
        examineStandardTime: nodeData?.examineStandardTime,
      }}
      disabled={mode === "ops"}
    >
      <Form.Item label="是否启用" name="isExamineEnable">
        <Switch onChange={handleChangeIsExamineEnable} />
      </Form.Item>
      <Form.Item label="标准处理时间" tooltip="小时" shouldUpdate>
        {({ getFieldValue }) => {
          const isExamineEnable = getFieldValue("isExamineEnable");
          return (
            <Form.Item name="examineStandardTime">
              <InputNumber
                disabled={!isExamineEnable}
                onBlur={handleChangeExamineStandardTime}
              />
            </Form.Item>
          );
        }}
      </Form.Item>
    </Form>
  );
}
