import Taro, { Component } from '@tarojs/taro'
import { View, Image, Text, Navigator } from '@tarojs/components'

import { AtSearchBar } from 'taro-ui'
import ListMore from '@/c/pageDataList/listMore'
import Footer from '@/c/footer'
import './index.scss'
import { arrToObj } from '@utils'

class Index extends Component {
  onShareAppMessage() {
    return {
      title: '洪楼Plus-查学区',
    }
  }
  onShareTimeline () {
    return {
      title: '南昌买房，看洪楼Plus',
    }
  }

  constructor (props) {
    super(props)
    this.state = {
      searchKey: '',          
      page_size: 10,
      page: 1,
      isListEnd: false,
      isListLoading: false,
      isListEmpty: false,
      dataList: [],
      curNav: 1,
      isDShow: false,
      curObj: {},
      searchIndex: -1,
      area_type: [],
      school_type: [],
      school_attrib: [],
      searchText1: '',
      searchText2: '',
      searchText3: '',
    }
  }

  config = {
    navigationBarTitleText: '查学校',
  }

  componentWillMount () {
    this.getDataList()
  }

  componentDidShow () { }

  componentDidHide () { }

  renderSearch () {
    const { searchKey } = this.state
    return (
      <AtSearchBar
        value={searchKey}
        fixed={true}
        placeholder="请输入学校名称"
        onChange={this.onSeachChange.bind(this)}
        onActionClick={this.onSelectActionClick.bind(this)}
      />
    )
  }
  onSeachChange (value) {
    this.setState({
      searchKey: value
    })
  }
  onClearHandle () {
    this.setState({
      searchKey: '',
      page: 1
    }, () => {
      this.getDataList()
    })
  }
  onSelectActionClick () {
    this.setState({
      page: 1,
    }, () => {
      this.getDataList()
    })
  }

  searchIndex1Render () {
    const { area_type } = this.state
    const dictData = Taro.getStorageSync('dictData')
    const edIcon = require('@img/images/icon_g_ed.png')
    const rangeViews = dictData.area_type.map(item =>{
      let curClassName = "stc-op"
      area_type.forEach(curVal =>{
        if (curVal === item.val) {
          curClassName = "stc-op cur"
        }
      })
      return (
        <View className={curClassName} key={item.val} onClick={this.comSelectHandle.bind(this, item, 'area_type')}>{item.key}
          <Image className="img" src={edIcon} />
        </View>
      )
    })
    let allClassName = area_type.length === 0 ? 'stc-op cur' : 'stc-op'
    return (
      <View className="stc-wrap">
        <View className="stc-item">
          <View className={allClassName} onClick={this.comSelectHandle.bind(this, 'all', 'area_type')}>不限</View>
          {rangeViews}
        </View>
      </View>
    )
  }
  searchIndex2Render () {
    const { school_type } = this.state
    const dictData = Taro.getStorageSync('dictData')
    const edIcon = require('@img/images/icon_g_ed.png')
    const rangeViews = dictData.school_type.map(item =>{
      let curClassName = "stc-op"
      school_type.forEach(curVal =>{
        if (curVal === item.val) {
          curClassName = "stc-op cur"
        }
      })
      return (
        <View className={curClassName} key={item.val} onClick={this.comSelectHandle.bind(this, item, 'school_type')}>{item.key}
          <Image className="img" src={edIcon} />
        </View>
      )
    })
    let allClassName = school_type.length === 0 ? 'stc-op cur' : 'stc-op'
    return (
      <View className="stc-wrap">
        <View className="stc-item">
          <View className={allClassName} onClick={this.comSelectHandle.bind(this, 'all', 'school_type')}>不限</View>
          {rangeViews}
        </View>
      </View>
    )
  }
  searchIndex3Render () {
    const { school_attrib } = this.state
    const dictData = Taro.getStorageSync('dictData')
    const edIcon = require('@img/images/icon_g_ed.png')
    const rangeViews = dictData.school_attrib.map(item =>{
      let curClassName = "stc-op"
      school_attrib.forEach(curVal =>{
        if (curVal === item.val) {
          curClassName = "stc-op cur"
        }
      })
      return (
        <View className={curClassName} key={item.val} onClick={this.comSelectHandle.bind(this, item, 'school_attrib')}>{item.key}
          <Image className="img" src={edIcon} />
        </View>
      )
    })
    let allClassName = school_attrib.length === 0 ? 'stc-op cur' : 'stc-op'
    return (
      <View className="stc-wrap">
        <View className="stc-item">
          <View className={allClassName} onClick={this.comSelectHandle.bind(this, 'all', 'school_attrib')}>不限</View>
          {rangeViews}
        </View>
      </View>
    )
  }
  renderTop () {
    const { searchIndex } = this.state
    const {
      searchText1,
      searchText2,
      searchText3,
    } = this.state
    return (
      <View className="l-search-top t2">
        <View className="st-nav">
          <View className={(searchIndex === 1 || searchText1) ? 'stn-op col-3 cur' : 'stn-op col-3'} onClick={this.searchNavHandle.bind(this, 1)}>
            <View className="stn-label">{searchText1 || '区域'}</View>
            <View className="stn-sign"></View>
          </View>
          <View className={(searchIndex === 2 || searchText2) ? 'stn-op col-3 cur' : 'stn-op col-3'} onClick={this.searchNavHandle.bind(this, 2)}>
            <View className="stn-label">{searchText2 || '类型'}</View>
            <View className="stn-sign"></View>
          </View>
          <View className={(searchIndex === 3 || searchText3) ? 'stn-op col-3 cur' : 'stn-op col-3'} onClick={this.searchNavHandle.bind(this, 3)}>
            <View className="stn-label">{searchText3 || '属性'}</View>
            <View className="stn-sign"></View>
          </View>
        </View>
        <View className={searchIndex > 0 ? 'st-content show' : 'st-content'}>
          {
            searchIndex === 1
            &&
            this.searchIndex1Render()
          }
          {
            searchIndex === 2
            &&
            this.searchIndex2Render()
          }
          {
            searchIndex === 3
            &&
            this.searchIndex3Render()
          }
          <View className="l-floor-footer t2">
            <View className="lff-flex">
              <View className="lff-btn t3" onClick={this.resetHandle.bind(this)} >重置</View>
              <View className="lff-btn" onClick={this.saveHandle.bind(this)}>搜索</View>
            </View>
          </View>
        </View>
      </View>
    )
  }
  searchNavHandle (index) {
    let { searchIndex } = this.state
    searchIndex = index === searchIndex ? -1 : index
    this.setState({
      searchIndex
    })
  }
  resetHandle () {
    const { searchIndex } = this.state
    // this.setState({
    //   searchIndex: -1,
    // })
    if (searchIndex === 1) {
      this.setState({
        area_type: [],
        searchText1: '',
      })
    }
    if (searchIndex === 2) {
      this.setState({
        school_type: [],
        searchText2: '',
      })
    }
    if (searchIndex === 3) {
      this.setState({
        school_attrib: [],
        searchText3: '',
      })
    }
  }
  comSelectHandle (curOp, str) {
    if (curOp === 'all') {
      this.setState({
        [str]: []
      })
    } else {
      const state = this.state
      let curStr = state[str]
      let ed = false
      let curI = 0
      curStr.forEach((item, index) => {
        if (curOp.val === item) {
          ed = true
          curI = index
        }
      })
      if (ed) {
        curStr.splice(curI, 1)
      } else {
        curStr.push(curOp.val)
      }
      this.setState({
        [str]: curStr
      })
    }
  }
  saveHandle () {
    const dictData = Taro.getStorageSync('dictData')
    const { searchIndex } = this.state
    const { 
      area_type,
      school_type,
      school_attrib,
    } = this.state
    if (searchIndex === 1) {
      let tagArr = []
      area_type.forEach(v => {
        tagArr.push(arrToObj(dictData.area_type)[v])
      })
      if (tagArr) {
        this.setState({
          searchText1: tagArr.join(',')
        })
      }
    }
    if (searchIndex === 2) {
      let tagArr = []
      school_type.forEach(v => {
        tagArr.push(arrToObj(dictData.school_type)[v])
      })
      if (tagArr) {
        this.setState({
          searchText2: tagArr.join(',')
        })
      }
    }
    if (searchIndex === 3) {
      let tagArr = []
      school_attrib.forEach(v => {
        tagArr.push(arrToObj(dictData.school_attrib)[v])
      })
      if (tagArr) {
        this.setState({
          searchText3: tagArr.join(',')
        })
      }
    }
    this.setState({
      page: 1,
      searchIndex: -1,
    }, () => {
      this.getDataList()
    })
  }

