<!--  -->
<template>
  <div class="Big-Book-input">
    <div class="Big-Book-content m">
      <div class="Big-Book-main">
                

        <div class="Big-Book-en">
          <span>汉字：</span>
          <el-input
            style="width: 300px; margin-right: 10px"
            placeholder="请输入汉字"
            v-model="curQueItem.new_word"
            @blur="onBlur(curQueItem, 'new_word')"
          ></el-input>
          <el-button type="danger" size="small" @click="deleteGroup"
            >删除</el-button
          >
        </div>
        <div class="Big-Book-en">
          <span>拼音：</span>
          <el-input
            style="width: 300px; margin-right: 10px"
            placeholder="请输入拼音"
            v-model="curQueItem.pinyin"
            @blur="onBlur(curQueItem, 'pinyin')"
          ></el-input>
          <el-button type="primary" size="small" @click="getPinyin(curQueItem)"
            >生成拼音</el-button
          >
        </div>
        <div class="Big-Book-en">
          <span>词性：</span>
          <el-input
            style="width: 300px"
            placeholder="请输入词性"
            v-model="curQueItem.cixing"
            @blur="onBlur(curQueItem, 'cixing')"
          ></el-input>
        </div>

        <div class="Book_flex_start">
          <span>释义：</span>
          <div>
            <div
              class="Big-Book-en"
              v-for="(item, i) in curQueItem.definition_list"
              :key="i"
            >
              <el-input
                style="width: 300px"
                type="textarea"
                :autosize="{ minRows: 2, maxRows: 4 }"
                placeholder="请输入释义"
                v-model="curQueItem.definition_list[i]"
                @blur="trimEvent(i)"
              ></el-input>
              <img
                @click="deletecn(i)"
                class="close"
                src="../../../assets/adult/del-close.png"
                alt=""
              />
            </div>
          </div>
        </div>
        <div class="addoption" @click="Addcn">添加释义</div>
        <div class="Big-Book-mp3">
          <Upload
            :changeFillId="changeMp3"
            :datafileList="fileCon.mp3_list"
            :filleNumber="mp3Number"
            :uploadType="'mp3'"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Upload from "./Upload.vue";
import "@/utils/pinyin_dict_withtone";
import "@/utils/pinyinUtil";

export default {
  components: {
    Upload,
  },
  props: ["curQueItem", "index", "type", "deleteGroup"],
  data() {
    return {
      mp3Number: 1,
      fileCon: {
        img_list: [],
        mp3_list: [],
      },
    };
  },
  computed: {},
  watch: {},
  //方法集合
  methods: {
    onBlur(item, field) {
      item[field] = item[field] ? item[field].trim() : "";
    },
    trimEvent(i) {
      let str = this.curQueItem.definition_list[i].trim();
      this.$set(this.curQueItem.definition_list, i, str);
    },
    // 删除释义
    deletecn(index) {
      if (this.curQueItem.definition_list.length <= 1) {
        this.$message.warning("至少要保留一个");
        return;
      }
      this.curQueItem.definition_list.splice(index, 1);
    },
    // 点击生成拼音
    getPinyin(item) {
      console.log(item);
      let bool = false;
      let value = "";
      if (item.new_word == "") {
        this.$message.info("请先输入内容,再生成拼音");
        bool = true;
      } else {
        value = item.new_word;
      }
      if (bool) {
        return;
      }
      let result = pinyinUtil.getPinyin(value);
      item.pinyin = result;
    },
    // 添加释义
    Addcn() {
      this.curQueItem.definition_list.push("");
    },
    changeMp3(fileList) {
      const articleImgList = JSON.parse(JSON.stringify(fileList));
      const articleImgRes = [];
      articleImgList.forEach((item) => {
        if (item.response) {
          const obj = {
            name: item.name,
            url: item.response.file_info_list[0].file_url,
            id: item.response.file_info_list[0].file_id,
            media_duration: item.response.file_info_list[0].media_duration,
            file_relative_path:
              item.response.file_info_list[0].file_relative_path,
          };
          articleImgRes.push(obj);
        }
      });
      //this.articleImgList = articleImgRes;
      this.curQueItem.mp3_list = JSON.parse(JSON.stringify(articleImgRes));
    },
    changeImage(fileList) {
      console.log(fileList);
      const articleImgList = JSON.parse(JSON.stringify(fileList));
      const articleImgRes = [];
      articleImgList.forEach((item) => {
        if (item.response) {
          const obj = {
            name: item.name,
            url: item.response.file_info_list[0].file_url,
            id: item.response.file_info_list[0].file_id,
            file_relative_path: file_relative_path,
          };
          articleImgRes.push(obj);
        }
      });
      //this.articleImgList = articleImgRes;
      this.curQueItem.img_list = JSON.parse(JSON.stringify(articleImgRes));
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    if (this.curQueItem) {
      this.fileCon.img_list = this.curQueItem.img_list;
      this.fileCon.mp3_list = this.curQueItem.mp3_list;
    }
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类
.Big-Book-input {
  .Big-Book-content {
    &.m {
      display: flex;
      justify-content: flex-start;
      align-items: flex-start;
    }
    .Big-Book-con {
      display: flex;
      align-items: center;
    }
    .Big-Book-title {
      font-size: 16px;
      line-height: 40px;
      color: #000;
      margin-right: 15px;
    }
    .Big-Book-main {
      position: relative;
      width: 100%;
      > div {
        margin-bottom: 10px;
        &.Big-Book-pinyin,
        &.Big-Book-con,
        &.Big-Book-en {
          margin-bottom: 8px;
          span {
            width: 60px;
            text-align: right;
          }
        }
        &.Big-Book-yb {
          display: flex;
          align-items: flex-start;
          span {
            width: 60px;
            text-align: right;
          }
        }
      }
      .close {
        width: 24px;
        cursor: pointer;
      }
    }
  }
  .addoption {
    width: 148px;
    height: 40px;
    background: #f3f3f3;
    border: 1px dashed rgba(0, 0, 0, 0.15);
    box-sizing: border-box;
    border-radius: 4px;
    text-align: center;
    line-height: 40px;
    cursor: pointer;
    font-size: 14px;
    color: #000000;
    margin-top: 20px;
  }
}
</style>