<template>
  <div class="model-item-container">
    <br />
    <el-form label-position="top">
      <el-form-item label="请输入小说标题 *">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="请输入小说开头">
        <el-input
          v-model="form.desc"
          type="textarea"
          :rows="5"
          :maxlength="1000"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <el-form label-width="100px" :model="form" label-position="left">
      <el-form-item label="文章长度">
        <div style="text-align: right; width: 100%">
          <el-button
            @click="form.long = 'short'"
            plain
            :class="{ act: form.long === 'short' }"
          >
            短
          </el-button>
          <el-button
            @click="form.long = 'default'"
            plain
            :class="{ act: form.long === 'default' }"
          >
            中
          </el-button>
          <el-button
            @click="form.long = 'long'"
            plain
            :class="{ act: form.long === 'long' }"
          >
            长
          </el-button>
        </div>
      </el-form-item>
    </el-form>

    <div class="btn_group">
      <el-button
        type="primary"
        style="width: 100%"
        :loading="load"
        @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>
  </div>
</template>

<script setup>
import { ref,defineEmits } from 'vue';
import { image_base64 } from './data.js';
import { advertisement, generate_pc_get } from '@/api/aleditor.js';
const emits = defineEmits(['closeType', 'setHtml', 'getImage']);
const form = ref({
  title: '',
  desc: '',
  long: 'small',
});
const load = ref(false);

const create = () => {
  load.value = true;
  const p = {
    count: form.value.num,
    data: {
      title: form.value.title,
      prev: form.value.desc,
      next: ""
    },
    generateType: 2,
    domain: "novel",
    length: form.value.long,
    noblock: true,
    query_type: 'comment',
  };
  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;
      }
      emits('setHtml', '<h3>' + form.value.title + '</h3><p>' + form.value.desc + res.data.result.content + '</p>');
    })
    .catch(() => {
      load.value = false;
    });
};
</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%;
}

.model-item-part {
  -webkit-box-flex: 0;
  -ms-flex: none;
  flex: none;
  font-size: 14px;
}

.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 {
  width: 100%;
}

.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;
}
</style>
