import { Component, ReactNode } from 'react';
import { AtTabBar } from 'taro-ui';
import Taro from '@tarojs/taro';
import 'taro-ui/dist/style/index.scss';
import './index.scss';

const navList: Array<Taro.TabBarItem> = [
  {
    pagePath: '/pages/index/index',
    text: '主页',
  },
  {
    pagePath: '/pages/repair/repair',
    text: '维修',
  },
  {
    pagePath: '/pages/user/user',
    text: '我的',
  },
];

export default class Index extends Component {
  state = {
    selected: 0,
    tabList: [
      {
        title: '主页',
        iconType: 'home',
      },
      {
        title: '维修',
        iconType: 'settings',
      },
      {
        title: '我的',
        iconType: 'user',
      },
    ],
  };

  handleClick(idx: number) {
    this.switchTab(idx, navList[idx].pagePath);
  }

  switchTab(idx: number, url: string) {
    this.setSelected(idx);
    Taro.switchTab({ url });
  }

  setSelected(idx: number) {
    this.setState({
      selected: idx,
    });
  }

  render(): ReactNode {
    return (
      <AtTabBar
        fixed
        tabList={this.state.tabList}
        onClick={this.handleClick.bind(this)}
        current={this.state.selected}
      />
    );
  }
}
