import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Spin, notification, Modal } from 'antd'
|
|
import Api from '@/api'
|
import UtilsDM from '@/utils/utils-datamanage.js'
|
import asyncComponent from '@/utils/asyncComponent'
|
import asyncSpinComponent from '@/utils/asyncSpinComponent'
|
import MKEmitter from '@/utils/events.js'
|
import './index.scss'
|
|
const MutilForm = asyncSpinComponent(() => import('@/tabviews/zshare/mutilform'))
|
const NormalButton = asyncComponent(() => import('@/tabviews/zshare/actionList/normalbutton'))
|
|
class TabForm extends Component {
|
static propTpyes = {
|
config: PropTypes.object
|
}
|
|
state = {
|
BID: '', // 上级ID
|
config: null, // 图表配置信息
|
loading: false, // 数据加载状态
|
data: null,
|
BData: '',
|
group: null
|
}
|
|
UNSAFE_componentWillMount () {
|
const { config } = this.props
|
|
let _config = fromJS(config).toJS()
|
let _data = null
|
let BID = ''
|
let BData = ''
|
|
if (_config.setting.supModule) {
|
BData = window.GLOB.CacheData.get(_config.setting.supModule)
|
} else {
|
BData = window.GLOB.CacheData.get(_config.$pageId)
|
}
|
if (BData) {
|
BID = BData.$BID || ''
|
}
|
|
if (_config.wrap.datatype === 'dynamic') {
|
_config.setting.onload = _config.setting.sync === 'true' ? 'false' : 'true'
|
|
if (_config.setting.sync === 'true' && window.GLOB.SyncData.has(_config.dataName)) {
|
_data = window.GLOB.SyncData.get(_config.dataName) || []
|
_data = _data[0] || {$$empty: true}
|
_data.$$uuid = _data[_config.setting.primaryKey] || ''
|
|
_config.setting.sync = 'false'
|
|
window.GLOB.SyncData.delete(_config.dataName)
|
}
|
} else {
|
_data = {$$empty: true}
|
}
|
|
if (!_config.wrap.groupLabel) {
|
if (_config.subcards.length > 1) {
|
_config.wrap.groupLabel = 'show'
|
} else {
|
_config.wrap.groupLabel = 'hidden'
|
}
|
}
|
|
_config.subcards = _config.subcards.map(group => {
|
if (group.subButton.enable === 'false') {
|
group.subButton.style.display = 'none'
|
group.$button = 'no-button'
|
}
|
|
return group
|
})
|
|
_config.titleStyle = {}
|
|
if (_config.style.fontSize) {
|
_config.titleStyle = {fontSize: parseInt(_config.style.fontSize)}
|
}
|
|
this.setState({
|
data: _data,
|
group: _config.subcards[0],
|
BID: BID || '',
|
BData: BData || '',
|
config: _config,
|
arr_field: _config.columns.map(col => col.field).join(',')
|
})
|
}
|
|
componentDidMount () {
|
const { config } = this.state
|
|
MKEmitter.addListener('reloadData', this.reloadData)
|
MKEmitter.addListener('mkFormSubmit', this.mkFormSubmit)
|
MKEmitter.addListener('resetSelectLine', this.resetParentParam)
|
MKEmitter.addListener('refreshByButtonResult', this.refreshByButtonResult)
|
|
if (config.setting.useMSearch) {
|
MKEmitter.addListener('searchRefresh', this.searchRefresh)
|
}
|
|
if (config.setting.sync === 'true') {
|
MKEmitter.addListener('transferSyncData', this.transferSyncData)
|
}
|
|
this.initExec()
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('reloadData', this.reloadData)
|
MKEmitter.removeListener('mkFormSubmit', this.mkFormSubmit)
|
MKEmitter.removeListener('searchRefresh', this.searchRefresh)
|
MKEmitter.removeListener('resetSelectLine', this.resetParentParam)
|
MKEmitter.removeListener('transferSyncData', this.transferSyncData)
|
MKEmitter.removeListener('refreshByButtonResult', this.refreshByButtonResult)
|
}
|
|
initExec = () => {
|
const { config } = this.state
|
|
if (config.wrap.datatype === 'dynamic' && config.setting.onload === 'true') {
|
setTimeout(() => {
|
this.loadData()
|
}, config.setting.delay || 0)
|
}
|
}
|
|
transferSyncData = (syncId) => {
|
const { config } = this.state
|
|
if (config.$syncId !== syncId) return
|
|
let _data = window.GLOB.SyncData.get(config.dataName) || []
|
_data = _data[0] || {$$empty: true}
|
_data.$$uuid = _data[config.setting.primaryKey] || ''
|
|
this.setState({data: _data})
|
|
window.GLOB.SyncData.delete(config.dataName)
|
|
MKEmitter.removeListener('transferSyncData', this.transferSyncData)
|
}
|
|
searchRefresh = (searchId) => {
|
const { config } = this.state
|
|
if (config.$searchId !== searchId) return
|
|
this.setState({}, () => {
|
this.loadData()
|
})
|
}
|
|
reloadData = (menuId, id) => {
|
const { config } = this.state
|
|
if (config.uuid !== menuId) return
|
|
this.loadData()
|
}
|
|
/**
|
* @description 按钮执行完成后页面刷新
|
* @param {*} menuId // 菜单Id
|
* @param {*} position // 刷新位置
|
* @param {*} btn // 执行的按钮
|
*/
|
refreshByButtonResult = (menuId, position, btn, id) => {
|
const { config, group, BID } = this.state
|
|
if (group.uuid !== menuId) return
|
|
if ((position === 'mainline' || position === 'popclose') && config.setting.supModule && BID) {
|
MKEmitter.emit('reloadData', config.setting.supModule, BID)
|
} else {
|
this.loadData()
|
}
|
|
if (id) {
|
MKEmitter.emit('resetSelectLine', config.uuid, id, '')
|
}
|
|
this.execSuccess(btn, id)
|
}
|
|
resetParentParam = (MenuID, id, data) => {
|
const { config } = this.state
|
if (!config.setting.supModule || config.setting.supModule !== MenuID) return
|
|
if (id !== this.state.BID || id !== '') {
|
if (config.wrap.datatype === 'static' || (config.setting.supModule && !id)) {
|
this.setState({
|
data: null,
|
BID: id,
|
BData: data
|
}, () => {
|
this.setState({
|
data: {$$empty: true}
|
})
|
})
|
} else {
|
this.setState({ BID: id, BData: data }, () => {
|
this.loadData()
|
})
|
}
|
}
|
}
|
|
execSuccess = (btn, id) => {
|
if (btn.linkmenu && btn.linkmenu.length > 0) {
|
let menu_id = btn.linkmenu[btn.linkmenu.length - 1]
|
let menu = window.GLOB.mkThdMenus.filter(m => m.MenuID === menu_id)[0] || ''
|
|
if (!menu) return
|
|
let newtab = {
|
...menu,
|
param: {$BID: id || ''}
|
}
|
|
MKEmitter.emit('modifyTabs', newtab, true)
|
}
|
}
|
|
async loadData () {
|
const { config, arr_field, BID } = this.state
|
|
if (config.wrap.datatype === 'static') {
|
this.setState({
|
data: {$$empty: true}
|
})
|
return
|
} else if (config.setting.supModule && !BID) {
|
this.setState({
|
data: null
|
}, () => {
|
this.setState({data: {$$empty: true}})
|
})
|
MKEmitter.emit('resetSelectLine', config.uuid, '', '')
|
return
|
}
|
|
let searches = []
|
if (config.setting.useMSearch) { // 主表搜索条件
|
searches = window.GLOB.SearchBox.get(config.$searchId) || []
|
}
|
|
if (config.$s_req && searches.filter(item => item.required && item.value === '').length > 0) {
|
return
|
}
|
|
this.setState({
|
loading: true
|
})
|
|
let _orderBy = config.setting.order || ''
|
let param = UtilsDM.getQueryDataParams(config.setting, arr_field, searches, _orderBy, 1, 1, BID)
|
|
let result = await Api.genericInterface(param)
|
if (result.status) {
|
let _data = result.data && result.data[0] ? result.data[0] : {$$empty: true}
|
|
_data.$$uuid = _data[config.setting.primaryKey] || ''
|
|
this.setState({
|
data: null,
|
loading: false
|
}, () => {
|
this.setState({data: _data})
|
})
|
|
MKEmitter.emit('resetSelectLine', config.uuid, _data.$$uuid, _data)
|
|
if (result.message) {
|
if (result.ErrCode === 'Y') {
|
Modal.success({
|
title: result.message
|
})
|
} else if (result.ErrCode === 'S') {
|
notification.success({
|
top: 92,
|
message: result.message,
|
duration: 2
|
})
|
}
|
}
|
} else {
|
this.setState({
|
loading: false,
|
})
|
|
if (!result.message) return
|
if (result.ErrCode === 'N') {
|
Modal.error({
|
title: result.message,
|
})
|
} else if (result.ErrCode !== '-2') {
|
notification.error({
|
top: 92,
|
message: result.message,
|
duration: 10
|
})
|
}
|
}
|
}
|
|
mkFormSubmit = (btnId) => {
|
const { group } = this.state
|
|
if (group.uuid !== btnId) return
|
|
this.formRef.handleConfirm().then(res => {
|
MKEmitter.emit('triggerFormSubmit', {menuId: btnId, form: res})
|
})
|
}
|
|
changeGroup = (group) => {
|
this.setState({
|
group: null
|
}, () => {
|
this.setState({group})
|
})
|
}
|
|
render() {
|
const { config, loading, BID, BData, data, group } = this.state
|
|
if (config.wrap.empty === 'hidden' && (!data || data.$$empty)) return null
|
if (config.idCtrl && (!data || data.$$empty)) return null
|
|
return (
|
<div className="custom-tab-form-box" id={'anchor' + config.uuid} style={{...config.style}}>
|
{loading ?
|
<div className="loading-mask">
|
<div className="ant-spin-blur"></div>
|
<Spin />
|
</div> : null
|
}
|
{config.wrap.groupLabel !== 'hidden' ? <div className={'mk-normal-form-title ' + config.wrap.tabtype}>
|
{config.subcards.map(card => (
|
<div key={card.uuid} onClick={() => this.changeGroup(card)} style={config.titleStyle} className={'form-title' + (group && group.uuid === card.uuid ? ' active' : '')}>
|
{card.setting.title}
|
</div>))
|
}
|
</div> : null}
|
{group && data ? <MutilForm
|
BID={BID}
|
BData={BData}
|
data={data}
|
action={group}
|
unload={config.setting.supModule && !BID}
|
inputSubmit={() => this.mkFormSubmit(group.uuid)}
|
wrappedComponentRef={(inst) => this.formRef = inst}
|
/> : null}
|
{group && data ? <div className={'mk-form-action ' + (group.$button || '')}>
|
<NormalButton
|
BID={BID}
|
btn={group.subButton}
|
setting={config.setting}
|
columns={config.columns}
|
selectedData={data.$$empty ? [] : [data]}
|
/>
|
</div> : null}
|
</div>
|
)
|
}
|
}
|
|
export default TabForm
|