import Taro, { Component } from '@tarojs/taro'
import { Provider } from '@tarojs/redux'

import Index from './pages/index'

import configStore from './store'

import 'taro-ui/dist/style/index.scss'
import './app.scss'

import api from './api'
Taro.api = api

import * as tools from './utils/tool'
Taro.$msg = tools.msgBase
Taro.$CR = tools.configRight
Taro.$msgConfirm = tools.confirmMsgFn

// 如果需要在 h5 环境中开启 React Devtools
// 取消以下注释：
// if (process.env.NODE_ENV !== 'production' && process.env.TARO_ENV === 'h5')  {
//   require('nerv-devtools')
// }

const store = configStore()

class App extends Component {

  config = {
    pages: [
      'pages/index/index',
    ],
    subPackages: [
      {
        root: 'pagesHouse/',
        pages: [
          'index',
          'list',
          'indexDtl',
          'indexDtlMore',
          'indexDtlAround',
          'indexDtlMap',
          'produceType',
          'photo',
          'news',
          'follow',
          'history',
          'search',
          'pkList',
          'pkDtl',
          'pkAdd',
          'price',
          'discountGroup',
          'roomPrice',
          'roomPriceImg',
          'roomPriceList',
        ]
      },
      {
        root: 'pagesMore/',
        pages: [
          'news/index',
          'news/list',
          'news/indexDtl',
          'news/tpDtl',
          'center/index',
          'center/group',
          'center/queryBuyHouse',
          'center/feedback',
          'center/loanCalc',
          'center/webViews',
          'center/message',
          'center/coupon',
          'center/sxf',
          'comment/add',
          'comment/list',
          'comment/dtl',
          // 'qa/list',
          // 'qa/add',
          // 'qa/dtl',
        ]
      },
      {
        root: 'pagesPlan/',
        pages: [
          'apply'
        ]
      },
      {
        root: 'pagesSchool/',
        pages: [
          'index',
          'indexDtl',
          'houseList',
          'class',
          'theme',
          'area',
        ]
      },
      {
        root: 'pagesQa/',
        pages: [
          'index',
          'add',
          'dtl',
          'dtlSub',
        ]
      },
    ],
    window: {
      backgroundTextStyle: 'light',
      navigationBarBackgroundColor: '#fff',
      navigationBarTitleText: 'WeChat',
      navigationBarTextStyle: 'black'
    },
    plugins: {
      "chooseLocation": {
      "version": "1.0.6",
      "provider": "wx76a9a06e5b4e693e"
      }
    },
    permission: {
      "scope.userLocation": {
      "desc": "你的位置信息将用于小程序定位"
      }
    }
  }

  componentWillMount () {
    // Taro.loadFontFace({
    //   global: true,
    //   family: 'pf',
    //   source: 'url("http://h5.honglounews.com/PF.TTF")',
    // })
    Taro.api.base.admdicttree().then(res =>{
      const cObj = res || {}
      let newDict = {}
      for (let k in cObj) {
        const cArr = cObj[k].map(item => {
          return {
            ...item,
            key: item.dict_label,
            val: item.dict_value
          }
        })
        newDict[k] = cArr
      }
      Taro.$dictData = newDict
      Taro.setStorageSync('dictData', newDict)
    })
    Taro.removeStorageSync('APP_indexOpen')
    Taro.setStorageSync('APP_newsDot', 'yes')
    let that = this
    let uiObj = Taro.getStorageSync('APP_userInfo')
    if (uiObj && uiObj.phone === '15625261123') {
      return
    }
    Taro.login({
      success: function (res) {
        if (res.code) {
          Taro.api.base.apiwxautologin({code: res.code}).then(data => {
            if (data.token === 'error') {
              Taro.removeStorageSync('APP_token')
            } else {
              const userInfo = {
                nickName: data.nickname,
                avatarUrl: data.avatar,
                phone: data.phone,
              }
              Taro.setStorageSync('APP_userInfo', userInfo)
              that.setState({
                userInfo
              })
              Taro.setStorageSync('APP_token', data.token)
            }
          })
        }
      }
    })
  }

  componentDidMount () {}
  componentDidShow () {}

  componentDidHide () {}

  componentDidCatchError () {}

  // 在 App 类中的 render() 函数没有实际作用
  // 请勿修改此函数
  render () {
    return (
      <Provider store={store}>
        <Index />
      </Provider>
    )
  }
}

Taro.render(<App />, document.getElementById('app'))
