import { forwardRef, useImperativeHandle, useState, useRef } from "react";
import { Form, Switch, Button, message, InputNumber } from "antd";
import { useMutation, useQuery } from "@tanstack/react-query";
import { GetVqdConfigBase, UpdateConfigBase } from "../api/config";
import type { UpdateConfigBaseReq, VqdConfigBaseDetailRes } from "../types/config";
import { useGlobal } from "../Context";
export default function VqdTaskPage() {

  const [form] = Form.useForm();
  const { ErrorHandle } = useGlobal();

  const { mutate: updateMutate, isPending: updating } = useMutation({
    mutationFn: UpdateConfigBase,
    onSuccess: () => {
      message.success("更新成功");
    },
    onError: ErrorHandle,
  });
  const handleSave = () => {
    form.submit()
  }
  // 获取存储列表
  const { refetch } = useQuery({
    queryKey: ["config"],
    queryFn: () =>
      GetVqdConfigBase()
        .then((res) => {
          const formValues = {
            save_day: res.data.save_day,
          };
          form.setFieldsValue(formValues);
        })
        .catch((err) => {
          ErrorHandle(err);
          throw err;
        }),
    retry: 2,
  });
  return (
    <div>
      <Form
        form={form}
        style={{ width: "400px", padding: "20px" }}
        layout="vertical"
        onFinish={(values) => {
          if (updating) return
          console.log(values);

          const {
            save_day,
          } = values as {
            save_day: number;
          };
          const payload: UpdateConfigBaseReq = {
            save_day,
          };

          const id = (values as any).id;
          updateMutate({ ...payload });
        }}
      >
        <Form.Item name="save_day" label="数据保存天数" rules={[{ required: true, message: "请输入数据保存天数" }]}>
          <InputNumber min={1} defaultValue={3} />
        </Form.Item>
        {/* <Form.Item name="frm_num" label="连续分析帧数(2-64), 默认为10, 最大为 64" rules={[{ required: true, message: "请输入连续分析帧数" }]}>
          <InputNumber max={64} min={2} defaultValue={10} />
        </Form.Item>
        <Form.Item label="是否使用深度学习版本" name="is_deep_learn">
          <Switch />
        </Form.Item> */}
        <Button type="primary" onClick={handleSave}>
          保存
        </Button>
      </Form>
    </div>
  );
}
