<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import { CircleCloseFilled } from '@element-plus/icons-vue'

import { useTag } from '../../composables/useQuestion'

defineEmits(['remove'])

const route = useRoute()

const modelValue: any = defineModel()

const formRef = ref<FormInstance>()
const form = reactive({
  questions: modelValue,
})

const { tagList } = useTag(route.query.id as string)

function getTagCount(id: string) {
  return tagList.value.find((item) => item.id === id)?.count || '未计算'
}

defineExpose({ formRef })
</script>

<template>
  <el-form ref="formRef" label-width="120px" :model="form" scroll-to-error>
    <el-card
      shadow="hover"
      :id="`site-card${index}`"
      class="box-card"
      v-for="(item, index) in form.questions"
      :key="item">
      <el-icon @click="$emit('remove', index)" v-if="form.questions.length > 1" class="close" size="28" color="#c01c40">
        <CircleCloseFilled />
      </el-icon>
      <el-form-item
        label="本题分值"
        class="head-r"
        :prop="`questions.${index}.score`"
        :rules="{ required: true, message: '请输入' }">
        <el-input-number v-model="item.score" :min="0" :max="100" controls-position="right" />
      </el-form-item>
      <el-form-item label="题目标题" :prop="`questions.${index}.title`" :rules="{ required: true, message: '请输入' }">
        <el-input v-model="item.title" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="题干内容">
        <el-input v-model="item.content" :rows="5" type="textarea" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="评分规则">
        <el-radio-group v-model="item.answer.rule.type">
          <el-radio label="1">标签规则+计算结果</el-radio>
          <el-radio label="2">仅标签规则</el-radio>
          <el-radio label="3">仅计算结果</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="正确标签规则"
        :prop="`questions.${index}.answer.choose`"
        :rules="{ required: true, message: '请输入' }">
        <el-select v-model="item.answer.choose">
          <el-option v-for="item in tagList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
        <p style="margin-left: 10px">满足该标签的用户数量：{{ getTagCount(item.answer.choose) }}</p>
      </el-form-item>
      <el-form-item label="答案解析" v-if="false">
        <el-input v-model="item.answer_analysis" :rows="5" type="textarea" placeholder="请输入" />
      </el-form-item>
    </el-card>
  </el-form>
</template>
<style lang="scss" scoped>
.close {
  position: absolute;
  top: -10px;
  right: -10px;
  cursor: pointer;
}
.box-card {
  padding-top: 20px;
  position: relative;
  margin-bottom: 30px;
  overflow: visible;
}
.head-box {
  display: flex;
  margin-bottom: 30px;
  .head-r {
    margin-left: auto;
  }
}
.upload-box {
  display: flex;
  .upload-loading {
    width: 300px;
    display: flex;
    align-items: center;
    &:hover {
      .succ-icon2 {
        display: block;
      }
      .succ-icon1 {
        display: none;
      }
    }
    .succ-icon2 {
      display: none;
      cursor: pointer;
    }
  }
}
</style>
