<template>
  <template v-for="(item, index) in questionsInfo.logics" :key="index">
    <div
      v-if="item.skip_type === skipType && item.question_index === activeQuestion.question_index"
    >
      <div class="mt10">
        <template v-for="(log, logIndex) in item.logic" :key="logIndex">
          <div>
            <div class="question-before mb10">
              <div class="logics-content flex align-center space-between">
                <!--              固定分组-->
                <!--          if  always-->
                <yl-select
                  v-model="log.logic"
                  :options="logIndex === 0 ? settingIfOptions : settingAndOptions"
                  class="if mr10"
                  @change="logicIf($event, index)"
                ></yl-select>
                <div style="flex: 1" class="action-button align-center">
                  <van-icon
                    style="margin-top: -1px"
                    v-if="logIndex !== 0"
                    name="del"
                    size="0.54rem"
                    class="ml10 theme-color"
                    class-prefix="mobilefont"
                    @click="deleteLogic(logIndex, item.logic, index)"
                  />
                  <van-icon
                    name="add-o"
                    size="0.5rem"
                    class="ml10 theme-color"
                    @click="addLogicItem(logIndex, item.logic)"
                  />

                  <van-icon
                    v-if="logIndex === 0"
                    :name="delSvg"
                    size="0.5rem"
                    class="ml10"
                    @click="remoteLogic(index)"
                  />
                </div>
              </div>
              <div class="flex align-center space-between">
                <!--          问题-->
                <yl-select
                  v-if="log.logic !== 'always'"
                  v-model="log.question_index"
                  class="question mt10"
                  :options="beforeQuesOptions"
                  placeholder="请选择问题"
                  @change="getQuestionType($event, log)"
                ></yl-select>
                <!--          为空 不为空-->
                <yl-select
                  v-if="log.logic !== 'always'"
                  v-model="log.is_answer"
                  class="answer ml10 mt10"
                  :options="answerOptions"
                  :change="changeEmpty(log)"
                ></yl-select>
              </div>
              <!--单选-->
              <BeforeRadio
                v-if="
                  log.logic !== 'always' && log.is_answer !== 0 && [1].includes(log.question_type)
                "
                :activeQuestion="activeQuestion"
                :logic="log"
                :logIndex="logIndex"
                :parentLogic="logics"
                :beforeQuesOptions="beforeQuesOptions"
              >
              </BeforeRadio>
              <!--              多选-->
              <BeforeCkeckbox
                v-if="
                  log.logic !== 'always' && log.is_answer !== 0 && [2].includes(log.question_type)
                "
                :activeQuestion="activeQuestion"
                :logic="log"
                :parentLogic="logics"
                :logIndex="logIndex"
                :beforeQuesOptions="beforeQuesOptions"
              >
              </BeforeCkeckbox>
              <!--              填空-->
              <!--              {{ questionsInfo.logics[index].logic }}-->
              <BeforeCompletion
                v-if="
                  log.logic !== 'always' && log.is_answer !== 0 && [4].includes(log.question_type)
                "
                :activeQuestion="activeQuestion"
                :parentLogic="logics"
                :logic="log"
                :logIndex="logIndex"
                :index="index"
                :beforeQuesOptions="beforeQuesOptions"
                @update-logic="updateLogic"
              >
              </BeforeCompletion>

              <BeforeRate
                v-if="
                  log.logic !== 'always' &&
                  log.is_answer !== 0 &&
                  [5, 106].includes(log.question_type)
                "
                :activeQuestion="activeQuestion"
                :logic="log"
                :parentLogic="logics"
                :logIndex="logIndex"
                :index="index"
                :beforeQuesOptions="beforeQuesOptions"
              >
              </BeforeRate>

              <BeforeMartrixRadio
                v-if="
                  log.logic !== 'always' && log.is_answer !== 0 && [9].includes(log.question_type)
                "
                :activeQuestion="activeQuestion"
                :parentLogic="logics"
                :logic="log"
                :logIndex="logIndex"
                :index="index"
                :beforeQuesOptions="beforeQuesOptions"
              >
              </BeforeMartrixRadio>
              <BeforeMartrixCheckbox
                v-if="
                  log.logic !== 'always' && log.is_answer !== 0 && [10].includes(log.question_type)
                "
                :activeQuestion="activeQuestion"
                :logic="log"
                :parentLogic="logics"
                :logIndex="logIndex"
                :index="index"
                :beforeQuesOptions="beforeQuesOptions"
              >
              </BeforeMartrixCheckbox>

              <BeforeMartrixComplation
                v-if="
                  log.logic !== 'always' && log.is_answer !== 0 && [8].includes(log.question_type)
                "
                :parentLogic="logics"
                :activeQuestion="activeQuestion"
                :logic="log"
                :logIndex="logIndex"
                :index="index"
                :beforeQuesOptions="beforeQuesOptions"
              >
              </BeforeMartrixComplation>
            </div>
          </div>
        </template>
        <!--            如果是题前隐藏-->
        <div v-if="skipType === 1" class="tip-text">隐藏本题</div>
        <!--            如果是题后跳转-->
        <div v-if="skipType === 0" class="">
          <div class="jump-text mr10 tip-text">跳到</div>
          <yl-select
            v-model="item.skip_question_index"
            class="skip-select mt10"
            :options="skipOption"
          ></yl-select>
        </div>
      </div>
    </div>
  </template>
  <div class="text-center mt10">
    <van-button class="add-action-btn br12" size="small" @click="addLogic">添加逻辑</van-button>
  </div>
