<template>
  <QuestionBase>
    <template #content>
      <div class="stem">
        <el-input
          v-if="data.property.stem_type === stemTypeList[0].value"
          v-model="data.stem"
          rows="3"
          resize="none"
          type="textarea"
          placeholder="输入题干"
        />

        <RichText v-if="data.property.stem_type === stemTypeList[1].value" v-model="data.stem" placeholder="输入题干" />

        <el-input
          v-show="data.property.is_enable_description === 'true'"
          v-model="data.description"
          rows="3"
          resize="none"
          type="textarea"
          placeholder="输入描述"
        />
      </div>

      <div class="content">
        <label class="subtitle">内容</label>
        <ul>
          <li v-for="(item, i) in data.option_list" :key="i" class="content-item">
            <span class="question-number" @dblclick="changeOptionType(data)">
              {{ computedQuestionNumber(i, data.option_number_show_mode) }}.
            </span>
            <el-input v-model="item.content" placeholder="输入内容" @blur="handleItemAnswer(item)" />
            <UploadAudio
              v-if="data.other.audio_generation_method === 'upload'"
              :key="item.audio_file_id || i"
              :file-id="item.audio_file_id"
              :item-index="i"
              @upload="uploads"
              @deleteFile="deleteFiles"
            />
            <div v-else-if="data.other.audio_generation_method === 'auto'" class="auto-matically">
              <AudioPlay :file-id="item.audio_file_id" theme-color="gray" />
              <span class="auto-btn" @click="handleMatically">自动生成</span>
            </div>
            <SoundRecord
              v-else
              :wav-blob="item.audio_wav"
              :record-time="item.audio_wav_time"
              :item-index="i"
              @deleteWav="deleteWav"
              @updataWav="updataWav"
            />
            <SvgIcon icon-class="delete" class="delete pointer" @click="deleteOption(i, item.audio_file_id)" />
          </li>
        </ul>
      </div>

      <div class="footer">
        <span class="add-option" @click="addOption">
          <SvgIcon icon-class="add-circle" size="14" /> <span>增加选项</span>
        </span>
      </div>
    </template>

    <template #property>
      <el-form :model="data.property">
        <el-form-item label="题干">
          <el-radio
            v-for="{ value, label } in stemTypeList"
            :key="value"
            v-model="data.property.stem_type"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
        <el-form-item label="题号">
          <el-input v-model="data.property.question_number" />
        </el-form-item>
        <el-form-item label-width="45px">
          <el-radio
            v-for="{ value, label } in questionNumberTypeList"
            :key="value"
            v-model="data.other.question_number_type"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
        <el-form-item label="描述">
          <el-radio
            v-for="{ value, label } in switchOption"
            :key="value"
            v-model="data.property.is_enable_description"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
        <el-form-item label="分值">
          <el-radio
            v-for="{ value, label } in scoreTypeList"
            :key="value"
            v-model="data.property.score_type"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
        <el-form-item label-width="45px">
          <el-input v-model="data.property.score" type="number" />
        </el-form-item>
        <el-form-item label="类型">
          <el-radio
            v-for="{ value, label } in toneTypeList"
            :key="value"
            v-model="data.property.answer_mode"
            :label="value"
            @change="handleChangeType"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
        <el-form-item label="音频">
          <el-radio
            v-for="{ value, label } in audioGenerationMethodList"
            :key="value"
            v-model="data.other.audio_generation_method"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
      </el-form>
    </template>
  </QuestionBase>
</template>

<script>
import QuestionMixin from '@/views/exercise_questions/create/components/common/QuestionMixin';
import UploadAudio from '../common/UploadAudio.vue';
import SoundRecord from '../common/SoundRecord.vue';

import { changeOptionType } from '@/views/exercise_questions/data/common';
import {
  getOption,
  ChooseToneData,
  audioGenerationMethodList,
  toneList,
  toneTypeList,
} from '@/views/exercise_questions/data/chooseTone';

