<template>
  <div class="choose-question-container">
    <div
      v-if="showActions"
      class="choose-question-context"
      :class="chooseQuestionId === element.id ? 'choose-question-active' : ''"
      @click="chooseItem"
    >
      <van-cell>
        <template #title>
          <span class="title"> {{ getQuestionType(element.question_type) }}</span>
        </template>
        <template #right-icon>
          <question-action
            v-if="chooseQuestionId === element.id"
            v-model:data="element"
            :questionsInfo="questionsInfo"
            :questions="questions"
            :questionIndex="index"
            @move="emit('move', $event)"
            @copy="emit('copy', $event)"
            @delete="emit('delete', $event)"
            @setting="emit('setting', $event)"
            @logics="emit('logics', $event)"
          ></question-action>
        </template>
      </van-cell>
      <slot></slot>
      <!--      题目操作-->
      <van-cell v-if="chooseQuestionId === element.id" class="choose-question-active-container">
        <template #icon>
          <slot name="action" :element="element" :index="index"></slot>
        </template>
      </van-cell>
    </div>
    <div v-else>
      <slot></slot>
    </div>
  </div>
</template>
<script setup>
import QuestionAction from '@/views/Design/components/ActionCompoents/QuestionAction.vue';
import { basicQuesTypeList } from '@/utils/common.js';
import { watch } from 'vue';

const props = defineProps({
  questions: {
    type: Array,
    default: () => []
  },
  questionsInfo: {
    type: Object,
    default: () => {
      // 后续扩展
    }
  },
  chooseQuestionId: {
    type: String,
    default: '0'
  },
  showActions: {
    type: Boolean,
    default: true
  }
});
const element = defineModel('element', {
  type: Object,
  default: () => {
    return {};
  }
});
const index = defineModel('index');

// watch(
//   () => index.value,
//   (newVal) => {
//     element.value.title = newVal + 1;
//   }
// );

// 获取题目选项
const getQuestionType = (type) => {
  let typeName = null;
  basicQuesTypeList.map((item) => {
    if (Number(item.question_type) === Number(type)) {
      typeName = item.name;
    }
  });
  return typeName;
};

// if (props.index + 1 !== Number(element.value.title)) {
//   element.value.title = props.index + 1;
// }

// 选中题目后出现的操作
const emit = defineEmits(['getChooseQuestionId', 'move', 'copy', 'setting', 'logics']);

// 选中题目
const chooseItem = () => {
  emit('getChooseQuestionId', element.value, index.value);
};
</script>
<style scoped lang="scss">
@import '@/assets/css/theme';

.choose-question-container {
  padding: 12px 12px 0;

  & .choose-question-context {
    overflow: hidden;
    padding: 10px;
    border-radius: 16px;
    background: #fff;

    & .title {
      position: relative;
      font-weight: 800;
      font-size: 14px;

      &::after {
        content: ' ';
        position: absolute;

        //padding: 0 5px;
        bottom: -2px;
        left: -1px;
        width: 100%;
        height: 7px;
        border-radius: 4px;
        background: linear-gradient(90deg, #70b937 0%, rgba(112, 185, 55, 0) 100%);
        opacity: 0.4;
      }
    }
  }

  & .choose-question-active {
    outline: 2px solid #70b936;
  }

  & .ml10 {
    margin-left: 10px;
  }

  & .fs20 {
    font-size: 20px;
  }

  & .choose-question-active-container {
    display: flex;
    align-items: center;
    justify-content: space-between;

    //margin: 0 20px;
    color: #666;
    font-size: 12px;

    & .choose-question-active-container-icon {
      font-size: 14px;
    }

    & .choose-question-active-container-name {
      font-size: 12px;
    }
  }
}
</style>
