import React, { useState, useEffect } from 'react';
import { Form, Input, Button, Icon, Select, Tabs, Radio, DatePicker,message } 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'

const { MonthPicker, RangePicker, WeekPicker } = DatePicker;
/**
 *
 *
 * @param {*} props
 * @returns
 */
 const Edit = (props) => {
  const [ tab, changeTab ] = useState('basic')
  const contentId = props.location.query.contentId
  const [ data, setData ] = useState({})
  if(contentId){
    useEffect(() => {
      getDetail(contentId);
    },[])

  // 查询列表
  const getDetail = (contentId) => {
    request({
        url: '/api/admin/extendContent/' + contentId,
        method: 'GET',
    }).then((data) => {
        console.log(data)
        setData(data)
    })
  }
  }

  const cancelPage = () =>{
    router.push({
      pathname: '/carouselFigure/carouselFigureList',
    });
  }
 
    const fields = [
      {
        label: '所属项目',
        name: 'buildingId',
        render: <BuildSelect />,
        value: data.buildingId,
      },
      {
        label: '商品图片',
        name: 'image',
        type: FieldTypes.ImageUploader,
        value: data.image,
      },
      {
        label: '标题',
        name: 'title',
        type: FieldTypes.Text,
        value: data.title,
      },
      {
        label: '发布位置',
        name: 'showPosition',
        type: FieldTypes.Select,
        dict: [{
          label: '首页',
          value: 'index'
        },
        {
          label: '商城',
          value: 'mall'
        }],
        value: data.showPosition,
      },
      {
        label: '类型',
        name: 'contentType',
        type: FieldTypes.Select,
        dict: [{
          label: '活动',
          value: 'activity'
        },
        {
          label: '项目',
          value: 'project'
        },
        {
          label: '资讯',
          value: 'news'
        },
        {
          label: '其他',
          value: 'other'
        }],
        value: data.contentType,
      },
      {
        label: '发布内容',
        name: 'content',
        render: <Wangedit />,
        value: data.content,
      },
      {
        label: '状态',
        name: 'status',
        type: FieldTypes.Select,
        dict: [{
          label: "启用",
          value: 1
        },
        {
          label: "停用",
          value: 0
        },],
        value: data.status != null ? data.status : 1,
      },
    ]
  
    const handleSubmit = val => { 
      val.showType = 'banner'
      if(contentId){
        request({
          url: '/api/admin/extendContent/'+contentId,
          method: 'PUT',
          data: val,
        }).then((data) => {
          cancelPage()
        }).catch((err) => {
          message.info(err.msg || err.message)
        })
      }else{
        request({
          url: '/api/admin/extendContent',
          method: 'POST',
          data: val,
        }).then((data) => {
          cancelPage()
        }).catch((err) => {
          message.info(err.msg || err.message)
        })
      }
    }

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



export default Edit
