<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="1000"
          show-word-limit
          placeholder="如：原神，甘雨"
        />
      </el-form-item>
      <el-form-item label="风格" style="text-align: left">
        <el-button :type="form.fengge === '绘图' ? 'primary' : undefined" plain>
          绘图
        </el-button>
        <el-button
          :type="form.fengge === '3D渲染' ? 'primary' : undefined"
          plain
        >
          3D渲染
        </el-button>
        <el-button :type="form.fengge === '手绘' ? 'primary' : undefined" plain>
          手绘
        </el-button>
        <el-button :type="form.fengge === '水彩' ? 'primary' : undefined" plain>
          水彩
        </el-button>
        <el-button :type="form.fengge === '插画' ? 'primary' : undefined" plain>
          插画
        </el-button>
        <el-button :type="form.fengge === '素描' ? 'primary' : undefined" plain>
          素描
        </el-button>
        <el-button :type="form.fengge === '写实' ? 'primary' : undefined" plain>
          写实
        </el-button>
      </el-form-item>
      <el-form-item label="比例" style="text-align: left">
        <el-button :type="form.bili === '方图' ? 'primary' : undefined" plain>
          方图
        </el-button>
        <el-button :type="form.bili === '横图' ? 'primary' : undefined" plain>
          横图
        </el-button>
        <el-button :type="form.bili === '竖图' ? 'primary' : undefined" plain>
          竖图
        </el-button>
      </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 { generate, imgTask, getBase64 } from '@/api/aleditor.js';
const emits = defineEmits(['closeType', 'setHtml', 'getImage']);
const form = ref({
  title: '',
  fengge: '绘图',
  bili: '方图',
});
const load = ref(false);

const create = () => {
  const p = {
    cnt: 1,
    docId: localStorage.getItem('fileId') || undefined,
    text: `${form.value.fengge} ${form.value.bili} | ${form.value.title}`,
  };
  load.value = true;
  generate({ data: p, noload: true })
    .then(r => {
      if (r.errCode !== 0) return;
      get(r.data.task_id);
    })
    .catch(() => {
      load.value = false;
    });
};

const get = id => {
  imgTask({
    data: {
      id,
    },
  })
    .then(r => {
      if (r.errCode !== 0) return;
      if (r.data.ratio !== 1) {
        let t = setTimeout(() => {
          clearTimeout(t);
          get(id);
        }, 500);
        return;
      }
      getBase64Func(r.data.images[0].id);
    })
    .catch(() => {
      load.value = false;
    });
};

const getBase64Func = id => {
  getBase64({
    data: {
      id,
      docId: localStorage.getItem('fileId') || undefined,
    },
  })
    .then(r => {
      load.value = false;
      if (r.data.errCode !== 0) return;
      emits('getImage', r.data.data);
    })
    .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%;
}

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