<template>
  <div class="option-action">
    <draggable
      :is-preview="isPreview"
      :data="element"
      item-key="optison_index"
      :handle="handle"
      chosenClass="chosen"
      animation="300"
      :scroll="true"
      @update:data="updateData"
    >
      <!-- eslint-disable-next-line -->
      <template #item="{ element, index }">
        <div class="flex align-center option-action-container space-between">
          <slot name="item" :element="element" :index="index"></slot>
          <span v-if="active" class="flex ml10">
            <!--<van-icon class-prefix="mobilefont"
            name="setting "
              @click="openMoveModel(element, index)
              "/>-->

            <van-icon class-prefix="mobilefont" name="del1 " @click="deleteOption(index)" />

            <van-icon
              class="ml10"
              class-prefix="mobilefont"
              name="gengduo "
              @click="openOptionActionModel(element, index)"
            />
          </span>
        </div>
      </template>
    </draggable>
  </div>
  <!--  操作项弹窗-->
  <van-action-sheet v-model:show="show">
    <div class="van-action-sheet-title">选项操作</div>

    <van-cell-group :border="false" class="br12 round-group">
      <van-cell title="固定置底">
        <template #right-icon>
          <van-switch
            v-model="activeOption.is_fixed"
            class="option-action-sheet-switch"
            size="0.5rem"
            :active-value="1"
            @change="handleLastOption"
            :inactive-value="0"
          ></van-switch>
        </template>
      </van-cell>
      <van-cell title="设为其他项">
        <template #right-icon>
          <van-switch
            v-model="activeOption.is_other"
            class="option-action-sheet-switch"
            size="0.5rem"
            :active-value="1"
            :inactive-value="0"
          ></van-switch>
        </template>
      </van-cell>
      <!--复选时出现-->
      <checkbox-action
        v-if="question?.question_type === 2"
        v-model="activeOption.is_remove_other"
      ></checkbox-action>
    </van-cell-group>
  </van-action-sheet>
  <van-action-sheet
    v-model:show="moveShow"
    cancel-text="取消"
    :actions="actions"
    @select="optionMove"
    @cancel="moveShow = false"
  >
    <!--    <template #description>-->
    <!--      <div class="flex flex-start">操作选项</div>-->
    <!--    </template>-->
    <!--    <van-cell-group :border="false" class="ml10">-->
    <!--      <van-cell title="下移选项" :border="false" @click="optionMove('down')"></van-cell>-->
    <!--      <van-cell title="上移选项" :border="false" @click="optionMove('up')"></van-cell>-->
    <!--    </van-cell-group>-->
  </van-action-sheet>
</template>
<script setup>
import CheckboxAction from './components/OptionItemAction/CheckboxAction.vue';
import { ref } from 'vue';
import { showConfirmDialog } from 'vant';
import Draggable from '@/views/Design/components/Draggable.vue';

// 是否是预览模式
const isPreview = defineModel('isPreview', { default: false, type: Boolean });

defineProps({
  active: {
    type: Boolean,
    default: false
  },
  question: {
    type: Object,
    default: () => {
      // 空
    }
  },
  handle: {
    type: String,
    default: ''
  }
});
const element = defineModel('data', {
  type: Array,
  default: () => []
});

const actions = [
  { name: '上移选项', action: 'up' },
  { name: '下移选项', action: 'down' }
];
// const emit = defineEmits(['update:data']);
const show = ref(false);
const moveShow = ref(false);
const activeOption = ref({});
const activeIndex = ref(-1);

const updateData = (newValue) => {
  element.value = newValue;
};

/**
 * 将当前激活的选项置底
 */
function handleLastOption(value) {
  // 检查是否有有效的选项被激活
  if (value === 0) return;
  if (!activeIndex.value >= 0) return;

  // 寻找当前激活的选项
  const activeOption = element.value[activeIndex.value];
  activeOption.is_fixed = value;
  // // 触发数据更新
  // updateData([...element.value]);
}

// emit('update:data');
/**
 * @name 打开model弹窗
 * @created_date 2025/3/4
 * @description
 **/
const openOptionActionModel = (item, index) => {
  show.value = true;
  activeOption.value = item;
  activeIndex.value = index;
};
// const openMoveModel = (item, index) => {
//   moveShow.value = true;
//   activeOption.value = item;
//   activeIndex.value = index;
// };
// 上下移动
const optionMove = (action) => {
  switch (action.action) {
    case 'up':
      if (activeIndex.value === 0) {
        moveShow.value = false;
        return false;
      }
      // 向上移动
      element.value.splice(activeIndex.value - 1, 0, element.value.splice(activeIndex.value, 1)[0]);
      activeIndex.value -= 1;
      break;
    case 'down':
      if (activeIndex.value === element.value.length - 1) {
        moveShow.value = false;
        return false;
      }
      element.value.splice(activeIndex.value + 1, 0, element.value.splice(activeIndex.value, 1)[0]);
      activeIndex.value += 1;
      break;
  }
};

// 删除当前选项
const deleteOption = (index) => {
  showConfirmDialog({
    title: '提示',
    message: '是否确认删除选项？'
  })
    .then(() => {
      // on confirm
      element.value.splice(index, 1);
    })
    .catch(() => {
      // on cancel
    });
};
</script>
<style scoped lang="scss">
.ml10 {
  margin-left: 10px;
}

.option-action {
  font-size: 20px;

  & .option-action-container {
    font-size: 16px;

    & .mobilefont {
      font-size: 16px;
    }

    & .mobilefont + .mobilefont {
      margin-left: 10px;
    }

    & .van-icon + .van-icon {
      margin-left: 10px;
    }

    & .van-icon + .mobilefont {
      margin-left: 10px;
    }
  }
}
</style>
