import { FC, useState } from "react";
import { PlusOutlined } from "@ant-design/icons";
import { Button, Table } from "antd";
import { DageTableActions } from "@dage/pc-components";
import { IFileTemplateFormResponse } from "@/types";
import { FileTemplateModal } from "../FileTemplateModal";
import style from "./index.module.scss";

export interface FileTemplateTableProps {
  /**
   * 模块名称
   * norm:指标 | assess:考核
   * @default 'assess'
   */
  module?: "norm" | "assess";
  value?: IFileTemplateFormResponse[];
  tips?: string;
  onChange?: (val: IFileTemplateFormResponse[]) => void;
}

export const FileTemplateTable: FC<FileTemplateTableProps> = ({
  tips,
  value,
  module = "assess",
  onChange,
}) => {
  const [checkedItem, setCheckedItem] =
    useState<IFileTemplateFormResponse | null>(null);
  const [templateVisible, setTemplateVisible] = useState(false);

  const handleTemplate = (val: IFileTemplateFormResponse, isEdit?: boolean) => {
    const temp = value ? [...value] : [];

    if (isEdit) {
      const idx = temp.findIndex((i) => i.id === val.id);
      temp.splice(idx, 1, val);
    } else {
      temp.push(val);
    }

    onChange?.(temp);
  };

  const handleDelete = (index: number) => {
    const temp = value ? [...value] : [];
    temp.splice(index, 1);
    onChange?.(temp);
  };

  const handleCloseModal = () => {
    setTemplateVisible(false);
    setCheckedItem(null);
  };

  return (
    <div>
      <div className={style.top}>
        <Button
          className="second-button"
          icon={<PlusOutlined />}
          size="large"
          type="primary"
          onClick={() => setTemplateVisible(true)}
        >
          新增
        </Button>

        {tips && <p>{tips}</p>}
      </div>

      <Table
        className="cus-table mw650"
        dataSource={value}
        rowKey="id"
        columns={[
          {
            title: "资料名称",
            dataIndex: "name",
            key: "name",
            align: "center",
          },
          {
            title: "必须上传",
            key: "age",
            align: "center",
            render: (item: IFileTemplateFormResponse) => {
              return item.isUpload ? "是" : "否";
            },
          },
          {
            title: "模板",
            key: "address",
            align: "center",
            render: (item: IFileTemplateFormResponse) => {
              return item.fileName || "/";
            },
          },
          {
            title: "操作",
            align: "center",
            render: (item: IFileTemplateFormResponse, record, index) => {
              return (
                <DageTableActions
                  onEdit={() => {
                    setCheckedItem(item);
                    setTemplateVisible(true);
                  }}
                  onDelete={handleDelete.bind(undefined, index)}
                />
              );
            },
          },
        ]}
      />

      <FileTemplateModal
        item={checkedItem}
        open={templateVisible}
        module={module}
        onOk={handleTemplate}
        onCancel={handleCloseModal}
      />
    </div>
  );
};
