import React, { useState, useEffect } from 'react';
import { Form, Input, Button, Icon, Tabs, Row, Col, Table, Pagination, Alert, message } from 'antd';
import { FormattedMessage } from 'umi-plugin-react/locale';
import styles from '../style/GoodsList.less';
import router from 'umi/router';
import moment from 'moment';
import BuildSelect from '../../components/SelectButton/BuildSelect'
import request from '../../utils/request'

const { TabPane } = Tabs;




function header(props) {
  const [ carType, setCarType ] = useState({})
  const callback = (key) => {
    setCarType(key)
    getList({ pageNum: 1, pageSize: 10 , type: key});
  }
  
// 获取初始化数据
const [ data, setData ] = useState({})

useEffect(() => {
  getList({ pageNum: 1, pageSize: 10 , type: 'platform'});
},[])

// 查询列表
const getList = (params) => {
  request({
      url: '/api/admin/tdPointsRules',
      method: 'GET',
      params: { ...params },
  }).then((data) => {
      console.log(data)
      setData(data)
  })
}

const changePageNum = (pageNumber) => {
  getList({ pageNum: pageNumber, pageSize: 10, type: carType })
}


  // 提交事件
  const handleSubmit = (e, props) => {
    e.preventDefault();
    props.form.validateFields((err, values) => {
      if (!err) {
        getList({ pageNum: 1, pageSize: 10, ...values, type: carType})
      }
    });
  }

  const changeStatus = (row) => () => {
    request({
      url: '/api/admin/tdPointsRules/change',
      method: 'PUT',
      data: { ...row },
    }).then((data) => {
      message.info('操作成功！')
      getList({ pageNum: 1, pageSize: 10, type: carType})
    })
  }

const columns = [
    {
      title: '类型',
      dataIndex: 'ruleName',
      key: 'ruleName',
      align: 'center',
    },
    {
      title: '获取积分',
      dataIndex: 'pointsAmount',
      key: 'pointsAmount',
      align: 'center',
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      align: 'center',
      render: (status) => <span>{status == 1 ? '启用' : '停用'}</span>
    },
    {
      title: '操作时间',
      dataIndex: 'updateDate',
      key: 'updateDate',
      align: 'center',
      render: (updateDate) => <><span>{updateDate != null ? moment(updateDate).format('YYYY-MM-DD') : ''}</span></>
    },
    {
      title: '操作',
      dataIndex: 'handle',
      key: 'handle',
      align: 'center',
      render: (x,row) => <><span style={{ color: '#EF273A', marginRight: '20px' }} onClick={changeStatus(row)}>{row.status == 1?'停用':'启用'}<Icon type="stop" className={styles.shoppingCart} /></span>
                      <span style={{ color: '#FF925C' }}>{row.buildingId != null ? '编辑'`${<Icon type="form" className={styles.edit} />}` : ''}</span></>,
    },
  ];

  const { getFieldDecorator } = props.form
  return (
    <>
      <Tabs onChange={callback} type="card">
        <TabPane tab="平台积分" key="platform">
          <Table style={{ marginTop: '40px' }} dataSource={data.records} columns={columns} pagination={false}/>
          <div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '30px' }}>
            <Pagination showQuickJumper defaultCurrent={1} total={data.total} onChange={changePageNum} />
          </div>
        </TabPane>
        <TabPane tab="项目积分" key="project">
        <Form layout="inline" onSubmit={e => handleSubmit(e, props)}>
            <Form.Item>
              {getFieldDecorator('buildingId')(
                <BuildSelect />,
              )}
            </Form.Item>
             
            <Form.Item>
              <Button type="primary" htmlType="submit" className={styles.searchBtn}>
                搜索
              </Button>
            </Form.Item>
          </Form>
          <Table style={{ marginTop: '40px' }} dataSource={data.records} columns={columns} pagination={false}/>
          <div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '30px' }}>
            <Pagination showQuickJumper defaultCurrent={1} total={data.total} onChange={changePageNum} />
          </div>
        </TabPane>
      </Tabs>,
    </>
  )
}
const WrappedHeader = Form.create({ name: 'header' })(header);

export default WrappedHeader
