<template>
  <div class="short_video">
    <div style="padding: 25px 25px 0 25px">
      <el-table
        border
        header-row-class-name="head"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column prop="title" label="主题/产品" width="280">
          <template #default="scope">
            <div
              contenteditable
              class="input"
              v-text="scope.row.title"
              @input="e => setText(scope, 'title', e)"
            ></div>
          </template>
        </el-table-column>
        <el-table-column prop="desc" label="描述">
          <template #default="scope">
            <div
              contenteditable
              class="input"
              v-text="scope.row.desc"
              @input="e => setText(scope, 'desc', e)"
            ></div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="">
          <template #default="scope">
            <el-button
              :icon="CircleClose"
              type="primary"
              link
              @click="() => deltable(scope)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <br />
      <el-button :icon="CirclePlus" type="primary" link @click="addtable">
        添加
      </el-button>
      <br />
      <el-form>
        <el-form-item label="文章长度">
          <div style="text-align: right; width: 100%">
            <el-button
              @click="long = 'short'"
              plain
              :class="{ act: long === 'short' }"
            >
              短
            </el-button>
            <el-button
              @click="long = 'default'"
              plain
              :class="{ act: long === 'default' }"
            >
              中
            </el-button>
            <el-button
              @click="long = 'long'"
              plain
              :class="{ act: long === 'long' }"
            >
              长
            </el-button>
          </div>
        </el-form-item>
      </el-form>
      <div class="btn_group">
        <el-button type="primary" style="width: 100%" @click="create"
          >生成内容</el-button
        >
        <div class="bottom-content-safe-tip">
          <img :src="image_base64.tanhao" class="content-safe-tip-icon" />
          <div class="content-safe-tip-text">
            声明：内容为概率模型生成，可能会产生不正确的信息，不代表我司的观点和立场
          </div>
        </div>
      </div>

      <template v-if="list.length">
        <div class="item" v-for="(item, i) in list" :key="i">
          <div class="content" v-text="item.text"></div>
          <div class="btn_div">
            <el-tooltip effect="dark" content="左侧导入" placement="top">
              <el-icon
                @click="() => inputText(item.text)"
                style="
                  left: 50;
                  top: 50%;
                  transform: translate(-50%, -50%);
                  cursor: pointer;
                "
                color="#cacaca"
                ><DArrowLeft
              /></el-icon>
            </el-tooltip>
          </div>
          <div class="btn_div">
            <el-tooltip effect="dark" content="复制" placement="top">
              <el-icon
                style="
                  left: 50;
                  top: 50%;
                  transform: translate(-50%, -50%);
                  cursor: pointer;
                "
                color="#cacaca"
                ><CopyDocument
              /></el-icon>
            </el-tooltip>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<script setup>
import { image_base64 } from './data.js';
import { CircleClose, CirclePlus } from '@element-plus/icons-vue';
import { ref, defineEmits } from 'vue';
import { ElMessage } from 'element-plus';
import { advertisement, generate_pc_get } from '@/api/aleditor.js';
const emits = defineEmits(['closeType', 'setHtml', 'getImage']);
const long = ref('short');
const load = ref(false);
const tableData = ref([
  {
    title: ' ',
    desc: ' ',
  },
]);
const list = ref([]);

const inputText = text => {
  emits('setHtml', text);
};
const addtable = () => {
  tableData.value.push({
    title: ' ',
    desc: ' ',
  });
};
const deltable = scope => {
  if (tableData.value.length <= 1)
    return ElMessage.error('至少需要一个或以上内容');
  tableData.value.splice(scope.$index, 1);
};
const setText = (scope, key, e) => {
  tableData.value[scope.$index][key] = e.target.innerText;
};

const create = () => {
  load.value = true;
  const p = {
    count: 5,
    data: {
      others: tableData.value.map(v => {
        return {
          name: v.title,
          desc: v.desc,
        };
      }),
    },
    generateType: 33,
    length:
      long.value === 'short' ? 400 : long.value === 'default' ? 600 : 1000,
    noblock: true,
    query_type: 'koubo_multi',
    docId: localStorage.getItem('fileId') || undefined,
  };
  advertisement({
    data: p,
    noload: true,
  }).then(r => {
    if (r.errCode !== 0) return (load.value = false);
    p.req = { doc_id: r.data.doc_id };
    get(p);
  });
};

const get = p => {
  generate_pc_get({
    data: p,
    noload: true,
  })
    .then(res => {
      load.value = false;
      if (res.errCode !== 0 && res.data.status === 'RUNNING') {
        let t = setTimeout(() => {
          get(p);
          clearTimeout(t);
        }, 200);
        return;
      }
      const list = res.data.result.content;
      const l = [];
      for (let i = 0; i < list.length; i++) {
        const v = list[i];
        l.push({
          text: v,
        });
      }
      list.value.push(...l);
    })
    .catch(() => {
      load.value = false;
    });
};
</script>

<style>
.short_video .head,
.short_video .el-table th.el-table__cell {
  background-color: #fafafa;
  color: #111;
  font-weight: 400;
}

.short_video .el-table .el-table__cell {
  padding: 0;
}

.short_video .input,
.short_video .el-table .cell {
  height: 45px;
  line-height: 45px;
  outline: none;
}
</style>

<style scoped>
.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;
}
.bottom-content-safe-tip {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  padding: 15px 0;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  width: 100%;
  zoom: 0.83;
}
.bottom-content-safe-tip .content-safe-tip-icon {
  width: 15px;
  height: 15px;
  margin-right: 10px;
}
.bottom-content-safe-tip .content-safe-tip-text {
  font-size: 12px;
  line-height: 15px;
  color: #cacaca;
}
.item {
  width: 100%;
  min-height: 45px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  margin-bottom: 20px;
  text-align: left;
  position: relative;
  padding: 10px 10px 10px 0;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  border-bottom: 1px solid #eee;
}

.content {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
  outline: none;
  line-height: 28px;
  text-align: justify;
  overflow-y: auto;
}

.hover_class,
.item:hover {
  background-color: #f5f5f5;
  border-bottom: 1px solid #f8f8f8;
}

.btn_div {
  height: 30px;
  width: 30px;
  position: relative;
}
</style>
