import React, { useEffect, useState } from 'react';
import { Table, Button } from 'antd';
import type { ColumnsType, TablePaginationConfig } from 'antd/es/table';
import { bankGetPage } from '../services';
import { useIntl } from 'umi';
import BankFormModal from './BankFormModal';

interface BankInfo {
  id: string;
  account: string;
  accountName: string;
  bank: string;
  city: string;
  currency: string;
  interbankNumber: string;
  nation: string;
  province: string;
  supplierId: string;
  swiftCode: null;
}

const BankInfoTab: React.FC = () => {
  //双语
  const intl = useIntl();
  //列表渲染数据
  const [data, setData] = useState<BankInfo[]>([]);
  //列表加载
  const [loading, setLoading] = useState(false);
  //列表分页
  const [pagination, setPagination] = useState<TablePaginationConfig>({ current: 1, pageSize: 10, total: 0 });
  //列表方法
  const getList = async (pageNo: number = 1, pageSize: number = 10) => {
    setLoading(true);
    try {
      const { code, data } = await bankGetPage({pageNo, pageSize});
      if (code === 200) {
        setData(data.records);
        setPagination({ current: pageNo, pageSize, total:data.total }); 
      }
    } finally {
      setLoading(false);
    }
  };

  //增改查
  const [formVisible, setFormVisible] = useState(false);
  const [editingRecord, setEditingRecord] = useState<BankInfo | null>(null);
  const [isViewMode, setIsViewMode] = useState(false);
  const handleFormSubmit = () => {
    setFormVisible(false);
    getList();
  };
  const handleAdd = () => {
    setEditingRecord(null);
    setIsViewMode(false);
    setFormVisible(true);
  };

  const handleEdit = (record: BankInfo) => {
    setEditingRecord(record);
    setIsViewMode(false);
    setFormVisible(true);
  };

  const handleView = (record: BankInfo) => {
    setEditingRecord(record);
    setIsViewMode(true);
    setFormVisible(true);
  };

  //初始化
  useEffect(() => {
    getList();
  }, []);
  // 表格头部
  const columns: ColumnsType<BankInfo> = [
    {
      title: 'page.workbench.bank.index',
      dataIndex: 'index',
      key: 'index',
      width: 80,
      align: 'center',
      render: (_: any, __: any, index: number) => index + 1 
    },
    {
      title: 'page.workbench.bank.interbankNumber',
      dataIndex: 'interbankNumber',
      key: 'interbankNumber', ellipsis: true
    },
    {
      title: 'page.workbench.bank.bank',
      dataIndex: 'bank',
      key: 'bank', ellipsis: true
    },
    {
      title: 'page.workbench.bank.accountName',
      dataIndex: 'accountName',
      key: 'accountName', ellipsis: true
    },
    {
      title: 'page.workbench.bank.account',
      dataIndex: 'account',
      key: 'account', ellipsis: true
    },
    {
      title: 'page.workbench.bank.currency',
      dataIndex: 'currency',
      key: 'currency', ellipsis: true
    },
    {
      title: 'page.workbench.bank.nation',
      dataIndex: 'nation',
      key: 'nation', ellipsis: true
    },
    {
      title: 'page.workbench.bank.province',
      dataIndex: 'province',
      key: 'province', ellipsis: true
    },
    {
      title: 'page.workbench.bank.city',
      dataIndex: 'city',
      key: 'city', ellipsis: true
    },
    {
      title: 'page.workbench.bank.updateTime',
      dataIndex: 'updateTime',
      key: 'updateTime', ellipsis: true
    },
    {
      title: 'page.workbench.attachments.action',
      dataIndex: 'option',
      width: 120,
      render: (_: any, record: BankInfo) => (
        <>
          <a style={{ marginRight: 8 }} onClick={() => handleView(record)}>查看</a>
          <a onClick={() => handleEdit(record)}>修改</a>
        </>
      ),
    },
  ];
  return (
    <div style={{ padding: '0 30px 0 0' }}>
      <div style={{ marginBottom: 16 }}>
        <Button type="primary" onClick={handleAdd}>新增</Button>
      </div>
      <Table
        rowKey="id"
        className="custom-table"
        columns={columns.map(column => ({
          ...column,
          title: intl.formatMessage({ id: column.title as string }) 
        }))}
        dataSource={data}
        pagination={pagination}
        loading={loading}
        onChange={(pagination) => getList(pagination.current!, pagination.pageSize!)}
      />
        <BankFormModal
        visible={formVisible}
        onOk={handleFormSubmit}
        onCancel={() => setFormVisible(false)}
        initialValues={editingRecord || undefined}
        readOnly={isViewMode}
      />
    </div>
  );
};

export default BankInfoTab;