<template>
  <div class="model-item-container">
    <br />
    <el-form label-width="100px" :model="form" label-position="top">
      <el-form-item label="请概述您想要搜寻的文献内容">
        <el-input
          v-model="form.title"
          type="textarea"
          :rows="5"
          maxlength="500"
          show-word-limit
          placeholder="您可以输入任何线索，如：
·  互联网环境下中小学教育信息化的发展与展望
·  本文从教育信息化的概念出发，分析了互联网环境下中小学教育信息化的发展现状。
·  中小学教育、信息化、互联网、发展、展望"
        />
      </el-form-item>
    </el-form>

    <div class="btn_group">
      <el-button
        type="primary"
        style="width: 100%"
        :disabled="!form.title"
        @click="getRec"
        :loading="load"
      >
        检索
      </el-button>
    </div>

    <div style="text-align: left" v-if="form.list.length">
      <div style="padding: 5px">
        <el-checkbox @change="e => chagne(e)" label="全选" size="large" />
        <el-button
          type="primary"
          text
          bg
          style="float: right"
          @click="exportsText"
          >全部导出</el-button
        >
      </div>
      <div class="item" v-for="(item, i) in form.list" :key="i">
        <div class="son sonLeft">
          <el-checkbox v-model="select[i]" size="large" />
        </div>
        <div class="son">
          <div class="title" v-text="item.title"></div>
          <div class="author" v-text="author(item.authors)"></div>
          <div class="author">
            <span v-text="item.publication" style="padding-right: 16px"></span>
            <span v-text="item.publish_date" style="padding-right: 16px"></span>
            <el-button
              @click="
                yinyongShow = true;
                yinyong = item.cite;
              "
              type="primary"
              link
            >
              引用
            </el-button>
            <el-button
              text
              bg
              type="primary"
              plain
              @click="() => see(item.url)"
            >
              查看
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <el-dialog v-model="yinyongShow" title="引用">
      <div v-html="yinyong" style="text-align: left"></div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, defineEmits } from 'vue';
import { recommendation } from '@/api/aleditor.js';
const emits = defineEmits(['closeType', 'setHtml', 'setImage', 'setTitle']);
const form = ref({
  title: '',
  list: [],
});

const yinyong = ref('');
const yinyongShow = ref(false);
const load = ref(false);

const select = ref([]);

const chagne = e => {
  for (let i = 0; i < form.value.list.length; i++) {
    select.value[i] = e;
  }
};

const see = url => {
  window.open(url);
};

const author = list => {
  return list.join(',');
};

const getRec = () => {
  load.value = true;
  recommendation({
    data: {
      text: form.value.title,
    },
    noload: true,
  }).then(r => {
    load.value = false;
    if (r.errCode !== 0) return;
    form.value.list = r.data || [];
  }).catch(() => {
    load.value = false;
  });
};

const exportsText = () => {
  for (let i = 0; i < form.value.list.length; i++) {
    select.value[i] && emits('setHtml', form.value.list[i].cite);
  }
};
</script>

<style scoped>
.model-item-container {
  text-align: center;
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  min-height: 0;
  width: 95%;
  height: 100%;
}

.act {
  color: var(--el-button-hover-text-color);
  border-color: var(--el-button-hover-border-color);
  background-color: var(--el-button-hover-bg-color);
  outline: 0;
}

.btn_group {
  padding-right: 1em;
}

.item {
  border-top: 1px solid #dcdfe6;
  margin-top: 24px;
  padding-top: 24px;
}

.son {
  width: calc(100% - 20px);
  display: inline-block;
}
.sonLeft {
  width: 20px;
}

.son .title {
  font-weight: 700;
  font-size: 14px;
  line-height: 22px;
  color: #4b4b4b;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.son .author {
  font-weight: 400;
  font-size: 10px;
  line-height: 18px;
  color: #8e8e8e;
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
