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


class EditColumnShowPage 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 verifyType = data ? data.VERIFY_TYPE : 0
    const visible = {}
    visible['ENUM'] = controlType === 3 || controlType === 5

    visible['INPUT_DATA_LEVEL'] = controlType === 11 || controlType === 17
    visible['IS_ONLY_CHECK_LEAF'] = controlType === 11 || controlType === 17
    visible['INPUT_DATA_API'] = controlType === 9 || controlType === 13 || controlType === 11 || controlType === 17 || controlType === 24
    visible['INPUT_ID_FIELD'] = controlType === 9 || controlType === 13 || controlType === 11 || controlType === 17 || controlType === 25 || controlType === 24
    visible['INPUT_SAVE_FIELD'] = controlType === 9 || controlType === 13 || controlType === 11 || controlType === 17 || controlType === 24
    visible['INPUT_NAV_FIELD'] = controlType === 9 || controlType === 13 || controlType === 11 || controlType === 17 || controlType === 24
    visible['INPUT_FILTER_FIELD'] = controlType === 9 || controlType === 13 || controlType === 24
    visible['INPUT_LABEL_FIELD'] = controlType === 9 || controlType === 13 || controlType === 17 || controlType === 24
    visible['INPUT_SHOW_FIELD'] = controlType === 9 || controlType === 13 || controlType === 17 || controlType === 24
    visible['INPUT_SORT_FIELD'] = controlType === 9 || controlType === 13 || controlType === 17 || controlType === 24
    visible['INPUT_SORT_ORDER'] = controlType === 9 || controlType === 13 || controlType === 17 || controlType === 24
    visible['INPUT_FILTER_ORG_TYPE'] = controlType === 9 || controlType === 13 || controlType === 17 || controlType === 24
    visible['INPUT_KEEP_VALUE_BY_NO_SRC'] = controlType === 9 || controlType === 13 || controlType === 17 || controlType === 24
    visible['MAX_LEN'] = controlType === 1 || controlType === 6 || controlType === 7
    visible['MIN_LEN'] = controlType === 1 || controlType === 6 || controlType === 7
    visible['MIN_VAL'] = controlType === 7
    visible['MAX_VAL'] = controlType === 7
    visible['IS_CHECK_MAX'] = controlType === 7
    visible['IS_CHECK_MIN'] = controlType === 7
    visible['STEP'] = controlType === 7

    visible['SELECTD_LABEL_FIELD'] = controlType ===24
    visible['CODE_RULE_TYPE'] = controlType === 14
    visible['IS_IGNORE_TENANT'] = controlType === 9

    visible['VERIFY_EXPRESS'] = verifyType !== null && verifyType !== undefined && verifyType !== 0
    visible['VERIFY_ERROR_INFO'] = verifyType !== null && verifyType !== undefined && verifyType !== 0
    return visible
  };

  onEditChange = (params) => {
    const { field, value, onSave, onSetVisible } = params
    if (field === 'IS_DEFAULT') {
      onSave({
        IS_COPY: value,
      })
    }
    else if (field === 'FIELD_NAME') {
      onSave({
        IS_CAN_BATCH_EDIT: (value === 'CODE' ? false : true),
      })
    }
    else if (field === 'CONTROL_TYPE') {
      onSetVisible()
    }
    else if (field === 'VERIFY_TYPE') {
      let exp = null
      let errorInfo = null
      if (value === 1) {
        exp = '^$|^(-?\\d+)(\\.\\d+)?$'
        errorInfo = '请输入数值'
      }
      else if (value === 2) {
        exp = '^$|^1\\d{10}$'
        errorInfo = '请输入手机号'
      }
      else if (value === 3) {
        exp = '^$|^(\\d{4}-|\\d{3}-)?(\\d{8}|\\d{7})$'
        errorInfo = '请输入固定电话'
      }
      else if (value === 4) {
        exp = '^$|^\\d{15}(\\d\\d[0-9xX])?$'
        errorInfo = '请输入身份证号'
      }
      else if (value === 5) {
        exp = '^$|^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$'
        errorInfo = '请输入电子邮件'
      }
      else if (value === 6) {
        exp = '^$|^[0-9a-fA-F]$'
        errorInfo = '请输入十六进制'
      }
      else if (value === 7) {
        exp = '^$|^(\\d{4}|\\d{2})-((1[0-2])|(0?[1-9]))-(([12][0-9])|(3[01])|(0?[1-9]))$'
        errorInfo = '请输入日期'
      }
      else if (value === 8) {
        exp = '^$|^[\\u4e00-\\u9fa5],{0,}$'
        errorInfo = '请输入中文'
      }
      else if (value === 9) {
        exp = '^$|^[A-Za-z0-9]+$'
        errorInfo = '请输入英文字符串'
      }
      else if (value === 10) {
        exp = '^$|^\\w+$'
        errorInfo = '请输入英文字符串含符号'
      }
      else if (value === 11) {
        exp = '^$|^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$ ；^[a-zA-z]+://(w+(-w+)*)(.(w+(-w+)*))*(?S*)?$'
        errorInfo = '请输入网址'
      }
      else if (value === 12) {
        exp = '^$|^((2[0-4]\\d|25[0-5]|[01]?\\d\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$'
        errorInfo = '请输入IP地址'
      }
      else if (value === 13) {
        exp = '^$|^\\d+$'
        errorInfo = '请输入非负整数'
      }
      else if (value === 14) {
        exp = '^$|[0-9]*[1-9][0-9]*$'
        errorInfo = '请输入正整数'
      }
      else if (value === 15) {
        exp = '^$|^-[0-9]*[1-9][0-9]*$'
        errorInfo = '请输入负整数'
      }
      else if (value === 16) {
        exp = '^$|^-?\\d+$'
        errorInfo = '请输入整数'
      }
      else if (value === 17) {
        exp = '^$|^(-?\\d+)(\\.\\d+)?$'
        errorInfo = '请输入小数'
      }
      else if (value === 18) {
        exp = '^$|^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$'
        errorInfo = '请输入正实数'
      }
      else if (value === 19) {
        exp = '^$|^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$'
        errorInfo = '请输入负实数'
      }
      else if (value === 20) {
        exp = '^$|^(-?\\d+)(\\.\\d+)?$'
        errorInfo = '请输入实数'
      }
      onSave({
        VERIFY_EXPRESS: exp,
        VERIFY_ERROR_INFO: errorInfo,
      })
    }
  };

  onRowDropDownFilter(params) {
    this.onEditFilter(params)
  }

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

  onGetTableConfig() {
    let tableColumns = [
      { controlType: 1, field: 'CODE', label: '编号' },
      { controlType: 4, field: 'IS_DEFAULT', label: '是否显示' },
      { controlType: 4, field: 'EDITABLE', label: '是否可编辑' },
      { controlType: 4, field: 'REQUIRED', label: '是否必填' },
      { controlType: 4, field: 'IS_COPY', label: '是否拷贝' },
      { controlType: 4, field: 'IS_CAN_BATCH_EDIT', label: '允许批次编辑' },
      { controlType: 1, field: 'LABEL', label: '显示名称', isRequire: true, width: 120 },
      { controlType: 1, field: 'FIELD_NAME', label: '字段名称', isRequire: true, width: 300 },
      { controlType: 3, field: 'CONTROL_TYPE', label: '控件类型', data: { enumName: 'PFControlTypeEnum' } },
      { controlType: 3, field: 'CASE', label: '大小写', data: { enumName: 'FMCaseProcEnum' } },
      { controlType: 1, field: 'WIDTH', label: '宽度(1,2,3)', width: 100 },
      { controlType: 1, field: 'HEIGHT', label: '高度(多行文本默认4）', width: 100 },
      // {
      //   controlType: 9, field: 'Nav_PageEditPanel.LABEL', label: '所属区域', width: 100, data: {
      //     api: 'PF/PageEditPanel/OrderPaged',
      //     showField: 'LABEL',
      //     labelField: 'LABEL',
      //     filterField: 'LABEL',
      //     navField: 'LABEL,ID',
      //     saveField: 'Nav_PageEditPanel.LABEL,PAGE_EDIT_PANEL_ID',
      //     idField: 'PAGE_EDIT_PANEL_ID',
      //   }
      // },
      { controlType: 1, field: 'DEFAULT_VALUE', label: '默认值', width: 80 },
      { controlType: 3, field: 'SHOW_TYPE', label: '列显示方式（子表）', data: { enumName: 'ShowType' }  },
      { controlType: 1, field: 'NUM', label: '顺序索引' },
    ]


    const tableConfig = {
      sort: 'NUM',
      order: 0,
      api: 'PF/EditColumn/OrderPaged',
      columns: tableColumns,
      checked: true,
      deleteApi: 'PF/EditColumn/Delete',
      batchDeleteApi: "PF/EditColumn/BatchDelete",
      editConfig: {
        data: {
          ORG_ID: this.props.orgId,
          PAGE_EDIT_ID: this.props.pageTableId,
          NUM: 0,
          IS_DEFAULT: true,
          EDITABLE: true,
          IS_COPY: true,
          FORMAT_TYPE: 1,
          CONTROL_TYPE: 1,
          IS_CAN_BATCH_EDIT: true,
        },
        columns: [
          { controlType: 1, field: 'CODE', label: '编号' },
          { controlType: 1, field: 'LABEL', label: '显示名称', isRequire: true },
          { controlType: 1, field: 'FIELD_NAME', label: '字段名称', isRequire: true },
          { controlType: 3, field: 'CONTROL_TYPE', label: '控件类型', data: { enumName: 'PFControlTypeEnum' } },
          { controlType: 3, field: 'CASE', label: '大小写', data: { enumName: 'FMCaseProcEnum' } },
          { controlType: 4, field: 'IS_DEFAULT', label: '是否显示' },
          { controlType: 4, field: 'EDITABLE', label: '是否可编辑' },
          { controlType: 4, field: 'REQUIRED', label: '是否必填' },
          { controlType: 4, field: 'IS_COPY', label: '是否拷贝' },
          { controlType: 4, field: 'IS_CAN_BATCH_EDIT', label: '允许批次编辑' },
          { controlType: 4, field: 'IS_IGNORE_TENANT', label: '忽略租户标识' },
          {
            controlType: 9, field: 'Nav_PageEditPanel.LABEL', label: '所属区域', data: {
              api: 'PF/PageEditPanel/OrderPaged',
              showField: 'LABEL',
              labelField: 'LABEL',
              filterField: 'LABEL',
              navField: 'LABEL,ID',
              saveField: 'Nav_PageEditPanel.LABEL,PAGE_EDIT_PANEL_ID',
              idField: 'PAGE_EDIT_PANEL_ID',
            }
          },
          { controlType: 1, field: 'ENUM', label: '枚举名称' },
          { controlType: 1, field: 'DEFAULT_VALUE', label: '默认值' },
          { controlType: 1, field: 'INPUT_DATA_API', label: 'API' },
          { controlType: 1, field: 'INPUT_DATA_LEVEL', label: '显示几级' },
          { controlType: 4, field: 'IS_ONLY_CHECK_LEAF', label: '父级不可选' },
          { controlType: 1, field: 'INPUT_ID_FIELD', label: '关联ID字段', promptInfo: '关联ID字段 配置保存主键ID的字段名称' },
          { controlType: 1, field: 'REMARK', label: '备注说明', promptInfo: '备注说明信息' },
          {
            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: 'INPUT_LABEL_FIELD', label: '显示字段', promptInfo: '显示字段（多个字段,分隔）' },
          { controlType: 1, field: 'INPUT_SHOW_FIELD', label: '下拉显示字段', promptInfo: '下拉显示字段（多个字段,分隔）' },
          { controlType: 1, field: 'SELECTD_LABEL_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: 4, field: 'INPUT_KEEP_VALUE_BY_NO_SRC', label: '非法输入保留值' },
          { controlType: 3, field: 'SHOW_TYPE', label: '子表列显示方式', data: { enumName: 'ShowType' } },
          { controlType: 1, field: 'SHOW_FORM_CODE', label: '查询表单编号' },
          { controlType: 1, field: 'SHOW_ID_FIELD', label: '查询ID字段名称' },
          { controlType: 1, field: 'EDIT_FORM_CODE', label: '编辑表单编号' },
          { controlType: 1, field: 'EDIT_FORM_FILTER', label: '编辑过滤字段' },
          { controlType: 1, field: 'EDIT_FORM_FILTER_VALUE', label: '编辑过滤值' },
          { controlType: 3, field: 'VERIFY_TYPE', label: '验证类型', data: { enumName: 'PFEditColumnVerifyTypeEnum' } },
          { controlType: 1, field: 'VERIFY_EXPRESS', label: '验证表达式' },
          { controlType: 1, field: 'VERIFY_ERROR_INFO', label: '验证错误提示' },
          { controlType: 3, field: 'CODE_RULE_TYPE', label: '编码规则类型', data: { enumName: 'PFCodeRuleType' } },

          { controlType: 4, field: 'IS_CHECK_MAX', label: '限制最大值' },
          { controlType: 1, field: 'MAX_VAL', label: '最大值'},
          { controlType: 4, field: 'IS_CHECK_MIN', label: '限制最小值' },
          { controlType: 1, field: 'MIN_VAL', label: '最小值'},
          { controlType: 1, field: 'STEP', label: '步长'},

          { controlType: 1, field: 'WIDTH', label: '宽度(1,2,3，默认1)' },
          { controlType: 1, field: 'HEIGHT', label: '高度(多行文本默认4）'},
          { controlType: 4, field: 'PERMIT_EDITABLE', label: '审核是否可编辑' },
          { controlType: 1, field: 'PROMPT_INFO', label: '提示信息' },
          { controlType: 1, field: 'NUM', label: '顺序索引' },
          { controlType: 7, field: 'MAX_LEN', label: '最大长度限制', promptInfo: '为0表示不限制最大长度' },
          { controlType: 7, field: 'MIN_LEN', label: '最小长度限制' },
          { controlType: 1, field: 'VERIFY_MSG', label: '验证错误信息' },
          { controlType: 4, field: 'IS_AND_FILITER', label: '是否交集过滤' },
        ],
        getApi: 'PF/EditColumn/Get',
        saveApi: 'PF/EditColumn/Update',

        onChange: this.onEditChange,
        onVisible: this.onEditVisible,
        onFilter: this.onEditFilter,
        pageTableId: this.props.pageTableId,
        includes: ['Nav_PageEditPanel'],
      },
      rule: {
        rules: [
          { field: 'PAGE_EDIT_ID', operator: 1, value: this.props.pageTableId }
        ]
      },
      includes: ['Nav_PageEditPanel'],
      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' },
        { id: guid(), btnType: 7, code: 'fill' },
      ]
    }
    return [tableConfig]
  }

  onTableBtnParam(params) {
    const { btnConfig, record } = params ? params : {}
    if (btnConfig.code === 'filter') {
      return {
        content: <EditColumnFilterShowPage orgId={this.props.orgId} editColumnId={record.ID}>
          <Button icon={'info'} type={'default'} size={'small'} title={'过滤'} />
        </EditColumnFilterShowPage>
      }
    }
    else if (btnConfig.code === 'fill') {
      return {
        content: <EditColumnFillMapShowPage orgId={this.props.orgId} editColumnId={record.ID}>
          <Button icon={'info'} type={'default'} size={'small'} title={'填充明细'} />
        </EditColumnFillMapShowPage>
      }
    }
    return super.onTableBtnParam(params)
  }

  getRowOtherBtns({ record }) {
    return <>
      <EditColumnFilterShowPage orgId={this.props.orgId} editColumnId={record.ID}>
        <Button icon={'info'} type={'default'}>过滤</Button>
      </EditColumnFilterShowPage>
      <EditColumnFillMapShowPage orgId={this.props.orgId} editColumnId={record.ID}>
        <Button icon={'info'} type={'default'}>填充明细</Button>
      </EditColumnFillMapShowPage>
    </>
  }
}


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