</template>
<script setup>
import delSvg from '@/assets/img/del.svg';
import { v4 as uuidv4 } from 'uuid';
import { settingIfOptions, settingAndOptions, answerOptions } from '@/utils/questionSteeingList.js';
import YlSelect from '@/components/YLSelect.vue';
import BeforeRadio from './BeforComponent/BeforeRadio.vue';
import BeforeCkeckbox from './BeforComponent/BeforeCheckbox.vue';
import BeforeCompletion from './BeforComponent/BeforeCompletion.vue';
import BeforeRate from './BeforComponent/BeforeRate.vue';
import BeforeMartrixRadio from './BeforComponent/BeforeMartrixRadio.vue';
import BeforeMartrixCheckbox from './BeforComponent/BeforeMartrixCheckbox.vue';
import BeforeMartrixComplation from './BeforComponent/BeforeMartrixComplation.vue';
import { ref, watch } from 'vue';
const props = defineProps({
  activeQuestion: {
    type: Object,
    default: () => {
      return {};
    }
  }
});

const questionsInfo = defineModel('questionsInfo', {
  type: Object,
  default: () => {
    return {};
  }
});
const logics = ref(questionsInfo.value.logics);
const questions = questionsInfo.value.questions;
const skipType = defineModel('skipType', {
  type: [Number, String],
  default: ''
});
// 变量为beforeList  questions里面 获取activeQuestion 之前所有的题目

const questionIndex = questions.findIndex((item) => {
  return item.id === props.activeQuestion.id;
});

const changeEmpty = (log) => {
  if (log.is_answer === 0) {
    log.option_index = 0;
  }
};

// 当前题目与之前的列表
let beforeQuesOptions = ref([]);
watch(
  () => skipType.value,
  () => {
    if (skipType.value === 0) {
      beforeQuesOptions.value = questions.slice(0, questionIndex + 1).map((item) => {
        return {
          ...item,
          label: item.title + ' ' + item.stem,
          value: item.question_index
        };
      });
    } else {
      // 当前题目之前的列表
      if (questionIndex > 0) {
        beforeQuesOptions.value = questions.slice(0, questionIndex).map((item) => {
          return {
            ...item,
            label: item.title + ' ' + item.stem,
            value: item.question_index
          };
        });
      }
    }
  },
  {
    immediate: true
  }
);

// if (props.skipType === 0) {
//   beforeQuesOptions = questions.slice(0, questionIndex + 1).map((item) => {
//     return {
//       ...item,
//       label: item.title + ' ' + item.stem,
//       value: item.question_index
//     };
//   });
// } else {
//   // 当前题目之前的列表
//   if (questionIndex > 0) {
//     beforeQuesOptions = questions.slice(0, questionIndex).map((item) => {
//       return {
//         ...item,
//         label: item.title + ' ' + item.stem,
//         value: item.question_index
//       };
//     });
//   }
// }

