import React from "react";
import { Form, Input, Space, Button, message } from "antd";
import baseRequest from "../../utils/baseRequest";

export default function RollbackUserLeaveSpace() {
  const [form] = Form.useForm();

  const rollback = async (value) => {
    if (!value.userid || !value.streamer_userid) {
      alert("请填写用户和主播的ID");
      return;
    }
    try {
      const base = baseRequest();
      const response = await fetch(`/op/vas/rollback_zone_exit_status`, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          userid: parseInt(value.userid, 10),
          streamer_userid: parseInt(value.streamer_userid, 10),
          ...base,
        }),
      });
      const data = await response.json();
      console.log(data);
      if (data.ret === -1) {
        alert(data.msg);
        return;
      }
      form.resetFields();
      message.success("恢复成功");
    } catch (error) {
      console.error(error);
    }
  };
  //表单提交失败
  const onFinishFailed = (errorInfo) => {
    console.log("Failed:", errorInfo);
  };
  return (
    <div className="mt-4" style={{ marginLeft: 20, marginRight: 20 }}>
      <p className="text-red-400 mb-4">
        该功能只适用于用户加入空间后误操作点击退出空间后需要恢复的情况
      </p>
      <Form
        name="rollback"
        form={form}
        onFinish={rollback}
        onFinishFailed={onFinishFailed}
      >
        <Space direction="vertical" style={{ marginBottom: 20 }}>
          <Form.Item label="请输入用户ID" name="userid" style={{ margin: 0 }}>
            <Input type="number" />
          </Form.Item>
          <Form.Item
            label="请输入主播ID"
            name="streamer_userid"
            style={{ margin: 0 }}
          >
            <Input type="number" />
          </Form.Item>
          <Button type="primary" htmlType="submit">
            恢复
          </Button>
        </Space>
      </Form>
    </div>
  );
}
