import {useState} from "react";
import Taro, {useDidShow} from '@tarojs/taro'
import {Button, Cell, CellGroup, Space, Empty, ConfigProvider, Divider} from '@nutui/nutui-react-taro'
import {ArrowRight} from '@nutui/icons-react-taro'
import {AppEvent} from "@/api/app/appEvent/model";
import {listAppEvent, removeAppEvent} from "@/api/app/appEvent";

const AppEventList = () => {
  const [list, setList] = useState<AppEvent[]>([])

  const reload = () => {
    listAppEvent({})
      .then(data => {
        setList(data || [])
      })
      .catch(() => {
        Taro.showToast({
          title: '获取数据失败',
          icon: 'error'
        });
      })
  }

  const onDel = async (id?: number) => {
    await removeAppEvent(id)
    Taro.showToast({
      title: '删除成功',
      icon: 'success'
    });
    reload();
  }

  useDidShow(() => {
    reload()
  });

  if (list.length == 0) {
    return (
      <ConfigProvider>
        <div className={'h-full flex flex-col justify-center items-center'} style={{
          height: 'calc(100vh - 300px)',
        }}>
          <Empty
            style={{
              backgroundColor: 'transparent'
            }}
            description="暂无数据"
          />
          <Space>
            <Button onClick={() => Taro.navigateTo({url: '/app/appEvent/add'})}>新增应用事件</Button>
          </Space>
        </div>
      </ConfigProvider>
    )
  }

  return (
    <ConfigProvider>
      <CellGroup>
        {list.map((item) => (
          <Cell
            key={item.id}
            title={item.eventName || '未命名事件'}
            description={item.eventType}
            align="center"
            rightIcon={<ArrowRight />}
            onClick={() => Taro.navigateTo({url: `/app/appEvent/add?id=${item.id}`})}
          />
        ))}
      </CellGroup>
      <Space direction="vertical" style={{padding: '16px'}}>
        <Button onClick={() => Taro.navigateTo({url: '/app/appEvent/add'})}>
          新增应用事件
        </Button>
      </Space>
    </ConfigProvider>
  );
};

export default AppEventList;
