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

<script>
import get from 'lodash/get'
import * as R from 'ramda'
import { mapGetters } from 'vuex'
import expectStatus from '@/constants/expectStatus'
import { getNameFilter } from '@/utils/common/tableFilter'
import WindowsMixin from '@/mixins/windows'
import ListMixin from '@/mixins/list'
import ColumnsMixin from '../mixins/columns'
import SingleActionsMixin from '../mixins/singleActions'

export default {
  name: 'CloudaccountList',
  mixins: [WindowsMixin, ListMixin, ColumnsMixin, SingleActionsMixin],
  props: {
    id: String,
    getParams: {
      type: [Object, Function],
      default: () => ({}),
    },
    cloudaccount: Object,
  },
  data () {
    return {
      list: this.$list.createList(this, {
        id: this.id,
        resource: 'samlusers',
        getParams: this.getParam,
        apiVersion: 'v1',
        steadyStatus: Object.values(expectStatus.samluser).flat(),
        filterOptions: {
          name: getNameFilter(),
        },
      }),
      groupActions: [
        {
          label: this.$t('cloudenv.text_104'),
          permission: 'samlusers_create',
          action: () => {
            this.createDialog('SamluserCreateDialog', {
              onManager: this.onManager,
              cloudaccount: this.cloudaccount,
              defaultDomainId: this.userInfo.projectDomainId,
              defaultProjectId: this.userInfo.projectId,
              userId: this.userInfo.id,
            })
          },
          meta: () => {
            return {
              validate: (this.$store.getters.isAdminMode || get(this.cloudaccount, 'domain_id') === this.$store.getters.userInfo.projectDomainId),
              buttonType: 'primary',
            }
          },
        },
        {
          label: this.$t('table.action.delete'),
          permission: 'samlusers_delete',
          action: () => {
            this.createDialog('DeleteResDialog', {
              vm: this,
              data: this.list.selectedItems,
              columns: this.columns,
              title: this.$t('table.action.delete'),
              name: this.$t('res.samlusers'),
              onManager: this.onManager,
            })
          },
          meta: () => this.$getDeleteResult(this.list.selectedItems),
        },
      ],
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
    exportDataOptions () {
      return {
        title: this.$t('dictionary.samluser'),
        downloadType: 'local',
        items: [
          { field: 'id', title: 'ID' },
          ...this.columns,
          { field: 'manager', title: this.$t('common_624', [this.$t('dictionary.cloudprovider')]) },
        ],
      }
    },
  },
  watch: {
    cloudEnv (val) {
      this.$nextTick(() => {
        this.list.fetchData(0)
      })
    },
  },
  created () {
    this.initSidePageTab('samluser-detail')
    this.list.fetchData()
  },
  methods: {
    getParam () {
      const ret = {
        ...(R.is(Function, this.getParams) ? this.getParams() : this.getParams),
      }
      if (this.cloudEnv) ret.cloud_env = this.cloudEnv
      return ret
    },
    handleOpenSidepage (row) {
      this.sidePageTriggerHandle(this, 'SamluserSidePage', {
        id: row.id,
        resource: 'samlusers',
        getParams: this.getParams,
        refresh: this.refresh,
        apiVersion: 'v1',
      }, {
        list: this.list,
      })
    },
  },
}
</script>