  getDataList () {
    let { page_size, page, dataList, isListEmpty } = this.state
    const { 
      searchKey,
      area_type,
      school_type,
      school_attrib,
    } = this.state
    Taro.api.house.admschoollist({
      page,
      page_size,
      school_name: searchKey,
      area_type: area_type.join(','),
      school_type: school_type.join(','),
      school_attrib: school_attrib.join(','),
    }).then(res => {
      const curData = res.list || []
      let isListEnd = false
      if (curData.length > 0) {
        if (page === 1) {
          dataList = curData
        } else {
          dataList = dataList.concat(curData)
        }
        if (curData.length === page_size) {
          isListEnd = false
        } else {
          isListEnd = true
        }
      }
      if (curData.length === 0 && page === 1) {
        isListEmpty = true
        dataList = []
      } else {
        isListEmpty = false
      }
      this.setState({
        dataList,
        isListEnd,
        isListEmpty,
        isListLoading: false
      })
    })
  }
  onScrollToLower (e) {
    let { isListEnd, isListLoading, page } = this.state
    if (!isListEnd && !isListLoading) {
      page++
      this.setState({
        page,
        isListLoading: true
      }, () => {
        this.getDataList()
      })
    }
  }

  renderList () {
    const dictData = Taro.getStorageSync('dictData')
    const { dataList, isListEnd, isListLoading, isListEmpty } = this.state
    const rankList = dataList.map((item, index) => {
      return (
        <Navigator url={'/pagesMore/school/indexDtl?id=' + item.id} className="lb-item" key={index}>
          <View className="lb-img">
            <Image className="img" src={item.pri_image + '_xs'} />
          </View>
          <View className="lb-info">
            <View className="lb-p1">{item.school_name}</View>
            <View className="lb-p2">{item.address}</View>
            <View className="lb-tag">
              <View className="t t1">{arrToObj(dictData.school_attrib)[item.school_attrib]}</View>
              <View className="t t4">{arrToObj(dictData.school_type)[item.school_type]}</View>
            </View>
          </View>
        </Navigator>
      )
    })
    return (
      <ScrollView
        className='l-scroll-view'
        scrollY
        scrollWithAnimation
        scrollTop="0"
        lowerThreshold="30"
        onScrollToLower={this.onScrollToLower.bind(this)}
      >
        <View className="l-list-box">
          {rankList}
        </View>
        <ListMore isListEnd={isListEnd} isListLoading={isListLoading} isListEmpty={isListEmpty} />
      </ScrollView>
    )
  }

  render () {
    return (
      <View className="l-box has-footer">
        {this.renderSearch()}
        {this.renderTop()}
        {this.renderList()}
        <Footer current={3} />
      </View>
    )
  }
}

export default Index
