
import React, { useState, useRef, useEffect } from 'react';
import { useIntl, FormattedMessage, useAccess } from '@umijs/max';
import type { FormInstance } from 'antd';
import { Button, message, Modal, Image } from 'antd';
import { ActionType, FooterToolbar, PageContainer, ProColumns, ProTable } from '@ant-design/pro-components';
import { PlusOutlined, DeleteOutlined, ExclamationCircleOutlined, RedoOutlined } from '@ant-design/icons';
// import { getNoticeList, removeNotice, addNotice, updateNotice, exportNotice } from '@/services/system/notice';
import { getBiddingDocumentList, saveBiddingDocument, deleteBiddingDocument, pushBiddingDocumentDetail,pushJoinList,pushJoin,pushBiddingDocumentDetailAll } from '@/services/JoinZZ/biddingDocument';
import BiddingDocumentForm from './edit';
import { getDictValueEnum } from '@/services/system/dict';
import moment from 'moment';
import { KeepAlive } from 'react-activation';
import './index.less'
import DictTag from '../../../components/DictTag';
import { getDataEnumList } from '../../../services/system/Enum';

const stateEnum = [
    { label: '待发布', value: 0 },
    { label: '已发布', value: 1 },
]

const NoticeTableList: React.FC = () => {
    const formTableRef = useRef<FormInstance>();

    const [modalVisible, setModalVisible] = useState<boolean>(false);

    const actionRef = useRef<ActionType>();
    const intl = useIntl();
    const [currentRow, setCurrentRow] = useState<any>();
    const [enumList,setEnumList] = useState()

    
    
  
    const handleRemoveOne = async (selectedRow: API.System.Notice) => {
        const hide = message.loading(intl.formatMessage({ id: 'public.loadingDelete' }));
        if (!selectedRow) return true;
        try {
            const params = [selectedRow.uuid];
            const resp = await deleteBiddingDocument(params.join(','));
            hide();
            if (resp.code === 200) {
                message.success(intl.formatMessage({ id: 'public.deleteSuccess' }));
            } else {
                message.error(resp.msg);
            }
            return true;
        } catch (error) {
            hide();
            message.error(intl.formatMessage({ id: 'public.deleteError' }));
            return false;
        }
    };

    useEffect(() => {
        getEnum()
    }, [])

    const getEnum = ()=>{
        getDataEnumList({ enumUuid: '2024810214616101' }).then((res) => {
            console.log(res.rows)

            const enumOptions = res.rows.map((item: any) => ({
                label: item.dataName,
                value: item.uuid,
            }));
            setEnumList(enumOptions); // 设置 enumList
        })
    }
    const columns: ProColumns<API.System.Notice>[] = [
        {
            title: '序号',
            dataIndex: 'id',
            valueType: 'text',
            hideInSearch: true,
            render: (_, record: any) => {
                // console.log('record---',_,record)
                return record.rowIndex;
            },
        },
        {
            title: '标题',
            dataIndex: 'title',
            valueType: 'text',
        },
        {
            title: '展示日期',
            dataIndex: 'date',
            valueType: 'text',
            hideInSearch: true,
        },
        {
            title: '浏览次数',
            dataIndex: 'vievNum',
            valueType: 'text',
            hideInSearch: true,
        },
        {
            title: '状态',
            dataIndex: 'pushStatus',
            valueType: 'radio',
            request: async () => stateEnum,
            render: (_, record) => {
                return (<DictTag enums={stateEnum} value={record.pushStatus} />);
            },
            hideInSearch: true,
        },
        {
            title: '操作',
            dataIndex: 'option',
            width: '120px',
            valueType: 'option',
            render: (_, record: any) => [
                <Button
                    type="link"
                    size="small"
                    key="edit"

                    onClick={() => {
                        setModalVisible(true);
                        setCurrentRow(record);
                    }}
                >
                    编辑
                </Button>,
        
                <Button
                    type="link"
                    size="small"
                    key="publish"
                    disabled={record.pushStatus == 0 ? false : true}
                    onClick={() => {
                        let params = {
                            uuid: record.uuid,
                        }
                        pushBiddingDocumentDetail(params).then((res: any) => {
                            if (res.code == 200) {
                                if (actionRef.current) {
                                    actionRef.current.reload();
                                }
                                message.success(res.msg)
                            } else {
                                message.error(res.msg)
                            }
                        })
                    }}
                >
                    {record.pushStatus == 0 ? '发布' : '已发布'}
                </Button>,
                <Button
                    type="link"
                    size="small"
                    danger
                    key="batchRemove"

                    onClick={async () => {
                        Modal.confirm({
                            title: intl.formatMessage({ id: 'public.delete' }),
                            content: intl.formatMessage({ id: 'public.confirmdeletemsg' }),
                            okText: intl.formatMessage({ id: 'public.confirm' }),
                            cancelText: intl.formatMessage({ id: 'public.cancel' }),
                            onOk: async () => {
                                const success = await handleRemoveOne(record);
                                if (success) {
                                    if (actionRef.current) {
                                        actionRef.current.reload();
                                    }
                                }
                            },
                        });
                    }}
                >
                    <FormattedMessage id="public.delete" defaultMessage="删除" />
                </Button>,
            ],
        },
    ];
    /**
     * 一键发布
     */
    const publishing = async () => {
        try {
            const resp = await pushJoinList();
            if (resp.code === 200) {
                message.success('发布成功');
                if(actionRef){
                    actionRef.current.reload();
                }
            } else {
                message.error(resp.msg);
            }
        } catch (error) { }
    }
    return (
        <KeepAlive name={'招标文件'} path="/JoinZZ/biddingDocument">
            <PageContainer>
                <div style={{ width: '100%', float: 'right' }}>
                    <ProTable<API.System.Notice>
                        actionRef={actionRef}
                        formRef={formTableRef}
                        rowKey="uuid"
                        key="newsList"
                        search={{
                            labelWidth: 120,
                        }}
                        toolBarRender={() => [
                            <Button
                                type="primary"
                                key="add"
                                onClick={async () => {
                                    setCurrentRow(undefined);
                                    setModalVisible(true);
                                }}
                            >
                                <PlusOutlined /> <FormattedMessage id="public.add" defaultMessage="新建" />
                            </Button>,
                            <Button
                            type="primary"
                            key="fabu"
                            onClick={publishing}
                            >
                                一键发布
                            </Button>,
                        ]}
                        request={(params) => {
                            return getBiddingDocumentList({ ...params } as API.System.NoticeListParams).then((res) => {

                                res.rows.forEach((item: any, index: any) => {
                                    item.rowIndex = index + 1
                                })
                                const result = {
                                    data: res.rows,
                                    total: res.total,
                                    success: true,

                                };
                                return result;
                            })
                        }
                        }
                        columns={columns}
                    />
                </div>
                <BiddingDocumentForm
                    onSubmit={async (values) => {
                        let success = false;
                        if (currentRow) {
                            values.uuid = currentRow.uuid;
                        }

                        success = await saveBiddingDocument({ ...values });
                        if (success) {
                            setModalVisible(false);
                            setCurrentRow(undefined);
                            if (actionRef.current) {
                                actionRef.current.reload();
                            }
                        }
                    }}
                    onCancel={() => {
                        setModalVisible(false);
                        setCurrentRow(undefined);
                    }}
                    currentRow={currentRow}
                    open={modalVisible}
                />
            </PageContainer>
        </KeepAlive>

    );
};

export default NoticeTableList;
