import React, { useState, useEffect, useContext, useRef, memo } from 'react';
import { Progress, Input, Tooltip, Form } from 'antd'
import { getUrl2, isDev, CBreadcrumb, ActionBtn, ThemeContext, ModalWrapper } from 'educoder'
import axios from 'axios'

function EditVideoModal (props) {
    const modalEl = useRef(null);
    const theme = useContext(ThemeContext);
    const { history, videoId, cover_url, title, created_at, isReview, onEditVideo, visible, setVisible,
            form, editSuccess } = props;
    const getFieldDecorator = form.getFieldDecorator
    const username = props.match.params.username
    const _title = form.getFieldsValue().title;

    function toList() {
        history.push(`/users/${username}/videoes`)
    }
    function toUpload() {
        history.push(`/users/${username}/videoes/upload`)
    }
    function onOk() {
        form.validateFieldsAndScroll((err, values) => {
            
            if (!err) {
                const url = `/users/${username}/videos/${videoId}.json`
                axios.put(url, {
                    title: _title
                }).then((response) => {
                    if (response.data) {
                        onCancel()
                        editSuccess()
                    }
                }).catch((e) => {

                })
            } else {
                // $("html").animate({ scrollTop: $('html').scrollTop() - 100 })
            }
        })

        // setVisible(false)

    }
    function onCancel() {
        setVisible(false)
    }
    useEffect(() => {
        modalEl.current.setVisible(visible)
    }, [visible])
    useEffect(() => {
        visible && form.setFieldsValue({
            title,
        })
    }, [visible])
    return (
        <ModalWrapper 
            ref={modalEl}
            width="600px"
            title={`视频编辑`}
            { ...props }
            onOk={onOk}
            onCancel={onCancel}
            className="editVideoModal"
        >
            <Form.Item
                label="视频标题"
                className="title formItemInline"
            >
                
                {getFieldDecorator('title', {
                    rules: [{
                        required: true, message: '请输入标题',
                    }, {
                        max: 30, message: '最大限制为30个字符',
                    }],
                })(
                    <Input placeholder="" className="titleInput" maxLength="30"
                            addonAfter={String(_title ? _title.length : 0)} />
                )}
            </Form.Item>
        </ModalWrapper>
    )
}
const WrappedEditVideoModal = Form.create({ name: 'editVideoModal' })(EditVideoModal);
export default WrappedEditVideoModal