import React from 'react';
import { connect } from 'dva';
import { Button, Row, Col, Form, Input, message } from 'antd';

import EditBaseComponent from "../../../baseComponents/EditBaseComponent/index.js";
import { initFilter, extendRule } from '../../../utils/common.js';

const { TextArea } = Input;
class FormConfigByNamePage extends EditBaseComponent {
  constructor(props) {
    super(props);
    this.state = {
      data: {},
      btns: [],
      title: '',
      historyData: [],
      remark: '',
    };
  };
  /*
      {params,colConfig,thisRef}
     */
  onHeadDropDownFilter(params) {
    const { params: tempParams, colConfig, thisRef } = params;
    if (colConfig.field === 'Nav_Menu.NAME') {
      extendRule(tempParams, 'IS_LEAF', 1, 'false');
      extendRule(tempParams, 'PARENT_ID', 2, '');
    }
  }
  onLoadData() {
    super.onLoadData();
    const columns = [
      { controlType: 3, field: 'ConfigType', label: '类型', data: { enumName: 'PFInitFormConfigTypeEnum' } },
      { controlType: 1, field: 'TableName', label: '表名', isRequire: true },
      {
        controlType: 9, field: 'Nav_Menu.NAME', label: '父级菜单', data: {
          api: 'PF/Menu/OrderPaged',
          showField: 'NAME',
          labelField: 'NAME',
          filterField: 'NAME',
          navField: 'NAME,ID',
          saveField: 'Nav_Menu.NAME,MenuId',
          idField: 'MenuId',
        }
      },
      { controlType: 1, field: 'MenuName', label: '菜单名称' },
      { controlType: 1, field: 'MenuIcon', label: '菜单图标' },
    ]
    this.setEditConfig({
      editConfig: {
        columns,
      },
    });
    this.setState({ btnLoading: false });
    this.setState({
      data: {
        TableName: null,
        ConfigType: 0,
      },
    })
  }


  getSqlsByFormId = () => {
    const { login, dispatch } = this.props;
    const { data, } = this.state;
    if (!data || !data.TableName) {
      message.error('请输入表名');
      return;
    }

    var json = initFilter(login.OrgId, data.TableName, '', 1, 1, login.userId,
      data.ConfigType, data.MenuId, data.MenuName, data.MenuIcon);
    this.setState({ btnLoading: true });
    dispatch({
      type: 'PFForm/initFormConfigByName',
      payload: json,
      onComplete: (ret) => {
        if (ret) {
          message.success('执行成功');
          this.setState({ data: {} });
        }
        setTimeout(() => { this.setState({ btnLoading: false }) }, 2000);
      }
    });

  }
  getSqlsByFormId2 = () => {
    const { login, dispatch } = this.props;
    const { data, } = this.state;
    if (!data || !data.TableName) {
      message.error('请输入表名');
      return;
    }

    var json = initFilter(login.OrgId, data.TableName, '', 1, 1, login.userId,
      data.ConfigType, data.MenuId, data.MenuName, data.MenuIcon, true);
    this.setState({ btnLoading: true });
    dispatch({
      type: 'PFForm/initFormConfigByName',
      payload: json,
      onComplete: (ret) => {
        if (ret) {
          message.success('执行成功');
          this.setState({ data: {} });
        }
        setTimeout(() => { this.setState({ btnLoading: false }) }, 2000);
      }
    });

  }

  render() {
    const { btnLoading, sendInfo, data } = this.state;
    const headColumnConfig = this.getEditConfig();
    const columns = headColumnConfig ? headColumnConfig.columns : [];

    return (
      <div>
        <Form>
          <Row style={{ marginTop: '20px' }}>
            {
              this.createControls(columns, data)
            }
          </Row>
          <Row style={{ marginTop: '20px' }}>
            <Col style={{ textAlign: 'right' }}>
              <Button type={'primary'} icon={'eye'} loading={btnLoading} onClick={this.getSqlsByFormId} >确认</Button>
              <Button style={{ margin: "0 20px" }} type={'primary'} icon={'eye'} loading={btnLoading} onClick={this.getSqlsByFormId2} >保存并发送</Button>
            </Col>
          </Row>
        </Form>
      </div>
    );
  }
}
export default connect(({ login, loading, custom, app }) => ({ login, loading, custom, app }))(Form.create()(FormConfigByNamePage));
