<template>
  <ai-list v-if="!isShowDetail">
    <template slot="title">
      <ai-title title="村民圈" :isShowBottomBorder="false" :instance="instance" v-model="areaId" @change="changeArea"></ai-title>
    </template>
    <template slot="tabs">
      <el-tabs v-model="currIndex">
        <el-tab-pane v-for="(tab,i) in tabs" :key="i" :label="tab.label">
          <component :areaId="areaId" :ref="tab.name" v-if="currIndex == i" :is="tab.comp" @change="onChange" lazy :instance="instance" :dict="dict" />
        </el-tab-pane>
      </el-tabs>
    </template>
  </ai-list>
  <Detail v-else-if="componentName === 'Detail'" :params="params" :instance="instance" :dict="dict" @change="onChange"></Detail>
  <CommentsDetail v-else-if="componentName === 'CommentsDetail'" :areaId="areaId" :params="params" :instance="instance" :dict="dict" @change="onChange"></CommentsDetail>
</template>

<script>
  import List from './components/List.vue'
  import CommentsList from './components/CommentsList'
  import CommentsDetail from './components/CommentsDetail'
  import Detail from './components/Detail'
  import { mapState } from 'vuex'

  export default {
    name: 'AppVillagersCircle',
    label: '村民圈',

    components: {
      CommentsDetail,
      CommentsList,
      List,
      Detail
    },

    props: {
      instance: Function,
      dict: Object,
      permissions: Function
    },

    computed: {
      ...mapState(['user']),

      tabs () {
        const tabList = [
          {label: '信息审核', name: 'List', comp: List, permission: ''},
          {label: '评论审核', name: 'CommentsList', comp: CommentsList, permission: ''}
        ]

        return tabList
      }
    },

    data () {
      return {
        currIndex: '0',
        componentName: '',
        params: {},
        areaId: '',
        isShowDetail: false
      }
    },

    created() {
      this.areaId = this.user.info.areaId
    },

    methods: {
      changeArea () {
        this.$nextTick(() => {
          this.$refs[this.tabs[Number(this.currIndex)].name][0].getList()
        })
      },

      onChange (data) {
        if (data.type === 'List') {
          this.componentName = 'List'
          this.isShowDetail = false
          this.params = data.params
        }
        if (data.type === 'CommentsList') {
          this.componentName = 'CommentsList'
          this.isShowDetail = false
          this.params = data.params
        }

        if (data.type === 'Detail') {
          this.componentName = 'Detail'
          this.isShowDetail = true
          this.params = data.params
        }

        if (data.type === 'CommentsDetail') {
          this.componentName = 'Statistics'
          this.isShowDetail = true
          this.params = data.params
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
