import { useAjax } from "@/Hook/useAjax"
import { syncMediaImageApi } from "@/services/adqV3/global"
import { DatePicker, Form, message, Modal, Radio } from "antd"
import { RangePickerProps } from "antd/lib/date-picker"
import React, { useState } from "react"
import moment from "moment"
import '../../tencentAdPutIn/index.less'

/**
 * 同步素材
 * @returns 
 */
const SyncCloudSc: React.FC<{ onChange?: () => void, accountId: number }> = ({ onChange, accountId }) => {

    /********************************************/
    const [form] = Form.useForm()

    const syncMediaImage = useAjax((params) => syncMediaImageApi(params))
    const [visible, setVisible] = useState<boolean>(false)
    /********************************************/

    const handleOk = () => {
        form.validateFields().then(valid => {
            console.log(valid)
            const { createTime, ...params } = valid
            syncMediaImage.run({ ...params, accountIds: [accountId], createTimeMin: moment(createTime[0]).format('YYYY-MM-DD') + ' 00:00:00', createTimeMax: moment(createTime[1]).format('YYYY-MM-DD') + ' 23:59:59' }).then(res => {
                if (res) {
                    message.success('同步成功')
                    onChange?.()
                    setVisible(false)
                }
            })
        })
    }

    const disabledDate: RangePickerProps['disabledDate'] = current => {
        // 禁止选择当前日期之前的日期  
        return current && current > moment().endOf('day');
    };

    return <>
        <a onClick={() => setVisible(true)}>同步素材</a>
        {visible && <Modal
            title={<strong>同步素材</strong>}
            open={visible}
            onCancel={() => {
                setVisible(false)
            }}
            onOk={handleOk}
            className="modalResetCss"
            confirmLoading={syncMediaImage.loading}
            okText={'同步'}
        >
            <Form
                name="basicSyncCloudSc"
                form={form}
                layout='vertical'
                autoComplete="off"
                initialValues={{ type: 'image', createTime: [moment(), moment()] }}
            >
                <Form.Item label={<strong>素材类型</strong>} name="type" rules={[{ required: true, message: '请选择素材类型!' }]}>
                    <Radio.Group buttonStyle="solid">
                        <Radio.Button value="image">图片</Radio.Button>
                        <Radio.Button value="video">视频</Radio.Button>
                    </Radio.Group>
                </Form.Item>
                <Form.Item
                    label={<strong>创建时间</strong>}
                    name="createTime"
                    rules={[
                        { required: true, message: '请选择创建时间' },
                        () => ({
                            validator(_, value) {
                                if (value) {
                                    const [startDate, endDate] = value;
                                    // 检查选择的日期范围是否超过30天  
                                    if (endDate.diff(startDate, 'days') >= 30) {
                                        return Promise.reject(new Error('选择的日期范围不能超过30天！'));
                                    }
                                }
                                return Promise.resolve();
                            },
                        })
                    ]}
                >
                    <DatePicker.RangePicker disabledDate={disabledDate} />
                </Form.Item>
            </Form>
        </Modal>}
    </>
}

export default React.memo(SyncCloudSc)