<template>
  <ai-list v-if="!isShowDetail">
    <template slot="title">
      <ai-title title="问卷表单" :isShowBottomBorder="false" :instance="instance"></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" :permissions="permissions"/>
        </el-tab-pane>
      </el-tabs>
    </template>
  </ai-list>
  <Add v-else-if="componentName === 'Add'" :areaId="areaId" :params="params" :instance="instance" :dict="dict" :permissions="permissions" @change="onChange"></Add>
  <Statistics v-else-if="componentName === 'Statistics'" :areaId="areaId" :params="params" :instance="instance" :dict="dict" :permissions="permissions" @change="onChange"></Statistics>
</template>

<script>
  import FormList from './components/FormList.vue'
  import Template from './components/Template'
  import Add from './components/Add'
  import Statistics from './components/Statistics'
  import { mapState } from 'vuex'

  export default {
    name: 'AppAskForm',
    label: '问卷表单',

    components: {
      FormList,
      Add,
      Statistics,
      Template
    },

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

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

      tabs () {
        const tabList = [
          {label: '表单列表', name: 'FormList', comp: FormList, permission: ''},
          {label: '共享模板', name: 'Template', comp: Template, permission: ''}
        ].filter(() => {
          return true
        })

        return tabList
      }
    },

    data () {
      return {
        activeName: 'JoinEvent',
        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.isQuote) {
            this.currIndex = 0
          }
        }

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

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

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

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