<template>
  <div class="check-article">
    <div class="main">
      <div class="main-top">
        <div style="display: flex">
          <b>校对样式</b>
        </div>
        <!-- <div class="btn-box">
          <el-button type="primary" @click="savePinyin">保存</el-button>
        </div> -->
      </div>
      <div class="article">
        <div v-for="(item, index) in indexArr" :key="index + 'paragraph'" class="paragraph">
          <div v-for="(items, indexs) in item" :key="indexs + 'words'" class="sentence-box">
            <div
              class="sentence"
              :style="{
                marginRight: items.marginRight ? '8px' : '',
                color: activeIndex === index + '_' + items.sentenceIndex + '_' + items.wordIndex ? '#F2555A' : '',
              }"
              @click="selectItem(items, index)"
            >
              <span
                class="words"
                :class="[/^[0-9]*$/.test(items.text)] ? (/^[\u4e00-\u9fa5]/.test(items.text) ? 'hanzi' : 'en') : ''"
              >
                {{ items.text }}
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-dialog
      v-if="dialogFlag"
      :visible.sync="dialogFlag"
      :show-close="false"
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      :modal="false"
      width="250px"
      class="login-dialog"
    >
      <div class="check-box">
        <div class="content">
          <div class="words-box">
            <span class="words">
              {{ itemActive.text }}
            </span>
          </div>
        </div>
        <el-form>
          <el-form-item label="字体">
            <el-radio-group v-model="itemActive.fontFamily">
              <el-radio v-for="(item, index) in fontFamilyList" :key="'fontFamilyList' + index" :label="item.value">
                <span>{{ item.name }}</span>
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div class="btn-box">
          <el-button type="info" size="small" @click="cancleDialog">取消</el-button>
          <el-button type="primary" size="small" @click="surePinyin">保存</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { publicMethods, reparse } from '@/api/api';
import th from 'element-ui/lib/locale/lang/th';

