// 添加模板目录树
import React from 'react';
import {Form, Input, Row, Col, Modal, message } from 'antd';
import { $http } from 'containers/config/https';

class AddTemplateDirectory extends React.Component {
    constructor() {
        super();
        this.state = {
            userInfo: {},
            treeSelectData: {},
            flag: 'add',
        }
    }

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

    // 关闭弹窗
    handleCancel = () => {
        this.setState({visible: false})
    }

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

    // 保存
    handleSave = () => {
        let { flag } = this.state;
        if (flag === 'compile') {
            this.handelInterface('06020005', '编辑');
        } else {
            this.handelInterface('06020004', '新增');
        }
    }

    handelInterface = (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.treeSelectData.ID
                } else {
                    obj.parentID = this.state.treeSelectData&&this.state.treeSelectData.ID ? this.state.treeSelectData.ID : '1'
                }

                // 处理需要传递的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 {visible, flag, userInfo} = this.state;
        const formItemInput  = {
            labelCol: {span: 12},
            wrapperCol: {span: 12}
        };
        return (
            <Modal
                visible={visible}
                title={flag === 'add' ? '新增模板目录树' : '编辑模板目录树'}
                okText="保存"
                onCancel={this.handleCancel}
                onOk={this.handleSave}
                width={600}
            >
                <Form>
                    <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>
                </Form>
            </Modal>
        )
    }
};
const AddTemplateDirectoryForm = Form.create()(AddTemplateDirectory);
export default AddTemplateDirectoryForm;