<template>
  <div class="model-item-container">
    <div top="" class="model-item-part">
      <div class="progress_bar">
        <div class="bar_item" v-for="(item, i) in steps" :key="item.text + i">
          <div class="item_child">
            <div
              class="cricle c_play"
              :style="{
                backgroundColor:
                  status.step >= i
                    ? 'rgb(228, 242, 255)'
                    : 'rgb(238, 238, 238)',
                backgroundImage: getBG(i),
              }"
              v-text="status.step < i ? i + 1 : ''"
            ></div>
            <div
              class="item-child-label active"
              style="width: 50px"
              v-text="item.text"
            ></div>
          </div>
          <div class="c_right c_right_grey"></div>
        </div>
      </div>
    </div>
    <el-form
      v-if="status.step === 0"
      label-width="100px"
      :model="form"
      label-position="left"
    >
      <el-form-item label="标题">
        <el-input v-model="form.title" />
      </el-form-item>
      <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-item label="自动配图">
        <div style="text-align: right; width: 100%">
          <el-switch v-model="form.isImg" />
        </div>
      </el-form-item>
      <el-form-item label="摘要条数">
        <div style="text-align: right; width: 100%">
          <el-input-number v-model="form.num" :min="1" />
        </div>
      </el-form-item>
    </el-form>

    <div v-if="status.step === 0" class="btn_group">
      <el-button type="primary" @click="nextFunc" :loading="load"
        >下一步</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 class="step4" v-if="status.step === 3">
      <b>标题:</b>
      {{ form.title }}
    </div>
  </div>
</template>

<script setup>
import { ref, defineEmits } from 'vue';
import { image_base64 } from './data.js';
import { commit, generate_pc_get, textImageMatch } from '@/api/aleditor.js';
const emits = defineEmits(['closeType', 'setHtml', 'setImage']);
const steps = [
  {
    text: '标题',
  },
  {
    text: '摘要',
  },
  {
    text: '大纲',
  },
  {
    text: '内容',
  },
];
const form = ref({
  title: '',
  num: 1,
  isImg: false,
  long: 'short',
});

const status = ref({
  step: 0,
  progress: 0,
});

const load = ref(false);

const getBG = i => {
  if (status.value.step == i) return 'url(' + image_base64.play + ')';
  if (status.value.step > i) return 'url(' + image_base64.duigou + ')';
  return undefined;
};

const nextFunc = () => {
  if (!form.value.title) return;
  load.value = true;
  if (form.value.isImg)
    return textImageMatch({
      data: {
        texts: [form.value.title],
      },
      noload: true,
    })
      .then(r => {
        if(r.errCode !== 0) return
        emits('setImage', r.data[form.value.num]);
        createEdior();
      })
      .catch(() => {
        load.value = false;
      });
  createEdior();
};

const createEdior = () => {
  const p = {
    data: { title: form.value.title, contents: [] },
    domain: '',
    generateType: form.value.long === 'short' ? 11 : 9,
    intro_count: form.value.long === 'short' ? undefined : form.value.num,
    length: form.value.long,
    query_type: form.value.long === 'short' ? 'all' : 'intro',
  };
  commit({
    data: p,
    noload: true,
  })
    .then(r => {
      if (!r.data) return;
      status.value.step = 3;
      p.req = {
        doc_id: r.data.doc_id,
      };
      get(p);
    })
    .catch(() => {
      load.value = false;
    });
};

const get = p => {
  generate_pc_get({
    data: p,
    noload: true,
  })
    .then(res => {
      if (res.errCode !== 0 && res.data.status === 'RUNNING') {
        let t = setTimeout(() => {
            clearTimeout(t);
            get();
        }, 200);
        return
      }
      emits('closeType');
      emits('setHtml', res.data.result.intro);
      load.value = false;
    })
    .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;
}

.bar_item,
.progress_bar {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
}
.progress_bar {
  width: 100%;
  height: 80px;
  line-height: 80px;
  font-size: 14px;
}

.bar_item {
  width: 90px;
}

.item_child {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}

.c_right_grey {
  background-image: url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNyIgaGVpZ2h0PSIxNSIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJNLjE1NiAxLjAxYS41NDcuNTQ3IDAgMDAwIC43NjVMNS43MzYgNy41bC01LjU4IDUuNzI0YS41NDguNTQ4IDAgMDAuMTcuODgyLjUyLjUyIDAgMDAuNTc3LS4xMTdMNi44MzcgNy45YS41NzMuNTczIDAgMDAwLS43OTlMLjkwMyAxLjAxYS41MjEuNTIxIDAgMDAtLjc0NyAweiIgZmlsbD0iI0UxRTFFMSIvPjwvc3ZnPg==);
}
.c_right {
  background-repeat: no-repeat;
  background-position: 50%;
  background-size: 29%;
  width: 25px;
  height: 100%;
}

.cricle {
  width: 25px;
  height: 25px;
  border-radius: 50%;
  line-height: 25px;
  text-align: center;
  color: #8e8e8e;
  font-size: 16px;
  background-repeat: no-repeat;
  background-position: 50%;
  margin-right: 5px;
  background-size: 40%;
}

.item-child-label.active {
  color: #4b4b4b;
}

.item-child-label {
  color: #8e8e8e;
}

.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 {
  position: absolute;
  width: 100%;
  text-align: right;
  bottom: 0;
  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;
}

.step4 {
  padding: 40px;
  text-align: left;
  color: #8e8e8e;
  background-color: #f5f5f5;
  line-height: 1.1em;
}
.step4 * {
  vertical-align: middle;
}
</style>
