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

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

export default class result extends Component {
    state = {
        loading: true,
        codes: {
            patrolResult: [
                { code: '0', value: '正常' },
                { code: '-1', value: '异常' },
            ],
        },
    }
    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)
        //#region 从后端转换成前段所需格式

        //#endregion
        this.form.current && this.form.current.setFieldsValue(this.record)

        this.setState({ loading: false })
        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', 'patrolResult']}
                        rules={[{ required: true, message: '请选择本期巡查结果' }]}
                    >
                        <Radio.Group buttonStyle="solid">
                            {codes.patrolResult.map(item => {
                                return (
                                    <Radio.Button key={item.code} value={+item.code}>
                                        {item.value}
                                    </Radio.Button>
                                )
                            })}
                        </Radio.Group>
                    </Form.Item>
                    <Form.Item label="异常情况描述" name={['patrolInfo', 'patrolResultRemark']}>
                        <Input.TextArea autoSize placeholder="请输入异常情况描述" />
                    </Form.Item>
                </Form>
            </Spin>
        )
    }
}
