import React from 'react';
import { connect } from 'dva';
import { Button, } from 'antd';
import PageConfigShowBaseComponent from "../Lib/PageConfigShowBaseComponent";
import { guid, extendRule } from "../../../utils/common";
import ColumnFilterShowPage from './ColumnFilterShowPage';

class ColumnShowPage extends PageConfigShowBaseComponent {
  constructor(props) {
    super(props);
    this.state = {
      data: {},
      paginationIndex: 1,
      paginationTotalCount: 0,
      rowData: {},
      visible: false,
      selectedRowKeys: []
    };
  };
  componentDidMount() {
    super.componentDidMount();
  };

  onEditVisible = (data) => {
    const controlType = data ? data.CONTROL_TYPE : 0;
    const visible = {};

    visible['INPUT_DATA_API'] = controlType === 9 || controlType === 24;
    visible['INPUT_ID_FIELD'] = controlType === 9 || controlType === 24|| controlType === 25;
    visible['INPUT_SAVE_FIELD'] = controlType === 9 || controlType === 24;
    visible['INPUT_NAV_FIELD'] = controlType === 9 || controlType === 24;
    visible['INPUT_FILTER_FIELD'] = controlType === 9 || controlType === 24;
    visible['INPUT_LABEL_FIELD'] = controlType === 9 || controlType === 24;
    visible['INPUT_SHOW_FIELD'] = controlType === 9 || controlType === 24;
    visible['INPUT_SORT_FIELD'] = controlType === 9 || controlType === 24;
    visible['INPUT_SORT_ORDER'] = controlType === 9 || controlType === 24;
    visible['INPUT_FILTER_ORG_TYPE'] = controlType === 9 || controlType === 24;
    return visible;
  };

  onEditChange = (params) => {
    const { field, value, onSave, onSetVisible } = params;
    if (field === 'CONTROL_TYPE') {
      onSetVisible();
    }
  };
  onRowDropDownFilter(params) {
    this.onEditFilter(params);
  }

  onEditFilter = (params) => {
    const { params: selectParams, colConfig, thisRef } = params;
    if (colConfig.field === 'Nav_PageTablePanel.LABEL') {
      extendRule(selectParams, 'PAGE_TABLE_ID', 1, (thisRef.props.pageTableId ? thisRef.props.pageTableId :
        thisRef.state.editConfig.pageTableId))
    }
  };

