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/taGoods',
        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 toEditGoods = (goodsId) => () => {
    router.push({
      pathname: '/integralMall/editGoods',
      query: {
        goodsId
      },
    });
  }

  
  const changeGoodsStatus = (row) => () => {
    Modal.confirm({
      title: '确认下架该商品？',
      okText: '确认',
      cancelText: '取消',
      onOk() {
        request({
          url: '/api/admin/taGoods/change',
          method: 'PUT',
          data: { ...row },
        }).then((data) => {
          message.info('操作成功！')
          getList({ pageNum: 1, pageSize: 10 });
        })
      }
    });
  }
  /**
   *
   *
   * @param {*} props
   * @returns
   */
  const columns = [
    {
      title: '商品图片',
      dataIndex: 'imgUrl',
      key: 'imgUrl',
      align: 'center',
      render: (text, record) => <img src={record.imgUrl} className={styles.touxiang} />,
    },
    {
      title: '商品名称',
      dataIndex: 'goodsName',
      key: 'goodsName',
      align: 'center',

    },
    {
      title: '所属积分',
      dataIndex: 'pointPrice',
      key: 'pointPrice',
      align: 'center',
    },
    {
      title: '总数量',
      dataIndex: 'totalNum',
      key: 'totalNum',
      align: 'center',
    },
    {
      title: '已兑换数量',
      dataIndex: 'exchanged',
      key: 'exchanged',
      align: 'center',
      render: (x,row) => <><span>{row.totalNum - row.inventory}</span></>
    },
    {
      title: '剩余数量',
      dataIndex: 'inventory',
      key: 'inventory',
      align: 'center',
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      align: 'center',
      render: (status)=> <><span>{status == 1 ? '已上架' : '未上架'}</span></>
    },
    {
      title: '操作',
      dataIndex: 'handle',
      key: 'handle',
      align: 'center',
      render: (x, row) => <><span style={{ color: '#EF273A', marginRight: '20px' }} onClick={changeGoodsStatus(row)}>{row.status == 1 ? '下架' : '上架'}<Icon type="shopping-cart" className={styles.shoppingCart} />
                            </span><span style={{ color: '#FF925C' }} onClick={toEditGoods(row.goodsId)}>编辑<Icon type="form" className={styles.edit} /></span></>,
    },
  ];

  const { getFieldDecorator } = props.form
  return (

    <>
      <Form layout="inline" onSubmit={e => handleSubmit(e, props)}>
        <Form.Item>
          {getFieldDecorator('goodsName')(
            <Input
              prefix={<Icon type="text" style={{ color: 'rgba(0,0,0,.25)' }} />}
              placeholder="商品名称"
            />,
          )}
        </Form.Item>
        <Form.Item>
          {getFieldDecorator('status')(
            <Select style={{ width: '180px' }} placeholder="状态">
              <Option value="1">已上架</Option>
              <Option value="0">已下架</Option>
            </Select>,
          )}
        </Form.Item>
        <Form.Item>
          {getFieldDecorator('buildingId')(
            <BuildSelect />,
          )}
        </Form.Item>
        <Form.Item>
          {getFieldDecorator('priceLesser')(
            <Input
              prefix={<Icon type="text" style={{ color: 'rgba(0,0,0,.25)' }} />}
              placeholder="价格小"
            />,
          )}
        </Form.Item>
        <Form.Item>
          {getFieldDecorator('priceGreater')(
            <Input
              prefix={<Icon type="text" style={{ color: 'rgba(0,0,0,.25)' }} />}
              placeholder="价格大"
            />,
          )}
        </Form.Item>
        <Form.Item>
          <Button type="primary" htmlType="submit" className={styles.searchBtn}>
            搜索
          </Button>
        </Form.Item>
      </Form>
      <Button type="danger" className={styles.addBtn} onClick={toEditGoods()}>新增</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
