<template>
  <div class="full">
    <el-select-v2
      v-if="editable"
      v-model="selected"
      :options="options"
      class="full"
      :clearable="clearable"
      :size="size"
      :multiple="multiple"
      :collapse-tags="multiple"
      :collapse-tags-tooltip="multiple"
      :fit-input-width="false"
      @change="onChange"
    />
    <span v-else class="plain-label">
      <template v-if="selectedTemplates.length === 1">
        <el-icon
          class="view"
          @click="previewFlow(selectedTemplates[0].uuid)"
          :title="$t('通用.预览')"
        >
          <View />
        </el-icon>
        <OverflowTooltip :content="selectedTemplates[0].templateName" max-width="120px" />
      </template>
      <template v-else-if="selectedTemplates.length >= 2">
        <el-popover placement="bottom" trigger="hover" :width="220">
          <template #reference>
            <span class="link-text">
              {{ $t('工况库.关联N个流程模版', { count: selectedTemplates.length }) }}
            </span>
          </template>
          <div class="template-list">
            <div v-for="item in selectedTemplates" :key="item.uuid" class="template-item">
              <el-icon class="view" @click="previewFlow(item.uuid)">
                <View />
              </el-icon>
              <OverflowTooltip :content="item.templateName" class="item-name" />
            </div>
          </div>
        </el-popover>
      </template>
    </span>
    <flowViewDialog
      v-if="flowVisible"
      v-model:showDialog="flowVisible"
      :flowUuid="currentPreviewUuid"
    ></flowViewDialog>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, computed } from 'vue';
import { useTaskStore } from '@/stores/taskPool';
import flowViewDialog from '@/components/common/flow/flowViewDialog.vue';
import OverflowTooltip from '@/components/common/text/overflowTooltip.vue';
import { View } from '@element-plus/icons-vue';

interface Props {
  modelValue?: any;
  clearable?: boolean;
  editable?: boolean;
  size?: '' | 'large' | 'default' | 'small';
  multiple?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: '',
  clearable: false,
  editable: true,
  size: 'default',
  multiple: true,
});
const emits = defineEmits(['update:modelValue', 'change']);

const parseValue = (value: string): string[] | string => {
  if (!value) return props.multiple ? [] : '';
  if (props.multiple) {
    return value.split(',').filter((v) => v.trim());
  }
  return value;
};

const stringifyValue = (value: string[] | string): string => {
  if (props.multiple && Array.isArray(value)) {
    return value.join(',');
  }
  return String(value || '');
};

const selected = ref<string[] | string>(parseValue(props.modelValue));

watch(
  () => props.modelValue,
  (v) => {
    selected.value = parseValue(v);
  }
);

watch(selected, (v) => {
  const stringValue = stringifyValue(v);
  emits('update:modelValue', stringValue);
});

const taskStore = useTaskStore();

const options = computed(() => taskStore.options);

const selectedCodes = computed(() => {
  if (props.multiple && Array.isArray(selected.value)) {
    return selected.value;
  }
  return selected.value ? [selected.value] : [];
});

const selectedTemplates = computed(() => {
  return taskStore.templates.filter((t: any) => selectedCodes.value.includes(t.templateCode));
});

const onChange = (val: string[] | string) => {
  emits('change', stringifyValue(val));
};

const flowVisible = ref(false);
const currentPreviewUuid = ref('');

const previewFlow = (uuid: string) => {
  currentPreviewUuid.value = uuid;
  flowVisible.value = true;
};
</script>

<style scoped lang="scss">
.full {
  width: 100%;
}

.plain-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  vertical-align: middle;
  line-height: 1;
  max-width: 100%;
  .view {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    cursor: pointer;
    color: var(--el-color-primary);
    flex-shrink: 0;
  }
  .link-text {
    color: var(--el-color-primary);
    cursor: pointer;
    &:hover {
      text-decoration: underline;
    }
  }
}

.template-list {
  .template-item {
    display: flex;
    align-items: center;
    padding: 4px 0;
    .item-name {
      flex: 1;
      min-width: 0;
    }
    .view {
      flex-shrink: 0;
      cursor: pointer;
      color: var(--el-color-primary);
      margin-right: 8px;
    }
  }
}
</style>
