import React, { PureComponent, Fragment } from 'react';
import {
Table,
Button,
Select,
InputNumber,
Popconfirm,
Divider,
message,
} from 'antd';
import {
CHARGE_UNIT_YEAR,
CHARGE_UNIT_SEASON,
CHARGE_UNIT_HALF_YEAR,
DURATION_HALF_YEAR,
DURATION_YEAR,
DURATION_SEASON,
} from '../../../utils/config';
import styles from './TableForm.less';
const chargeUnitMap = [{
text: CHARGE_UNIT_YEAR,
value: CHARGE_UNIT_YEAR,
}, {
text: CHARGE_UNIT_HALF_YEAR,
value: CHARGE_UNIT_HALF_YEAR,
}, {
text: CHARGE_UNIT_SEASON,
value: CHARGE_UNIT_SEASON,
}];
const durationMap = {
[CHARGE_UNIT_SEASON]: DURATION_SEASON,
[CHARGE_UNIT_HALF_YEAR]: DURATION_HALF_YEAR,
[CHARGE_UNIT_YEAR]: DURATION_YEAR,
};
export default class TableForm extends PureComponent {
constructor(props) {
super(props);
this.state = {
data: props.value,
loading: props.loading,
};
}
componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
this.setState({
data: nextProps.value,
});
}
}
getRowByKey(key, newData) {
return (newData || this.state.data).filter(item => item.key === key)[0];
}
index = 0;
cacheOriginData = {};
toggleEditable=(e, key) => {
e.preventDefault();
const newData = this.state.data.map(item => ({ ...item }));
const target = this.getRowByKey(key, newData);
if (target) {
// 进入编辑状态时保存原始数据
if (!target.editable) {
this.cacheOriginData[key] = { ...target };
}
target.editable = !target.editable;
this.setState({ data: newData });
}
}
chargeUnitSelectable=(value) => {
const tmp = this.state.data.find(item => item.chargeUnit === value);
return tmp !== undefined;
}
remove(record) {
const { key, isNew } = record;
const newData = this.state.data.filter(item => item.key !== key);
this.setState({ data: newData });
if (!isNew) {
this.props.onDelete({ goodsId: key });
}
}
newPrice = () => {
const newData = this.state.data.map(item => ({ ...item }));
newData.push({
key: `NEW_TEMP_ID_${this.index}`,
cpPrice: '',
merchantPrice: '',
terminalPrice: '',
editable: true,
isNew: true,
});
this.index += 1;
this.setState({ data: newData });
}
handleFieldChange(value, fieldName, key) {
const newData = this.state.data.map(item => ({ ...item }));
const target = this.getRowByKey(key, newData);
if (target) {
target[fieldName] = value;
this.setState({ data: newData });
}
}
handleSelectChange(value, key) {
const newData = this.state.data.map(item => ({ ...item }));
const target = this.getRowByKey(key, newData);
if (target) {
target.chargeUnit = value;
this.setState({ data: newData });
}
}
saveRow(e, key) {
e.persist();
if (this.clickedCancel) {
this.clickedCancel = false;
return;
}
const target = this.getRowByKey(key) || {};
if (!target.chargeUnit || !target.cpPrice || !target.merchantPrice || !target.terminalPrice) {
message.error('请填写完整价格信息');
e.target.focus();
return;
}
let sort = 0;
const { chargeUnit, cpPrice, merchantPrice, terminalPrice, duration } = target;
if (chargeUnit === CHARGE_UNIT_HALF_YEAR) {
sort = 1;
} else if (chargeUnit === CHARGE_UNIT_YEAR) {
sort = 2;
}
if (target.isNew) {
this.props.onCreate({
sort,
cpPrice,
duration,
chargeUnit,
merchantPrice,
terminalPrice,
});
} else {
this.props.onUpdate({
sort,
cpPrice,
duration,
chargeUnit,
merchantPrice,
terminalPrice,
});
}
delete target.isNew;
this.toggleEditable(e, key);
}
cancel(e, key) {
this.clickedCancel = true;
e.preventDefault();
const newData = this.state.data.map(item => ({ ...item }));
const target = this.getRowByKey(key, newData);
if (this.cacheOriginData[key]) {
Object.assign(target, this.cacheOriginData[key]);
target.editable = false;
delete this.cacheOriginData[key];
}
this.setState({ data: newData });
this.clickedCancel = false;
}
render() {
const columns = [{
title: '计价单位',
dataIndex: 'chargeUnit',
key: 'chargeUnit',
width: '15%',
align: 'center',
render: (text, record) => {
if (record.editable) {
return (
);
}
return text;
},
}, {
title: '时长(天)',
dataIndex: 'duration',
key: 'duration',
width: '15%',
align: 'center',
render: (_, record) => {
if (record.chargeUnit) {
return durationMap[record.chargeUnit];
}
return '';
},
}, {
title: '供应商售价(¥)',
dataIndex: 'cpPrice',
key: 'cpPrice',
width: '20%',
align: 'center',
render: (text, record) => {
if (record.editable) {
return (
this.handleFieldChange(e, 'cpPrice', record.key)}
style={{ width: '100%', border: 'unset' }}
placeholder="请输入"
/>
);
}
return text;
},
}, {
title: '平台方售价(¥)',
dataIndex: 'merchantPrice',
key: 'merchantPrice',
width: '20%',
align: 'center',
render: (text, record) => {
if (record.editable) {
return (
this.handleFieldChange(value, 'merchantPrice', record.key)}
style={{ width: '100%', border: 'unset' }}
placeholder="请输入"
/>
);
}
return text;
},
}, {
title: '终端显示价格(¥)',
dataIndex: 'terminalPrice',
key: 'terminalPrice',
width: '20%',
align: 'center',
render: (text, record) => {
if (record.editable) {
return (
this.handleFieldChange(value, 'terminalPrice', record.key)}
style={{ width: '100%', border: 'unset' }}
placeholder="请输入"
/>
);
}
return text;
},
}, {
title: '操作',
key: 'action',
align: 'center',
render: (text, record) => {
if (!!record.editable && this.state.loading) {
return null;
}
if (record.editable) {
if (record.isNew) {
return (
this.saveRow(e, record.key)}>添加
this.remove(record.key)}>
删除
);
}
return (
this.saveRow(e, record.key)}>保存
this.cancel(e, record.key)}>取消
);
}
return (
this.toggleEditable(e, record.key)}>编辑
this.remove(record)}>
删除
);
},
}];
return (
{
return styles.editable;
}}
/>
);
}
}