import React from "react";
import {inject, observer} from "mobx-react";
import {toJS} from "mobx";
import * as API from "../../apis/DataShowService";
import {
    WeaTools,
    WeaRightMenu,
    WeaDialog,
    WeaLocaleProvider,
} from "ecCom";
import {Button} from "antd";
import SqlTextForm from "./SqlTextForm";

const getLabel = WeaLocaleProvider.getLabel;
let Base64 = WeaTools.Base64;
const store = ["dataShowStore"];

@inject(...store)
@observer
export default class SqlTestDialog extends React.Component {

    constructor(props) {
        super(props);
    }

    getRightMenu =() =>{

        let btn = [];
        return btn;
    };

    //检查返回值
    checkValuePROC = (type) => {
        const {dataShowStore} = this.props;
        const {sqlTextForm, newForm,source} = dataShowStore;
        const {form} = sqlTextForm;
        const {dataShowFormHiddenFields, WebserviceMethodParameterDatas, WeaTableEdit_MergedFieldData} = toJS(dataShowStore.status);

        let id = dataShowFormHiddenFields.id || "";

        form.validateForm().then(f => {
            if (f.isValid) {
                //合并hiddenfield参数
                let sqltextValue = newForm.getFormParams().sqltext ? newForm.getFormParams().sqltext : '';
                let sqltext = Base64.encode(sqltextValue);
                let searchByIdValue = newForm.getFormParams().searchById ? newForm.getFormParams().searchById : '';
                let searchById = Base64.encode(searchByIdValue);
                let formParams = Object.assign({}, newForm.getFormParams(), {
                    sqltext: sqltext,
                    searchById: searchById
                });
                let params = Object.assign(dataShowFormHiddenFields, formParams);
                params.operator = "save";
                if (source) {
                    params.typename = source;
                }

                let datas = WeaTableEdit_MergedFieldData && WeaTableEdit_MergedFieldData.map(data => {
                    let transSQL = data.new_transql ? data.new_transql : '';
                    return Object.assign({}, data, {new_transql: Base64.encode(transSQL)});
                });
                params.dtinfo_MergedFieldData = JSON.stringify(datas);
                params.dtinfo_WebserviceMethodParameterDatas = JSON.stringify(WebserviceMethodParameterDatas);

                //dataShowStore.setState({showsSqlTextFormDialog: false});
                dataShowStore.setState({showTextFormButton: true});
                let checkParams = {
                    showname: formParams.showname,
                    sqlTextForm: JSON.stringify(form.getFormParams()),
                };
                API.saveFormParam(checkParams).then(result => {
                    if (result.status === '1') {
                        //关闭sql表单,不是重置
                        //dataShowStore.initSqlTestForm();
                        dataShowStore.setState({
                            showsSqlTextFormDialog: false,
                            showsSqlTextFormDialog2:false,
                        });

                        API.getIndexTest(id).then(result=>{
                            if (result.ext === 'true') {
                                if (result.status) {
                                    //获取测试的form数据
                                    dataShowStore.getDataShowTestForm(newForm.getFormParams(),dataShowFormHiddenFields, WebserviceMethodParameterDatas,type);
                                } else {
                                    dataShowStore.setState({
                                        testInitDatas: result.data.base,
                                    });
                                    console.log("type================" + type);
                                    if (type == 'INDEX_TEST') {
                                        dataShowStore.setState({
                                            showIndexTestDialog: true,
                                        });
                                    }
                                    if (type == 'BTN_TEST') {
                                        dataShowStore.setState({
                                            showIndexTestDialog2: true,
                                        });
                                    }
                                }
                            } else {
                                //异常处理

                            }
                        });
                    }
                });

            } else {
                f.showErrors();
                this.setState({date: new Date()});
            }
        });
    };

    render() {
        const {dataShowStore,showSqlDialog} = this.props;
        const{type} = dataShowStore.status;
        let buttons = [
            <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@lr22nl@integrationDatashowPreviewDialogButtonNextStep`} type="primary" onClick={() => {this.checkValuePROC(type)}}>{getLabel('1402','下一步')}</Button>,
        ];

        return (
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@bdidvr`}
                title={getLabel('510613', '手动输入表单参数')}
                moduleName="integration"
                visible={showSqlDialog}
                hasScroll={true}
                style={{width: 780, height: 520}}
                onCancel={dataShowStore.initSqlTestForm}
                buttons={buttons}
                moreBtn={{
                    datas : this.getRightMenu(),
                    collectParams: []
                }}
            >
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@5qf51e`}>
                    <SqlTextForm ecId={`${this && this.props && this.props.ecId || ''}_SqlTextForm@ll2bdy`}/>
                </WeaRightMenu>

            </WeaDialog>
        )
    }
}