import React from 'react'
import { connect } from 'dva'
import { Button } from 'antd'
import TableParamShowPage from './TableParamShowPage'
import ColumnShowPage from './ColumnShowPage'
import BtnShowPage from '../Common/BtnShowPage'
import TablePanelShowPage from './TablePanelShowPage'
import PageConfigShowBaseComponent from "../Lib/PageConfigShowBaseComponent"
import { guid, extendRule } from "../../../utils/common"

class TableShowPage extends PageConfigShowBaseComponent {
  constructor (props) {
    super(props)
    this.state = {
      data: {},
      paginationIndex: 1,
      paginationTotalCount: 0,
      rowData: {},
      visible: false
    }
  };
  onRowDropDownFilter (params) {
    this.onEditFilter(params)
  }

  onEditFilter = (params) => {
    const { params: selectParams, colConfig } = params
    if (colConfig.field === 'Nav_ParamScheme.NAME') {
      extendRule(selectParams, 'ENABLE_STATUS', 1, 0)
    }
  };
  onGetTableConfig () {
    const tableConfig = {
      sort: 'NUM',
      order: 0,
      api: 'PF/PageTable/OrderPaged',
      columns: [
        { field: 'LABEL_NAME', label: '显示名称', isRequire: true },
        { field: 'CODE', label: '编号', isRequire: true },
        { field: 'TABLE_NAME', label: '表名' },
        { field: 'DATA_TABLE', label: '数据库表名' },
        { field: 'SHOW_CHECK', label: '是否显示选择列', isBool: true },
        { field: 'IS_SHOW_ROW_NO', label: '显示序号', isBool: true },
        { field: 'API_URL', label: 'API接口' },
        { field: 'SEND_TYPE', label: '发送方式' },
        { field: 'SORT', label: '排序字段' },
        { field: 'ORDER', label: '排序方式', data: { options: '升序:0,降序:1' } },
        { field: 'RELATION_FIELD', label: '与父表关联字段' },
        { field: 'DEL_API_URL', label: '删除API接口' },
        { field: 'BATCH_DEL_API_URL', label: '批次API接口' },
        { field: 'CLASS_FUNC_NAME', label: '自定义样式函数' },
        { field: 'FOOTER_SUM_TYPE', label: '页脚汇总类型', data: { enumName: 'TableFooterSumTypeEnum' } },
        { field: 'CUSTOM_PARAMS', label: '自定义参数', promptInfo: '自定义参数  格式：参数名称=参数值  多个参数时以||分割' },
        { field: 'NUM', label: '顺序索引' },
      ],
      deleteApi: 'PF/PageTable/Delete',
      editConfig: {
        data: {
          ORG_ID: this.props.orgId,
          PAGE_FORM_ID: this.props.formId,
          NUM: 0,
          SEND_TYPE: 'Post',
          ORDER: 0,
          PARENT_ID: this.props.parentId
        },
        columns: [
          { controlType: 1, field: 'LABEL_NAME', label: '显示名称', isRequire: true },
          { controlType: 1, field: 'CODE', label: '编号', isRequire: true },
          { controlType: 1, field: 'TABLE_NAME', label: '表名' },
          { controlType: 1, field: 'DATA_TABLE', label: '数据库表名' },
          { controlType: 4, field: 'SHOW_CHECK', label: '显示选择列' },
          { controlType: 1, field: 'MAX_SELECT', label: '最多可选数', promptInfo: '多选最多可选择个数' },
          { controlType: 4, field: 'IS_SHOW_ROW_NO', label: '显示序号' },
          { controlType: 1, field: 'API_URL', label: 'API接口' },
          { controlType: 1, field: 'SEND_TYPE', label: '发送方式' },
          { controlType: 1, field: 'SORT', label: '排序字段' },
          { controlType: 3, field: 'ORDER', label: '排序方式', data: { options: '升序:0,降序:1' } },
          { controlType: 1, field: 'SORTS', label: '多字段排序字段', promptInfo: '多个使用逗号隔开' },
          { controlType: 1, field: 'ORDERS', label: '多字段排序方式', promptInfo: '（0：升序，1：降序） 多个使用逗号隔开' },
          { controlType: 1, field: 'POSITION', label: '分页位置', promptInfo: 'top|bottom|both（默认bottom)' },
          { controlType: 1, field: 'WIDTH', label: '表格宽度' },
          { controlType: 1, field: 'HEIGTH', label: '表格高度' },
          { controlType: 1, field: 'CLASS_FIELD', label: '样式字段' },
          { controlType: 1, field: 'DEL_API_URL', label: '删除API接口' },
          { controlType: 1, field: 'BATCH_DEL_API_URL', label: '批次API接口' },
          { controlType: 1, field: 'RELATION_FIELD', label: '与父表关联字段' },
          { controlType: 1, field: 'PARENT_RELATION_FIELD', label: '父表关联字段', promptInfo: '子表关联父表，父表字段名称 默认为ID' },
          {
            controlType: 9, field: 'Nav_ParamScheme.NAME', label: '实体参数模板', data: {
              api: 'PF/ParamScheme/OrderPaged',
              navField: 'NAME,ID',
              saveField: 'Nav_ParamScheme.NAME,PARAM_SCHEME_ID',
              idField: 'PARAM_SCHEME_ID',
            }
          },
          { controlType: 3, field: 'FILTER_ORG_TYPE', label: '过滤组织类型', data: { enumName: 'PFFilterOrgTypeEnum' } },
          { controlType: 1, field: 'CLASS_FUNC_NAME', label: '自定义样式函数' },
          { controlType: 3, field: 'FOOTER_SUM_TYPE', label: '页脚汇总类型', data: { enumName: 'TableFooterSumTypeEnum' } },
          { controlType: 1, field: 'CUSTOM_PARAMS', label: '自定义参数', promptInfo: '自定义参数  格式：参数名称=参数值  多个参数时以||分割' },
          { controlType: 1, field: 'NUM', label: '顺序索引' },
          { controlType: 4, field: 'IS_SHOW_CARD', label: '卡片显示' },
          { controlType: 1, field: 'PIC_CODE_FIELD', label: '卡片图片配置' },
          { controlType: 4, field: 'NO_SELECT_FIELD', label: '禁止过滤参数' },
          { controlType: 1, field: 'TITLE', label: '导出文件标题' },
          { controlType: 4, field: 'IGNORE_ORG_RULE', label: '忽略组织权限' }
        ],
        getApi: 'PF/PageTable/Get',
        saveApi: 'PF/PageTable/Update',
        onFilter: this.onEditFilter,
      },
      rule: {
        rules: [
          { field: 'PAGE_FORM_ID', operator: 1, value: this.props.formId },
          { field: 'PARENT_ID', operator: 1, value: this.props.parentId }
        ]
      },
      rowBtns: [
        { id: guid(), btnType: 7, code: 'edit' },
        { id: guid(), btnType: 4, code: 'delete' },
        { id: guid(), btnType: 7, code: 'column' },
        { id: guid(), btnType: 7, code: 'table' },
        { id: guid(), btnType: 7, code: 'tableParam' },
        { id: guid(), btnType: 7, code: 'btn' },
        { id: guid(), btnType: 7, code: 'panel' },
      ]
    }
    return [tableConfig]
  }

