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

class EditShowPage 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/PageEdit/OrderPaged',
      columns: [
      ],
      deleteApi: 'PF/PageEdit/Delete',
      editConfig: {
        data: {
          'ORG_ID': this.props.orgId,
          'PAGE_FORM_ID': this.props.formId,
          'NUM': 0,
          'SEND_TYPE': '2',
          'QUERY_SEND_TYPE': '2',
          'PARENT_ID': this.props.parentId
        },
        columns: [
          { controlType: 1, field: 'LABEL_NAME', label: '显示名称', isRequire: true },
          { controlType: 1, field: 'CODE', label: '编码', isRequire: true },
          { controlType: 1, field: 'EDIT_NAME', label: '数据库表', isRequire: true },
          { controlType: 1, field: 'CUSTOM_PARAMS', label: '自定义参数', promptInfo: '自定义参数  格式：参数名称=参数值  多个参数时以||分割' },

          //编辑表身
          { controlType: 1, field: 'RELATION_FIELD', label: '关联字段', isRequire: true },
          { controlType: 1, field: 'NAV_PROPERTY', label: '导航属性', isRequire: true },
          { controlType: 1, field: 'WIDTH', label: '表格宽度' },
          { controlType: 1, field: 'HEIGTH', label: '表格高度' },
          { controlType: 4, field: 'IS_DETAIL_REQUIRED', label: '是否必填' },
          { controlType: 1, field: 'SORT_FIELDS', label: '排序字段', promptInfo: '多个使用逗号隔开' },
          { controlType: 1, field: 'SORT_ORDERS', label: '排序方式', promptInfo: '多个使用逗号隔开 0-升序 1-降序' },
          { controlType: 1, field: 'NAV_ROW_CLASS_FILTER', label: '行样式', promptInfo: '格式：字段1,条件1,值1,样式1|字段2,条件2,值2,样式2' },
          { controlType: 4, field: 'IS_SUB_ADD_BTN_DISABLED', label: '是否禁用新增按钮' },
          { controlType: 4, field: 'IS_SUB_DELETE_BTN_DISABLED', label: '是否禁用删除按钮' },
          { controlType: 4, field: 'SHOW_CHECK', label: '显示选择列' },
          { controlType: 4, field: 'IS_PAGE', label: '是否分页' },
          { controlType: 1, field: 'DEFAULT_PAGE_SIZE', label: '默认分页条数' },

          //编辑表头
          { controlType: 1, field: 'API_URL', label: '保存API路径', isRequire: true },
          { controlType: 4, field: 'IS_CONTAINS_NAV', label: '保存包含导航' },
          { controlType: 1, field: 'QUERY_API_URL', label: '查询API路径', isRequire: true },
          { controlType: 1, field: 'BATCH_UPDATE_API', label: '批量保存API' },
          { controlType: 1, field: 'BATCH_QUERY_API', label: '批量查询API' },
          { controlType: 1, field: 'CODE_FIELD_NAME', label: '编号字段名称' },
          { controlType: 3, field: 'CODE_RULE_TYPE', label: '编码规则类型', data: { enumName: 'PFCodeRuleType' } },
          { controlType: 3, field: 'EDIT_PAGE_ENTITY_ORG_TYPE', label: '实体组织类型', data: { enumName: 'PFEntityOrgTypeEnum' } },

          {
            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: 1, field: 'NUM', label: '顺序索引' },

          {
            controlType: 1, field: 'PARENT_COPY_FIELD', label: '父节点复制字段', promptInfo: '父节点复制字段（多个字段,分隔）', span: { span: 24 }, formItemLayout: {
              labelCol: { span: 3 },
              wrapperCol: { span: 17 },
            }
          },
          {
            controlType: 1, field: 'CHILD_SET_FIELD', label: '子节点赋值字段', promptInfo: '子节点赋值字段（多个字段,分隔）', span: { span: 24 }, formItemLayout: {
              labelCol: { span: 3 },
              wrapperCol: { span: 17 },
            }
          },

          {
            controlType: 1, field: 'SUB_TABLE_CONDITION', label: '子表显示条件', promptInfo: 'tableCode,fieldName,op,value 不填则默认全部显示，多个以|隔开', span: { span: 24 }, formItemLayout: {
              labelCol: { span: 3 },
              wrapperCol: { span: 17 },
            }
          },
        ],
        getApi: 'PF/PageEdit/Get',
        saveApi: 'PF/PageEdit/Update',
        onVisible: this.onEditVisible,
        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: 'editPage' },
      ]
    }
    if (this.props.parentId) {//编辑表身
      tableConfig.columns = [
        { field: 'LABEL_NAME', label: '显示名称', isRequire: true },
        { field: 'CODE', label: '编码', isRequire: true },
        { field: 'EDIT_NAME', label: '数据库表', isRequire: true },
        { field: 'CUSTOM_PARAMS', label: '自定义参数', promptInfo: '自定义参数  格式：参数名称=参数值  多个参数时以||分割' },

        { field: 'RELATION_FIELD', label: '关联字段', isRequire: true },
        { field: 'NAV_PROPERTY', label: '导航属性', isRequire: true },
        { field: 'WIDTH', label: '表格宽度' },
        { field: 'HEIGTH', label: '表格高度' },
        { field: 'IS_DETAIL_REQUIRED', label: '是否必填', isBool: true },
        { field: 'SORT_FIELDS', label: '排序字段', promptInfo: '多个使用逗号隔开' },
        { field: 'SORT_ORDERS', label: '排序方式', promptInfo: '多个使用逗号隔开 0-升序 1-降序' },
        { field: 'NAV_ROW_CLASS_FILTER', label: '行样式', promptInfo: '格式：字段1,条件1,值1,样式1|字段2,条件2,值2,样式2' },
        { field: 'IS_SUB_ADD_BTN_DISABLED', label: '是否禁用新增按钮', isBool: true },
        { field: 'IS_SUB_DELETE_BTN_DISABLED', label: '是否禁用删除按钮', isBool: true },
        { field: 'SHOW_CHECK', label: '显示选择列', isBool: true },
        { field: 'IS_PAGE', label: '是否分页', isBool: false },
        { field: 'DEFAULT_PAGE_SIZE', label: '默认分页条数',promptInfo: '不配置则为10' },

        { field: 'NUM', label: '顺序索引' },
      ]
      tableConfig.rowBtns.push({ id: guid(), btnType: 7, code: 'btn' })
      tableConfig.rowBtns.push({ id: guid(), btnType: 7, code: 'panel' })
    }
    else {//编辑表头
      tableConfig.columns = [
        { field: 'LABEL_NAME', label: '显示名称', isRequire: true },
        { field: 'CODE', label: '编码', isRequire: true },
        { field: 'EDIT_NAME', label: '数据库表', isRequire: true },
        { field: 'CUSTOM_PARAMS', label: '自定义参数', promptInfo: '自定义参数  格式：参数名称=参数值  多个参数时以||分割' },

        { field: 'API_URL', label: '保存API路径', isRequire: true },
        { field: 'QUERY_API_URL', label: '查询API路径', isRequire: true },
        { field: 'BATCH_UPDATE_API', label: '批量保存API' },
        { field: 'BATCH_QUERY_API', label: '批量查询API' },
        { field: 'CODE_FIELD_NAME', label: '编号字段名称' },
        { field: 'CODE_RULE_TYPE', label: '编码规则类型', data: { enumName: 'PFCodeRuleType' } },

        { field: 'NUM', label: '顺序索引' },
      ]

      tableConfig.rowBtns.push({ id: guid(), btnType: 7, code: 'btn' })
      tableConfig.rowBtns.push({ id: guid(), btnType: 7, code: 'panel' })
    }
    return [tableConfig]
  }


  onEditVisible = (data) => {
    console.log('data', data)
    const parentId = data ? data.PARENT_ID : null
    const visible = {}
    visible['RELATION_FIELD'] = parentId ? true : false
    visible['NAV_PROPERTY'] = parentId ? true : false
    visible['WIDTH'] = parentId ? true : false
    visible['HEIGTH'] = parentId ? true : false
    visible['IS_DETAIL_REQUIRED'] = parentId ? true : false
    visible['SORT_FIELDS'] = parentId ? true : false
    visible['SORT_ORDERS'] = parentId ? true : false
    visible['IS_SUB_ADD_BTN_DISABLED'] = parentId ? true : false
    visible['IS_SUB_DELETE_BTN_DISABLED'] = parentId ? true : false
    visible['SHOW_CHECK'] = parentId ? true : false
    visible['NAV_ROW_CLASS_FILTER'] = parentId ? true : false
    visible['API_URL'] = parentId ? false : true
    visible['QUERY_API_URL'] = parentId ? false : true
    visible['BATCH_UPDATE_API'] = parentId ? false : true
    visible['BATCH_QUERY_API'] = parentId ? false : true
    visible['CODE_FIELD_NAME'] = parentId ? false : true
    visible['CODE_RULE_TYPE'] = parentId ? false : true
    visible['EDIT_PAGE_ENTITY_ORG_TYPE'] = parentId ? false : true

    return visible
  };

  onTableBtnParam(params) {
    const { btnConfig, record } = params ? params : {}
    if (btnConfig.code === 'column') {
      return {
        content: <EditColumnShowPage orgId={this.props.orgId} pageTableId={record.ID} tableParentId={this.props.parentId}>
          <Button icon={'bars'} css={'default'} size={'small'} title={'字段'} />
        </EditColumnShowPage>
      }
    }
    else if (btnConfig.code === 'editPage') {
      return {
        content: <EditShowPage {...this.props} formId={this.props.formId} orgId={this.props.orgId} parentId={record.ID}>
          <Button icon={'folder-add'} css={'default'} size={'small'} title={'管理'} />
        </EditShowPage>
      }
    }
    else if (btnConfig.code === 'btn') {
      return {
        content: <BtnShowPage orgId={this.props.orgId} tableId={record.ID} btnPageType={1} parentId={this.props.parentId}>
          <Button icon={'star'} css={'default'} size={'small'} title={'按钮'} />
        </BtnShowPage>
      }
    }
    else if (btnConfig.code === 'panel') {
      return {
        content: <EditPanelShowPage orgId={this.props.orgId} pageTableId={record.ID}>
          <Button icon={'info'} css={'default'} size={'small'} title={'区域'} />
        </EditPanelShowPage>
      }
    }
    return super.onTableBtnParam(params)
  }

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

      <EditShowPage {...this.props} formId={this.props.formId} orgId={this.props.orgId} parentId={record.ID}>
        <Button icon={'folder-add'} css={'default'}>管理</Button>
      </EditShowPage>

      {
        (() => {
          if (!this.props.parentId) {
            return <BtnShowPage orgId={this.props.orgId} tableId={record.ID} btnPageType={1}>
              <Button icon={'star'} css={'default'}>按钮</Button>
            </BtnShowPage>
          }
        })()
      }

      {
        (() => {
          if (!this.props.parentId) {
            return <EditPanelShowPage orgId={this.props.orgId} pageTableId={record.ID}>
              <Button icon={'info'} css={'default'}>区域</Button>
            </EditPanelShowPage>
          }
        })()
      }

    </>
  }

}


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