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

const { getState, subscribe } = store

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

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

    constructor(props) {
        super(props)

        this.unsubscribe = subscribe('business', business => {
            const initGrade = this.getInitGrade(business.completedDate)
            this.form.current.setFieldsValue({
                patrolInfo: {
                    initGrade,
                },
            })
        })
    }

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

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

    componentWillUnmount() {
        this.unsubscribe()
    }

    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 从后端转换成前段所需格式
        if (this.record) {
            const { patrolInfo } = this.record
            if (this.record.houseInfo.completedDate)
                patrolInfo.initGrade = this.getInitGrade(getState('business').completedDate)
        }
        _state.codes = await getDictData(
            'house_patrol_init_grade',
            'house_patrol_damage_grade',
            'house_grade'
        )
        //#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
        }
    }

    getInitGrade(completedDate) {
        const date = completedDate.find(p => p.id === this.props.id)
        if (date) {
            const { value: year } = date
            if (year > 1999) {
                return 1
            }
            if (year > 1994 && year < 2000) {
                return 2
            }
            if (year > 1979 && year < 1995) {
                return 3
            }
            if (year < 1980) {
                return 4
            }
        }
    }

    render() {
        const { loading, codes, initGradeValue } = this.state
        return (
            <Spin spinning={loading} indicator={<AntIcon type="loading" />}>
                <Form {...layout} ref={this.form}>
                    <Form.Item
                        label="初始等级"
                        name={['patrolInfo', 'initGrade']}
                        tooltip="初始等级无法手动更改，由房屋详情的竣工日期决定：2000年之后竣工的为一级，1995年~1999年竣工的为二级，1980年~1994年竣工的为三级，早于1980年竣工的为四级。选择房屋竣工日期后，初始等级会自动填充。"
                        rules={[{ required: true, message: '请选择初始等级' }]}
                    >
                        <Radio.Group disabled buttonStyle="solid">
                            {codes.housePatrolInitGrade.map(item => {
                                return (
                                    <Radio.Button key={item.code} value={+item.code}>
                                        {item.value}
                                    </Radio.Button>
                                )
                            })}
                        </Radio.Group>
                    </Form.Item>

                    <Form.Item
                        label="损坏等级"
                        name={['patrolInfo', 'damageGrade']}
                        rules={[{ required: true, message: '请选择损坏等级' }]}
                    >
                        <Radio.Group buttonStyle="solid">
                            {codes.housePatrolDamageGrade.map(item => {
                                return (
                                    <Radio.Button key={item.code} value={+item.code}>
                                        {item.value}
                                    </Radio.Button>
                                )
                            })}
                        </Radio.Group>
                    </Form.Item>

                    <Form.Item
                        label="综合等级"
                        name={['patrolInfo', 'comprehensiveGrade']}
                        rules={[{ required: true, message: '请选择综合等级' }]}
                    >
                        <Radio.Group buttonStyle="solid">
                            {codes.houseGrade.map(item => {
                                return (
                                    <Radio.Button key={item.code} value={+item.code}>
                                        {item.value}
                                    </Radio.Button>
                                )
                            })}
                        </Radio.Group>
                    </Form.Item>
                </Form>
            </Spin>
        )
    }
}
