import { useAjax } from "@/Hook/useAjax"
import { ApiParamsChatListProps, setRemarkApi } from "@/pages/weComTask/API/global"
import { App, Input, Modal } from "antd"
import React, { useEffect, useState } from "react"

interface Props {
    listData: ApiParamsChatListProps,
    editSelectedRow: any[],
    corpId: string,
    visible?: boolean
    onClose?: () => void
    onChange?: () => void
}
/**
 * 设置群备注
 * @returns 
 */
const RemarkSet: React.FC<Props> = ({ visible, onClose, onChange, listData, editSelectedRow, corpId }) => {

    /*******************************/
    const [remark, setRemark] = useState<string>()
    const { message } = App.useApp()

    const setRemarkA = useAjax((params: ApiParamsChatListProps) => setRemarkApi(params))
    /*******************************/

    useEffect(() => {
        if (editSelectedRow.length === 1) {
            setRemark(editSelectedRow[0]?.remark)
        }
    }, [editSelectedRow])

    const handleOk = () => {
        let { remark: a, ...params } = listData
        setRemarkA.run({ ...params, remark, chatIdList: editSelectedRow.map(item => item.chatId), corpId }).then(res => {
            if (res?.data) {
                message.success('设置成功')
                onChange?.()
            }
        })
    }

    return <Modal
        title="设置备注"
        open={visible}
        onCancel={onClose}
        onOk={handleOk}
    >
        <Input placeholder="请输入备注" value={remark} onChange={(e) => setRemark(e.target.value)} />
    </Modal>
}

export default React.memo(RemarkSet)