<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="isEnable(data.property.is_enable_description)"
          v-model="data.description"
          rows="3"
          resize="none"
          type="textarea"
          placeholder="输入描述"
        />
      </div>

      <div class="content">
        <label class="title-little">题目：</label>
        <ul>
          <li v-for="(item, i) in data.option_list" :key="i" v-loading="item.loadings" class="content-item">
            <span
              v-if="data.property.learn_type === 'dictation'"
              class="question-number"
              title="双击切换序号类型"
              @dblclick="changeOptionType(data)"
            >
              {{ computedQuestionNumber(i, data.option_number_show_mode) }}
            </span>
            <el-input
              v-model="item.content"
              :maxlength="data.property.learn_type === 'dictation' ? null : 1"
              :placeholder="data.property.learn_type === 'dictation' ? '输入汉字或词汇' : '输入一个汉字'"
              @blur="handleChineseStrokes(item)"
            />
            <el-input
              v-model="item.pinyin"
              :placeholder="data.property.learn_type === 'dictation' ? '拼音间用空格隔开' : '输入拼音'"
            />
            <UploadAudio
              v-if="data.other.audio_generation_method === 'upload'"
              :key="item.audio_file_id || i"
              :file-id="item.audio_file_id"
              :item-index="i"
              :show-upload="!item.audio_file_id"
              @upload="uploads"
              @deleteFile="deleteFiles"
            />
            <div v-else-if="data.other.audio_generation_method === 'auto'" class="auto-matically">
              <AudioPlay v-if="item.audio_file_id" :file-id="item.audio_file_id" theme-color="gray" />
              <span v-loading="item.loading" class="auto-btn" @click="handleMatically(item)">{{
                item.audio_file_id ? '已生成' : '自动生成'
              }}</span>
            </div>
            <SoundRecord v-else :wav-blob.sync="item.audio_file_id" />
            <el-input
              v-if="data.property.learn_type !== 'dictation'"
              v-model="item.definition"
              placeholder="输入释义"
            />
            <el-input
              v-if="data.property.learn_type !== 'dictation'"
              v-model="item.collocation"
              placeholder="输入搭配"
            />
            <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-number
            v-model="data.property.score"
            :min="0"
            :step="data.property.score_type === scoreTypeList[0].value ? 1 : 0.1"
          />
        </el-form-item>
        <el-form-item label="类型">
          <el-radio
            v-for="{ value, label } in learnTypeList"
            :key="value"
            v-model="data.property.learn_type"
            :label="value"
            @change="handleChangeType"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
        <el-form-item v-if="data.property.learn_type !== 'dictation'" label="田字格数">
          <el-input
            v-model="data.property.tian_number"
            type="number"
            @blur="data.property.tian_number = handleInputNumber(data.property.tian_number)"
          />
        </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 '../common/QuestionMixin.js';
import UploadAudio from '../common/UploadAudio.vue';
import SoundRecord from '../common/SoundRecord.vue';
import { GetStaticResources } from '@/api/app';
import { changeOptionType, handleInputNumber } from '@/views/exercise_questions/data/common';

import {
  chineseData,
  learnTypeList,
  audioGenerationMethodList,
  getOption,
} from '@/views/exercise_questions/data/chinese';

export default {
  name: 'ChineseQuestion',
  components: {
    UploadAudio,
    SoundRecord,
  },
  mixins: [QuestionMixin],
  data() {
    return {
      learnTypeList,
      audioGenerationMethodList,
      data: JSON.parse(JSON.stringify(chineseData)),
      changeOptionType,
      handleInputNumber,
    };
  },
  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.$confirm('是否删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.data.option_list.splice(i, 1);
          this.data.file_id_list.splice(this.data.file_id_list.indexOf(file_id), 1);
        })
        .catch(() => {});
    },
    // 自动生成音频
    handleMatically(item) {
      if (item.pinyin.trim()) {
        this.$set(item, 'loading', true);
        let MethodName = 'tool-PinyinToVoiceFile';
        let data = {
          pinyin: item.pinyin.trim(),
        };
        GetStaticResources(MethodName, data)
          .then((res) => {
            item.loading = false;
            if (res.status === 1) {
              item.audio_file_id = res.file_id;
              this.data.file_id_list.push(res.file_id);
            }
          })
          .catch(() => {
            item.loading = false;
          });
      }
    },
    // 改变类型
    handleChangeType() {
      if (this.data.property.learn_type !== 'dictation') {
        this.data.option_list.forEach((item) => {
          if (item.content.trim()) {
            item.content = item.content.substring(0, 1);
          }
        });
      }
    },
    // 生成汉字
    handleChineseStrokes(item) {
      if (item.content.trim()) {
        this.$set(item, 'loadings', true);
        let content_arr = item.content.trim().split('');
        let content_arrs = [];
        let content_arr_strokes = [];
        content_arr.forEach((itemc) => {
          if (itemc.trim()) {
            content_arrs.push(itemc.trim());
          }
        });
        content_arrs.forEach((itemc, indexc) => {
          content_arr_strokes.push(null);
          let MethodName = 'hz_resource_manager-GetHZStrokesContent';
          let data = {
            hz: itemc,
          };
          GetStaticResources(MethodName, data).then((res) => {
            let obj = {
              hz: itemc.trim(),
              strokes: res,
            };
            content_arr_strokes[indexc] = obj;
          });
        });
        item.loadings = false;

        item.hz_strokes_list = content_arr_strokes;
      }
    },
  },
};
</script>

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

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

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

    .auto-matically {
      display: flex;
      flex-shrink: 0;
      align-items: center;
      width: 233px;
      padding: 5px 12px;
      background-color: $fill-color;
      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>
