import React, { PureComponent } from "react";
import { connect } from "dva";
import { Icon, Checkbox, Select, Input, Tabs } from "antd";
import styles from "./index.less";
import CartListComp from "@/components/GoodsTableList/index.js";
import { getCustomer } from '@/utils/request';
class CartList extends PureComponent {
    constructor(props) {
        super(props);
        const match = this.props.match;
        const label = match.params && match.params.label;
        this.state = {
            tabIndex: label || '1',
            tabloading: false
        };
    }

    componentWillMount() {
        const { dispatch } = this.props;
        this.pageChange(0, 10, this.state.tabIndex || "1");
        // dispatch({
        //     type: "public/getStoreLength",
        //     payload: { customer: getCustomer().id }
        // });
    }

    componentWillReceiveProps(nextProps) {
      const { match } = this.props;
      const nextMatch = nextProps.match;
      if (match.params && nextMatch.params && match.params.label != nextMatch.params.label) {
        const label = nextMatch.params.label;
        this.setState({
          tabIndex: label || '1',
          tabloading: true
        });
        this.pageChange(0, 10, label || "1");
      }
    }

    pageChange(page = 0, pageSize = 10, key = 1) {
        const { dispatch } = this.props;
        const searchConditions = {
            page: page,
            size: pageSize,
            customer: getCustomer().id
        };
        this.setState({
            tabloading: true
        });
        dispatch({
            type: "cartList/getCartListData"
        });
        if (key == 1) {
          // 补货
          dispatch({
              type: "goodsPurchase/purchaseGoods",
              payload: searchConditions
          }).finally(() => {
              this.setState({
                  tabloading: false
              });
          });
        } else if (key == 2) {
            dispatch({
                type: "goodsPurchase/getGoodsRecentPageData",
                payload: searchConditions
            }).finally(() => {
                this.setState({
                    tabloading: false
                });
            });
        } else if (key == 3) {
            dispatch({
                type: "public/getCheckStore",
                payload: searchConditions
            }).finally(() => {
                this.setState({
                    tabloading: false
                });
            });
        } else if (key == 4) {
          dispatch({
            type: "goodsHot/getGoodsHotPageData",
            payload: searchConditions
        }).finally(() => {
            this.setState({
                tabloading: false
            });
        });
        }
    }

    tabsCallback(key) {
        // key 1: 只能补货 2:最近购买 3:关注商品
        this.pageChange(0, 10, key);
        this.setState({
          tabIndex: key
        });
    }
    render() {
        const { goodsDataNew, purchaseData, checkStore, cartListData, goodsRecentPageData, storeLength, goodsHotData } = this.props;
        const pagination = this.props.pagination || {};
        const checkStoreData = checkStore && checkStore[0] ? checkStore[0].storelist : [];
        const checkstoreLength = checkStoreData.length || 0;
        const tabIndex = this.state.tabIndex;
        // const tableData = this.state.tabIndex != 3 ? goodsDataNew : checkStoreData;
        let tableData = [];
        switch (tabIndex) {
          case "1":
            tableData = purchaseData;
            break;
          case "2":
            tableData = goodsRecentPageData;
            tableData = (goodsRecentPageData || {}).content;
            pagination.total = (goodsRecentPageData || {}).totalElements;
            pagination.pageSize = (goodsRecentPageData || {}).size;
            break;
          case "3":
            tableData = checkStoreData;
            break;
          case "4":
            tableData = (goodsHotData || {}).content;
            pagination.total = (goodsHotData || {}).totalElements;
            pagination.pageSize = (goodsHotData || {}).size;
            break;
          default:
            break;
        }
        if (cartListData && tableData) {
            for (const cartGroup of cartListData) {
                if (cartGroup && cartGroup.cartlist) {
                    const cartList = cartGroup.cartlist;
                    for (const cart of cartList) {
                        for (const goods of tableData) {
                            if (cart.goodsId === (this.state.tabIndex != 3 ? goods.id : goods.goodsId)) {
                                goods.inCart = true;
                            }
                        }
                    }
                }
            }
        }
        return (
            <div className={styles.cartListBox}>
                {/* <div className={styles.positionField}>全部</div> */}
                {/* <div className={styles.classHead}>
                    <span className={styles.nameIndex}>首页</span>
                    <span className={styles.nameChild}>
                        <Icon type="right" className={styles.nameIcon} />补货
                    </span>
                    <span className={styles.nameChild}>
                        <Icon type="right" className={styles.nameIcon} />智能补货
                    </span>
                </div> */}
                {/* teb菜单 */}
                <Tabs activeKey={this.state.tabIndex} onChange={this.tabsCallback.bind(this)} tabPosition="top" className={styles.tabsTabPane} tabBarGutter="0">
                    {/* <Tabs.TabPane tab={`补货（${pagination.total || 0}）`} key="1" ></Tabs.TabPane> */}
                    <Tabs.TabPane tab={`最近购买（${(goodsRecentPageData || {}).totalElements || "0"}）`} key="2" ></Tabs.TabPane>
                    <Tabs.TabPane tab={`关注商品（${checkstoreLength || "0"}）`} key="3" ></Tabs.TabPane>
                    {/* <Tabs.TabPane tab={`热卖促销（${(goodsHotData || {}).totalElements || "0"}）`} key="4" ></Tabs.TabPane> */}
                </Tabs>
                {/* 购物车列表 */}
                <CartListComp
                  goodsData={tableData}
                  {...this.props}
                  tabIndex={this.state.tabIndex}
                  hideTableFooter={!1}
                  tabloading={this.state.tabloading}
                  paginationData={pagination}
                  paginationChange={this.pageChange.bind(this)}
                />
                {/* 购物车列表 结束 */}
            </div>
        );
    }
}


export default connect(state => ({
    goodsSubSpecsData: state.goodsPurchase.goodsSubSpecsData,
    goodsDataNew: state.goodsPurchase.goodsData,
    checkStore: state.public.checkStore,
    goodsPurchase: state.goodsPurchase,
    pagination: state.goodsPurchase.pagination,
    cartListData: state.cartList.cartListData,
    storeLength: state.public.storeLength,                    // 关注数量
    purchaseData: state.goodsPurchase.purchaseData,
    goodsHotData: state.goodsHot.goodsHotPageData, // 热卖促销
    goodsRecentPageData: state.goodsPurchase.goodsRecentPageData // 最近购买
}))(CartList);
