import { getAdAccountAllOfMember } from "@/API/global";
import { useAjax } from "@/Hook/useAjax";
import { updateCallUserAppApi } from "@/pages/weComTask/API/weChatApiLicense";
import { App, Form, Modal, Radio, Select } from "antd";
import React, { useEffect } from "react";

interface EditCallUserProps {
    selectedRows: any[]
    visible?: boolean
    onClose?: () => void
    onChange?: () => void
}


export const EditCallUser: React.FC<EditCallUserProps> = ({ visible, onClose, onChange, selectedRows }) => {

    /**************************************/
    const [form] = Form.useForm();
    const { message } = App.useApp();

    const allOfMember = useAjax(() => getAdAccountAllOfMember())
    const updateCallUserApp = useAjax((params) => updateCallUserAppApi(params))
    /**************************************/

    useEffect(() => {
        allOfMember.run()
    }, [])

    const handleOk = () => {
        form.validateFields().then((values) => {
            if ((values?.userIdList?.length === 0 || values?.userIdList === undefined) && values?.bugStatus === undefined) {
                message.error('请选择通知人员或者购买状态')
                return
            }
            updateCallUserApp.run({
                ...values,
                corpIdList: selectedRows.map(item => item.corpId)
            }).then(res => {
                if (res?.data) {
                    message.success('更新成功')
                    onChange?.()
                }
            })
        })

    }

    return <Modal
        title={<strong>修改应用信息更新通知人员</strong>}
        open={visible}
        onCancel={onClose}
        onOk={handleOk}
        width={600}
        confirmLoading={updateCallUserApp.loading}
    >
        <Form
            form={form}
            labelCol={{ span: 4 }}
            wrapperCol={{ span: 20 }}
            labelAlign="left"
            autoComplete="off"
            colon={false}
        >
            <Form.Item label={<strong>到期通知人员</strong>} name="userIdList">
                <Select
                    mode="multiple"
                    allowClear
                    placeholder="请选择到期通知人员"
                    showSearch
                    options={allOfMember?.data?.data?.map((item: any) => ({ label: item.nickname, value: item.userId }))}
                    filterOption={(input, option) =>
                        ((option?.label ?? '') as string).toLowerCase().includes(input.toLowerCase())
                    }
                />
            </Form.Item>
            <Form.Item label={<strong>购买状态</strong>} name="bugStatus">
                <Select
                    allowClear
                    placeholder="请选择购买状态"
                    showSearch
                    options={[{ label: '已购买', value: 1 }, { label: '未购买', value: 0 }]}
                    filterOption={(input, option) =>
                        ((option?.label ?? '') as string).toLowerCase().includes(input.toLowerCase())
                    }
                />
            </Form.Item>
        </Form>
    </Modal>
}


export default React.memo(EditCallUser)