export default {
  name: 'ChooseToneQuestion',
  components: {
    UploadAudio,
    SoundRecord,
  },
  mixins: [QuestionMixin],
  data() {
    return {
      toneList,
      changeOptionType,
      audioGenerationMethodList,
      toneTypeList,
      data: JSON.parse(JSON.stringify(ChooseToneData)),
    };
  },
  methods: {
    addOption() {
      this.data.option_list.push(getOption());
    },
    uploads(file_id, index) {
      this.data.option_list[index].audio_file_id = file_id;
      this.data.file_id_list.push(file_id);
    },
    deleteFiles(file_id, itemIndex) {
      this.data.option_list[itemIndex].audio_file_id = '';
      this.data.file_id_list.splice(this.data.file_id_list.indexOf(file_id), 1);
    },
    // 删除小题
    deleteOption(i, file_id) {
      this.data.option_list.splice(i, 1);
      this.data.file_id_list.splice(this.data.file_id_list.indexOf(file_id), 1);
    },
    // 自动生成音频
    handleMatically() {},
    // 清除录音
    deleteWav(index) {
      this.data.option_list[index].audio_wav = '';
      this.data.option_list[index].audio_wav_time = 0;
    },
    // 更新录音内容和时间
    updataWav(index, wav, time) {
      this.data.option_list[index].audio_wav = wav;
      this.data.option_list[index].audio_wav_time = time;
    },
    // 答案
    handleItemAnswer(item) {
      const index = this.data.answer.answer_list.findIndex((items) => items.mark === item.mark);
      let content = item.content.trim();
      let content_arr = content.split(' ');
      let select_item = '';
      let content_preview = '';
      content_arr.forEach((items) => {
        let items_trim = items.trim();
        if (items_trim) {
          let items_yuan = JSON.parse(JSON.stringify(items_trim)).replace(/0|1|2|3|4/, '');
          let indexs = items.search(/0|1|2|3|4/);
          if (this.data.property.answer_mode === 'select') {
            // 如果是选择声调 把声调放在拼音后面
            // select_item += `${items_yuan + items_trim.substring(indexs, indexs + 1)} `;
            select_item += `${items_trim.substring(indexs, indexs + 1)} `;
          } else if (this.data.property.answer_mode === 'label') {
            // 如果是标注声调 把声调放在对应字母后面
            select_item += `${items_trim} `;
          }
          content_preview += `${items_yuan} `;
        }
      });
      if (index === -1) {
        let obj = {
          mark: item.mark,
          value: select_item.trim().split(' '),
        };
        this.data.answer.answer_list.push(obj);
      } else {
        this.data.answer.answer_list[index].value = select_item.trim().split(' ');
      }
      item.content_view = content_preview.trim().split(' ');
    },
    // 改变类型
    handleChangeType() {
      this.data.option_list.forEach((item) => {
        this.handleItemAnswer(item);
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  display: flex;
  flex-direction: column;

  .subtitle {
    margin: 8px 0;
    font-size: 14px;
    color: #4e5969;
  }

  .content-item {
    .upload-wrapper {
      margin-top: 0;
    }

    :deep .file-name {
      width: 140px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .auto-matically {
      display: flex;
      align-items: center;
      width: 233px;
      padding: 5px 12px;
      background: #f2f3f5;
      border-radius: 2px;

      .audio-wrapper {
        margin-right: 12px;

        :deep .audio-play {
          width: 16px;
          height: 16px;
          color: #000;
          background-color: initial;
        }

        :deep .audio-play.not-url {
          color: #a1a1a1;
        }

        :deep .voice-play {
          width: 16px;
          height: 16px;
        }
      }

      .auto-btn {
        font-size: 14px;
        font-weight: 400;
        line-height: 22px;
        color: #1d2129;
        cursor: pointer;
      }
    }

    .delete {
      flex-shrink: 0;
      width: 16px;
      height: 16px;
    }
  }
}
</style>
@/views/exercise_questions/data/chooseTone