  onTableBtnParam (params) {
    const { btnConfig, record } = params ? params : {}
    if (btnConfig.code === 'column') {
      return {
        content: <ColumnShowPage orgId={this.props.orgId} pageTableId={record.ID}>
          <Button icon={'bars'} type={'default'} size={'small'} title={'字段'} />
        </ColumnShowPage>
      }
    }
    else if (btnConfig.code === 'table') {
      return {
        content: <TableShowPage formId={this.props.formId} orgId={this.props.orgId} parentId={record.ID} dispatch={this.props.dispatch} app={this.props.app} login={this.props.login} custom={this.props.custom}  >
          <Button icon={'folder-add'} type={'default'} size={'small'} title={'管理'} />
        </TableShowPage>
      }
    }
    else if (btnConfig.code === 'tableParam') {
      return {
        content: <TableParamShowPage orgId={this.props.orgId} pageTableId={record.ID}>
          <Button icon={'info'} type={'default'} size={'small'} title={'过滤'} />
        </TableParamShowPage>
      }
    }
    else if (btnConfig.code === 'btn') {
      return {
        content: <BtnShowPage orgId={this.props.orgId} tableId={record.ID} btnPageType={0}>
          <Button icon={'star'} type={'default'} size={'small'} title={'按钮'} />
        </BtnShowPage>
      }
    }
    else if (btnConfig.code === 'panel') {
      return {
        content: <TablePanelShowPage orgId={this.props.orgId} pageTableId={record.ID}>
          <Button icon={'info'} css={'default'} size={'small'} title={'区域'} />
        </TablePanelShowPage>
      }
    }
    return super.onTableBtnParam(params)
  }


  getRowOtherBtns ({ record }) {
    return <>
      <ColumnShowPage orgId={this.props.orgId} pageTableId={record.ID}>
        <Button icon={'bars'} type={'default'}>字段</Button>
      </ColumnShowPage>

      <TableShowPage formId={this.props.formId} orgId={this.props.orgId} parentId={record.ID} dispatch={this.props.dispatch} app={this.props.app} login={this.props.login} custom={this.props.custom}  >
        <Button icon={'folder-add'} type={'default'}>管理</Button>
      </TableShowPage>

      <TableParamShowPage orgId={this.props.orgId} pageTableId={record.ID}>
        <Button icon={'info'} type={'default'}>过滤</Button>
      </TableParamShowPage>

      <BtnShowPage orgId={this.props.orgId} tableId={record.ID} btnPageType={0}>
        <Button icon={'star'} type={'default'}>按钮</Button>
      </BtnShowPage>
    </>
  }

}

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