import React, { useState, useEffect } from 'react';
import { Form, Input, Button, Icon, Select, message, Table, Divider, Tag, Pagination, Modal,Breadcrumb } from 'antd';
import { FormattedMessage } from 'umi-plugin-react/locale';
import styles from '../../style/GoodsList.less';
import router from 'umi/router';
import BuildSelect from '../../../components/SelectButton/BuildSelect'

import request from '../../../utils/request'

const { Option } = Select;


function header(props) {
  // 获取初始化数据
  const [ data, setData ] = useState({})

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

  // 查询列表
  const getList = (params) => {
    request({
        url: '/api/admin/taNewsType',
        method: 'GET',
        params: { ...params },
    }).then((data) => {
        console.log(data)
        setData(data)
    })
  }
  
  // 提交事件
  const handleSubmit = (e, props) => {
    e.preventDefault();
    props.form.validateFields((err, values) => {
      if (!err) {
        getList({ pageNum: 1, pageSize: 10, ...values })
      }
    });
  }

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


  // 跳转到编辑资讯
  const toEditNews = (id) => () => {
    router.push({
      pathname: '/news/type/editNews',
      query: {
        id
      },
    });
  }

  
  const changeNewsStatus = (row, newsId) => () => {
    Modal.confirm({
      title: '确认删除该商品？',
      okText: '确认',
      cancelText: '取消',
      onOk() {
        request({
          url: '/api/admin/taNewsType/' + newsId,
          method: 'DELETE',
          data: { ...row },
        }).then((data) => {
          message.info('操作成功！')
          getList({ pageNum: 1, pageSize: 10 });
        })
      }
    });
  }
  /**
   *
   *
   * @param {*} props
   * @returns
   */
  const columns = [
    {
      title: '类型图',
      dataIndex: 'newsTypeImg',
      key: 'newsTypeImg',
      align: 'center',
      render: (text, record) => <img src={record.newsTypeImg} className={styles.touxiang} />,
    },
    {
      title: '名称',
      dataIndex: 'newsTypeName',
      key: 'newsTypeName',
      align: 'center',

    },
    {
      title: '操作',
      dataIndex: 'handle',
      key: 'handle',
      align: 'center',
      render: (x, row) => <><span style={{ color: '#EF273A', marginRight: '20px' }} onClick={changeNewsStatus(row, row.newsTypeId)}>{row.status == 1 ? '删除' : '上架'}<Icon type="shopping-cart" className={styles.shoppingCart} />
                            </span><span style={{ color: '#FF925C' }} onClick={toEditNews(row.newsTypeId)}>编辑<Icon type="form" className={styles.edit} /></span></>,
    },
  ];

  const { getFieldDecorator } = props.form
  return (

    <>
      <Form layout="inline" onSubmit={e => handleSubmit(e, props)}>
        <Form.Item>
          {getFieldDecorator('status')(
            <Select style={{ width: '180px' }} placeholder="请选择">
              <Option value="1">已上架</Option>
              <Option value="0">已下架</Option>
            </Select>,
          )}
        </Form.Item>
      </Form>
      <Button type="primary" className={styles.addBtn} onClick={toEditNews()}>新增</Button>
      <Table 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>
    </>
  )
}
const WrappedHeader = Form.create({ name: 'header' })(header);

export default WrappedHeader
