<script setup lang="ts">
import { computed } from 'vue';
import { objectTypeRecord, scopeTypeRecord } from '@/constants/business';
import { $t } from '@/locales';

defineOptions({ name: 'MenuContextPanel' });

interface Props {
  total?: number;
  loading?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
  total: 0,
  loading: false
});

const scopeType = defineModel<Api.SystemManage.ScopeType>('scopeType', {
  required: true
});

const objectType = defineModel<Api.SystemManage.ObjectType | undefined>('objectType');

const isObjectScope = computed(() => scopeType.value === 'object');
const scopeOptions = computed(() => [
  { label: $t(scopeTypeRecord.global), value: 'global' satisfies Api.SystemManage.ScopeType },
  { label: $t(scopeTypeRecord.object), value: 'object' satisfies Api.SystemManage.ScopeType }
]);

const objectTypeOptions = computed(() => [
  { label: $t(objectTypeRecord.product), value: 'product' satisfies Api.SystemManage.ObjectType },
  { label: $t(objectTypeRecord.project), value: 'project' satisfies Api.SystemManage.ObjectType }
]);

const currentContextLabel = computed(() => {
  if (!isObjectScope.value) {
    return $t(scopeTypeRecord.global);
  }

  if (!objectType.value) {
    return `${$t(scopeTypeRecord.object)} / --`;
  }

  return `${$t(scopeTypeRecord.object)} / ${$t(objectTypeRecord[objectType.value])}`;
});

const currentScopeSummary = computed(() => {
  if (!isObjectScope.value) {
    return $t('page.system.menu.globalResourceSummary');
  }

  if (objectType.value === 'product') {
    return $t('page.system.menu.objectResourceSummaryProduct');
  }

  if (objectType.value === 'project') {
    return $t('page.system.menu.objectResourceSummaryProject');
  }

  return $t('page.system.menu.objectResourceSummary');
});
</script>

<template>
  <ElCard class="menu-context-panel" body-class="menu-context-panel__body">
    <div v-loading="props.loading" class="menu-context-panel__layout">
      <div class="menu-context-panel__controls">
        <div class="menu-context-panel__field menu-context-panel__field--switch">
          <ElSegmented v-model="scopeType" :options="scopeOptions" />
        </div>

        <span v-if="isObjectScope" class="menu-context-panel__divider" aria-hidden="true">|</span>

        <div v-if="isObjectScope" class="menu-context-panel__field menu-context-panel__field--inline">
          <span class="menu-context-panel__field-label menu-context-panel__field-label--inline">
            {{ $t('page.system.menu.objectType') }}
          </span>
          <ElSelect v-model="objectType" class="w-full" :placeholder="$t('page.system.menu.objectTypePlaceholder')">
            <ElOption v-for="item in objectTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </ElSelect>
        </div>
      </div>

      <div class="menu-context-panel__info">
        <div class="menu-context-panel__info-main">
          <div class="menu-context-panel__info-item">
            <span class="menu-context-panel__info-label">{{ $t('page.system.menu.currentContext') }}</span>
            <strong class="menu-context-panel__info-value">{{ currentContextLabel }}</strong>
          </div>

          <div class="menu-context-panel__info-item">
            <span class="menu-context-panel__info-label">{{ $t('page.system.menu.currentResourceCount') }}</span>
            <strong class="menu-context-panel__info-value">{{ props.total }}</strong>
          </div>
        </div>

        <p class="menu-context-panel__info-desc">{{ currentScopeSummary }}</p>
      </div>
    </div>
  </ElCard>
</template>

<style lang="scss" scoped>
.menu-context-panel {
  border: 1px solid var(--el-border-color-light);
  background: var(--el-fill-color-blank);
  box-shadow: none;
}

:deep(.menu-context-panel__body) {
  padding: 16px 18px;
}

.menu-context-panel__layout {
  display: flex;
  align-items: center;
  gap: 20px;
}

.menu-context-panel__controls {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
  flex-shrink: 0;
}

.menu-context-panel__info {
  flex: 1;
  min-width: 0;
  padding-left: 20px;
  border-left: 1px solid var(--el-border-color-lighter);
}

.menu-context-panel__field {
  display: flex;
  align-items: center;
  gap: 10px;
}

.menu-context-panel__field--switch {
  flex-shrink: 0;
}

:deep(.menu-context-panel__field--switch .el-segmented) {
  width: auto;
  padding: 6px;
  background: var(--el-fill-color-light);
  border-radius: 12px;
}

:deep(.menu-context-panel__field--switch .el-segmented__item) {
  min-height: 40px;
  min-width: 96px;
  padding: 0 22px;
  font-size: 14px;
}

.menu-context-panel__field-label {
  color: var(--el-text-color-secondary);
  font-size: 13px;
  line-height: 1.5;
  white-space: nowrap;
}

.menu-context-panel__field--inline {
  min-width: 0;
}

.menu-context-panel__field-label--inline {
  flex-shrink: 0;
}

.menu-context-panel__divider {
  color: var(--el-border-color-darker);
  font-size: 16px;
  line-height: 1;
  user-select: none;
}

:deep(.menu-context-panel__field--inline .el-select) {
  width: 170px;
}

.menu-context-panel__info-main {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
}

.menu-context-panel__info-item {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 180px;
}

.menu-context-panel__info-label {
  color: var(--el-text-color-secondary);
  font-size: 12px;
  line-height: 1.5;
}

.menu-context-panel__info-value {
  color: var(--el-text-color-primary);
  font-size: 15px;
  font-weight: 600;
  line-height: 1.5;
}

.menu-context-panel__info-desc {
  margin-top: 10px;
  color: var(--el-text-color-regular);
  font-size: 13px;
  line-height: 1.6;
}

@media (width <= 1200px) {
  .menu-context-panel__layout {
    flex-direction: column;
    align-items: stretch;
  }

  .menu-context-panel__controls {
    width: 100%;
    justify-content: flex-start;
  }

  .menu-context-panel__info {
    padding-left: 0;
    padding-top: 14px;
    border-left: none;
    border-top: 1px solid var(--el-border-color-lighter);
  }
}

@media (width <= 768px) {
  .menu-context-panel__controls {
    flex-wrap: wrap;
  }

  .menu-context-panel__info-item {
    width: 100%;
    min-width: 0;
    gap: 8px;
    justify-content: space-between;
  }
}
</style>
