import React, { useMemo, useRef, useEffect, useState } from 'react';

import { message, Radio, Input, Button } from 'antd';
import apis from '@/services/apis';
import router from 'umi/router';
import request from '@/utils/request';
import Poster from '@/components/Poster';
import ImageUploader from '@/components/XForm/ImageUpload';
import logo from '../../../../assets/logo.svg';
import yinhao from '../../../../assets/yinhao.png';
import touxiang from '../../../../assets/touxiang.jpg';
import poster1 from '../../../../assets/poster1.png';
import poster2 from '../../../../assets/poster2.png';
import xiaochengxu from '../../../../assets/xiaochengxu.png';

const { TextArea } = Input;
export default props => {
  const target = { id: '', type: 'news' };

  const { newsId } = props;
  const [inputValue, changeInput] = useState('');
  const [textAreaValue, changeTextArea] = useState('');
  const [imgValue, changeImg] = useState('');
  const [posterId, setPosterId] = useState('');

  useEffect(() => {
    if (newsId) {
      request({ ...apis.activity.poster, params: { targetId: newsId, targetType: 'news' } })
        .then(data => {
          if (data.length > 0) {
            setPosterId(data[0].posterId);
            changeImg(data[0].posterImg);
            changeTextArea(data[0].posterDescription);
            changeInput(data[0].posterTitle);
          }
        })
        .catch(err => {
          message.info(err.msg || err.message);
        });
    }
  }, []);

  const submitPoster = () => {
    if (newsId) {
      if (posterId) {
        request({
          ...apis.activity.updatePoster,
          urlData: { id: posterId },
          data: {
            targetId: newsId,
            targetType: 'news',
            posterImg: imgValue,
            posterTitle: inputValue,
            posterDescription: textAreaValue,
          },
        })
          .then(data => {
            message.info('保存成功');
          })
          .catch(err => {
            message.info(err.msg || err.message);
          });
      } else {
        request({
          ...apis.activity.addPoster,
          data: {
            targetId: newsId,
            targetType: 'news',
            posterImg: imgValue,
            posterTitle: inputValue,
            posterDescription: textAreaValue,
          },
        })
          .then(data => {
            setPosterId(data.posterId);
            message.info('保存成功');
          })
          .catch(err => {
            message.info(err.msg || err.message);
          });
      }
    } else {
      message.warn('请先保存基本信息数据');
    }
  };

  const cancelPage = () => {
    router.go(-1);
  };

  return (
    <div>
      <Poster target={target} {...props} />
    </div>
  );
};
