import { Button, Space, Modal, FormInstance } from "antd";
import { inject, observer } from "mobx-react";
import BTable from "@/components/b_table";
import { useEffect, useRef, useState } from "react";
import { Store } from "antd/lib/form/interface";
import SimpleForm from "@/components/form/simple_form";
import React from "react";
import { studyColumns, defaultConfig } from "./political_column";

const PoliticalStudy = (props: Store) => {
  const { politicalStudyStore } = 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 recordRef = useRef<any>(null);

  useEffect(() => {
    politicalStudyStore.getlist();
    setProjectConfig(defaultConfig);
  }, [politicalStudyStore]);
  const column_widget = (any, record) => {
    return (
      <Space wrap>
        <Button
          type="dashed"
          size="small"
          onClick={() => {
            edit(record);
          }}
        >
          编辑
        </Button>

        <Button
          type="dashed"
          danger
          size="small"
          onClick={() => {
            politicalStudyStore.deleteItem(record.identity);
          }}
        >
          删除
        </Button>
      </Space>
    );
  };
  const edit = (records) => {
    setIsModalOpen(true);
    let data = {
      ...records,
      file_url: [{url:records.file_url ?? ""}],
    };
    setRecord(data);
    formRef.current?.setFieldsValue(recordRef.current);
    
  };
  const onFinish = (values: any) => {
    let data = {
      ...values,
      score: Number(values.score),
    };
    if (values.file_url.length > 0) {
      data.file_url = values.file_url[0].url;
    } else {
      data.file_url = "";
    }
    if (!record?.id) {
      politicalStudyStore.add(data);
    } else {
      politicalStudyStore.putItem(record.id, data);
    }
    setIsModalOpen(false);
  };

  const onFinishFailed = () => {};
  return (
    <div className="contentBox">
      <Space direction="vertical" size="middle" style={{ display: "flex" }}>
        <Space direction="horizontal" size={"middle"}>
          <Button
            type="default"
            onClick={() => {
              setRecord(null);
              setProjectConfig(defaultConfig);
              setIsModalOpen(true);
            }}
          >
            发布文章
          </Button>
        </Space>
        <BTable
          store={politicalStudyStore}
          columns={[
            ...studyColumns,
            {
              title: "操作",
              dataIndex: "id",

              render: (any, record) => column_widget(any, record),
            },
          ]}
          dataSource={politicalStudyStore.list}
        />
        <Modal
          title={!record?.id ? "添加文章" : "编辑文章"}
          width={1200}
          open={isModalOpen}
          afterClose={() => formRef.current?.resetFields()}
          onOk={() => formRef.current?.submit()}
          okText="确定"
          cancelText="取消"
          onCancel={() => {
            setIsModalOpen(false);
          }}
        >
          <SimpleForm
            formRef={formRef}
            current={record}
            createCallback={() => {
              if (record?.id) {
                formRef.current?.setFieldsValue(record);
              } else {
                formRef.current?.setFieldsValue(null);
              }
            }}
            formName="card_basic"
            colProps={25}
            subBtnName="提交"
            formDatas={projectConfig}
            onFinish={onFinish}
            initialValues={true}
            onFinishFailed={onFinishFailed}
          />
        </Modal>
      </Space>
    </div>
  );
};

export default inject("politicalStudyStore")(observer(PoliticalStudy));
