text: import React, { PureComponent, Fragment } from 'react';
  import { connect } from 'dva';
  import {
    Row,
    Col,
    Card,
    Form,
    Icon,
    Button,
    Dropdown,
    Menu,
    Divider,
    Popconfirm,
    Input,
  } from 'antd';
  import PageHeaderLayout from '../../layouts/PageHeaderLayout';
  import StandardTable from '../../components/StandardTable';
  import FileModal from '../../components/Modal/FileModal';
  import styles from './style.less';
  import Picture from '../../components/Picture';
  
  const FormItem = Form.Item;
  const getValue = obj =>
    Object.keys(obj)
      .map(key => obj[key])
      .join(',');
  
  @connect(({ file, loading }) => ({
    file,
    loading: loading.models.file,
  }))
  @Form.create()
  export default class TableList extends PureComponent {
    state = {
      selectedRows: [],
      formValues: {},
    };
  
    componentDidMount() {
      const { dispatch } = this.props;
      dispatch({
        type: 'file/fetch',
      });
    }
  
    handleStandardTableChange = (pagination, filtersArg, sorter) => {
      const { dispatch } = this.props;
      const { formValues } = this.state;
  
      const filters = Object.keys(filtersArg).reduce((obj, key) => {
        const newObj = { ...obj };
        newObj[key] = getValue(filtersArg[key]);
        return newObj;
      }, {});
  
      const params = {
        currentPage: pagination.current,
        pageSize: pagination.pageSize,
        ...formValues,
        ...filters,
      };
      if (sorter.field) {
        params.sorter = `${sorter.field}_${sorter.order}`;
      }
  
      dispatch({
        type: 'file/fetch',
        payload: params,
      });
    };
  
    handleSearch = e => {
      e.preventDefault();
  
      const { dispatch, form } = this.props;
  
      form.validateFields((err, fieldsValue) => {
        if (err) return;
  
        const values = {
          ...fieldsValue,
        };
  
        this.setState({
          formValues: values,
        });
  
        dispatch({
          type: 'file/fetch',
          payload: values,
        });
      });
    };
  
    handleFormReset = () => {
      const { form, dispatch } = this.props;
      form.resetFields();
      this.setState({
        formValues: {},
      });
      dispatch({
        type: 'file/fetch',
        payload: {},
      });
    };
  
    handleMenuClick = e => {
      const { dispatch } = this.props;
      const { selectedRows } = this.state;
  
      if (!selectedRows) return;
  
      switch (e.key) {
        case 'remove':
          dispatch({
            type: 'thinking/remove',
            payload: {
              userName: selectedRows.map(row => row.username).join(','),
            },
            callback: () => {
              this.setState({
                selectedRows: [],
              });
            },
          });
          break;
        default:
          break;
      }
    };
  
    handleSelectRows = rows => {
      this.setState({
        selectedRows: rows,
      });
    };
  
    handleAdd = formData => {
      const { dispatch } = this.props;
      dispatch({
        type: 'file/create',
        payload: formData,
      });
    };
  
    handleEdit = (id, formData) => {
      const { dispatch } = this.props;
      dispatch({
        type: 'file/patch',
        payload: { id, formData },
      });
    };
  
    handelDelete = id => {
      const { dispatch } = this.props;
      dispatch({
        type: 'file/remove',
        payload: id,
      });
    };
  
    renderForm() {
      const { form } = this.props;
      const { getFieldDecorator } = form;
      return (
        <Form onSubmit={this.handleSearch} layout="inline">
          <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
            <Col md={8} sm={24}>
              <FormItem label="文件名">
                {getFieldDecorator('name')(<Input placeholder="请输入" />)}
              </FormItem>
            </Col>
            <Col md={8} sm={24}>
              <span className={styles.submitButtons}>
                <Button type="primary" htmlType="submit">
                  查询
                </Button>
                <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
                  重置
                </Button>
              </span>
            </Col>
          </Row>
        </Form>
      );
    }
  
    render() {
      const {
        file: { data },
        loading,
      } = this.props;
      const { selectedRows } = this.state;
  
      const columns = [
        {
          title: '文件名',
          dataIndex: 'name',
        },
        {
          title: '描述',
          dataIndex: 'describe',
        },
        {
          title: '文件',
          render(val) {
            if (val.file) {
              const url = val.file;
              const fileName = url.substring(url.lastIndexOf('/') + 1);
              if (fileName.indexOf('.') > 0) {
                const postfix = fileName.substring(fileName.lastIndexOf('.'));
                const re = new RegExp('(.jpg|.JPG|.jpeg|.JPEG|.png|.PNG|.gif|.GIF)$');
                if (re.test(postfix)) {
                  return <Picture images={[{ src: val.file }]} />;
                }
              }
              return (
                <a href={val.file} target="_blank" rel="noopener noreferrer">
                  下载
                </a>
              );
            }
            return '无';
          },
        },
        {
          title: '操作',
          render: (text, record) => (
            <Fragment>
              <FileModal
                titleModal="编辑文件"
                record={record}
                onOk={this.handleEdit.bind(this, record.id)}
              >
                <a>编辑</a>
              </FileModal>
              <Divider type="vertical" />
              <Popconfirm title="确认删除？" onConfirm={this.handelDelete.bind(this, record.id)}>
                <a href="">删除</a>
              </Popconfirm>
            </Fragment>
          ),
        },
      ];
  
      const menu = (
        <Menu onClick={this.handleMenuClick} selectedKeys={[]}>
          <Menu.Item key="remove">删除</Menu.Item>
        </Menu>
      );
  
      return (
        <PageHeaderLayout title="发布文件">
          <Card bordered={false}>
            <div className={styles.tableList}>
              <div className={styles.tableListForm}>{this.renderForm()}</div>
              <div className={styles.tableListOperator}>
                <FileModal titleModal="发布文件" record={{}} onOk={this.handleAdd}>
                  <Button icon="plus" type="primary">
                    新建
                  </Button>
                </FileModal>
                {selectedRows.length > 0 && (
                  <span>
                    <Button>批量操作</Button>
                    <Dropdown overlay={menu}>
                      <Button>
                        更多操作 <Icon type="down" />
                      </Button>
                    </Dropdown>
                  </span>
                )}
              </div>
              <StandardTable
                selectedRows={selectedRows}
                loading={loading}
                data={data}
                columns={columns}
                rowKey="id"
                onSelectRow={this.handleSelectRows}
                onChange={this.handleStandardTableChange}
              />
            </div>
          </Card>
        </PageHeaderLayout>
      );
    }
  }
  