import { AimOutlined, DeleteOutlined, PlusOutlined, SettingOutlined } from "@ant-design/icons";
import {
  Col,
  Row,
  Form,
  Input,
  Select,
  Button,
  Tooltip,
  Popover,
  Popconfirm,
} from "antd";
import React, { useEffect, useMemo, useState } from "react";
import CustomColorPicker from "@/components/CustomColorPicker";
import { ColumnItem as ColumnItemType, TableItemType } from "@/type";
import { TABLE_TYPE_OPTIONS } from "@/constants";
import { createColumn } from "@/utils";
import { useModel } from "umi";
import { DndContext } from "@dnd-kit/core";
import type { DragEndEvent } from "@dnd-kit/core";
import {
  SortableContext,
  arrayMove,
  verticalListSortingStrategy,
} from "@dnd-kit/sortable";
import { restrictToParentElement } from "@dnd-kit/modifiers";
import ColumnItem from "./ColumnItem";
import LangInput from "@/components/LangInput";
import LangInputTextarea from "@/components/LangInputTextarea";
import { validateAliasName, validateTableCode } from "@/utils/validator";
import { useLocalStorageState } from "ahooks";

export default function TableItem({
  data,
  onChange,
  active,
  setActive,
}: {
  data: TableItemType;
  onChange: (data: TableItemType) => void;
  active?: string;
  setActive: (active: string) => void;
}) {
  const { tableColumnList = [] } = data;
  const { addTable, deleteTable, graph } = useModel("erModel");
  const [list, setList] = useState(tableColumnList);
  const [table, setTable] = React.useState(data?.table);

  useEffect(() => {
    setList(tableColumnList);
  }, [tableColumnList]);

  // 隐藏默认字段
  const [hideDefaultColumn, setHideDefaultColumn] = useLocalStorageState(
    "er-hideDefaultColumn",
    {
      defaultValue: false,
      listenStorageChange: true,
    }
  );

  const filterList = useMemo(() => {
    if (hideDefaultColumn) {
      return list.filter((item) => !item.isPreDefined);
    }
    return list;
  }, [hideDefaultColumn, list]);

  const handleTableChange = (key: string, value: any) => {
    onChange({
      tableColumnList,
      table: { ...table, [key]: value },
      isTable: true,
    });
    setTable({ ...table, [key]: value });
  };

  const handleChangeColumn = (index: number, key: string, value: any) => {
    const data = tableColumnList[index];
    const newData = { ...data, [key]: value };
    setList(tableColumnList.map((item, i) => (i === index ? newData : item)));
    onChange({
      isTable: true,
      table,
      tableColumnList: tableColumnList.map((item, i) => {
        if (index === i) {
          return { ...item, [key]: value };
        }
        return item;
      }),
    });
  };

  // 添加表
  const handleAddChildTable = () => {
    addTable(table.id);
  };

  // 添加字段
  const handleAddColumn = () => {
    const newColumn: ColumnItemType = createColumn(
      table.id,
      tableColumnList.length + 1
    );
    onChange({
      table,
      tableColumnList: [...tableColumnList, newColumn],
      isTable: true,
    });
  };

  const handleDeleteColumn = (columnId: string) => {
    onChange({
      table,
      tableColumnList: tableColumnList.filter((item) => item.id !== columnId),
      isTable: true,
    });
  };

  const handleDragEnd = (dragItem: DragEndEvent) => {
    const { active, over } = dragItem;
    if (!active || !over) return; // 处理边界情况

    const activeIndex = tableColumnList.findIndex(
      (item) => item.id === active.id
    );
    const overIndex = tableColumnList.findIndex((item) => item.id === over.id);

    const newList = arrayMove(tableColumnList, activeIndex, overIndex);
    setList(newList);
    onChange({
      table,
      tableColumnList: newList,
      isTable: true,
    });
  };

  const handleAimTable = (e: React.MouseEvent) => {
    e.stopPropagation();
    graph?.centerCell(graph.getCellById(table.id));
    graph?.select(table.id);
  }

  return (
    <div
      className="
      w-full 
      border-l-solid 
      border-l-[#e6e6e6] 
      border-l-[4px] 
      border-b-solid 
      border-b-[#e4e4e4] 
      border-b-[1px] 
      m-b-4px
      p-l-16px"
      style={{
        borderLeftColor: table.style?.color || "#eee",
        marginLeft: table.parentBusinessTableId ? 10 : 0,
      }}
    >
      <div
        className="
        header 
        flex 
        items-center 
        justify-between 
        leading-[40px] 
        px-[10px] 
        hover:bg-[#fafafa] 
        cursor-pointer 
        m-b-[10px]"
        onClick={() => setActive(active === table.id ? "" : table.id)}
      >
        <div className="font-bold truncate flex-1">
          {table.schemaName}(
          {table?.langNameList?.find((item) => item.name === "zh-CN")?.value})
        </div>
        <div>
          <Tooltip title="画布定位">
            <AimOutlined onClick={handleAimTable}/>
          </Tooltip>
          
          <Popover
            trigger="click"
            placement="right"
            content={
              <div className="w-200px" onClick={(e) => e.stopPropagation()}>
                <Form layout="vertical" initialValues={table}>
                  <Form.Item label="表名称" name="langNameList">
                    <LangInput
                      value={table.langNameList}
                      onChange={(lang) => {
                        handleTableChange("langNameList", lang);
                      }}
                    />
                  </Form.Item>
                  <Form.Item label="描述" name="langDescriptionList">
                    <LangInputTextarea
                      value={table.langDescriptionList}
                      onChange={(lang) => {
                        handleTableChange("langDescriptionList", lang);
                      }}
                    />
                  </Form.Item>
                </Form>
              </div>
            }
          >
            <Tooltip title="更多设置">
              <SettingOutlined className="mx-4px" onClick={(e) => e.stopPropagation()} />
            </Tooltip>
          </Popover>
          <i
            className="iconfont icon-open inline-block"
            style={{
              transform:
                active === table.id ? "rotate(180deg)" : "rotate(0deg)",
              transition: "all 0.3s",
            }}
          />
        </div>
      </div>
      <div
        className="content overflow-hidden"
        style={{
          display: "grid",
          gridTemplateRows: active === table.id ? "1fr" : "0fr",
          transition: "all 0.3s",
        }}
      >
        <div className="overflow-hidden">
          <Form
            layout="horizontal"
            labelCol={{ span: 8 }}
            initialValues={table}
            size="small"
          >
            <Form.Item
              label="类型"
              labelCol={{ span: 4 }}
              wrapperCol={{ span: 21 }}
            >
              <Select
                placeholder="请选择"
                options={TABLE_TYPE_OPTIONS}
                value={table.type}
                disabled
                onChange={(val) => handleTableChange("type", val)}
              />
            </Form.Item>

            <Form.Item
              label="编码"
              name="schemaName"
              labelCol={{ span: 4 }}
              wrapperCol={{ span: 21 }}
              rules={[
                { required: true, message: "请输入编码" },
                validateTableCode,
              ]}
            >
              <Tooltip title={table.schemaName}>
                <Input
                  placeholder="请输入"
                  value={table.schemaName}
                  onChange={(e) =>
                    handleTableChange("schemaName", e.target.value)
                  }
                />
              </Tooltip>
            </Form.Item>

            <Form.Item
              label="别名"
              name="aliasName"
              labelCol={{ span: 4 }}
              wrapperCol={{ span: 21 }}
              rules={[
                { required: true, message: "请输入编码" },
                validateAliasName,
              ]}
            >
              <Tooltip title={table.aliasName}>
                <Input
                  placeholder="请输入"
                  value={table.aliasName}
                  onChange={(e) =>
                    handleTableChange("aliasName", e.target.value)
                  }
                />
              </Tooltip>
            </Form.Item>
          </Form>

          <div className="flex justify-between m-b-10px">
            <CustomColorPicker
              color={table.style?.background}
              onChange={(color) =>
                handleTableChange("style", { ...table.style, color })
              }
            >
              <div
                className="rounded-4px cus-btn w-32px h-32px bg-#eee flex-none cursor-pointer shadow-inner"
                style={{ background: table.style?.color || "#eee" }}
              ></div>
            </CustomColorPicker>
            <div className="flex gap-4px">
              {!table.parentBusinessTableId && (
                <Button
                  type="primary"
                  className="w-50px"
                  onClick={handleAddChildTable}
                >
                  子表
                </Button>
              )}
              <Button
                type="primary"
                className="w-50px"
                onClick={handleAddColumn}
              >
                字段
              </Button>
              <Popconfirm
                okType="primary"
                title="确定删除该表？"
                okText="确定"
                cancelText="取消"
                onConfirm={() => deleteTable(table.id)}
              >
                <div className="rounded-4px cus-btn w-32px h-32px bg-#eee flex-none text-center leading-32px color-red cursor-pointer">
                  <DeleteOutlined />
                </div>
              </Popconfirm>
            </div>
          </div>

          <div className="column-content border-solid border-1px border-#e4e4e4 border-x-none p-y-10px">
            {/* 字段内容 */}
            <DndContext
              onDragEnd={handleDragEnd}
              modifiers={[restrictToParentElement]}
            >
              <SortableContext
                items={filterList.map((item) => item.id)}
                strategy={verticalListSortingStrategy}
              >
                {filterList.map((column, index) => {
                  return (
                    <ColumnItem
                      key={column.id}
                      column={column}
                      onChange={(key, val) =>
                        handleChangeColumn(index, key, val)
                      }
                      onDelete={handleDeleteColumn}
                    />
                  );
                })}
              </SortableContext>
            </DndContext>
          </div>
        </div>
      </div>
    </div>
  );
}
