import React from 'react'
import { connect } from 'dva'
import PageConfigShowBaseComponent from "../Lib/PageConfigShowBaseComponent"
import { guid } from "../../../utils/common"

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

  onGetTableConfig () {
    const tableConfig = {
      sort: 'NUM',
      order: 0,
      api: 'PF/Btn/OrderPaged',
      columns: [
        { field: 'LABEL', label: '显示名称' },
        { field: 'CODE', label: '编码' },
        { field: 'BTN_TYPE', label: '按钮类型', data: { enumName: 'PFBtnTypeEnum' } },
        { field: 'BTN_FUN_TYPE', label: '所属功能类型', data: { enumName: 'PFBtnFuncTypeEnum' } },
        { field: 'IS_RULE', label: '是否权限控制', isBool: true },
        { field: 'CLICK_EVENT', label: '点击事件' },
        { field: 'CSS', label: 'CSS', isRequire: true },
        { field: 'ICON', label: 'ICON', isRequire: true },
        { field: 'FORM_CODE', label: '表单编码' },
        { field: 'CUSTOM_PARAMS', label: '自定义参数', promptInfo: '自定义参数  格式：参数名称=参数值  多个参数时以||分割' },
        { field: 'ENABLE_STATUS', label: '启用标志', data: { enumName: 'FMEnableStatusEnum' } },
        { field: 'NUM', label: '顺序索引' },
      ],
      deleteApi: 'PF/Btn/Delete',
      editConfig: {
        data: {
          ORG_ID: this.props.orgId,
          NUM: 0,
          BTN_TYPE: 0,
          BTN_FUN_TYPE: 0,
          IS_RULE:true,
          PAGE_EDIT_ID: this.props.btnPageType == 1 ? this.props.tableId : null,
          PAGE_CUSTOM_FORM_ID: this.props.btnPageType == 2 ? this.props.tableId : null,
          PAGE_TABLE_ID: this.props.btnPageType == 0 ? this.props.tableId : null,
          BTN_PAGE_TYPE: this.props.btnPageType
        },
        columns: [
          { controlType: 1, field: 'LABEL', label: '显示名称', isRequire: true },
          { controlType: 1, field: 'CODE', label: '编码' },
          { controlType: 3, field: 'BTN_TYPE', label: '按钮类型', data: { enumName: 'PFBtnTypeEnum' } },
          { controlType: 3, field: 'BTN_FUN_TYPE', label: '所属功能类型', data: { enumName: 'PFBtnFuncTypeEnum' } },
          { controlType: 4, field: 'IS_RULE', label: '是否权限控制' },
          { controlType: 4, field: 'SELECT_SHOW', label: '选择控制可用' },
          { controlType: 1, field: 'CLICK_EVENT', label: '点击事件' },
          { controlType: 1, field: 'CSS', label: 'CSS', isRequire: true },
          { controlType: 1, field: 'ICON', label: 'ICON', isRequire: true },
          { controlType: 1, field: 'FORM_CODE', label: '表单编码' },
          { controlType: 1, field: 'WORD_TEMPLATE_CODE', label: '模板编码' },
          { controlType: 1, field: 'CUSTOM_PARAMS', label: '自定义参数', promptInfo: '自定义参数  格式：参数名称=参数值  多个参数时以||分割' },
          { controlType: 3, field: 'SAVE_CLOSE_TYPE', label: '保存关闭窗体类型', data: { enumName: 'PFBtnSaveCloseTypeEnum' } },
          { controlType: 3, field: 'SAVE_CLEAR_TYPE', label: '保存后清空数据类型', data: { enumName: 'PFBtnSaveClearTypeEnum' } },
          { controlType: 3, field: 'ENABLE_STATUS', label: '启用标志', data: { enumName: 'FMEnableStatusEnum' } },
          { controlType: 4, field: 'IS_COMFIRM', label: '需确认' },
          { controlType: 1, field: 'API_URL', label: 'API' },
          { controlType: 4, field: 'IS_SAME_LEVEL', label: '是否同级' },//树组件时生效
          { controlType: 4, field: 'IS_DEFAULT', label: '是否默认' },//树组件时生效
          { controlType: 1, field: 'NUM', label: '顺序索引' },
          {
            controlType: 1, field: 'BTN_CONDITION', label: '按钮显示条件', promptInfo: '不填默认都显示 格式如：Level,1,1|Node.NAME,1,1  多个条件时以|分割', span: { span: 24 }, formItemLayout: {
              labelCol: { span: 2 },
              wrapperCol: { span: 17 },
            }
          },
          {
            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 },
            }
          }
        ],
        getApi: 'PF/Btn/Get',
        saveApi: 'PF/Btn/Update',
        onChange: this.onEditChange,
        onVisible: this.onEditVisible,
      },
      rule: {
        rules: [
          {
            field: (this.props.btnPageType == 1 ? 'PAGE_EDIT_ID' : (this.props.btnPageType == 2 ? 'PAGE_CUSTOM_FORM_ID' : 'PAGE_TABLE_ID')),
            operator: 1,
            value: this.props.tableId
          },
        ]
      },
      rowBtns: [
        { id: guid(), btnType: 7, code: 'edit' },
        { id: guid(), btnType: 4, code: 'delete' },
      ]
    }
    return [tableConfig]
  }


  onEditVisible = (data) => {
    const btnPageType = data ? data.BTN_PAGE_TYPE : 1
    const btnType = data ? data.BTN_TYPE : 99
    const visible = {}
    visible['BTN_FUN_TYPE'] = btnPageType == 0 || (btnPageType == 1 && this.props.parentId)
    visible['SAVE_CLOSE_TYPE'] = btnType === 1
    visible['SAVE_CLEAR_TYPE'] = btnType === 1
    visible['INPUT_SAVE_FIELD'] = btnType === 13
    visible['INPUT_NAV_FIELD'] = btnType === 13
    visible['IS_COMFIRM'] = btnType === 6 || btnType === 7
    visible['API_URL'] = btnType === 6 || btnType === 7 || btnType === 4 || btnType === 15
    visible['WORD_TEMPLATE_CODE'] = btnType === 18
    return visible
  };

  onEditChange = (params) => {
    const { field, value, onSave, onSetVisible } = params
    if (field === 'BTN_TYPE') {
      let css = ""
      let icon = ""
      if (value == 1) {//保存
        css = "primary"
        icon = "save"
      }
      else if (value == 2) {//刷新
        css = "default"
        icon = "reply"
      }
      else if (value == 3 || value == 13) {//添加，批量添加
        css = "primary"
        icon = "plus"
      }
      else if (value == 4) {//删除
        css = "danger"
        icon = "delete"
      }
      else if (value == 5) {//编辑
        css = "primary"
        icon = "edit"
      }
      else if (value == 6) {//自定义按钮事件
        css = "primary"
        icon = "edit"
      }
      else if (value == 7) {//自定义
        css = "primary"
        icon = "edit"
      }
      else if (value == 8) {//查看
        css = "default"
        icon = "eye"
      }
      else if (value == 9) {//查看
        css = "default"
        icon = "export2"
      }
      else if (value == 10) {//复制新增
        css = "primary"
        icon = "copy"
      }
      else if (value === 11) {//导入
        css = 'default'
        icon = 'download'
      }
      else if (value === 15) {//导入
        css = 'default'
        icon = 'download'
      }
      onSave({
        CSS: css,
        ICON: icon,
        IS_COMFIRM: value === 6 || value === 7
      })
      onSetVisible()
    }

  };

}


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