import * as React from 'react';
import { Button, Table, TableColumnsType, TablePaginationConfig, Modal } from 'antd';
import { PlusOutlined, DownloadOutlined } from '@ant-design/icons';
import Search from './components/Search';
import dayjs from 'dayjs';
import store from './store';
import { Record } from './types';
import './style.scss';

const ContentManagement: React.FC = () => {
    const {
        state,
        onClickSearch,
        onClickReset,
        onChangePagination,
        onClickDownload,
        init,
        reset
    } = store;
    const {
        listLoading,
        list,
        page
    } = state;

    React.useEffect(() => {
        init();
        return () => reset();
    }, []);

    const columns: TableColumnsType<Record> = [
        {
            title: '序号',
            dataIndex: 'index',
            width: 80,
            render: (text, record, index) => {
                return index + 1;
            }
        },
        {
            title: '模型分类',
            dataIndex: 'dialog_name',
        },
        {
            title: '安全策略',
            dataIndex: 'length',
            render: (text) => {
                return text + '条';
            }
        },
        {
            title: '更新时间',
            dataIndex: 'create_time',
            width: 200,
            render: (text) => {
                if (text) {
                    return dayjs(text).format('YYYY-MM-DD HH:mm:ss');
                } else {
                    return '--';
                }
            }
        },
        {
            title: '操作',
            dataIndex: 'operation',
            width: 120,
            fixed: 'right',
            render: (text, record) => {
                return (
                    <a
                        onClick={() => {
                            Modal.confirm({
                                title: '导出',
                                content: '确定导出 Excel 吗？',
                                onOk: async () => {
                                    await onClickDownload(record.id, record.dialog_name);
                                }
                            });
                        }}
                    >
                        <DownloadOutlined /> 导出
                    </a>
                );
            }
        }
    ];

    const paginationConfig: TablePaginationConfig = {
        showTotal: (total: number) => `共 ${total} 条`,
        showSizeChanger: true,
        pageSizeOptions: ['10', '20', '50', '100'],
        showQuickJumper: true,
        current: page.pageNum,
        pageSize: page.pageSize,
        total: page.total,
        onChange: async (page, pageSize) => {
            await onChangePagination(page, pageSize);
        },
    };

    return (
        <div className="page-container">
            {/* List Header */}
            <div className="list-header">
                <div className="list-header-title">
                    <h1>内容管理</h1>
                    <p>管理系统内容和安全策略</p>
                </div>
                <div className="list-header-actions">
                    <Button type="primary" icon={<PlusOutlined />}>
                        创建
                    </Button>
                </div>
            </div>

            {/* 搜索 + 表格区域 - 共用一个 content-section */}
            <div className="content-section">
                {/* 搜索区域 */}
                <div style={{ marginBottom: 16 }}>
                    <Search
                        onClickSearch={onClickSearch}
                        onClickReset={onClickReset}
                    />
                </div>
                
                {/* 表格区域 */}
                <Table
                    scroll={{ x: 'max-content' }}
                    rowKey={(record) => record.id}
                    loading={listLoading}
                    columns={columns}
                    dataSource={list}
                    pagination={paginationConfig}
                />
            </div>
        </div>
    );
};

export default ContentManagement;
