<template>
  <div>
    <label>标题：</label>
    <RichText
      v-if="property.isGetContent"
      ref="richText"
      v-model="data.title_con"
      :inline="true"
      :placeholder="'输入标题'"
      toolbar="fontselect fontsizeselect forecolor backcolor | underline | bold italic strikethrough alignleft aligncenter alignright"
      class="title-box"
      :font-size="unifiedAttrib?.font_size"
      :font-family="unifiedAttrib?.font"
    />
    <el-table :data="data.option" border style="width: 100%">
      <el-table-column fixed prop="number" label="序号" width="70">
        <template slot-scope="scope">
          <el-input v-model="scope.row.number" />
        </template>
      </el-table-column>
      <el-table-column fixed prop="con" label="内容" width="200">
        <template slot-scope="scope">
          <RichText
            v-if="property.isGetContent"
            ref="richText"
            v-model="scope.row.con"
            :inline="true"
            :item-index="scope.$index"
            :font-size="unifiedAttrib?.font_size"
            :font-family="unifiedAttrib?.font"
            toolbar="fontselect fontsizeselect forecolor backcolor | underline | bold italic strikethrough alignleft aligncenter alignright"
            @handleRichTextBlur="handleBlurCon"
          />
        </template>
      </el-table-column>
      <el-table-column v-if="isEnable(data.property.view_pinyin)" prop="con" label="拼音" width="150">
        <template slot-scope="scope">
          <RichText
            v-if="property.isGetContent"
            ref="richText"
            v-model="scope.row.pinyin"
            :inline="true"
            :font-size="unifiedAttrib?.pinyin_size"
            :font-family="'League'"
            toolbar="fontselect fontsizeselect forecolor backcolor | underline | bold italic strikethrough alignleft aligncenter alignright"
          />
        </template>
      </el-table-column>
      <el-table-column prop="interpret" label="翻译" width="200">
        <template slot-scope="scope">
          <RichText
            v-if="property.isGetContent"
            ref="richText"
            v-model="scope.row.interpret"
            :inline="true"
            toolbar="fontselect fontsizeselect forecolor backcolor | underline | bold italic strikethrough alignleft aligncenter alignright"
            :font-size="unifiedAttrib?.font_size"
            :font-family="unifiedAttrib?.font"
          />
        </template>
      </el-table-column>
      <el-table-column prop="note" label="注释" width="200">
        <template slot-scope="scope">
          <RichText
            v-if="property.isGetContent"
            ref="richText"
            v-model="scope.row.note"
            :inline="true"
            toolbar="fontselect fontsizeselect forecolor backcolor | underline | bold italic strikethrough alignleft aligncenter alignright"
            :font-size="unifiedAttrib?.font_size"
            :font-family="unifiedAttrib?.font"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column prop="img_list" label="图片" width="300">
        <template slot-scope="scope">
          <UploadPicture
            :file-info="scope.row.file_list[0]"
            :item-index="scope.$index"
            :show-upload="!scope.row.file_list[0]"
            @upload="uploadPic"
            @deleteFile="deletePic"
          />
        </template>
      </el-table-column> -->
      <el-table-column prop="notesColor" label="在课文中的颜色" width="130">
        <template slot-scope="scope">
          <el-color-picker v-model="scope.row.notesColor"></el-color-picker>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleDelete(scope.$index)">删除</el-button>
          <el-button size="mini" type="text" @click="moveElement(scope.row, scope.$index, 'up')">上移</el-button>
          <el-button size="mini" type="text" @click="moveElement(scope.row, scope.$index, 'down')">下移</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button icon="el-icon-plus" style="margin: 24px 0" @click="addElement">增加一个</el-button>
    <el-form :model="data.property" label-width="72px" label-position="left">
      <el-form-item label="拼音">
        <el-switch v-model="data.property.view_pinyin" active-value="true" inactive-value="false" />
      </el-form-item>
      <el-form-item label="拼音位置">
        <el-radio
          v-for="{ value, label } in pinyinPositionLists"
          :key="value"
          v-model="data.property.pinyin_position"
          :label="value"
          :disabled="!isEnable(data.property.view_pinyin)"
        >
          {{ label }}
        </el-radio>
      </el-form-item>
      <el-form-item label="">
        <el-checkbox
          v-model="data.property.is_first_sentence_first_hz_pinyin_first_char_upper_case"
          :disabled="!isEnable(data.property.view_pinyin)"
          true-label="true"
          false-label="false"
          >句首大写</el-checkbox
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import ModuleMixin from '../../common/ModuleMixin';
import RichText from '@/components/RichText.vue';
import UploadPicture from '../new_word/components/UploadPicture.vue';
import { pinyinPositionLists } from '@/views/book/courseware/data/notes';
import { isEnable } from '@/views/book/courseware/data/common';
export default {
  name: 'NotesPage',
  components: {
    RichText,
    UploadPicture,
  },
  props: ['dataNotes', 'unifiedAttrib'],
  mixins: [ModuleMixin],
  data() {
    return {
      data: this.dataNotes,
      pinyinPositionLists,
      isEnable,
    };
  },
  watch: {
    'data.property.is_first_sentence_first_hz_pinyin_first_char_upper_case': {
      handler(val, oldVal) {
        if (val === oldVal) return;

        if (isEnable(this.data.property.view_pinyin)) {
          this.handlePinyin();
        }
      },
      deep: true,
    },
  },
  methods: {
    // 删除行
    handleDelete(index) {
      this.data.option.splice(index, 1);
    },
    // 上移下移
    moveElement(dItem, index, type) {
      let obj = JSON.parse(JSON.stringify(dItem));
      if (type == 'up' && index > 0) {
        this.data.option.splice(index - 1, 0, obj);
        this.data.option.splice(index + 1, 1);
      }
      if (type == 'down' && index < this.data.option.length - 1) {
        this.data.option[index] = this.data.option.splice(index + 1, 1, this.data.option[index])[0];
      }
    },
    // 增加
    addElement() {
      this.data.option.push(this.getOption());
    },
    getOption() {
      return {
        number: '',
        con: '',
        pinyin: '',
        interpret: '', // 翻译
        note: '', // 注释
        img_list: [],
        file_list: [''], // 图片
        notesColor: '#8206BF', // 在课文中预览的注释颜色
      };
    },
    uploadPic(file_id, index) {
      this.data.option[index].file_list[0] = file_id;
      this.data.file_id_list.push(file_id);
      this.refreshKey = Math.random();
    },
    deletePic(file_id, index) {
      this.data.option[index].file_list[0] = '';
      this.data.file_id_list = this.data.file_id_list.filter((item) => item !== file_id);
    },
    handleBlurCon(i) {
      let text = this.data.option[i].con.replace(/<[^>]+>/g, '');
      this.data.option[i].pinyin = cnchar
        .spell(
          text,
          'array',
          this.data.property.is_first_sentence_first_hz_pinyin_first_char_upper_case === 'true' ? 'high' : 'low',
          'tone',
        )
        .join(' ');
    },
    handlePinyin() {
      this.data.option.forEach((item) => {
        let text = item.con.replace(/<[^>]+>/g, '');
        item.pinyin = cnchar
          .spell(
            text,
            'array',
            this.data.property.is_first_sentence_first_hz_pinyin_first_char_upper_case === 'true' ? 'high' : 'low',
            'tone',
          )
          .join(' ');
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.upload-file {
  display: flex;
  column-gap: 12px;
  align-items: center;
  margin: 8px 0;

  .file-name {
    display: flex;
    column-gap: 14px;
    align-items: center;
    justify-content: space-between;
    max-width: 360px;
    padding: 8px 12px;
    font-size: 14px;
    color: #1d2129;
    background-color: #f7f8fa;

    span {
      display: flex;
      column-gap: 14px;
      align-items: center;
    }
  }

  .svg-icon {
    cursor: pointer;
  }
}

.title-box {
  padding: 5px;
  margin: 5px 0;
  background-color: #f2f3f5;
  border: 1px solid #f2f3f5;
  border-radius: 4px;

  :deep p {
    margin: 0;
  }
}
</style>
<style lang="scss">
.tox .tox-editor-header {
  z-index: 3 !important;
}
</style>