  onGetTableConfig() {
    const tableConfig = {
      sort: 'NUM',
      order: 0,
      api: 'PF/Column/OrderPaged',
      checked: true,
      columns: [
        { controlType: 4, field: 'IS_DEFAULT', label: '是否显示' },
        { controlType: 3, field: 'CONTROL_TYPE', label: '控件类型', data: { enumName: 'PFColumnControlTypeEnum' }, width: 160 },
        { controlType: 1, field: 'LABEL', label: '显示名称', isRequire: true, width: 120 },
        { controlType: 1, field: 'FIELD_NAME', label: '字段名称', width: 300 },
        { controlType: 1, field: 'FORMAT', label: '格式化函数', width: 160 },
        { controlType: 1, field: 'WIDTH', label: '列宽' },
        {
          controlType: 9, field: 'Nav_PageTablePanel.LABEL', label: '所属区域', width: 100, data: {
            api: 'PF/PageTablePanel/OrderPaged',
            showField: 'LABEL',
            labelField: 'LABEL',
            filterField: 'LABEL',
            navField: 'LABEL,ID',
            saveField: 'Nav_PageTablePanel.LABEL,PAGE_TABLE_PANEL_ID',
            idField: 'PAGE_TABLE_PANEL_ID',
          }
        },
        { controlType: 3, field: 'FOOTER_SUM_TYPE', label: '页脚汇总类型', data: { enumName: 'ColumnFooterSumTypeEnum' } },
        { controlType: 1, field: 'CUSTOM_PARAMS', label: '自定义参数', promptInfo: '自定义参数  格式：参数名称=参数值  多个参数时以||分割' },
        { controlType: 1, field: 'NUM', label: '顺序索引' },
      ],
      deleteApi: 'PF/Column/Delete',
      batchDeleteApi: "PF/Column/BatchDelete",
      editConfig: {
        data: {
          ORG_ID: this.props.orgId,
          PAGE_TABLE_ID: this.props.pageTableId,
          NUM: 0,
          IS_DEFAULT: true,
          CONTROL_TYPE: 0,
        },
        columns: [
          { controlType: 1, field: 'LABEL', label: '显示名称', isRequire: true, width: 120 },
          { controlType: 1, field: 'FIELD_NAME', label: '字段名称', width: 300 },
          { controlType: 4, field: 'IS_DEFAULT', label: '是否显示' },
          { controlType: 4, field: 'IS_RULE', label: '是否权限控制' },
          { controlType: 3, field: 'CONTROL_TYPE', label: '控件类型', data: { enumName: 'PFColumnControlTypeEnum' } },
          { controlType: 1, field: 'ENUM', label: '枚举名称', width: 200 },
          { controlType: 1, field: 'FORMAT', label: '格式化函数', width: 160 },
          { controlType: 1, field: 'WIDTH', label: '列宽' },
          {
            controlType: 9, field: 'Nav_PageTablePanel.LABEL', label: '所属区域', data: {
              api: 'PF/PageTablePanel/OrderPaged',
              showField: 'LABEL',
              labelField: 'LABEL',
              filterField: 'LABEL',
              navField: 'LABEL,ID',
              saveField: 'Nav_PageTablePanel.LABEL,PAGE_TABLE_PANEL_ID',
              idField: 'PAGE_TABLE_PANEL_ID',
            }
          },
          { controlType: 1, field: 'INPUT_DATA_API', label: 'API' },
          { controlType: 1, field: 'INPUT_ID_FIELD', label: '关联ID字段', promptInfo: '关联ID字段 配置保存主键ID的字段名称' },
          {
            controlType: 1, field: 'INPUT_SAVE_FIELD', label: '保存字段', promptInfo: '保存字段（多个字段,分隔）', span: { span: 24 }, formItemLayout: {
              labelCol: { span: 3 },
              wrapperCol: { span: 17 },
            }
          },
          {
            controlType: 1, field: 'INPUT_NAV_FIELD', label: '导航字段', promptInfo: '导航字段（多个字段,分隔）', span: { span: 24 }, formItemLayout: {
              labelCol: { span: 3 },
              wrapperCol: { span: 17 },
            }
          },
          { controlType: 1, field: 'INPUT_FILTER_FIELD', label: '过滤字段', promptInfo: '过滤字段（多个字段,分隔）' },
          { controlType: 1, field: '', label: '显示字段', promptInfo: '显示字段（多个字段,分隔）' },
          { controlType: 1, field: 'INPUT_SHOW_FIELD', label: '下拉显示字段', promptInfo: '下拉显示字段（多个字段,分隔）' },
          { controlType: 1, field: 'INPUT_SORT_FIELD', label: '排序字段', promptInfo: '排序字段(配置关联表的字段，多字段用,号分割）' },
          { controlType: 1, field: 'INPUT_SORT_ORDER', label: '排序方式', promptInfo: '排序方式(配置关联表的字段，多字段用,号分割）' },
          { controlType: 3, field: 'INPUT_FILTER_ORG_TYPE', label: '组织过滤类型', data: { enumName: 'PFFilterOrgTypeEnum' } },

          { controlType: 1, field: 'SHOW_FORM_CODE', label: '查询表单编号' },
          { controlType: 1, field: 'SHOW_ID_FIELD', label: '查询ID字段名称' },
          { controlType: 3, field: 'FOOTER_SUM_TYPE', label: '页脚汇总类型', data: { enumName: 'ColumnFooterSumTypeEnum' } },
          { controlType: 1, field: 'CUSTOM_PARAMS', label: '自定义参数', promptInfo: '自定义参数  格式：参数名称=参数值  多个参数时以||分割' },
          { controlType: 1, field: 'NUM', label: '顺序索引' },
          { controlType: 4, field: 'NO_SORT', label: '不可排序' },
        ],
        getApi: 'PF/Column/Get',
        saveApi: 'PF/Column/Update',
        pageTableId: this.props.pageTableId,
        onChange: this.onEditChange,
        onVisible: this.onEditVisible,
        onFilter: this.onEditFilter,
      },
      rule: {
        rules: [
          { field: 'PAGE_TABLE_ID', operator: 1, value: this.props.pageTableId }
        ]
      },
      btns: [
        { id: guid(), btnType: 4, code: 'delete' },
      ],
      rowBtns: [
        { id: guid(), btnType: 7, code: 'edit' },
        { id: guid(), btnType: 4, code: 'delete' },
        { id: guid(), btnType: 7, code: 'filter' },
      ]
    };
    return [tableConfig];
  }

  onTableBtnParam(params) {
    const { btnConfig, record } = params ? params : {};
    if (btnConfig.code === 'filter') {
      return {
        content: <ColumnFilterShowPage orgId={this.props.orgId} columnId={record.ID}>
          <Button icon={'info'} type={'default'} size={'small'} title={'过滤'} />
        </ColumnFilterShowPage>
      };
    }
    return super.onTableBtnParam(params);
  }

}


export default connect(({ login, app, loading, custom }) => ({ login, app, loading, custom }))(ColumnShowPage);
