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 ruleId = props.location.query.ruleId
  const [ ruleData, setRuleData ] = useState({})
  if(ruleId){
    useEffect(() => {
      getRuleData(ruleId);
    },[])

  // 查询列表
  const getRuleData = (ruleId) => {
    request({ ...apis.integralMall.rulesDetail, urlData: { id: ruleId },}).then((data) => {
        console.log(data)
        setRuleData(data)
    })
  }
  }

  const fields = [
    {
      label: '获取积分',
      name: 'pointsAmount',
      type: FieldTypes.Text,
      value: ruleData.pointsAmount,
    },
    {
      label: '说明',
      name: 'remark',
      type: FieldTypes.Text,
      value: ruleData.remark,
    }
  ]

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

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

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

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