export default {
  components: {},
  props: ['data'],
  data() {
    return {
      loading: false,
      id: '',
      ArticelData: null,
      indexArr: [], // 索引数组
      activeIndex: null,
      itemActive: null,
      dialogFlag: false,
      checkPinyinInput: '',
      oldInput: '',
      tableData: [
        ['ā', 'á', 'ǎ', 'à', 'a'],
        ['ō', 'ó', 'ǒ', 'ò', 'o'],
        ['ē', 'é', 'ě', 'è', 'e'],
        ['ī', 'í', 'ǐ', 'ì', 'i'],
        ['ū', 'ú', 'ǔ', 'ù', 'u'],
        ['ǖ', 'ǘ', 'ǚ', 'ǜ', 'ü'],
        ['Ā', 'Á', 'Â', 'À', 'A'],
        ['Ō', 'Ó', 'Ô', 'Ò', 'O'],
        ['Ē', 'É', 'Ê', 'È', 'E'],
        ['Ī', 'Í', 'Î', 'Ì', 'I'],
        ['Ū', 'Ú', 'Û', 'Ù', 'U'],
        ['n', 'ń', 'ň', 'ǹ', 'n'],
        ['m̄', 'ḿ', 'm', 'm̀', 'n'],
      ],
      toneList: [' ', 'ˉ', 'ˊ', 'ˇ', 'ˋ'],
      fontFamilyList: [
        {
          value: '楷体',
          name: '中文',
        },
        {
          value: 'Sans-GBNPC',
          name: '拼音',
        },
        {
          value: 'robot',
          name: '英文',
        },
        {
          value: 'Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, sans-serif, alabo',
          name: '系统字体',
        },
      ],
    };
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getArticleData();
  },
  methods: {
    // 获取分析结果
    getArticleData() {
      this.loading = true;
      let newdata = [];
      this.data.detail.forEach((item) => {
        if (item.wordsList.length !== 0) {
          newdata.push(item);
        }
      });
      // this.ArticelData = JSON.parse(JSON.stringify(newdata));
      let saveArr = [];
      let arr = [];
      let saveIndex = 0;
      // 添加索引
      newdata.forEach((item, index) => {
        arr.push([]);
        item.wordsList.forEach((items, indexs) => {
          items.forEach((itemss, indexss) => {
            let obj = {
              text: itemss.chs,
              paraIndex: index,
              sentenceIndex: indexs,
              wordIndex: indexss,
              marginRight: true,
              saveIndex,
              fontFamily: itemss.fontFamily,
            };
            arr[index].push(obj);
            let saveObj = {
              word: itemss.chs,
              // pinyin_lt: itemss.pinyin_lt?itemss.pinyin_lt:''
            };
            saveArr.push(saveObj);
            saveIndex++;
          });
        });
      });
      this.indexArr = arr;
      this.ArticelData = saveArr;
      this.loading = false;
    },
    selectItem(item, index) {
      this.activeIndex = `${index}_${item.sentenceIndex}_${item.wordIndex}`;
      this.itemActive = item;
      this.dialogFlag = true;
      this.checkPinyinInput = '';
    },
    cancleDialog() {
      this.activeIndex = null;
      this.itemActive = null;
      this.checkPinyinInput = '';
      this.oldInput = '';
      this.dialogFlag = false;
    },
    surePinyin() {
      this.saveStyle();
      this.dialogFlag = false;
    },
    saveStyle() {
      this.$emit(
        'saveStyle',
        this.itemActive.paraIndex,
        this.itemActive.sentenceIndex,
        this.itemActive.wordIndex,
        this.itemActive.fontFamily,
      );
      this.$message.success('保存成功');
      this.activeIndex = null;
      // this.itemActive = null;
      this.dialogFlag = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.check-article {
  min-height: 100%;
  background: #f6f6f6;

  .wheader {
    background: #fff;
  }

  .el-button {
    padding: 5px 16px;
    font-size: 14px;
    font-weight: 400;
    line-height: 22px;
    color: #4e5969;
    background: #f2f3f5;
    border: 1px solid #f2f3f5;
    border-radius: 2px;

    &.el-button--primary {
      color: #fff;
      background: #165dff;
      border: 1px solid #165dff;
    }
  }

  .main {
    background: #fff;

    &-top {
      position: relative;
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 24px;

      b {
        margin-left: 16px;
        font-size: 24px;
        font-weight: 500;
        line-height: 34px;
        color: #000;
      }
    }

    .go-back {
      display: flex;
      align-items: center;
      width: 60px;
      padding: 5px 8px;
      font-size: 14px;
      font-weight: 400;
      line-height: 22px;
      color: #333;
      cursor: pointer;
      background: #fff;
      border: 1px solid #d9d9d9;
      border-radius: 4px;
      box-shadow: 0 2px 0 0 rgba(0, 0, 0, 2%);

      .el-icon-arrow-left {
        margin-right: 8px;
        font-size: 16px;
      }
    }

    .article {
      padding: 8px;
      background: #fcfcfc;
      border: 1px solid rgba(0, 0, 0, 8%);
      border-radius: 2px;
    }

    .paragraph {
      display: flex;
      flex-flow: wrap;
      width: 100%;
      margin-bottom: 24px;
      text-align: center;

      .sentence-box {
        display: flex;
        flex-flow: wrap;
        float: left;

        .sentence {
          margin-top: 8px;
          color: #000;
          text-align: center;
          cursor: pointer;

          .words {
            display: block;
            font-size: 20px;
            font-weight: 400;
            line-height: 28px;
          }

          .pinyin {
            display: block;
            height: 14px;
            font-family: 'League';
            font-size: 14px;
            font-weight: 400;
            line-height: 1;
          }
        }
      }
    }
  }
}

.check-box {
  padding: 24px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 4px 4px 0 rgba(0, 0, 0, 25%);

  .content {
    display: flex;
    align-items: center;
    justify-content: center;

    .words-box {
      color: #000;
      text-align: center;

      .words {
        display: block;
        font-size: 28px;
        font-weight: 400;
        line-height: 40px;
      }

      .pinyin {
        display: block;
        height: 20px;
        font-family: 'League';
        font-size: 20px;
        font-weight: 400;
        line-height: 1;
      }
    }
  }

  .checkPinyinInput {
    margin: 16px 0 8px;
  }

  .tips {
    margin: 0 0 24px;
    font-size: 12px;
    font-weight: 400;
    line-height: 18px;
    color: #a0a0a0;
  }

  .btn-box {
    text-align: right;

    .el-button {
      padding: 2px 12px;
      font-size: 12px;
      line-height: 20px;
    }
  }
}
</style>
<style lang="scss">
.check-article {
  .login-dialog {
    .el-dialog__header {
      padding: 0;
    }

    .el-dialog__body {
      padding: 0;
    }

    .el-input__inner {
      text-align: center;
      background: #f3f3f3;
    }
  }
}
</style>
