<template>
  <page-list
    :show-tag-filter="true"
    :show-tag-columns="true"
    :list="list"
    :columns="columns"
    :group-actions="groupActions"
    :single-actions="singleActions"
    :export-data-options="exportDataOptions" />
</template>

<script>
import WindowsMixin from '@/mixins/windows'
import ListMixin from '@/mixins/list'
import {
  getNameFilter,
  getBrandFilter,
  getStatusFilter,
  getTenantFilter,
  getAccountFilter,
  getRegionFilter,
  getCloudProviderFilter,
  getDescriptionFilter,
} from '@/utils/common/tableFilter'
import SingleActionsMixin from '../mixins/singleActions'
import ColumnsMixin from '../mixins/columns'

export default {
  name: 'WebAppList',
  mixins: [WindowsMixin, ListMixin, ColumnsMixin, SingleActionsMixin],
  props: {
    id: String,
    getParams: {
      type: Object,
    },
  },
  data () {
    return {
      list: this.$list.createList(this, {
        id: this.id,
        resource: 'webapps',
        getParams: this.getParam,
        filterOptions: {
          name: getNameFilter(),
          description: getDescriptionFilter(),
          brand: getBrandFilter('compute_engine_brands'),
          status: getStatusFilter('webapp'),
          tech_stack: {
            label: this.$t('compute.webapp.tech.stack'),
            filter: true,
            formatter: val => {
              return `tech_stack.contains("${val}")`
            },
          },
          projects: getTenantFilter(),
          cloudaccount: getAccountFilter(),
          manager: getCloudProviderFilter(),
          region: getRegionFilter(),
        },
      }),
      exportDataOptions: {
        items: [
          { label: 'ID', key: 'id' },
          { label: this.$t('compute.text_228'), key: 'name' },
          { label: this.$t('compute.text_268'), key: 'status' },
          { label: this.$t('compute.webapp.tech.stack'), key: 'tech_stack' },
          { label: this.$t('compute.webapp.os_type'), key: 'os_type' },
          { label: this.$t('compute.webapp.ip_addr'), key: 'ip_addr' },
          { label: this.$t('compute.webapp.domain'), key: 'hostname' },
          { label: this.$t('compute.webapp.server_farm'), key: 'server_farm' },
          { label: this.$t('table.title.brand'), key: 'provider' },
          { label: this.$t('res.cloudaccount'), key: 'manager' },
          { label: this.$t('res.region'), key: 'region' },
          { label: this.$t('res.project'), key: 'tenant' },
          { label: this.$t('table.title.user_tag'), key: 'user_tags' },
        ],
      },
      groupActions: [
        {
          label: this.$t('common.text00043'),
          permission: 'webapps_syncstatus',
          action: () => {
            this.onManager('batchPerformAction', {
              steadyStatus: ['ready'],
              managerArgs: {
                action: 'syncstatus',
              },
            })
          },
          meta: () => ({
            validate: this.list.selected.length,
          }),
        },
        {
          label: this.$t('table.action.set_tag'),
          permission: 'webapps_set_user_metadata',
          action: () => {
            this.createDialog('SetTagDialog', {
              data: this.list.selectedItems,
              columns: this.columns,
              onManager: this.onManager,
              mode: 'add',
              params: {
                resources: 'webapp',
              },
              tipName: this.$t('compute.webapp'),
            })
          },
          meta: () => ({
            validate: this.list.selected.length,
          }),
        },
      ],
    }
  },
  created () {
    this.initSidePageTab('detail')
    this.list.fetchData()
  },
  methods: {
    getParam () {
      const ret = {
        ...this.getParams,
        details: true,
      }
      return ret
    },
    handleOpenSidepage (row, tab) {
      this.sidePageTriggerHandle(this, 'WebAppSidePage', {
        id: row.id,
        resource: 'webapps',
        getParams: this.getParam,
      }, {
        list: this.list,
        tab,
      })
    },
  },
}
</script>
