<script setup>

import { computed } from 'vue';
import { FormGroup, Radiobutton, MultiSelect } from '@cloudron/pankow';
import { ACL_OPTIONS } from '../constants.js';

const props = defineProps({
  users: {
    type: Array,
    required: true,
  },
  groups: {
    type: Array,
    required: true,
  },
  manifest: {
    type: Object,
    required: true,
  },
  sso: {
    type: Boolean,
    default: false,
    required: false,
  },
  installation: {
    type: Boolean,
    required: true,
  },
});

const accessRestrictionOption = defineModel('option');
const accessRestriction = defineModel('acl');

const optionalSso = computed(() => {
  return !!props.manifest.optionalSso && props.installation;
});
const cloudronAuth = computed(() => {
  return !(!props.installation && !props.sso) && !!(props.manifest.addons['ldap'] || props.manifest.addons['oidc'] || props.manifest.addons['proxyAuth']);
});

</script>

<template>
  <div>
    <FormGroup>
      <label>{{ $t('appstore.installDialog.userManagement') }} <sup v-if="!manifest.addons.email"><a href="https://docs.cloudron.io/apps/#access-control" class="help" target="_blank"><i class="fa fa-question-circle"></i></a></sup></label>
      <div v-if="cloudronAuth && !manifest.addons.email">{{ $t('app.accessControl.userManagement.description') }}</div>
      <div v-if="!cloudronAuth && !manifest.addons.email">{{ $t('appstore.installDialog.userManagementNone') }}</div>
      <div v-if="manifest.addons.email" v-html="$t('appstore.installDialog.userManagementMailbox')"></div>
    </FormGroup>

    <div style="padding-top: 10px" v-if="!cloudronAuth || manifest.addons.email"/>

    <FormGroup>
      <label v-if="!cloudronAuth || manifest.addons.email">{{ $t('app.accessControl.userManagement.dashboardVisibility') }} <sup><a href="https://docs.cloudron.io/apps/#no-sso" class="help" target="_blank"><i class="fa fa-question-circle"></i></a></sup></label>
      <div v-if="!cloudronAuth || manifest.addons.email">{{ $t('app.accessControl.dashboardVisibility.description') }}</div>
    </FormGroup>

    <div style="padding-top: 10px" v-if="!cloudronAuth || manifest.addons.email"/>

    <div>
      <Radiobutton v-model="accessRestrictionOption" :value="ACL_OPTIONS.NOSSO" v-if="cloudronAuth && optionalSso" :label="$t('appstore.installDialog.userManagementLeaveToApp')"/>
      <Radiobutton v-model="accessRestrictionOption" :value="ACL_OPTIONS.ANY" :label="cloudronAuth ? $t('appstore.installDialog.userManagementAllUsers') : $t('app.accessControl.userManagement.visibleForAllUsers')"/>
      <Radiobutton v-model="accessRestrictionOption" :value="ACL_OPTIONS.RESTRICTED" :label="cloudronAuth ? $t('appstore.installDialog.userManagementSelectUsers') : $t('app.accessControl.userManagement.visibleForSelected')"/>
    </div>

    <div v-if="accessRestrictionOption === ACL_OPTIONS.RESTRICTED" style="margin-top: 12px; margin-left: 20px; display: flex; gap: 10px;">
      <div>
        {{ $t('appstore.installDialog.users') }}: <MultiSelect v-model="accessRestriction.users" :options="users" option-key="id" option-label="label" :search-threshold="20" />
      </div>
      <div>
        {{ $t('appstore.installDialog.groups') }}: <MultiSelect v-model="accessRestriction.groups" :options="groups" option-key="id" option-label="name" :search-threshold="20" />
      </div>
    </div>
  </div>
</template>
