import React, { useEffect, useState } from 'react';
import { PageContainer } from '@ant-design/pro-components';
import { Button, Card, Form, Input, Modal, Space, Table } from 'antd';
import { PlusCircleOutlined, ReloadOutlined, SearchOutlined } from '@ant-design/icons';
import type { ColumnsType } from 'antd/es/table';
import moment from 'moment';
import Edit from '@/pages/RoleManagement/edit';

interface DataType {
  name: string;
  sequence: number;
  creator: string;
  record_id: string;
}

const RoleManagement: React.FC = () => {
  const [form] = Form.useForm();
  const [loading, setLoading] = useState(false);
  const pagination = useState({});
  const [dataList, setDataList] = useState([]);
  const [editData, setEditData] = useState<object | null>({});
  const [visible, setVisible] = useState(false);
  const [searchData, setSearchData] = useState<object | null>({});

  const getList = () => {
    const data: any = [
      {
        name: '角色名称',
        sequence: 1,
        creator: '创建者',
        created_at: '2023-01-04T14:31:23.664+08:00',
        memo: '备注备注备注备注',
      },
    ];
    setDataList(data);
  };

  useEffect(() => {
    getList();
  }, []);

  // 搜索
  const onFinish = () => {
    form.validateFields().then((data) => {
      setLoading(true);
      setSearchData(data);
    });
  };

  useEffect(() => {
    getList();
  }, [searchData]);

  // 重置
  const onReset = () => {
    form.resetFields();
    setLoading(true);
    setSearchData(null);
  };

  // 新增
  const onAdd = () => {
    setEditData(null);
    setVisible(true);
  };

  // 分页切换
  const tableChange = (page: any) => {
    setLoading(true);
    const params = {
      q: 'page',
      current: page.current,
      pageSize: page.pageSize,
      ...searchData,
    };
    console.log(params);
    //   请求接口
  };

  // 编辑回调
  const handleEdit = () => {
    setVisible(false);
    getList();
  };

  // 编辑
  const toEdit = (data: any) => {
    setEditData(data);
    setVisible(true);
  };

  // 删除
  const toDel = (record: any) => {
    Modal.confirm({
      title: '删除',
      content: `确认删除角色：[${record.name}]`,
      onOk: () => {
        //   请求接口
      },
    });
  };

  const columns: ColumnsType<DataType> = [
    {
      title: '序号',
      align: 'center',
      key: 'index',
      render: (_: any, row: any, index: number) => index + 1,
    },
    {
      title: '角色名称',
      dataIndex: 'name',
      key: 'name',
      width: 250,
    },
    {
      title: '排序值',
      dataIndex: 'sequence',
      key: 'sequence',
      width: 250,
    },
    {
      title: '创建者',
      dataIndex: 'creator',
      key: 'creator',
    },
    {
      title: '创建时间',
      dataIndex: 'created_at',
      key: 'created_at',
      render: (v) => v && <span>{moment(v).format('YYYY-MM-DD HH:mm')}</span>,
    },
    {
      title: '备注',
      dataIndex: 'memo',
      key: 'memo',
      width: 250,
    },
    {
      title: '操作',
      key: 'action',
      render: (_, record) => (
        <Space size="middle">
          <a
            onClick={() => {
              toEdit(record);
            }}
          >
            编辑
          </a>
          <a
            style={{ color: 'red' }}
            onClick={() => {
              toDel(record);
            }}
          >
            删除
          </a>
        </Space>
      ),
    },
  ];
  const paginationProps = {
    showSizeChanger: true,
    showQuickJumper: true,
    showTotal: (total: number) => {
      return <span> 共 {total}条 </span>;
    },
    ...pagination,
  };
  return (
    <PageContainer>
      <div>
        <Card>
          <Form form={form} layout="inline" onFinish={onFinish}>
            <Form.Item name="name" label="角色名称">
              <Input placeholder="请输入角色名称" />
            </Form.Item>
            <Form.Item style={{ marginBottom: '10px' }}>
              <Space>
                <Button type="primary" htmlType="submit">
                  <SearchOutlined />
                  查询
                </Button>
                <Button htmlType="button" onClick={onReset}>
                  <ReloadOutlined />
                  重置
                </Button>
              </Space>
            </Form.Item>
          </Form>
          <Button htmlType="button" type="primary" style={{ margin: '20px 0' }} onClick={onAdd}>
            <PlusCircleOutlined />
            新增角色
          </Button>
          <Table
            columns={columns}
            dataSource={dataList}
            rowKey={(record) => record.record_id}
            pagination={paginationProps}
            loading={loading}
            onChange={tableChange}
          />
          {visible && <Edit visible={visible} editCallback={handleEdit} params={editData} />}
        </Card>
      </div>
    </PageContainer>
  );
};
export default RoleManagement;
