import React, { useState, useEffect } from 'react';
import { Input, Menu, Dropdown, Button, Icon, message, Table, Divider, Tag, Select, Form, Alert } from 'antd';
import { FormattedMessage } from 'umi-plugin-react/locale';
import channels from '../channel/channelList.less';
import BuildSelect from '../../components/SelectButton/BuildSelect'
import XForm, { FieldTypes } from '../../components/XForm';
import Wangedit from '../../components/Wangedit/Wangedit'
import router from 'umi/router';
import apis from '../../services/apis';
import request from '../../utils/request'

const { TextArea } = Input;
const { Option } = Select;

const header = props => {
  const goodsId = props.location.query.goodsId
  const [ goodsData, setGoodsData ] = useState({})
  if(goodsId){
    useEffect(() => {
      getGoodsData(goodsId);
    },[])

  // 查询列表
  const getGoodsData = (goodsId) => {
    request({ ...apis.integralMall.taGoods, urlData: { id: goodsId },}).then((data) => {
        console.log(data)
        setGoodsData(data)
    })
  }
  }

  const fields = [
    {
      label: '所属项目',
      name: 'buildingId',
      render: <BuildSelect />,
      value: goodsData.buildingId,
      rules: [
        {required: true, message: '请选择所属项目'},
      ]
    },
    {
      label: '商品封面图',
      name: 'imgUrl',
      type: FieldTypes.ImageUploader,
      value: goodsData.imgUrl,
      help: '建议图片尺寸：320*320px，比例1:1,格式：jpg，用于商品封面图',
      rules: [
        {required: true, message: '请选择商品封面图'},
      ]
    },
    {
      label: '商品主图',
      name: 'detailImgUrl',
      type: FieldTypes.ImageUploader,
      value: goodsData.detailImgUrl,
      help: '建议图片尺寸：750*750px，比例1:1,格式：jpg，用于商品主图',
      rules: [
        {required: true, message: '请选择商品主图'},
      ]
    },
    {
      label: '商品名称',
      name: 'goodsName',
      type: FieldTypes.Text,
      value: goodsData.goodsName,
      rules: [
        {required: true, message: '请输入商品名称'},
      ]
    },
    {
      label: '所需积分',
      name: 'pointPrice',
      type: FieldTypes.Text,
      value: goodsData.pointPrice,
      rules: [
        {required: true, message: '请输入所需积分'},
      ]
    },
    {
      label: '商品数量',
      name: 'totalNum',
      type: FieldTypes.Text,
      value: goodsData.totalNum,
      rules: [
        {required: true, message: '请输入商品数量'},
      ]
    },
    {
      label: '剩余数量',
      name: 'inventory',
      type: FieldTypes.Text,
      value: goodsData.inventory,
      rules: [
        {required: true, message: '请输入剩余数量'},
      ]
    },
    {
      label: '商品详情',
      name: 'goodsDescription',
      render: <Wangedit />,
      value: goodsData.goodsDescription,
    },
    {
      label: '状态',
      name: 'status',
      type: FieldTypes.Select,
      dict: [{label:"已上架",value:1},{label:"已下架",value:0}],
      value: goodsData.status != null ? goodsData.status : 1,
    },
    {
      label: '领取地址',
      name: 'address',
      type: FieldTypes.Text,
      value: goodsData.address,
      rules: [
        {required: true, message: '请输入领取地址'},
      ]
    },
  ]

   
  const handleSubmit = (values) => {
    if(goodsId){
      values.goodsId = goodsId
      request({ ...apis.integralMall.updateTaGoods, data: values,}).then((data) => {
        cancelPage()
      }).catch((err) => {
        message.info(err.msg || err.message)
      })
      }else{
      request({ ...apis.integralMall.addTaGoods, data: values,}).then((data) => {
        cancelPage()
      }).catch((err) => {
        message.info(err.msg || err.message)
      })
      }
  }

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

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

const WrappedNormalLoginForm = Form.create({ name: 'header' })(header);
export default WrappedNormalLoginForm
