<template>
  <div class="flex mt10 align-center space-between">
    <!--          行标签 -->
    <yl-select
      v-model="logic.row_index"
      :options="getOptions('row')"
      class="group"
      @change="changeHanlde('row', getOptions('row'), $event)"
    ></yl-select>
    <yl-select
      v-model="logic.cell_index"
      :options="getOptions('col')"
      class="ml10"
      @change="changeHanlde('col', getOptions('col'), $event)"
    ></yl-select>

    <!--是否被选中-->
    <yl-select v-model="logic.operator" :options="completionOptions" class="ml10"></yl-select>
    <yl-input v-model="logic.value" class="ml10" />
    <div class="action-setting flex ml10">
      <slot name="button"></slot>
    </div>
  </div>
</template>
<script setup>
import { completionOptions } from '@/utils/questionSteeingList.js';
import YlSelect from '@/components/YLSelect.vue';
import YlInput from '@/components/YLInput.vue';
const logic = defineModel('logic', {
  type: Object,
  default: () => {
    return {};
  }
});
const beforeQuesOptions = defineModel('beforeQuesOptions', {
  type: Array,
  default: () => {
    return [];
  }
});

const getOptions = (type) => {
  let options = [];
  beforeQuesOptions.value.forEach((item) => {
    if (item.question_index === logic.value.question_index) {
      if (item.question_type === 8) {
        // 不是分组
        if (logic.value.is_option_group === 0) {
          options = item.options[type === 'row' ? 0 : 1].map((optItem) => {
            return {
              ...optItem,
              label: optItem.option,
              value: optItem.option_index
            };
          });
        }
      }
    }
  });
  return options;
};

const changeHanlde = (type, options, value) => {
  let optionInfo = options.find((item) => {
    return item.option_index === value;
  });
  if (type === 'row') {
    logic.value.row_index = optionInfo?.option_index || null;
    logic.value.relation_question_row_index = optionInfo?.parent_question_index || 0;
    logic.value.row_type = optionInfo?.type || 0;
  } else if (type === 'col') {
    logic.value.cell_index = optionInfo?.option_index || null;
    logic.value.relation_question_cell_index = optionInfo?.parent_question_index || 0;
    logic.value.cell_type = optionInfo?.type || 0;
  }
};
</script>
<style scoped lang="scss"></style>
