import React, { useEffect, useState } from "react";
import {  useIntl } from 'umi';
import { Form, Button, Table, Select, Input } from 'antd';
import type { ColumnsType, TablePaginationConfig } from 'antd/es/table';
import { SearchOutlined } from '@ant-design/icons';
import { bank, categoryOption } from './services';

interface Data {
  deptName: string;
  categoryName: string;
  createTime: string;
  exitTime: string;
  exitReason: string;
}

interface CategoryOption {
  value: string;
  label: string;
}

const columns: ColumnsType<Data> = [
  {
    title: '序号',
    dataIndex: 'index',
    key: 'index',
    width: 80,
    align: 'center',
    render: (_: any, __: any, index: number) => index + 1,
  },
  {
    title: '消息内容',
    dataIndex: 'deptName',
    key: 'deptName',
  },
  {
    title: '业务类型',
    dataIndex: 'categoryName',
    key: 'categoryName',
  },
  {
    title: '发送时间',
    dataIndex: 'createTime',
    key: 'createTime',
  },
  {
    title: '审核单位',
    dataIndex: 'exitTime',
    key: 'exitTime',
  },
  {
    title: '审核状态',
    dataIndex: 'exitReason',
    key: 'exitReason',
  },
];

const CooperateEnterprise: React.FC = () => {
  //双语
  const intl = useIntl();
  //搜索
  const [searchForm] = Form.useForm();
  //列表数据
  const [data, setData] = useState<Data[]>([]);
   //列表加载
  const [loading, setLoading] = useState(false);
  //分页
  const [pagination, setPagination] = useState<TablePaginationConfig>({ current: 1, pageSize: 10, total: 0 });
  //下拉数据
  const [categoryOptions, setCategoryOptions] = useState<CategoryOption[]>([]);
  //列表数据方法
  const getList = async (params: { page: number; pageSize: number; parentCode: string;  }) => {
    setLoading(true);
    try {
      const response = await bank(params);
      if (response.code === 200) {
        setData(response.data);
        setPagination({ current: params.page, pageSize: params.pageSize, total: response.total });
      }
    } catch (error) {
      console.error('Failed to fetch data:', error);
    } finally {
      setLoading(false);
    }
  };
  //搜索重置
  const handleReset = () => {
    searchForm.resetFields();
    getList({ page: 1, pageSize: pagination.pageSize ?? 10, parentCode: ''  });
  };
  //搜索
  const handleSearch = (values: any) => {
    const { parentCode } = values;
    getList({
      page: 1,
      pageSize: pagination.pageSize ?? 10,
      parentCode: parentCode || ''
    });
  };
  //初始化
  useEffect(() => {
    categoryOption().then((res:any) => {
      const { code, data } = res;
      if(code == 200) {
        setCategoryOptions(data)
      }
    })
    getList({ page: 1, pageSize: 10, parentCode: '',  });
  }, []);

  return (
    <>
      <Form
        form={searchForm}
        layout="inline"
        onFinish={handleSearch}
        style={{ marginBottom: 16 }}
      >
        <Form.Item name="parentCode" label="消息内容">
            <Input placeholder="请输入消息内容" />
        </Form.Item>
        <Form.Item name="parentCode" label="业务类型">
          <Select placeholder="请选择业务类型">
            {categoryOptions.map((option) => (
              <Select.Option key={option.value} value={option.value}>
                {option.label}
              </Select.Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item>
          <Button type="primary" htmlType="submit" icon={<SearchOutlined />}>
            搜索
          </Button>
        </Form.Item>
        <Form.Item>
          <Button onClick={handleReset}>重置</Button>
        </Form.Item>
      </Form>
      <Table
        rowKey="id"
        className="custom-table"
        columns={columns}
        dataSource={data}
        pagination={pagination}
        loading={loading}
        onChange={(pagination) => getList({ page: pagination.current!, pageSize: pagination.pageSize!, parentCode: '', })}
      />
    </>
  );
};

export default CooperateEnterprise;