text: import React from 'react'
  import PropTypes from 'prop-types'
  import { routerRedux } from 'dva/router'
  import { connect } from 'dva'
  import { Row, Col, Button, Popconfirm } from 'antd'
  import { Page } from 'components'
  import queryString from 'query-string'
  import List from './List'
  import Filter from './Filter'
  import Modal from './Modal'
  import { Permission} from '../../utils/enums'
  import intl from 'react-intl-universal'
  
  const User = ({ location, dispatch, user, loading }) => {
    location.query = queryString.parse(location.search)
    const { list, pagination, currentItem, modalVisible, modalType, isMotion, selectedRowKeys, currentUser, permissions } = user
    const { pageSize } = pagination
  
    // 判断操作权限
    const permission = {
    	allowCreate: permissions ? permissions.some(p => p === Permission.CREATE) : false,
    	allowUpdate: permissions ? permissions.some(p => p === Permission.UPDATE) : false,
    	allowDelete: permissions ? permissions.some(p => p === Permission.DELETE) : false
    }
  
    const create_user_title = intl.get('create_user').defaultMessage('Create User')
    const update_user_title = intl.get('update_user').defaultMessage('Update User')
  
    const modalProps = {
      item: modalType === 'create' ? {} : currentItem,
      visible: modalVisible,
      maskClosable: false,
      confirmLoading: loading.effects['user/update'],
      title: `${modalType === 'create' ? create_user_title : update_user_title}`,
      wrapClassName: 'vertical-center-modal',
      onOk (data) {
        dispatch({
          type: `user/${modalType}`,
          payload: data,
        })
      },
      onCancel () {
        dispatch({
          type: 'user/hideModal',
        })
      },
    }
  
    const listProps = {
      permission,
      dataSource: list,
      loading: loading.effects['user/query'],
      pagination,
      location,
      isMotion,
      onChange (page, filters, sorter) {
        const { query, pathname } = location
        dispatch(routerRedux.push({
          pathname,
          query: {
            ...query,
            page: page.current,
            pageSize: page.pageSize,
            sortField: sorter.field,
            sortOrder: sorter.order,
            ...filters,
          },
        }))
      },
      onDeleteItem (id) {
        dispatch({
          type: 'user/delete',
          payload: id,
        })
      },
      onEditItem (item) {
        dispatch({
          type: 'user/showModal',
          payload: {
            modalType: 'update',
            currentItem: item,
          },
        })
      },
      rowSelection: {
        selectedRowKeys,
        onChange: (keys) => {
          dispatch({
            type: 'user/updateState',
            payload: {
              selectedRowKeys: keys,
            },
          })
        },
      },
    }
  
    const filterProps = {
      permission,
      isMotion,
      filter: {
        ...location.query,
      },
      onFilterChange (value) {
        dispatch(routerRedux.push({
          pathname: location.pathname,
          query: {
            ...value,
            page: 1,
            pageSize,
          },
        }))
      },
      onSearch (fieldsValue) {
        fieldsValue.keyword.length ? dispatch(routerRedux.push({
          pathname: '/user',
          query: {
            field: fieldsValue.field,
            keyword: fieldsValue.keyword,
          },
        })) : dispatch(routerRedux.push({
          pathname: '/user',
        }))
      },
      onAdd () {
        dispatch({
          type: 'user/showModal',
          payload: {
            modalType: 'create',
          },
        })
      },
      switchIsMotion () {
        dispatch({ type: 'user/switchIsMotion' })
      },
    }
  
    const handleDeleteItems = () => {
      dispatch({
        type: 'user/multiDelete',
        payload: {
          ids: selectedRowKeys,
        },
      })
    }
  
    const remove_msg = intl.get('remove').defaultMessage('Remove')
    const remove_records_confirm_msg = intl.get('records_delete_confirm').defaultMessage('Are you sure delete these items?')
  
    return (
      <Page inner>
        <Filter {...filterProps} />
        {
          permission.allowDelete && selectedRowKeys.length > 0 &&
          <Row style={{ marginBottom: 24, textAlign: 'right', fontSize: 13 }}>
            <Col>
              {intl.get('selected_items', {quality: selectedRowKeys.length}).defaultMessage('Selected ' + selectedRowKeys.length + ' items')}
              <Popconfirm title={remove_records_confirm_msg} placement="left" onConfirm={handleDeleteItems}>
                <Button type="primary" size="large" style={{ marginLeft: 8 }}>{remove_msg}</Button>
              </Popconfirm>
            </Col>
          </Row>
        }
        <List {...listProps} />
        {modalVisible && <Modal {...modalProps} />}
      </Page>
    )
  }
  
  User.propTypes = {
    user: PropTypes.object,
    location: PropTypes.object,
    dispatch: PropTypes.func,
    loading: PropTypes.object,
  }
  
  export default connect(({ user, loading }) => ({ user, loading }))(User)
  