<template>
  <ai-list v-if="!isShowDetail">
    <template slot="title">
      <ai-title title="订单管理" :isShowBottomBorder="false" :isShowArea="currIndex === '0'" :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>
</template>

<script>
  import GirdOrderList from './components/GirdOrderList.vue'
  import ResidentOrderList from './components/ResidentOrderList'
  import { mapState } from 'vuex'

  export default {
    name: 'AppIntegratingOrder',
    label: '订单管理',

    components: {
      ResidentOrderList,
      GirdOrderList
    },

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

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

      tabs () {
        const tabList = [
          {label: '网格积分订单', name: 'GirdOrderList', comp: GirdOrderList, permission: ''},
          {label: '居民积分订单', name: 'ResidentOrderList', comp: ResidentOrderList, permission: ''}
        ].filter(item => {
          return true
        })

        return tabList
      }
    },

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

    created () {
      this.areaId = this.user.info.areaId
      if (this.$route.query.id) {
        this.componentName = this.$route.query?.type
        this.params = {id: this.$route.query?.id}
        this.isShowDetail = true
      }
    },

    methods: {
      onAreaChange () {
        if (this.currIndex === '0') {
          this.$nextTick(() => {
            this.$refs[this.currIndex][0].getList()
          })
        }
      },

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

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

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