<!--
  功能：功能描述
  作者：Aaron
  时间：2023年07月31日 11:51:23
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class="bg-[#fff] p-5">
    <a-spin :spinning="loading">
      <div class="my-3">
        <a-steps :current="current" size="small">
          <a-step title="初始化">
            <template #icon>
              <SvgIcon :size="22" name="init" />
            </template>
          </a-step>

          <a-step title="退回">
            <template #icon>
              <SvgIcon :size="24" name="back" />
            </template>
          </a-step>
          <a-step title="开发">
            <template #icon>
              <SvgIcon :size="24" name="dev" />
            </template>
          </a-step>
          <a-step title="测试">
            <template #icon>
              <SvgIcon :size="24" name="test" />
            </template>
          </a-step>
          <a-step title="结束">
            <template #icon>
              <SvgIcon :size="24" name="end" />
            </template>
          </a-step>
        </a-steps>
      </div>

      <a-tabs default-active-key="1">
        <a-tab-pane key="1" tab="基础信息">
          <SchemaForm>
            <template #solution="{ formModel, field }">
              <div class="ql-box">
                <QuillEditor
                  theme="snow"
                  v-model:content="formModel[field]"
                  :options="editorOptions"
                  :readOnly="true"
                  contentType="html"
                />
              </div>
            </template>
          </SchemaForm>
        </a-tab-pane>
        <a-tab-pane key="2" tab="处理历史">
          <a-steps progress-dot :current="historyList.length - 1" direction="vertical">
            <a-step
              v-for="(item, index) in historyList"
              :key="index"
              :title="item.title"
              :description="item.remark"
            />
          </a-steps>
        </a-tab-pane>
      </a-tabs>
    </a-spin>
  </div>
</template>

<script lang="ts" setup>
  import { QuillEditor } from '@vueup/vue-quill';
  import '@vueup/vue-quill/dist/vue-quill.snow.css';
  import { useForm } from '@/components/core/schema-form';
  import { getEditFormSchema } from './formSchemas';
  import { useRoute } from 'vue-router';
  import { onMounted, ref } from 'vue';
  import { findOneById } from '@/api/issue';
  import { SvgIcon } from '@/components/basic/svg-icon';
  import { stateTypeList } from './data';
  const route = useRoute();
  const editorOptions = ref({
    theme: 'snow',
    modules: {
      toolbar: [
        [{ header: '1' }, { header: '2' }, { font: [] }],
        [{ list: 'ordered' }, { list: 'bullet' }],
        ['bold', 'italic', 'underline'],
        ['link', 'image'], // 添加图片按钮
      ],
    },
  });
  const loading = ref(false);

  const historyList = ref<
    Array<{
      state: number;
      remark: string;
      title?: string;
    }>
  >([]);

  const current = ref(0);

  const { id } = route.query;

  const [SchemaForm, formRef] = useForm({
    labelWidth: 150,
    schemas: getEditFormSchema({}, true),
    showActionButtonGroup: false,
    actionColOptions: {
      span: 24,
    },
    submitButtonOptions: {
      text: '保存',
    },
    disabled: true,
  });

  onMounted(async () => {
    loading.value = true;
    const res = await findOneById({ id: id as string });
    loading.value = false;

    if (res?.files && Array.isArray(res.files) && res.files.length) {
      res.files = res.files.map((e) => {
        return {
          name: e.originalFilename,
          url: e.url,
          id: e.id,
        };
      });
    }

    if (res.historys && Array.isArray(res.historys) && res.historys.length) {
      historyList.value = res.historys.map((e) => {
        return {
          state: e.state,
          remark: `${e.createUserName}于${e.createTime} : ${e.remark}`,
          title: stateTypeList.find((item) => item.value === e.state)?.label,
        };
      });
    }

    current.value = res?.state;

    formRef?.setFieldsValue(res);
    formRef?.clearValidate();
  });
</script>
<style lang="less" scoped></style>
