import { Button, Space, Modal, FormInstance } from "antd";
import { inject, observer } from "mobx-react";
import type { ColumnsType } from "antd/es/table";
import BTable from "@/components/b_table";
import { useEffect, useState } from "react";
import { UserDataType } from "@/model/userModel";
import { Store } from "antd/lib/form/interface";
import SimpleForm from "@/components/form/simple_form";
import React from "react";

const Tag = (props: Store) => {
  const { tagStore } = props;
  const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
  const [projectConfig, setProjectConfig] = useState<any>([]);
  const formRef = React.useRef<FormInstance>(null);
  const [record, setRecord] = useState<any>(null);
  const [tagId, setId] = useState<Number | null>(null);
  const columns: ColumnsType<UserDataType> = [
    { title: "标签昵称", dataIndex: "tag_name" },
    { title: "标签描述", dataIndex: "tag_desc" },
    {
      title: "操作",
      dataIndex: "id",
      render: (any, record) => (
        <div>
          <Space direction="vertical">
            <Space wrap>
              <Button
                type="dashed"
                size="small"
                onClick={() => {
                  edit(record);
                }}
              >
                编辑
              </Button>
              <Button
                type="dashed"
                danger
                size="small"
                onClick={() => {
                  tagStore.deleteItem(record.id);
                }}
              >
                删除
              </Button>
            </Space>
          </Space>
        </div>
      ),
    },
  ];
  const edit = (record) => {
    record = {
      ...record,
      imageUrl: [{ url: record.imageUrl }],
    };
    setProjectConfig(defaultConfig);
    setIsModalOpen(true);
    formRef.current?.setFieldsValue(record);
    setRecord(record);
    setId(record.id);
  };
  const onFinish = (values: any) => {
    if (!tagId) {
      tagStore.add(values);
    } else {
      tagStore.putItem(tagId, values);
    }
    setIsModalOpen(false);
  };
  useEffect(() => {
    tagStore.getlist();
  }, [tagStore]);
  const defaultConfig = [
    {
      type: "input",
      label: "标签名称",
      name: "tag_name",
      value: "",
      rules: [{ required: true, message: "请输入标签名称!" }],
    },
    {
      type: "input",
      label: "标签描述",
      name: "tag_desc",
      value: "",
      rules: [{ required: true, message: "请输入标签描述" }],
    },
  ];
  const onFinishFailed = () => {};
  return (
    <div className="contentBox">
      <Space direction="vertical" size="middle" style={{ display: "flex" }}>
        <Space direction="horizontal" size={"middle"}>
          <Button
            type="default"
            onClick={() => {
              setProjectConfig(defaultConfig);
              setId(null);
              setIsModalOpen(true);
            }}
          >
            添加
          </Button>
        </Space>

        <BTable store={tagStore} columns={columns} dataSource={tagStore.list} />
        <Modal
          title={!tagId ? "添加标签" : "编辑标签"}
          width={800}
          open={isModalOpen}
          afterClose={() => formRef.current?.resetFields()}
          onOk={() => formRef.current?.submit()}
          okText="确定"
          cancelText="取消"
          onCancel={() => {
            setId(null);
            setIsModalOpen(false);
          }}
        >
          <SimpleForm
            formRef={formRef}
            createCallback={() => {
              formRef.current?.setFieldsValue(record);
            }}
            formName="card_basic"
            colProps={4}
            subBtnName="提交"
            formDatas={projectConfig}
            onFinish={onFinish}
            initialValues={true}
            onFinishFailed={onFinishFailed}
          />
        </Modal>
      </Space>
    </div>
  );
};

export default inject("tagStore")(observer(Tag));
