
import { useRef, useState, useEffect } from 'react';
import { connect } from '@umijs/max';
import type { ActionType, ProFormInstance } from '@ant-design/pro-components';
import {
    ProFormText,
    ProFormSegmented,
    ProFormRadio,
    ProForm
} from '@ant-design/pro-components';
import { ConfigProvider, Flex, Space, Modal, message, Image, Row, Col, Switch } from 'antd';
import { addSection, updateSection, listSection, GetSectionDetail } from '@/apis/api';
import { Imageprefix } from '@/constants';

import UploadModel from '@/components/Common/upload';

const selectfieldNames = { label: 'name', value: 'id' };


const PagesMainInformationSectionCreate: React.FC = ({ dispatch, dictModel, openModel, id }: any) => {
    const formRef = useRef<ProFormInstance>();
    const [openPreview, setOpenPreview] = useState(false);
    const [detail, setDetail] = useState<InformationType.section | null>(null)

    useEffect(() => {
        setOpenPreview(openModel.openModal)
    }, [openModel.openModal])

    const [uploadedFilephotoName, setUploadedFilephotoName] = useState<string>('');
    const handleFileUploadedphoto = (filename: string) => {
        setUploadedFilephotoName(filename);
    };

    return (
        <>
            <ConfigProvider
                theme={{
                    token: {
                        colorPrimary: '#4FBE70',
                    }, components: {
                        Segmented: {
                            itemSelectedBg: '#19be6e',
                            itemSelectedColor: '#ffffff'
                        },
                    },
                }}
            >
                <Modal
                    open={openPreview}
                    title={id ? '编辑栏目/频道' : '添加栏目/频道'}
                    centered
                    maskClosable={false}
                    footer={null}
                    destroyOnClose
                    width={1000}
                    onCancel={() => {
                        dispatch({ type: 'openModel/getOpenModal', payload: false })
                    }}
                >
                    <ProForm<InformationType.section>
                        formRef={formRef}
                        request={async () => {
                            if (id) {
                                if (id.nextlevel) {
                                    setDetail(null)
                                    setUploadedFilephotoName('')
                                    return {
                                        name: '',
                                        code: '',
                                        cover_img: '',
                                        parent_id: id.nextlevel,
                                        description: '',
                                        display: 1,
                                        park: 2
                                    };
                                } else {
                                    let res = await GetSectionDetail({ id: id.id })
                                    setUploadedFilephotoName(res.data.cover_img)
                                    setDetail(res.data)
                                    return res.data;
                                }
                            } else {
                                setDetail(null)
                                setUploadedFilephotoName('')
                                return {
                                    name: '',
                                    code: '',
                                    cover_img: '',
                                    parent_id: -1,
                                    description: '',
                                    display: 1,
                                    park: 2
                                }
                            }
                        }}
                        submitter={{
                            searchConfig: {
                                resetText: '取消',
                                submitText: '保存',
                            },
                            render: (_, dom) => { return <Flex justify='flex-end'> <Space>{dom}</Space></Flex> },
                            onReset: () => {
                                setDetail(null)
                                setOpenPreview(false)
                            }
                        }}
                        onFinish={async (values) => {
                            if (id.nextlevel) {
                                values.parent_id = id.nextlevel;
                            } else if (detail && detail.id) {
                                values.parent_id = detail.parent_id
                            } else {
                                values.parent_id = -1
                            }
                            values.cover_img = uploadedFilephotoName ? uploadedFilephotoName : '';
                            console.log(values)
                            if (detail) {
                                values.id = id.id;
                                let res = await updateSection(values)
                                message.success('提交成功')
                                dispatch({ type: 'openModel/getOpenModal', payload: false })
                            } else {
                                let res = await addSection(values)
                                message.success('提交成功')
                                dispatch({ type: 'openModel/getOpenModal', payload: false })
                            }
                        }}
                    >
                        <Row gutter={[16, 16]}>
                            <Col span={24}>
                                <UploadModel multiple={false} form_name="cover_img" image_length={1} uploadTxt="上传封面图" image_type={3} imageUrl={uploadedFilephotoName} onUploadComplete={handleFileUploadedphoto}></UploadModel>
                            </Col>
                            <Col span={8}>
                                <ProFormText
                                    label="栏目名称"
                                    name="name"
                                    placeholder="请输入栏目名称"
                                    rules={[{ required: true, message: '请输入栏目名称' }]}
                                />
                            </Col>
                            <Col span={16}>
                                <ProFormText
                                    label="栏目编码，用英文字母表示，不可重复"
                                    name="code"
                                    placeholder="栏目编码，用英文字母表示，不可重复"
                                    rules={[{ required: true, message: '请输入栏目编码，用英文字母表示，不可重' }]}
                                />
                            </Col>
                            <Col span={8}>
                                <ProFormText
                                    name="description"
                                    label="描述"
                                    placeholder="请输入描述"
                                    rules={[{ required: true, message: '请输入描述' }
                                    ]}
                                />
                            </Col>
                            <Col span={8}>
                                <ProFormRadio.Group
                                    name="display"
                                    label="是否显示"
                                    request={async () => [
                                        { label: '是', value: 1 },
                                        { label: '否', value: 2 }
                                    ]}
                                />
                            </Col>
                            <Col span={8}>
                                <ProFormRadio.Group
                                    name="park"
                                    label="是否是科创园栏目"
                                    request={async () => [
                                        { label: '是', value: 1 },
                                        { label: '否', value: 2 }
                                    ]}
                                />
                            </Col>
                        </Row>



                    </ProForm>
                </Modal>
            </ConfigProvider>
        </>
    );
};



export default connect(({ dictModel, openModel }: any) => ({
    dictModel, openModel
}))(PagesMainInformationSectionCreate);