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 ImageUploader from '@/components/XForm/ImageUpload';
import Share from '@/components/Share';
import logo from '../../../../assets/logo.svg';
import poster2 from '../../../../assets/poster2.png';

export default props => {
  const { newsId } = props;
  const [inputValue, changeInput] = useState('');
  const [imgValue, changeImg] = useState('');
  const [shareContentId, setShareContentId] = useState('');

  useEffect(() => {
    if (newsId) {
      request({ ...apis.activity.shareContent, params: { targetId: newsId, targetType: 'news' } })
        .then(data => {
          console.log(data, '2222');
          if (data.length > 0) {
            setShareContentId(data[0].shareContentId);
            changeImg(data[0].shareContentImg);
            changeInput(data[0].shareContentTitle);
          }
        })
        .catch(err => {
          message.info(err.msg || err.message);
        });
    }
  }, []);

  const submitShare = () => {
    if (newsId) {
      if (shareContentId) {
        request({
          ...apis.activity.updateShareContent,
          urlData: { id: shareContentId },
          data: {
            targetId: newsId,
            shareContentType: 'news',
            shareContentImg: imgValue,
            shareContentTitle: inputValue,
          },
        })
          .then(data => {
            message.info('保存成功');
          })
          .catch(err => {
            message.info(err.msg || err.message);
          });
      } else {
        request({
          ...apis.activity.addShareContent,
          data: {
            targetId: newsId,
            shareContentType: 'news',
            shareContentImg: imgValue,
            shareContentTitle: inputValue,
          },
        })
          .then(data => {
            setShareContentId(data.shareContentId);
            message.info('保存成功');
          })
          .catch(err => {
            message.info(err.msg || err.message);
          });
      }
    } else {
      message.warn('请先保存基本信息数据');
    }
  };

  const cancelPage = () => {
    router.go(-1);
  };
  const target = { id: '', type: 'news' };
  return (
    <div style={{ padding: '20px' }}>
      <Share target={target} {...props} />
    </div>
  );
};
