import React, { Component } from 'react'
import {
Button,
Card,
Cascader,
Form,
Input,
Tag,
Popconfirm,
message as Message,
Radio,
Select,
} from 'antd'
import { isEqual } from 'lodash'
import { AntIcon, Auth, Container, QueryTable, QueryTableActions } from 'components'
import { api } from 'common/api'
import getDictData from 'util/dic'
import auth from 'components/authorized/handler'
import { toCamelCase } from 'util/format'
import { getSearchInfo } from 'util/query'
// 配置页面所需接口函数
const apiAction = {
page: api.houseCodePage,
}
// 用于弹窗标题
const name = '房屋编码'
export default class index extends Component {
state = {
codes: {
houseType: [],
houseIndustry: [],
},
options: {
areaTree: [],
},
type: '',
}
// 表格实例
table = React.createRef()
// 表格字段
columns = [
{
title: '房屋编码',
dataIndex: 'houseCode',
sorter: true,
width: 300,
render: (text, record) => (
<>
{`${record.areaName}-${record.roadName}-${record.commName}-${
record.note
}-${record.no.toString().padStart(3, '0')}`}
{text}
>
),
},
{
title: '房屋性质及行业',
dataIndex: 'type',
sorter: true,
width: 150,
render: (text, record) =>
this.bindCodeValue(text, 'house_type') +
(text === 2 ? `(${this.bindCodeValue(record.industry, 'house_industry')})` : ''),
},
{
title: '地址',
dataIndex: 'address',
sorter: true,
},
{
title: '登记时间',
dataIndex: 'createdTime',
sorter: true,
width: 150,
},
]
/**
* 构造函数,在渲染前动态添加操作字段等
* @param {*} props
*/
constructor(props) {
super(props)
const flag = auth({ houseCode: [['edit'], ['delete']] })
if (flag) {
this.columns.push({
title: '操作',
width: 150,
dataIndex: 'actions',
render: (text, record) => (
this.onOpen(record)}>编辑
this.onDelete(record)}
>
删除
),
})
}
}
/**
* 阻止外部组件引发的渲染,提升性能
* 可自行添加渲染条件
* [必要]
* @param {*} props
* @param {*} state
* @returns
*/
shouldComponentUpdate(props, state) {
return !isEqual(this.state, state)
}
/**
* 加载字典数据,之后开始加载表格数据
* 如果必须要加载字典数据,可直接对表格设置autoLoad=true
*/
componentDidMount() {
const { onLoading, onLoadData } = this.table.current
onLoading()
getDictData('house_type', 'house_industry').then(async res => {
const { data } = await api.getAreaTree()
this.setState(
{
codes: res,
options: {
areaTree: data,
},
},
() => {
onLoadData()
}
)
})
}
/**
* 调用加载数据接口,可在调用前对query进行处理
* [异步,必要]
* @param {*} params
* @param {*} query
* @returns
*/
loadData = async (params, query) => {
if (query.areaCode) {
query.areaCode = query.areaCode.pop()
}
const searchInfo = getSearchInfo({
query,
queryType: {
no: '=',
type: '=',
address: 'like',
houseCode: 'like',
},
})
const { data } = await apiAction.page({
...params,
searchInfo,
})
return data
}
/**
* 绑定字典数据
* @param {*} code
* @param {*} name
* @returns
*/
bindCodeValue(code, name) {
name = toCamelCase(name)
const codes = this.state.codes[name]
if (codes) {
const c = codes.find(p => p.code == code)
if (c) {
return c.value
}
}
return null
}
/**
* 打开新增/编辑弹窗
* @param {*} modal
* @param {*} record
*/
onOpen(record) {
const path = 'business/house/code/form'
window.openContentWindow({
key: record ? record.id : path,
title: record ? '修改房屋编码' : '新增房屋编码',
subTitle:
record &&
`${record.areaName}-${record.roadName}-${record.commName}-${record.note}-${record.no
.toString()
.padStart(3, '0')}`,
path,
param: {
id: record && record.id,
},
})
// modal.current.open({
// record
// })
}
/**
* 对表格上的操作进行统一处理
* [异步]
* @param {*} action
* @param {*} successMessage
*/
async onAction(action, successMessage) {
const { onLoading, onLoaded, onReloadData } = this.table.current
onLoading()
try {
if (action) {
await action
}
if (successMessage) {
Message.success(successMessage)
}
onReloadData()
} catch {
onLoaded()
}
}
/**
* 删除
* @param {*} record
*/
onDelete(record) {
this.onAction(apiAction.delete(record), '删除成功')
}
//#region 自定义方法
//#endregion
render() {
const { options, codes, type } = this.state
return (
{
if (values.hasOwnProperty('type')) {
this.setState({ type: values.type })
}
}}
query={
labels.join(' - ')}
fieldNames={{
label: 'name',
value: 'code',
children: 'children',
}}
options={options.areaTree}
className="w-400"
expandTrigger="hover"
placeholder="请选择所在区域"
/>
{/*
value && value.padStart(3, '0')}
max={999}
min={1}
precision={0}
step={1}
placeholder="请输入房屋序号"
/>
*/}
全部
{codes.houseType.map(item => (
{item.value}
))}
{type == 2 && (
)}
}
operator={
}
onClick={() => this.onOpen()}
>
新增{name}
}
/>
{this.props.supportInfo}
)
}
}