import { useAjax } from "@/Hook/useAjax"
import { useUpdateEffect } from "ahooks"
import { Button, DatePicker, Space, Table, Tag } from "antd"
import React, { useEffect, useState } from "react"
import { SearchOutlined } from '@ant-design/icons';
import dayjs from "dayjs"
import { groupTaskNotesTableConfig } from "./groupTableConfig"
import { getSendTaskGroupMsgListApi, GetSendTaskGroupMsgListProps } from "@/pages/weComTask/API/businessPlan/create"
import SearchBox from "@/pages/weComTask/components/searchBox";

interface Props {
    corpId: string
    taskId: number
}
/**
 * 群发记录
 * @returns 
 */
const GroupTaskNotes: React.FC<Props> = ({ corpId, taskId }) => {

    /*************************************/
    const [queryForms, setQueryForms] = useState<GetSendTaskGroupMsgListProps>({ pageNum: 1, pageSize: 20, corpId, sendTimeStartTime: dayjs().format('YYYY-MM-DD'), sendTimeEndTime: dayjs().format('YYYY-MM-DD'), taskId })

    const getSendTaskMsgList = useAjax((params) => getSendTaskGroupMsgListApi(params))
    /*************************************/


    useEffect(() => {
        setQueryForms({ ...queryForms, corpId, taskId })
    }, [corpId, taskId])

    useUpdateEffect(() => {
        if (queryForms.corpId && queryForms.taskId) {
            getSendTaskMsgList.run(queryForms)
        }
    }, [queryForms])

    return <Space direction="vertical" style={{ width: '100%' }}>
        <SearchBox
            bodyPadding={1}
            buttons={<Button onClick={() => getSendTaskMsgList.refresh()} loading={getSendTaskMsgList.loading} icon={<SearchOutlined />}>搜索</Button>}
        >
            <>
                <DatePicker placeholder="发送开始时间" value={queryForms?.sendTimeStartTime ? dayjs(queryForms.sendTimeStartTime) : undefined} onChange={(e, date: any) => { setQueryForms({ ...queryForms, sendTimeStartTime: date, pageNum: 1 }) }} />
                <DatePicker placeholder="发送结束时间" value={queryForms?.sendTimeEndTime ? dayjs(queryForms.sendTimeEndTime) : undefined} onChange={(e, date: any) => { setQueryForms({ ...queryForms, sendTimeEndTime: date, pageNum: 1 }) }} />
            </>
        </SearchBox>
        {/* 表 */}
        <Table
            style={{ marginBottom: 1 }}
            dataSource={getSendTaskMsgList?.data?.data?.records}
            loading={getSendTaskMsgList?.loading}
            columns={groupTaskNotesTableConfig(() => getSendTaskMsgList.refresh())}
            scroll={{ x: 400, y: 280 }}
            rowKey={(s) => {
                return s.id
            }}
            size='small'
            rowClassName={(record) => record?.errCode === 1 ? 'errorClassName' : ''}
            pagination={{
                total: getSendTaskMsgList?.data?.data?.total,
                showTotal: (total) => <Tag color="cyan">总共{total}数据</Tag>,
                showSizeChanger: true,
                showLessItems: true,
                defaultCurrent: 1,
                defaultPageSize: 200,//默认初始的每页条数
                current: getSendTaskMsgList?.data?.data?.current || 1,
                pageSize: getSendTaskMsgList?.data?.data?.size || 20,
                onChange: (page, pageSize) => {
                    setQueryForms({ ...queryForms, pageNum: page, pageSize })
                }
            }}
        />
    </Space>
}

export default React.memo(GroupTaskNotes)