<template>
  <div class="cont">
    <van-field
      v-model="element.stem"
      :label="element.stem"
      :required="element.config.is_required === 1"
      label-align="top"
      class="contenteditable-question-title"
    >
      <template #left-icon> {{ isPreview ? element.title : index + 1 }}. </template>
      <template #label>
        <contenteditable
          v-model="element.stem"
          :active="active"
          @blur="emitValue"
          className="contenteditable-label"
          :errorMessage="errorMessage"
        ></contenteditable>
      </template>
      <template #input>
        <el-input-number
          v-model="completionValue"
          v-if="element.config.text_type === 1"
          type="number"
          class="other_input"
          controls-position="right"
          :max="element.config.max"
          @blur="changeValue"
          style="padding: 0; background: #fff"
        ></el-input-number>

        <el-input-number
          v-model="completionValue"
          v-else-if="element.config.text_type === 2"
          :precision="element.config.decimal_few"
          :max="element.config.max"
          type="number"
          controls-position="right"
          class="other_input"
          @blur="changeValue"
          style="padding: 0; background: #fff"
        ></el-input-number>
        <div v-else style="width: 92%">
          <textarea
            v-if="!element.config.line_height || element.config.line_height != 0"
            v-model="completionValue"
            class="other_input"
            :placeholder="element.config.placeholder"
            :rows="element.config.line_height"
          ></textarea>
        </div>
      </template>
    </van-field>
  </div>
</template>

<script setup>
import { toRefs, watch } from 'vue';
import { ElInputNumber } from 'element-plus';
import { showFailToast } from 'vant';
const completionValue = defineModel('completionValue', { default: '', type: String });
const props = defineProps({
  isPreview: {
    type: Boolean,
    default: false
  },
  element: {
    type: Object,
    default: () => {
      // 补充
    }
  },
  index: {
    default: 0
  },
  active: {
    type: Boolean,
    default: false
  },
  questionType: { type: [String, Number], default: 4 }
});
const errorMessage = defineModel('errorMessage', {
  type: String,
  default: ''
});

// 创建一个本地副本以保存更改
const emit = defineEmits(['update:element']);
const { element } = toRefs(props);
const emitValue = () => {
  emit('update:element', element.value);
};
watch(
  () => completionValue.value,
  () => {
    if (!completionValue.value) {
      completionValue.value = null;
    }
  }
);

const changeValue = (values) => {
  console.log(element.value.config);
  if (
    completionValue.value < element.value.config.min &&
    element.value.config.min &&
    completionValue.value
  ) {
    completionValue.value = null;
    showFailToast('请输入大于' + element.value.config.min + '的数字');
  }
};
</script>

<style scoped lang="scss">
.cont {
  .other_input {
    width: 100%;
    margin-top: 10px;
    margin-bottom: 10px;
    padding: 0 12px;
    border: 1px solid #f4f4f4;
    border-radius: 5px;
    outline: none;
    resize: none;
  }

  .other_input::placeholder {
    color: #d7d7d7;
    font-size: 14px;
  }
}

.text_value {
  border: 1px solid #ccc !important;
}
</style>
