import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import toaster from './toast';
import { Modal, ModalBody, ModalFooter, InputGroup, InputGroupText, Input, Button } from 'reactstrap';
import { gettext, serviceURL } from '../utils/constants';
import { Utils } from '../utils/utils';
import { subscriptionAPI } from '../utils/subscription-api';
import Loading from './loading';
import SeahubModalHeader from '@/components/common/seahub-modal-header';
import '../css/layout.css';
import '../css/subscription.css';
const {
isOrgContext,
} = window.app.pageOptions;
const PlansPropTypes = {
plans: PropTypes.array.isRequired,
onPay: PropTypes.func.isRequired,
paymentType: PropTypes.string.isRequired,
handleContentScroll: PropTypes.func.isRequired,
};
class Plans extends Component {
constructor(props) {
super(props);
this.state = {
currentPlan: props.plans[0],
assetQuotaUnitCount: 1,
count: 1,
};
}
togglePlan = (plan) => {
this.setState({ currentPlan: plan }, () => {
});
};
onPay = () => {
let { paymentType } = this.props;
let { currentPlan, assetQuotaUnitCount, count } = this.state;
let totalAmount; let assetQuota; let newUserCount;
// parse
if (paymentType === 'paid') {
newUserCount = currentPlan.count;
totalAmount = currentPlan.total_amount;
} else if (paymentType === 'extend_time') {
newUserCount = currentPlan.count;
assetQuota = currentPlan.asset_quota;
totalAmount = currentPlan.total_amount;
} else if (paymentType === 'add_user') {
newUserCount = count;
totalAmount = count * currentPlan.price_per_user;
} else if (paymentType === 'buy_quota') {
assetQuota = (assetQuotaUnitCount) * currentPlan.asset_quota_unit;
totalAmount = assetQuotaUnitCount * currentPlan.price_per_asset_quota_unit;
} else {
toaster.danger(gettext('Internal Server Error'));
return;
}
this.props.onPay(currentPlan.plan_id, newUserCount, assetQuota, totalAmount);
};
onCountInputChange = (e) => {
let { currentPlan } = this.state;
if (!currentPlan.can_custom_count) {
return;
}
let count = e.target.value.replace(/^(0+)|[^\d]+/g, '');
if (count < 1) {
count = 1;
} else if (count > 9999) {
count = 9999;
}
this.setState({ count: count });
};
onAssetQuotaUnitCountInputChange = (e) => {
let { currentPlan } = this.state;
if (!currentPlan.can_custom_asset_quota) {
return;
}
let count = e.target.value.replace(/^(0+)|[^\d]+/g, '');
if (count < 1) {
count = 1;
} else if (count > 9999) {
count = 9999;
}
this.setState({ assetQuotaUnitCount: count });
};
renderPaidOrExtendTime = () => {
let { plans, paymentType } = this.props;
let { currentPlan } = this.state;
let boughtQuota = 0;
if (paymentType === 'extend_time') {
boughtQuota = currentPlan.asset_quota - 100;
}
let totalAmount = currentPlan.total_amount;
let originalTotalAmount = totalAmount;
return (
{'选择方案'}
{plans.map((item, index) => {
let selectedCss = item.plan_id === currentPlan.plan_id ? 'plan-selected' : '';
let countDescription = '¥' + item.price_per_user;
if (isOrgContext) {
countDescription += '/每用户';
}
return (
-
{item.name}
{countDescription}
);
})}
{paymentType === 'extend_time' && boughtQuota > 0 &&
{'增加空间'}
-
{currentPlan.asset_quota_unit + 'GB x ' + (boughtQuota / currentPlan.asset_quota_unit)}
{/* 续费时候需要减去附赠的100GB */}
{'¥' + (boughtQuota / currentPlan.asset_quota_unit) * currentPlan.price_per_asset_quota_unit}
}
{'方案汇总'}
-
{'所选方案'}
{currentPlan.name}
{isOrgContext &&
-
{'成员人数'}
{currentPlan.count + '人'}
}
-
{'可用空间'}
{'100GB(附赠)' + (boughtQuota > 0 ? '+' + boughtQuota + 'GB(扩充)' : '')}
-
{'到期时间'}
{currentPlan.new_term_end}
-
{'实际支付金额'}
{originalTotalAmount !== totalAmount &&
{'¥' + originalTotalAmount}
}
{'¥' + totalAmount + ' '}
);
};
renderAddUser = () => {
let { currentPlan, count } = this.state;
let operationIntro = '新增用户';
let originalTotalAmount = count * currentPlan.price_per_user;
let totalAmount = originalTotalAmount;
return (
);
};
renderBuyQuota = () => {
let { currentPlan, assetQuotaUnitCount } = this.state;
let operationIntro = '新增空间';
let originalTotalAmount = assetQuotaUnitCount * currentPlan.price_per_asset_quota_unit;
let totalAmount = originalTotalAmount;
return (
);
};
render() {
let { paymentType } = this.props;
if (paymentType === 'paid' || paymentType === 'extend_time') {
return this.renderPaidOrExtendTime();
} else if (paymentType === 'add_user') {
return this.renderAddUser();
} else if (paymentType === 'buy_quota') {
return this.renderBuyQuota();
} else {
toaster.danger(gettext('Internal Server Error'));
return;
}
}
}
Plans.propTypes = PlansPropTypes;
const PlansDialogPropTypes = {
isOrgContext: PropTypes.bool.isRequired,
paymentType: PropTypes.string.isRequired,
paymentTypeTrans: PropTypes.string.isRequired,
toggleDialog: PropTypes.func.isRequired,
};
class PlansDialog extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
isWaiting: false,
planList: [],
paymentSourceList: [],
};
}
getPlans = () => {
subscriptionAPI.getSubscriptionPlans(this.props.paymentType).then((res) => {
this.setState({
planList: res.data.plan_list,
paymentSourceList: res.data.payment_source_list,
isLoading: false,
});
}).catch(error => {
let errorMsg = Utils.getErrorMsg(error);
this.setState({
isLoading: false,
errorMsg: errorMsg,
});
});
};
onPay = (planID, count, asset_quota, totalAmount) => {
this.setState({ isWaiting: true });
let payUrl = serviceURL + '/subscription/pay/?payment_source=' + this.state.paymentSourceList[0] +
'&payment_type=' + this.props.paymentType + '&plan_id=' + planID +
'&total_amount=' + totalAmount;
if (count) {
payUrl += '&count=' + count;
}
if (asset_quota) {
payUrl += '&asset_quota=' + asset_quota;
}
window.open(payUrl);
};
onReload = () => {
window.location.reload();
};
componentDidMount() {
this.getPlans();
}
render() {
const { isLoading, isWaiting, planList } = this.state;
const { toggleDialog, paymentTypeTrans, paymentType } = this.props;
const modalStyle = (paymentType === 'paid' || paymentType === 'extend_time') ?
{ width: '560px', maxWidth: '560px' } : { width: '560px' };
if (isLoading) {
return (
{paymentTypeTrans}
);
}
if (isWaiting) {
return (
{paymentTypeTrans}
{'是否完成付款?'}
);
}
return (
{paymentTypeTrans}
);
}
}
PlansDialog.propTypes = PlansDialogPropTypes;
const propTypes = {
isOrgContext: PropTypes.bool.isRequired,
handleContentScroll: PropTypes.func,
};
class Subscription extends Component {
constructor(props) {
super(props);
this.paymentTypeTransMap = {
paid: '立即购买',
extend_time: '立即续费',
add_user: '增加用户',
buy_quota: '增加空间',
};
this.state = {
isLoading: true,
errorMsg: '',
isDialogOpen: false,
planName: this.props.isOrgContext ? '团队版' : '个人版',
userLimit: 20,
assetQuota: 1,
termEnd: '长期',
subscription: null,
paymentTypeList: [],
currentPaymentType: '',
errorMsgCode: ''
};
}
getSubscription = () => {
subscriptionAPI.getSubscription().then((res) => {
const subscription = res.data.subscription;
const paymentTypeList = res.data.payment_type_list;
if (!subscription) {
this.setState({
isLoading: false,
paymentTypeList: paymentTypeList,
});
} else {
let isActive = subscription.is_active;
let plan = subscription.plan;
this.setState({
isLoading: false,
subscription,
planName: plan.name,
userLimit: subscription.user_limit,
assetQuota: isActive ? subscription.asset_quota : plan.asset_quota,
termEnd: isActive ? subscription.term_end : '已过期',
paymentTypeList: paymentTypeList,
});
}
}).catch(error => {
let errorMsg = Utils.getErrorMsg(error);
this.setState({
isLoading: false,
errorMsg: errorMsg,
});
});
};
toggleDialog = () => {
this.setState({ isDialogOpen: !this.state.isDialogOpen });
};
togglePaymentType = (paymentType) => {
this.setState({ currentPaymentType: paymentType });
this.toggleDialog();
};
componentDidMount() {
this.getSubscription();
}
render() {
const { isLoading, errorMsg, planName, userLimit, assetQuota, termEnd,
isDialogOpen, paymentTypeList, currentPaymentType } = this.state;
if (isLoading) {
return ;
}
if (errorMsg) {
return {errorMsg}
;
}
return (
{this.props.isOrgContext &&
}
{'空间'}
{assetQuota ? assetQuota + 'GB' : '1GB'}
{paymentTypeList.map((item, index) => {
let name = this.paymentTypeTransMap[item];
return (
);
})}
{!this.state.subscription &&
{'销售咨询'}
{'微信扫码联系销售'}
}
{isDialogOpen &&
}
);
}
}
Subscription.propTypes = propTypes;
export default Subscription;