import React, { useState, useEffect } from 'react';
import { Form, Input, Button, Icon, Select, Tabs, Radio, DatePicker, message, Upload } from 'antd';
import { FormattedMessage } from 'umi-plugin-react/locale';
import styles from '../style/GoodsList.less';
import moment from 'moment';
import router from 'umi/router';
import BuildSelect from '../../components/SelectButton/BuildSelect'
import XForm, { FieldTypes } from '../../components/XForm';
import Wangedit from '../../components/Wangedit/Wangedit'
import request from '../../utils/request'
import PosterBottom from '../../assets/bottom.png'
import yinhao from '../../assets/yinhao.png'
import ImageUploader from '../../components/XForm/ImageUpload';
import logo from '../../assets/logo.png';
import touxiang from '../../assets/touxiang.jpg';
import poster1 from '../../assets/poster1.png';
import poster2 from '../../assets/poster2.png';

const { MonthPicker, RangePicker, WeekPicker } = DatePicker;
const { TextArea } = Input;

/**
 *
 *
 * @param {*} props
 * @returns
 */
const Edit = (props) => {
  const [tab, changeTab] = useState('basic')
  const dynamicId = props.location.query.dynamicId
  const [dynamicData, setDynamicData] = useState({})
  if (dynamicId) {
    useEffect(() => {
      getDynamicData(dynamicId);
    }, [])

    // 查询列表
    const getDynamicData = (dynamicId) => {
      request({
        url: '/api/admin/buildingDynamic/Details',
        method: 'GET',
        params: { dynamicId },
      }).then((data) => {
        console.log(data)
        setDynamicData(data)
      })
    }
  }

  const cancelPage = () => {
    router.push({
      pathname: '/activity/ActivityList',
    });
  }

  const Basic = (props) => {
    const fields = [
      {
        label: '所属项目',
        name: 'buildingId',
        render: <BuildSelect />,
        value: dynamicData.buildingId,
        rules: [
          { required: true, message: '请选择所属项目' },
        ]
      },
      {
        label: '主图',
        name: 'imgUrl',
        type: FieldTypes.ImageUploader,
        value: dynamicData.imgUrl,
      },
      {
        label: '活动标题',
        name: 'title',
        type: FieldTypes.Text,
        value: dynamicData.title,
        rules: [
          { required: true, message: '请输入活动标题' },
        ]
      },
      {
        label: '活动时间',
        name: 'activityTime',
        type: FieldTypes.RangePicker,
        value: dynamicData.startDate != null ? [moment(dynamicData.startDate, 'YYYY-MM-DD'), moment(dynamicData.endDate, 'YYYY-MM-DD')] : null,
        rules: [
          { required: true, message: '请选择活动时间' },
        ]
      },
      {
        label: '活动地点',
        name: 'address',
        type: FieldTypes.Text,
        value: dynamicData.address,
        rules: [
          { required: true, message: '请输入活动地点' },
        ]
      },
      {
        label: '活动人数',
        name: 'enlistNum',
        type: FieldTypes.Text,
        value: dynamicData.enlistNum,
        rules: [
          { required: true, message: '请输入活动人数' },
        ]
      },
      {
        label: '活动详情',
        name: 'desc',
        render: <Wangedit />,
        value: dynamicData.desc,
      },
      {
        label: '是否需要报名',
        name: 'isEnlist',
        render: <Radio.Group name="radiogroup">
          <Radio value={1}>是</Radio>
          <Radio value={0}>否</Radio>
        </Radio.Group>,
        value: dynamicData.isEnlist != null ? dynamicData.isEnlist - 0 : 1,
      },
      {
        label: '报名时间',
        name: 'signupTime',
        type: FieldTypes.RangePicker,
        value: dynamicData.enlistStart != null ? [moment(dynamicData.enlistStart, 'YYYY-MM-DD'), moment(dynamicData.enlistEnd, 'YYYY-MM-DD')] : null,
        rules: [
          { required: true, message: '请选择报名时间' },
        ]
      },
    ]

    const handleSubmit = val => {
      let { activityTime, signupTime, ...submitValue } = val
      const [startDate, endDate] = activityTime
      submitValue.startDate = moment(startDate).format('YYYY-MM-DD');
      submitValue.endDate = moment(endDate).format('YYYY-MM-DD');
      const [enlistStart, enlistEnd] = signupTime
      submitValue.enlistStart = moment(enlistStart).format('YYYY-MM-DD');
      submitValue.enlistEnd = moment(enlistEnd).format('YYYY-MM-DD');
      console.log('submit data --->', submitValue)
      if (dynamicId) {
        submitValue.dynamicId = dynamicId
        request({
          url: '/api/admin/buildingDynamic/update',
          method: 'PUT',
          data: submitValue,
        }).then((data) => {
          message.info("保存成功")
          cancelPage()
        }).catch((err) => {
          message.info(err.msg || err.message)
        })
      } else {
        request({
          url: '/api/admin/buildingDynamic/add',
          method: 'POST',
          data: submitValue,
        }).then((data) => {
          message.info("保存成功")
          cancelPage()
        }).catch((err) => {
          message.info(err.msg || err.message)
        })
      }
    }

    return <XForm onSubmit={handleSubmit} onCancel={cancelPage} fields={fields}></XForm>
  }

  const Poster = (props) => {
    const [inputValue, changeInput] = useState('')
    const [textAreaValue, changeTextArea] = useState('')
    const [imgValue, changeImg] = useState('')
    const [posterId, setPosterId] = useState('')

    if(dynamicId){
      useEffect(() => {
        request({
          url: '/api/admin/poster',
          method: 'GET',
          params: {targetId: dynamicId,targetType: 'activity'},
        }).then((data) => {
          console.log(data,"2222")
          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(dynamicId){
        if(posterId){
          request({
            url: '/api/admin/poster/'+posterId,
            method: 'PUT',
            data: {targetId: dynamicId,targetType: 'activity',posterImg: imgValue,posterTitle: inputValue,posterDescription: textAreaValue},
          }).then((data) => {
            message.info("保存成功")
          }).catch((err) => {
            message.info(err.msg || err.message)
          })
         }else{
          request({
            url: '/api/admin/poster',
            method: 'POST',
            data: {targetId: dynamicId,targetType: 'activity',posterImg: imgValue,posterTitle: inputValue,posterDescription: textAreaValue},
          }).then((data) => {
            setPosterId(data.posterId)
            message.info("保存成功")
          }).catch((err) => {
            message.info(err.msg || err.message)
          })
         }
       }else{
        message.warn("请先保存基本信息数据")
       }
    }

    return <div>
      <div style={{ display: 'flex' }}>
        <div style={{ width: '420px', height: '900px', display: 'inline-block', marginTop: '30px' }}>
          <div style={{ width: '375px', height: '785px', backgroundColor: '#fff', boxShadow: '0px 0px 16px 6px rgba(0,0,0,0.15)', position: 'relative', margin: '0 auto' }}>
            <img style={{ width: '100%', height: '300px' }} src={imgValue ? imgValue : poster1} alt="" />
            <div style={{ display: 'flex', alignItems: 'center', marginTop: '-24px' }}>
              <img style={{ width: '70px', height: '70px', border: '4px solid #fff', borderRadius: '35px', marginLeft: '16px' }} src={touxiang} alt="" />
              <span style={{ color: '#222', fontWeight: '600', margin: '24px 10px 0 14px', fontSize: '17px' }}>喵喵</span>
              <span style={{ color: '#999', marginTop: '25px', fontSize: '17px' }}>邀您阅读</span>
              <span style={{ color: '#999', margin: '25px 0 0 60px', fontSize: '17px' }}>2019.09.21</span>
            </div>
            <p style={{
              margin: '10px 20px', fontSize: '20px', color: '#222', fontWeight: '600',
              display: '-webkit-box', lineClamp: '3', height: '60px',
              WebkitLineClamp: '2',
              WebkitBoxOrient: 'vertical',
              overflow: 'hidden',
              textOverflow: 'ellipsis'
            }}>{inputValue ? inputValue : '海报标题'}</p>

            <img src={yinhao} style={{ width: '30px', marginLeft: '10px' }} alt="" />
            <p style={{
              margin: '16px 20px 28px 20px', fontSize: '17px', color: '#999',
              display: '-webkit-box', lineClamp: '3', height: '76px',
              WebkitLineClamp: '3',
              WebkitBoxOrient: 'vertical',
              overflow: 'hidden',
              textOverflow: 'ellipsis'
            }}>{textAreaValue ? textAreaValue : '海报描述'}</p>
            <img src={PosterBottom} style={{ width: '100%' }} alt="" />
          </div>
          <p style={{ textAlign: 'center', fontSize: '19px', color: '#666', marginTop: '30px' }}>海报模板</p>
        </div>

        <div >
          <div style={{ display: 'flex', width: '100%', margin: '60px 0' }}>
            <p style={{ minWidth: '200px', color: '#222', textAlign: 'right', margin: '0 30px 0 0' }}>海报图片</p>
            <ImageUploader value={imgValue} onChange={e => changeImg(e)} />
          </div>
          <div style={{ display: 'flex', alignItems: 'center', width: '100%', marginBottom: '60px' }}>
            <p style={{ minWidth: '200px', color: '#222', textAlign: 'right', margin: '0 30px 0 0' }}>海报标题</p>
            <Input style={{ width: '20vw' }} value={inputValue} placeholder="请输入海报标题" onChange={e => changeInput(e.target.value)} />
          </div>
          <div style={{ display: 'flex', margin: '10px 0 40px 0', width: '100%' }}>
            <p style={{ minWidth: '200px', color: '#222', textAlign: 'right', margin: '0 30px 0 0' }}>海报描述</p>
            <TextArea rows={5} value={textAreaValue} onChange={e => changeTextArea(e.target.value)} />
          </div>

        </div>
      </div>
      <Button type="primary" onClick={submitPoster} style={{ margin: '40px 40px 40px 30vw' }}> 确定</Button>
      <Button onClick={() => router.go(-1)}>取消</Button>
    </div>

  }



  const Share = (props) => {
    const [inputValue, changeInput] = useState('')
    const [imgValue, changeImg] = useState('')
    const [shareContentId, setShareContentId] = useState('')
    
    if(dynamicId){
      useEffect(() => {
        request({
          url: '/api/admin/shareContent',
          method: 'GET',
          params: {targetId: dynamicId,targetType: 'activity'},
        }).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(dynamicId){
        if(shareContentId){
          request({
            url: '/api/admin/shareContent/'+shareContentId,
            method: 'PUT',
            data: {targetId: dynamicId,shareContentType: 'activity',shareContentImg: imgValue,shareContentTitle: inputValue},
          }).then((data) => {
            message.info("保存成功")
          }).catch((err) => {
            message.info(err.msg || err.message)
          })
         }else{
          request({
            url: '/api/admin/shareContent',
            method: 'POST',
            data: {targetId: dynamicId,shareContentType: 'activity',shareContentImg: imgValue,shareContentTitle: inputValue},
          }).then((data) => {
            setShareContentId(data.shareContentId)
            message.info("保存成功")
          }).catch((err) => {
            message.info(err.msg || err.message)
          })
         }
       }else{
        message.warn("请先保存基本信息数据")
       }
    }

    return <div style={{ padding: '20px' }}>
      <div style={{ display: 'flex', margin: '10px 0 40px 0', width: '100%' }}>
        <p style={{ minWidth: '200px', color: '#222', textAlign: 'right', margin: '0 30px 0 0' }}>分享模板</p>
        <div>
          <p style={{ display: 'flex', alignItems: 'center', fontSize: '14px', color: '#999', margin: '0', lineHeight: '0' }}><img src={logo} style={{ width: '22px', marginRight: '10px' }} />知与行互动</p>
          <p style={{ fontSize: '16px', color: '#222', fontWeight: '600', margin: '0' }}>{inputValue ? inputValue : '置业V客厅 精准获客平台'}</p>
          <img style={{ width: '200px', height: '140px' }} src={imgValue ? imgValue : poster2} alt="" />
        </div>
      </div>
      <div style={{ display: 'flex', alignItems: 'center', width: '100%' }}>
        <p style={{ minWidth: '200px', color: '#222', textAlign: 'right', margin: '0 30px 0 0' }}>海报标题</p>
        <Input placeholder="请输入海报标题" value={inputValue} onChange={e => changeInput(e.target.value)} />
      </div>
      <div style={{ display: 'flex', width: '100%', marginTop: '40px' }}>
        <p style={{ minWidth: '200px', color: '#222', textAlign: 'right', margin: '0 30px 0 0' }}>分享图片</p>
        <ImageUploader value={imgValue} onChange={e => changeImg(e)} />
      </div>
      <Button type="primary" htmlType="submit" onClick={submitShare} style={{ margin: '40px 40px 40px 220px' }}> 确定</Button>
      <Button onClick={() => router.go(-1)}>取消</Button>
    </div>
  }

  return (
    <div>
      <div>
        <Radio.Group value={tab} buttonStyle="solid" onChange={e => changeTab(e.target.value)}>
          <Radio.Button value="basic">基本信息</Radio.Button>
          <Radio.Button value="poster">海报图片</Radio.Button>
          <Radio.Button value="share">分享设置</Radio.Button>
        </Radio.Group>
      </div>
      <div>
        {tab === 'basic' && <Basic />}
        {tab === 'poster' && <Poster />}
        {tab === 'share' && <Share />}
      </div>
    </div>
  );
}



export default Edit