// 跳转题目
const skipOption = [
  {
    value: -1,
    label: '正常完成'
  },
  {
    value: -2,
    label: '提前终止'
  },
  {
    value: -3,
    label: '配额超限'
  }
];
// questions 排除当前题目的所有数据 并且 排除questions里面无question_type的数据

const emit = defineEmits(['update-logic']);
const updateLogic = (ev) => {
  // 创建 logics 的副本
  const newLogics = [...questionsInfo.value.logics];
  // 更新逻辑项
  newLogics[ev.index.value].logic.splice(ev.logIndex.value, 1, ev.newVal);
  // 确保触发响应式更新
  questionsInfo.value.logics = [];
  questionsInfo.value.logics = newLogics;
  emit('update-logic', questionsInfo.value.logics);
};

// 跳转题目之后的list

skipOption.push(
  ...questions.slice(questionIndex + 1, questions.length).map((item) => {
    return {
      ...item,
      label: item.stem,
      value: item.question_index
    };
  })
);

// 题目选项
// todo   不同题型逻辑对应不同 需要开发

// 删除 题目设置
const deleteLogic = (logIndex, item) => {
  item.splice(logIndex, 1);
  // 如果小删除  删除后一个都没有了 就删除掉整个数组
  if (item.length === 0) {
    // logics.value.splice(index, 1);
  }
};
//  删除 一个 逻辑
const remoteLogic = (index) => {
  logics.value.splice(index, 1);
};

//  添加题目
const addLogicItem = (logIndex, item) => {
  item.splice(logIndex + 1, 0, {
    logic: 'and',
    question_index: null,
    question_type: 0,
    is_answer: 1,
    operator: '',
    option_index: null,
    relation_question_index: 0,
    type: 0,
    is_option_group: 0,
    group_index: null
  });
};

// 增加逻辑配置
const addLogic = () => {
  logics.value.push({
    skip_type: props.skipType,
    question_index: props.activeQuestion.question_index,
    question_id: props.activeQuestion.id,
    skip_question_index: '',
    id: uuidv4(),
    logic: [
      {
        location: 0,
        type: 0,
        logic: 'if',
        operator: '',
        is_answer: 1,
        question_type: props.activeQuestion.question_type,
        relation_question_index: 0,
        relation_question_row_index: 0,
        relation_question_cell_index: 0,
        is_option_group: 0,
        option_index: null,
        question_id: null
      }
    ]
  });
};
// 获取题目类型
const getQuestionType = (value, log) => {
  const question = beforeQuesOptions.value.filter((item) => item.question_index === value)[0];
  log.question_type = question.question_type;
};

const logicIf = (value, index) => {
  if (value === 'always') {
    const loginItem = logics.value[index].logic[0];
    logics.value[index].logic = [loginItem];
  }
};
</script>

<style scoped lang="scss">
@import '@/assets/css/theme';

.logics-content {
  border-color: transparent;
  outline-color: transparent;
}

.flex1 {
  flex: 1;
}

.mr10 {
  margin-right: 10px;
}

.mt10 {
  margin-top: 10px;
}

.mb10 {
  margin-bottom: 10px;
}

.mv10 {
  margin: 0 5px;
}

.action {
  flex: none;
  width: 40px;
}

.jump-text {
  width: 50px;
}

.tip-text {
  color: #000;
  font-weight: 500;
  font-size: 14px;
}

.add-action-btn {
  height: 35px;
  padding: 12px 18px;
  border: none;
  background: rgba(240, 248, 235, 1);
  color: $theme-color;
}

.question-before {
  width: 100%;

  & .if {
    //flex: none;

    margin-right: 10px;
    //width: 80%;
    //width: 100%;
  }

  & .action-button {
    display: flex;
    width: 100%;
  }
  & .question {
    //flex: none;
    //width: 210px;
  }

  & .group {
    //flex: none;
    //width: 75px;
  }

  & .answer {
    flex: none;
    width: 80px;
  }

  & .symbol {
    flex: none;
    width: 50px;
  }

  & .option {
    width: 100%;
  }

  & .skip-select {
    width: 200px;
  }
}
</style>
