import React, { Component } from 'react'
import { Form, Input, Radio, Spin } from 'antd'
import { AntIcon } from 'components'
import { cloneDeep, first, isEqual, last, sortBy } from 'lodash'
import getDictData from 'util/dic'

const layout = {
    labelCol: { flex: '150px' },
    wrapperCol: { flex: '1' },
}

export default class handling extends Component {
    state = {
        loading: true,
        codes: {
            housePatrolHandlingOpinion: [],
            housePatrolRectifyReform: [],
        },
    }
    form = React.createRef()

    shouldComponentUpdate(props, state) {
        return !isEqual(this.state, state)
    }

    componentDidMount() {
        this.fillData({
            record: this.props.record,
        })
    }

    call() {
        if (this.props.onRef) {
            this.props.onRef(this)
        }
    }

    /**
     * 填充数据
     * 可以在设置this.record之后对其作出数据结构调整
     * [异步,必要]
     * @param {*} params
     */
    async fillData(params) {
        this.record = cloneDeep(params.record)
        const _state = { loading: false }
        //#region 从后端转换成前段所需格式
        _state.codes = await getDictData(
            'house_patrol_handling_opinion',
            'house_patrol_rectify_Reform'
        )
        //#endregion
        this.form.current && this.form.current.setFieldsValue(this.record)

        this.setState(_state)
        this.call()
    }

    /**
     * 获取数据
     * 可以对postData进行数据结构调整
     * [异步,必要]
     * @returns
     */
    async getData() {
        const form = this.form.current

        const valid = await form.validateFields()
        if (valid) {
            const postData = form.getFieldsValue()
            //#region 从前段转换后端所需格式

            //#endregion
            return postData
        }
    }
    render() {
        const { loading, codes } = this.state
        return (
            <Spin spinning={loading} indicator={<AntIcon type="loading" />}>
                <Form {...layout} ref={this.form}>
                    <Form.Item
                        label="处理建议"
                        name={['patrolInfo', 'handlingOpinion']}
                        rules={[{ required: true, message: '请选择处理建议' }]}
                    >
                        <Radio.Group buttonStyle="solid">
                            {codes.housePatrolHandlingOpinion.map(item => {
                                return (
                                    <Radio.Button key={item.code} value={+item.code}>
                                        {item.value}
                                    </Radio.Button>
                                )
                            })}
                        </Radio.Group>
                    </Form.Item>

                    <Form.Item label="处理建议备注" name={['patrolInfo', 'handlingOpinionRemark']}>
                        <Input.TextArea
                            autoSize={{ minRows: 3 }}
                            placeholder="请输入处理建议备注"
                        />
                    </Form.Item>

                    <Form.Item
                        label="整改情况"
                        name={['patrolInfo', 'rectifyAndReform']}
                        rules={[{ required: true, message: '请选择整改情况' }]}
                    >
                        <Radio.Group buttonStyle="solid">
                            {codes.housePatrolRectifyReform.map(item => {
                                return (
                                    <Radio.Button key={item.code} value={+item.code}>
                                        {item.value}
                                    </Radio.Button>
                                )
                            })}
                        </Radio.Group>
                    </Form.Item>

                    <Form.Item label="整改情况备注" name={['patrolInfo', 'rectifyAndReformRemark']}>
                        <Input.TextArea
                            autoSize={{ minRows: 3 }}
                            placeholder="请输入整改情况备注"
                        />
                    </Form.Item>
                </Form>
            </Spin>
        )
    }
}
