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

const initialValues = {}

export default class form extends Component {
    state = {
        // 加载状态
        loading: true,

        codes: {
            constsType: [],
        },
    }

    // 表单实例
    form = React.createRef()

    // 初始化数据
    record = {}

    /**
     * mount后回调
     */
    componentDidMount() {
        this.props.created && this.props.created(this)
    }

    /**
     * 填充数据
     * 可以在设置this.record之后对其作出数据结构调整
     * [异步,必要]
     * @param {*} params
     */
    async fillData(params) {
        this.record = cloneDeep(params.record)
        //#region 从后端转换成前段所需格式,也可以在此处调用获取详细数据接口
        const codes = await getDictData('consts_type')
        this.setState({ codes })
        //#endregion
        this.form.current.setFieldsValue(this.record)

        this.setState({
            loading: false,
        })
    }

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

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

    //#region 自定义方法
    //#endregion

    render() {
        const { codes } = this.state

        return (
            <Form initialValues={initialValues} ref={this.form} className="yo-form">
                <Spin spinning={this.state.loading} indicator={<AntIcon type="loading" />}>
                    <div className="yo-form-group">
                        <Form.Item
                            label="角色名"
                            name="name"
                            rules={[{ required: true, message: '请输入应用名称' }]}
                        >
                            <Input autoComplete="off" placeholder="请输入角色名" />
                        </Form.Item>
                        <Form.Item
                            label="唯一编码"
                            name="code"
                            rules={[{ required: true, message: '请输入唯一编码' }]}
                        >
                            <Input autoComplete="off" placeholder="请输入唯一编码" />
                        </Form.Item>
                        <Form.Item
                            label="系统参数"
                            name="sysFlag"
                            rules={[{ required: true, message: '请选择参数类型' }]}
                        >
                            <Radio.Group disabled={this.record}>
                                <Radio.Button value="Y">是</Radio.Button>
                                <Radio.Button value="N">否</Radio.Button>
                            </Radio.Group>
                        </Form.Item>
                        <Form.Item
                            label="所属分类"
                            name="groupCode"
                            rules={[{ required: true, message: '请选择所属分类' }]}
                        >
                            <Select placeholder="请选择所属分类" disabled={this.record}>
                                {codes.constsType.map(item => (
                                    <Select.Option key={item.code} value={item.code}>
                                        {item.value}
                                    </Select.Option>
                                ))}
                            </Select>
                        </Form.Item>
                        <Form.Item
                            label="参数值"
                            name="value"
                            rules={[{ required: true, message: '请输入参数值' }]}
                        >
                            <Input autoComplete="off" placeholder="请输入参数值" />
                        </Form.Item>
                        <Form.Item label="备注" name="remark">
                            <Input.TextArea autoComplete="off" placeholder="请输入备注" />
                        </Form.Item>
                    </div>
                </Spin>
            </Form>
        )
    }
}
