// 新增基础数据集
import React from 'react';
import {Form, Modal, Input, Row, Col, message, Select} from 'antd';
import { $http } from 'containers/config/https';

let {Option} = Select;

class NewDictionary extends React.Component {
    constructor() {
        super();
        this.state = {
            userInfo: {},
            visible: false,
            flag: '',
        }
    }

    componentDidMount() {
        this.props.onRef(this);
    }

    // 关闭弹窗
    handleCancel = () => {
        this.setState({visible: false}, () => {
            if (this.state.flag === 'compile') {
                this.resetFields();
            }
        })
    }

    // 打开弹窗
    showModal = (obj) => {
        this.setState({
            visible: true,
            userInfo: obj.userInfo ? obj.userInfo : {},
            flag: obj&&obj.flag ? obj.flag : 'add',
        }, () => {
            if (this.state.flag === 'compile') {
                this.resetFields();
            }
        })
    }

    // 保存
    handleCreate = () => {
        let { flag } = this.state;
        if (flag === 'compile') {
            this.handelSave('06010015', '编辑');
        } else {
            this.handelSave('06010014', '新增');
        }
    }

    handelSave = (code, msg) => {
        const { form: { validateFields } } = this.props;
        validateFields((errors, values) => {
            // 验证成功
            if (errors === null) {
                let data = {
                    params: []
                }
                let obj = JSON.parse(JSON.stringify(values));
                // 当增加时点击复制把ID值传给后天然后后台根据ID值去复制权限，编辑时则根据ID去保存修改
                if (msg === '编辑') {
                    obj.ID = this.state.userInfo.ID
                }

                // 处理需要传递的obj
                data.params.push(obj);
                $http.post('urlADS', this, {
                    code: code,
                    data: data,
                    success: function (res) { 
                        if (+res.errorCode === 0) {
                            // 成功提示
                            message.success(msg + '成功');
                            // 新增或编辑成功重置form表单
                            this.resetFields();
                            // 关闭弹框
                            this.handleCancel();
                            // 刷新列表数据
                            this.props.handleQuery();
                        }
                    }
                })
            }
        });
    }

    // 重置表单
    resetFields() {
        this.props.form.resetFields();
    }

    render() {
        const { getFieldDecorator } = this.props.form;
        let {userInfo, visible, flag} = this.state;
        const formItemInput  = {
            labelCol: {span: 10},
            wrapperCol: {span: 14}
        };

        
        // 数据集类型
        let dataSetTypeOption = this.props.dataSetTypeSelectData&&this.props.dataSetTypeSelectData.map((item, index) => {
            return (<Option key={index} value={item.ID} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })
        return (
            <Modal
                visible={visible}
                title={flag === 'add' ? '新增基础数据集' : '编辑基础数据集'}
                okText="保存"
                onCancel={this.handleCancel}
                onOk={this.handleCreate}
                width={600}
            >
                <Form className="form-item-align">
                    <Row>
                        <Col span={12}>
                            <Form.Item label="数据集代码：" {...formItemInput}>
                                {getFieldDecorator('code', {
                                    initialValue: userInfo.code ? userInfo.code: undefined,
                                    rules: [{ required: true, message: '数据集代码不能为空' }]
                                })(
                                    <Input placeholder="请输入" />
                                )}
                            </Form.Item>
                        </Col>
                        <Col span={12}>
                            <Form.Item label="数据集名称：" {...formItemInput}>
                                {getFieldDecorator('descripts', {
                                    initialValue: userInfo.descripts ? userInfo.descripts: undefined,
                                    rules: [{ required: true, message: '数据集名称不能为空' }]
                                })(
                                    <Input placeholder="请输入" />
                                )}
                            </Form.Item>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={12}>
                            <Form.Item label="服务代码：" {...formItemInput}>
                                {getFieldDecorator('serviceCode', {
                                    initialValue: userInfo.serviceCode ? userInfo.serviceCode: undefined,
                                    rules: [{ required: true, message: '服务代码不能为空' }]
                                })(
                                    <Input placeholder="请输入" />
                                )}
                            </Form.Item>
                        </Col>
                        <Col span={12}>
                            <Form.Item label="输出代码：" {...formItemInput}>
                                {getFieldDecorator('resultCode', {
                                    initialValue: userInfo.resultCode ? userInfo.resultCode: undefined,
                                    rules: [{ required: true, message: '输出代码不能为空' }]
                                })(
                                    <Input placeholder="请输入" />
                                )}
                            </Form.Item>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={12}>
                            <Form.Item label="输出描述：" {...formItemInput}>
                                {getFieldDecorator('resultDescripts', {
                                    initialValue: userInfo.resultDescripts ? userInfo.resultDescripts: undefined,
                                    rules: [{ required: true, message: '输出描述不能为空' }]
                                })(
                                     <Input placeholder="请输入" />
                                )}
                            </Form.Item>
                        </Col>
                        <Col span={12}>
                            <Form.Item label="数据集字典类型：" {...formItemInput}>
                                {getFieldDecorator('dataSetType', {
                                    initialValue: userInfo.dataSetType ? userInfo.dataSetType: undefined,
                                    rules: [{ required: true, message: '数据集字典类型不能为空' }]
                                })(
                                    <Select
                                        showSearch optionFilterProp="seatchprop" allowClear
                                        style={{width: '100%'}} 
                                        placeholder="请选择"
                                    >
                                        {dataSetTypeOption}
                                    </Select>
                                )}
                            </Form.Item>
                        </Col>
                    </Row>
                </Form>
          </Modal>
        )
    }
};
const NewDictionaryForm = Form.create()(NewDictionary);
export default NewDictionaryForm;