/*
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */


import React, {useRef, useState} from "react";
import {PageContainer} from '@ant-design/pro-layout';
import {
  CheckCircleOutlined,
  DeleteOutlined,
  EditOutlined,
  EllipsisOutlined,
  ExclamationCircleOutlined,
  HeartOutlined,
  PlusOutlined
} from '@ant-design/icons';
import {Button, Card, Image, Modal, Space, Tag, Typography} from 'antd';
import ProList from '@ant-design/pro-list';
import {handleRemove, queryData} from "@/components/Common/crud";
import {getDBImage} from "@/pages/DataBase/DB";
import DBForm from "./components/DBForm";
import {ActionType} from "@ant-design/pro-table";

import styles from './index.less';
import {DataBaseItem} from "@/pages/DataBase/data";
import {checkHeartBeat} from "@/pages/DataBase/service";
import {showDataBase} from "@/components/Studio/StudioEvent/DDL";

const {Text} = Typography;

const url = '/api/database';
const cardBodyStyle = {
  backgroundColor: 'rgba(0, 0, 0, 0.08)',
  position: 'relative',
  zIndex: 999
};

const DataBaseTableList: React.FC<{}> = (props: any) => {
  const {dispatch} = props;
  const [chooseDBModalVisible, handleDBFormModalVisible] = useState<boolean>(false);
  const [values, setValues] = useState<any>({});
  const actionRef = useRef<ActionType>();


  const onRefreshDataBase = () => {
    showDataBase(dispatch);
  };

  const onEdit = (row: DataBaseItem) => {
    setValues(row);
    handleDBFormModalVisible(true);
  };

  const onCheckHeartBeat = (row: DataBaseItem) => {
    checkHeartBeat(row.id);
    actionRef.current?.reloadAndRest?.();
  };

  const onDeleteDataBase = (row: DataBaseItem) => {
    Modal.confirm({
      title: '删除数据源',
      content: `确定删除该数据源【${row.alias === "" ? row.name : row.alias}】吗？`,
      okText: '确认',
      cancelText: '取消',
      onOk: async () => {
        await handleRemove('api/database', [row]);
        onRefreshDataBase();
        actionRef.current?.reloadAndRest?.();
      }
    });
  };

  return (
    <PageContainer>
      <ProList
        actionRef={actionRef}
        toolBarRender={() => {
          return [
            <Button type="primary" onClick={() => {
              setValues({});
              handleDBFormModalVisible(true);
            }}>
              <PlusOutlined/> 新建
            </Button>,
          ];
        }}
        pagination={{
          defaultPageSize: 8,
          showSizeChanger: false,
        }}
        grid={{gutter: 16, column: 4}}
        request={(params, sorter, filter) => queryData(url, {...params, sorter: {id: 'descend'}, filter})}
        renderItem={(row) => {
          return (
            <Card
              style={{width: 300}}
              hoverable={true}
              bodyStyle={cardBodyStyle}
              cover={
                <div className={styles.cardImage}>
                  <Image
                    preview={false}
                    style={{padding: '20px 60px'}}
                    src={getDBImage(row.type)}
                    fallback="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMIAAADDCAYAAADQvc6UAAABRWlDQ1BJQ0MgUHJvZmlsZQAAKJFjYGASSSwoyGFhYGDIzSspCnJ3UoiIjFJgf8LAwSDCIMogwMCcmFxc4BgQ4ANUwgCjUcG3awyMIPqyLsis7PPOq3QdDFcvjV3jOD1boQVTPQrgSkktTgbSf4A4LbmgqISBgTEFyFYuLykAsTuAbJEioKOA7DkgdjqEvQHEToKwj4DVhAQ5A9k3gGyB5IxEoBmML4BsnSQk8XQkNtReEOBxcfXxUQg1Mjc0dyHgXNJBSWpFCYh2zi+oLMpMzyhRcASGUqqCZ16yno6CkYGRAQMDKMwhqj/fAIcloxgHQqxAjIHBEugw5sUIsSQpBobtQPdLciLEVJYzMPBHMDBsayhILEqEO4DxG0txmrERhM29nYGBddr//5/DGRjYNRkY/l7////39v///y4Dmn+LgeHANwDrkl1AuO+pmgAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAA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AhZ6NSiAARCjXbUHMCRMjZqBQiQIRCzTbUnAARcjYqhQgQoVCzDTUnQIScjUohAkQo1GxDzQkQIWejUogAEQo121BzAkTI2agUIkCEQs021JwAEXI2KoUIEKFQsw01J0CEnI1KIQJEKNRsQ80JECFno1KIABEKNdtQcwJEyNmoFCJAhELNNtScABFyNiqFCBChULMNNSdAhJyNSiECRCjUbEPNCRAhZ6NSiAARCjXbUHMCRMjZqBQiQIRCzTbUnAARcjYqhQgQoVCzDTUnQIScjUohAkQo1GxDzQkQIWejUogAEQo121BzAkTI2agUIkCEQs021JwAEXI2KoUIEKFQsw01J0CEnI1KIQJEKNRsQ80JECFno1KIABEKNdtQcwJEyNmoFCJAhELNNtScABFyNiqFCBChULMNNSdAhJyNSiEC/wGgKKC4YMA4TAAAAABJRU5ErkJggg=="
                  />
                </div>
              }
              actions={[
                <HeartOutlined key="setting" onClick={() => {
                  onCheckHeartBeat(row);
                }}/>,
                <EditOutlined key="edit" onClick={() => {
                  onEdit(row);
                }}/>,
                <DeleteOutlined key="delete" onClick={() => {
                  onDeleteDataBase(row);
                }}/>,
                <EllipsisOutlined key="ellipsis"/>,
              ]}
            >
              <Card.Meta
                title={<Tag color="gray" key={row.alias}>
                  {row.alias}
                </Tag>}
                description={(
                  <Space size={0}>
                    <Tag key={row.name}>
                      {row.name}
                    </Tag>
                    <Tag color="blue" key={row.groupName}>
                      {row.groupName}
                    </Tag>
                    {(row.status) ?
                      (<Tag icon={<CheckCircleOutlined/>} color="success">
                        正常
                      </Tag>) :
                      <Tag icon={<ExclamationCircleOutlined/>} color="warning">
                        异常
                      </Tag>}
                  </Space>
                )}
              />
            </Card>)
        }}
        headerTitle="数据源"
      />
      <DBForm onCancel={() => {
        handleDBFormModalVisible(false);
        setValues({});
      }}
              modalVisible={chooseDBModalVisible}
              onSubmit={() => {
                actionRef.current?.reloadAndRest?.();
              }}
              values={values}
      />
    </PageContainer>
  );
};


export default DataBaseTableList;
