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 Community = (props: Store) => {
  const { communityStore } = 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: "name" },
    { title: "社区描述", dataIndex: "desc" },
    {
      title: "操作",
      dataIndex: "id",
      render: (any, record) => (
        <div>
          <Space wrap>
            <Button
              type="dashed"
              size="small"
              onClick={() => {
                edit(record);
              }}
            >
              编辑
            </Button>
            <Button
              type="dashed"
              danger
              size="small"
              onClick={() => {
                communityStore.deleteItem(record.id);
              }}
            >
              删除
            </Button>
          </Space>
        </div>
      ),
    },
  ];
  const edit = (record) => {
    let data = {
      ...record,
      head_img: [{ url: record.head_img }],
    };
    setProjectConfig(defaultConfig);
    setIsModalOpen(true);
    formRef.current?.setFieldsValue(data);
    setRecord(data);
    setId(record.id);
  };
  const onFinish = (values: any) => {
    if (!tagId) {
      communityStore.add(values);
    } else {
      communityStore.putItem(tagId, values);
    }
    setIsModalOpen(false);
  };
  useEffect(() => {
    communityStore.getlist();
  }, [communityStore]);
  const defaultConfig = [
    {
      type: "input",
      label: "社区名称",
      name: "name",
      value: "",
      rules: [{ required: true, message: "请输入社区名称!" }],
    },
    {
      type: "input",
      label: "社区描述",
      name: "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={communityStore}
          columns={columns}
          dataSource={communityStore.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={25}
            subBtnName="提交"
            formDatas={projectConfig}
            onFinish={onFinish}
            initialValues={true}
            onFinishFailed={onFinishFailed}
          ></SimpleForm>
        </Modal>
      </Space>
    </div>
  );
};

export default inject("communityStore")(observer(Community));
