import React from 'react';
import {
    DeleteOutlined,
    EditOutlined,
    PlusOutlined,
    SearchOutlined,
    RedoOutlined,
    DownOutlined,
    ExclamationCircleFilled,
    UpOutlined,
    InfoCircleFilled,
    QuestionCircleFilled,
    DownloadOutlined
} from '@ant-design/icons';
import {connect, history} from '@umijs/max';
import {Button, Card, Divider, Dropdown, message, Modal, Popconfirm, Space, Switch, Tag, Row, Col} from 'antd';
import StandardTable from '@/components/StandardTable';

import { MyIcon } from "@/components/Icon"
import style from "@/global.less";
import styles from './SystemOrganizationList.less';
import datadictionary from "@/utils/dataDictionary";
import {formatDate} from "@/utils/formatUtils";
import { formatDictText, checkButtonAuthority } from "@/utils/globalCommon";

const { confirm } = Modal;

// 组织类型
const organization_type = datadictionary.organization_type || [];
const organization_status = datadictionary.organization_status || [];

const mockData = {
    list: [
        {
            id: '1',
            name: '总部',
            code: 'HQ',
            type: '1',
            parentId: '0',
            status: '1',
            createTime: '2023-01-01 10:00:00',
            createUser: 'admin'
        },
        {
            id: '2',
            name: '研发部',
            code: 'R&D',
            type: '2',
            parentId: '1',
            status: '1',
            createTime: '2023-01-02 14:30:00',
            createUser: 'admin'
        },
        {
            id: '3',
            name: '市场部',
            code: 'MKT',
            type: '2',
            parentId: '1',
            status: '1',
            createTime: '2023-01-03 09:15:00',
            createUser: 'admin'
        }
    ],
    pagination: {
        total: 3,
        pageSize: 10,
        current: 1
    }
};

@connect(({ systemOrganization }) => ({
    systemOrganization
}))
export default class SystemOrganizationList extends React.Component {
    state = {
        selectedRows: [],
        advancedFormVisible: false,
        filterData: {},
        pagination: {
            pageSize: 10,
            current: 1
        },
        tableData: mockData.list
    };

    componentDidMount() {
        this.getOrganizationList();
    }

    // 获取组织列表
    getOrganizationList = () => {
        const { dispatch } = this.props;
        const { pagination, filterData } = this.state;
        
        try {
            dispatch({
                type: 'systemOrganization/fetchList',
                payload: {
                    pageNum: pagination.current,
                    pageSize: pagination.pageSize,
                    ...filterData
                },
                callback: (res) => {
                    this.setState({ 
                        tableData: res?.list || mockData.list,
                        pagination: {
                            ...pagination,
                            total: res?.total || mockData.pagination.total
                        }
                    });
                }
            });
        } catch (error) {
            console.error('获取组织列表失败:', error);
            this.setState({ 
                tableData: mockData.list,
                pagination: mockData.pagination
            });
        }
    };

    // 表格选择变化
    handleSelectChange = (selectedRowKeys, selectedRows) => {
        this.setState({ selectedRows });
    };

    // 分页变化
    handleTableChange = (pagination) => {
        this.setState({
            pagination: {
                ...this.state.pagination,
                current: pagination.current
            }
        }, () => {
            this.getOrganizationList();
        });
    };

    // 新增组织
    handleAdd = () => {
        // 新增逻辑
        message.success('新增组织功能');
    };

    // 编辑组织
    handleEdit = (record) => {
        // 编辑逻辑
        message.success('编辑组织功能');
    };

    // 删除组织
    handleDelete = (record) => {
        confirm({
            title: '确定要删除这个组织吗?',
            icon: <ExclamationCircleFilled />,
            onOk: () => {
                // 删除逻辑
                message.success('删除组织成功');
            }
        });
    };

    // 批量删除
    handleBatchDelete = () => {
        const { selectedRows } = this.state;
        if (selectedRows.length === 0) {
            message.warning('请选择要删除的组织');
            return;
        }
        confirm({
            title: `确定要删除选中的${selectedRows.length}个组织吗?`,
            icon: <ExclamationCircleFilled />,
            onOk: () => {
                // 批量删除逻辑
                message.success('批量删除组织成功');
            }
        });
    };

    // 启用/禁用组织
    handleStatusChange = (record) => {
        const newStatus = record.status === '1' ? '0' : '1';
        confirm({
            title: `确定要${newStatus === '1' ? '启用' : '禁用'}这个组织吗?`,
            icon: <ExclamationCircleFilled />,
            onOk: () => {
                // 状态变更逻辑
                message.success(`${newStatus === '1' ? '启用' : '禁用'}组织成功`);
            }
        });
    };

    render() {
        const { selectedRows, tableData, pagination } = this.state;

        const columns = [
            {
                title: '组织名称',
                dataIndex: 'name',
                key: 'name',
                ellipsis: true
            },
            {
                title: '组织编码',
                dataIndex: 'code',
                key: 'code',
                ellipsis: true
            },
            {
                title: '组织类型',
                dataIndex: 'type',
                key: 'type',
                render: text => formatDictText(organization_type, text)
            },
            {
                title: '状态',
                dataIndex: 'status',
                key: 'status',
                render: text => (
                    <Tag color={text === '1' ? 'green' : 'red'}>
                        {text === '1' ? '启用' : '禁用'}
                    </Tag>
                )
            },
            {
                title: '创建时间',
                dataIndex: 'createTime',
                key: 'createTime',
                ellipsis: true
            },
            {
                title: '创建人',
                dataIndex: 'createUser',
                key: 'createUser',
                ellipsis: true
            },
            {
                title: '操作',
                key: 'action',
                fixed: 'right',
                width: 160,
                render: (_, record) => (
                    <Space size="middle">
                        <Button 
                            type="link" 
                            size="small" 
                            icon={<EditOutlined />} 
                            onClick={() => this.handleEdit(record)}
                        >
                            编辑
                        </Button>
                        <Button 
                            type="link" 
                            size="small" 
                            danger 
                            icon={<DeleteOutlined />} 
                            onClick={() => this.handleDelete(record)}
                        >
                            删除
                        </Button>
                        <Switch
                            checked={record.status === '1'}
                            onChange={() => this.handleStatusChange(record)}
                        />
                    </Space>
                )
            }
        ];

        return (
            <div className={style.contentWrapper}>
                <Card className={style.contentCard}>
                    <div className={styles.header}>
                        <div className={styles.headerLeft}>
                            <h2 className={styles.title}>组织管理</h2>
                        </div>
                        <div className={styles.headerRight}>
                            <Button 
                                type="primary" 
                                icon={<PlusOutlined />} 
                                onClick={this.handleAdd}
                            >
                                新增组织
                            </Button>
                            <Button 
                                danger 
                                style={{ marginLeft: 8 }} 
                                onClick={this.handleBatchDelete}
                                disabled={selectedRows.length === 0}
                            >
                                批量删除
                            </Button>
                        </div>
                    </div>
                    
                    <Divider />
                    
                    <StandardTable
                        rowKey="id"
                        selectedRows={selectedRows}
                        data={{
                            list: tableData,
                            pagination
                        }}
                        columns={columns}
                        onSelectRow={this.handleSelectChange}
                        onChange={this.handleTableChange}
                    />
                </Card>
            </div>
        );
    }
}