import React, { Component } from 'react' import Form, { FormComponentProps } from 'antd/lib/form' import { RouteComponentProps, withRouter } from 'react-router-dom' import GenneralDataCompo from '@/hoc/GenneralDataCompo' import './style.scss' import CustBreadcrumb from '@/components/CustComponents/CustBreadcrumb' import FormItemDecorator from '@/components/FormItemDecorator' import { Input, Select, Tooltip, InputNumber, DatePicker, Button, message } from 'antd' import { RIGHT_TYPE, RIGHT_TYPE_DICT } from '../constants' import questionIcon from '@/assets/images/ico_tips_question@2x.png' import { getTimestamp } from '@/shared/common/utils' import API from '@/api' import { AJAX_STATUS } from '@/shared/common/constants' import moment from 'moment' import arrowIcon from '@/assets/images/ico_arrow_blue@2x.png' interface AddActivityProps extends FormComponentProps, RouteComponentProps {} interface AddActivityState { businessList: Array } const SelectOption = Select.Option @(withRouter as any) class AddActivity extends Component { constructor(props: AddActivityProps) { super(props) this.state = { businessList: [], } } componentDidMount() { this.getFirstChannel() } getFirstChannel = async () => { try { const res = await API.firstChannel.getFirstChannel() if (res.code === AJAX_STATUS.SUCCESS) { this.setState({ businessList: [...res.data], }) } } catch (e) { throw new Error(e.message) } } onSubmit = () => { const { businessList } = this.state const record = this.props.location.state ? this.props.location.state.record : {} this.props.form.validateFields(async (err, values) => { if (!err) { const params = { ...values } // params.startActivityDate = params.dateList&¶ms.dateList.length > 0 && getTimestamp(params.dateList[0]) // params.endActivityDate = params.dateList&¶ms.dateList.length > 0 && getTimestamp(params.dateList[1]) params.startActivityDate = params.beginDate && getTimestamp(params.beginDate) params.endActivityDate = params.endDate && getTimestamp(params.endDate) if (values.channelId) { businessList.forEach(e => { if (e.id === values.channelId) { params.channelName = e.name } }) } params.activityId = record.activityId ? record.activityId : '' delete params.dateList try { const res = await API.reward.saveActivity(params) if (res.code === AJAX_STATUS.SUCCESS) { this.onCancle() } else { message.info(res.message, 2, null) } } catch (e) { throw new Error(e.message) } } }) } onCancle = () => { this.props.history.push('/award/activity/list') } blurUID = e => { const value = e.target.value if (!value) return this.props.form.validateFields(['uid'], async err => { if (!err) { try { const res = await API.reward.checkUid(value) if (res.code !== AJAX_STATUS.SUCCESS) { message.info(res.message, 2, null) } } catch (e) { throw new Error(e.message) } } }) } // validatorDateLiist = (rule, value, callback) => { // const record = this.props.location.state?this.props.location.state.record:{} // const {activityId, endActivityDate, startActivityDate} = record // if(activityId){ // const startDate = value&&value.length > 0 && getTimestamp(value[0]) // const endDate = value&&value.length > 0 && getTimestamp(value[1]) // const now = new Date().valueOf(); // if(now <= endActivityDate && now >= startActivityDate){ // if(now>endDate){ // callback("结束时间需要晚于当前时间") // } // if(startDate !== startActivityDate){ // callback("进行中的活动,开始时间不能修改") // } // }else if(now < startActivityDate){ // if(now>startDate){ // callback("开始和结束时间不能早于当前时间") // } // } // } // callback() // } render() { const routes = [ { path: '/award/activity/list', name: '拉新活动列表', }, { path: '', name: '编辑拉新活动', }, ] const formItemLayout = { labelCol: { xs: { span: 24 }, sm: { span: 4 }, }, wrapperCol: { xs: { span: 24 }, sm: { span: 20 }, }, } const record = this.props.location.state ? this.props.location.state.record : {} const { activityId, activityName, channelId, presentType, commodityId, allNumber, startActivityDate, endActivityDate, uid, opsActivityId, activityUrl, } = record const { form } = this.props const { businessList } = this.state const rightType = form.getFieldValue('presentType') const templateID = rightType === RIGHT_TYPE_DICT.many ? 1875 : 1810 const tryAndSmall = rightType === RIGHT_TYPE_DICT.try || rightType === RIGHT_TYPE_DICT.small ? true : false const runningFlag = activityId && moment().valueOf() < endActivityDate && moment().valueOf() > startActivityDate return (
{`${activityId ? '编辑' : '新增'}拉新活动`}
{rightType && ( )} {rightType && ( {templateID} question )} {form.getFieldDecorator('activityNumber', { initialValue: allNumber && allNumber, rules: [ { required: true, message: '请输入活动限制人数', }, ], })( , )} question {/* {form.getFieldDecorator('dateList', { initialValue: startActivityDate&&endActivityDate&&[moment(startActivityDate), moment(endActivityDate)], rules: [ { required: true, message: '请选择活动时间', }, { validator: this.validatorDateLiist, } ], })( )} */} {form.getFieldDecorator('beginDate', { initialValue: startActivityDate && moment(startActivityDate), rules: [ { required: true, message: '请选择活动起始时间', }, { validator: (rule, value, callback) => { if (runningFlag) { callback() return } if (moment(value).valueOf() < moment().valueOf()) { callback('起始时间不得小于当前时间') } callback() }, }, ], })()} ~ {form.getFieldDecorator('endDate', { initialValue: endActivityDate && moment(endActivityDate), rules: [ { required: true, message: '请选择活动结束时间', }, { validator: (rule, value, callback) => { const beginDate = this.props.form.getFieldValue('beginDate') if (moment(value).valueOf() < moment().valueOf()) { callback('结束时间不得小于当前时间') } if (moment(value).valueOf() < moment(beginDate).valueOf()) { callback('结束时间不得小于开始时间') } callback() }, }, ], })()} question {form.getFieldDecorator('uid', { initialValue: uid && uid, rules: [ { required: true, message: '请输入商户UID', }, { pattern: /^\d+$/, message: '请输入数字', }, ], })( { this.blurUID(e) }} />, )} question {form.getFieldDecorator('opsActivityId', { initialValue: opsActivityId && opsActivityId, rules: [ { required: true, message: '请输入OPS活动ID', }, { pattern: /^[a-zA-Z0-9]+$/, message: '仅支持字母和数字', }, ], })( , )} question {/* */}
) } } export default Form.create()(GenneralDataCompo(AddActivity))