<template>
  <ai-list class="AppReturnHomeRegister" v-if="!isShowDetail">
    <template slot="title">
      <ai-title title="返乡登记" :isShowBottomBorder="false" :fullname.sync="areaName" v-model="areaId" :instance="instance" @change="onAreaChange"></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="String(i)" v-if="currIndex == i" :is="tab.comp" @change="onChange" lazy :instance="instance" :dict="dict" :permissions="permissions"/>
        </el-tab-pane>
      </el-tabs>
    </template>
  </ai-list>
  <Detail v-else-if="component === 'Detail'" :params="params" :instance="instance" :dict="dict" :permissions="permissions" @change="onChange"></Detail>
</template>

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

  export default {
    name: 'AppReturnHomeRegister',
    label: '返乡登记',

    components: {
      List,
      Detail,
      AbnormalList
    },

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

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

      tabs () {
        const tabList = [
          {label: '返乡登记', name: 'List', comp: List, permission: ''},
          // {label: '历史异常人员', name: 'AbnormalList', comp: AbnormalList, permission: ''}
        ]

        return tabList
      }
    },

    data () {
      return {
        activeName: 'JoinEvent',
        currIndex: '0',
        component: 'List',
        params: {},
        areaId: '',
        isShowDetail: false,
        areaName: ''
      }
    },

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

    methods: {
      onChange (data) {
        if (data.type === 'Detail') {
          this.component = 'Detail'
          this.isShowDetail = true
          this.params = data.params
        }
        if (data.type === 'AbnormalList') {
          this.component = 'AbnormalList'
          this.isShowDetail = false
          this.params = data.params
        }

        if (data.type === 'List') {
          this.component = 'List'
          this.isShowDetail = false
          this.params = data.params

          this.$nextTick(() => {
            if (data.isRefresh) {
              this.$refs.component.getList()
            }
          })
        }
      },

      onAreaChange () {
        this.$refs[this.currIndex][0].changeArea()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .AppReturnHomeRegister {
    height: 100%;
  }
</style>
