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 XForm, { FieldTypes } from '../../components/XForm';
import router from 'umi/router';
import apis from '../../services/apis';
import request from '../../utils/request';
import moment from 'moment';


const header = props => {
    const defaultPass = 'abc@123';
    const newsId = props.location.query.id
    console.log("newsId" + newsId);
    const [ newsData, setNewsData ] = useState({})
    if(newsId){
      useEffect(() => {
        getNewsData(newsId);
      },[])
  
    // 查询列表
    const getNewsData = (newsId) => {
      request({
          ...apis.channel.get,
          urlData: { id: newsId }
      }).then((data) => {
          setNewsData(data)
      }).catch((err) => {
        message.error(err.msg || err.message)
      })
    }
    }
  
    const fields = [
      {
        label: '渠道代理商名称',
        name: 'channelProxyName',
        type: FieldTypes.Text,
        value: newsData.channelProxyName,
        rules: [
          { required: true, message: '请输入渠道代理商名称' },
        ]
      },    
      {
        label: '公司logo',
        name: 'channelImg',
        type: FieldTypes.ImageUploader,
        value: newsData.channelImg,
        rules: [
            { required: true, message: '请选择公司logo' },
          ],
        help: '用于渠道代理平台导航logo(建议尺寸42*42px)',
      },
      {
        label: '联系人',
        name: 'channelContactName',
        value: newsData.channelContactName,
        type: FieldTypes.Text,
        rules: [
          {required: true, message: '请输入联系人'},
          { max: 30, message: '联系人不超过30个字符' }
        ]
      },
      {
        label: '手机号',
        name: 'channelTel',
        value: newsData.channelTel,
        type: FieldTypes.Text,
        rules: [
          {required: true, message: '请输入手机号'},
        ]
      },
      {
        label: '账户名',
        name: 'userName',
        type: FieldTypes.Text,
        extra: `默认登录密码 ${defaultPass}`,
        value: newsData.userName,
      },
      {
        label: '有效期',
        name: 'expireDate',
        type: FieldTypes.DatePicker ,
        value: newsData.expireDate != null ? moment(newsData.expireDate, 'YYYY-MM-DD') : null,
        rules: [
          { required: true, message: '请选择有效期时间' },
        ],
      },
      {
        label: '维护小程序数量',
        name: 'appMaxNum',
        type: FieldTypes.Number,
        value: newsData.appMaxNum,
        rules: [
          {required: true, message: '请输入维护小程序数量'},
        ]
      },
    ]
  
     
    const handleSubmit = (values) => {
      if(newsId){
          values.newsTypeId = newsId
          request({ ...apis.channel.put, urlData: { id: newsId }, data: { ...values }}).then((data) => {
            cancelPage();
          }).catch((err) => {
            message.error(err.msg || err.message)
          })
        }else{
          request({ ...apis.channel.post, data: { ...values } }).then((data) => {
            cancelPage();
          }).catch((err) => {
            message.error(err.msg || err.message)
          })
        }
    }
  
    const cancelPage = () => {
      router.push({
        pathname: '/channel',
      });
    }
  
    return (
      <XForm onSubmit={handleSubmit} onCancel={cancelPage} fields={fields}></XForm>
    )
  }
  
  const WrappedNormalLoginForm = Form.create({ name: 'header' })(header);
  export default WrappedNormalLoginForm