<template>
  <ai-list v-if="!isShowDetail">
    <template slot="title">
      <ai-title :title="moduleName" :isShowBottomBorder="false" :isShowArea="true" 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" :moduleName="moduleName" :moduleId="moduleId" :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>
  <Add v-else-if="component === 'Add'" :moduleName="moduleName" :moduleId="moduleId" :areaId="areaId" :params="params" :instance="instance" :dict="dict" :permissions="permissions" @change="onChange"></Add>
  <Detail v-else-if="component === 'Detail'" :moduleName="moduleName" :moduleId="moduleId" :params="params" :instance="instance" :dict="dict" :permissions="permissions" @change="onChange"></Detail>
  <Comment v-else-if="component === 'Comment'" :moduleName="moduleName" :moduleId="moduleId" :areaId="areaId" :params="params" :instance="instance" :dict="dict" :permissions="permissions" @change="onChange"></Comment>
</template>

<script>
  import List from './components/List'
  import Add from './components/Add'
  import Comment from './components/Comment'
  import Detail from './components/Detail'
  import Audit from './components/Audit'
  import { mapState } from 'vuex'

  export default {
    name: 'AppContentInfo',
    label: '内容发布',

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

    data () {
      return {
        areaId: '',
        component: 'List',
        params: {},
        moduleId: '',
        include: [],
        moduleName: '',
        tabs: [],
        currIndex: '0',
        isShowDetail: false
      }
    },

    components: {
      Add,
      List,
      Audit,
      Comment,
      Detail
    },

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

    created () {
      this.moduleId = this.$route.query.moduleId
      this.areaId = this.user.info.areaId

      this.instance.post('/app/appcontentmoduleinfo/queryDetailById?id=' + this.$route.query.moduleId).then(res => {
        if (res.code === 0) {
          this.moduleName = res.data.moduleName
          this.tabs = [
            {label: this.moduleName, name: 'List', comp: List, permission: ''},
            {label: `${this.moduleName}审核`, name: 'Audit', comp: Audit, permission: 'app_appcontentinfo_examine'}
          ].filter(item => {
            return item.name !== 'Audit' || (res.data.needExamine === '1' && item.name === 'Audit' && this.permissions(item.permission))
          })
        }
      })
    },

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

        if (data.type === 'Detail') {
          this.component = 'Detail'
          this.params = data.params
          this.isShowDetail = Comment
        }

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

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

        if (data.type === 'Comment') {
          this.component = 'Comment'
          this.params = data.params
          this.isShowDetail = true
        }
      },

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

<style lang="scss">
  .doc-circulation {
    height: 100%;
    background: #F3F6F9;
    overflow: auto;
  }
</style>
