<script setup lang="ts">
import type {BreadcrumbItem} from "~/types/global";
import type {CmsNavigation} from "~/api/cms/cmsNavigation/model";
import {locationUrl, navTo, paramsId} from "~/utils/common";
import type {CmsArticle} from "~/api/cms/cmsArticle/model";
import useFormData from "~/utils/use-form-data";
import Banner from "@/components/Banner.vue";
import type {Layout} from "~/api/layout/model";
import {getCmsArticle} from "~/api/cms/cmsArticle";
import {listCmsNavigation} from "~/api/cms/cmsNavigation";
import Tags from "~/components/Tags.vue";


// 引入状态管理
const route = useRoute();
const layout = ref<Layout>({});
const breadcrumb = ref<BreadcrumbItem>();
const showPassword = ref<boolean>();
const category = ref<CmsNavigation[]>([]);
const fileList = ref<any>();

// 配置信息
const {form, assignFields} = useFormData<CmsArticle>({
  // 文章id
  articleId: undefined,
  // 文章模型
  model: undefined,
  // 文章标题
  title: undefined,
  // 分类类型
  type: undefined,
  // 展现方式
  showType: undefined,
  // 文章类型
  categoryId: undefined,
  // 文章分类
  categoryName: undefined,
  parentId: undefined,
  // 封面图
  image: undefined,
  // 附件
  files: undefined,
  // 缩列图
  thumbnail: undefined,
  // 视频地址
  video: undefined,
  // 上传的文件类型
  accept: undefined,
  // 来源
  source: undefined,
  // 文章内容
  content: undefined,
  // 虚拟阅读量
  virtualViews: undefined,
  // 实际阅读量
  actualViews: undefined,
  // 访问权限
  permission: undefined,
  // 访问密码
  password: undefined,
  password2: undefined,
  // 用户ID
  userId: undefined,
  // 用户昵称
  nickname: undefined,
  // 账号
  username: undefined,
  // 用户头像
  // userAvatar: undefined,
  author: undefined,
  // 所属门店ID
  shopId: undefined,
  //
  likes: undefined,
  // 排序
  sortNumber: undefined,
  // 备注
  comments: undefined,
  // 状态
  status: undefined,
  // 创建时间
  createTime: undefined,
  // 更新时间
  updateTime: undefined,
  tenantId: undefined,
  // 租户名称
  tenantName: undefined,
  // 租户logo
  logo: undefined,
  // 详情页路径
  detail: undefined
});

// 请求数据
const reload = async () => {
  await getCmsArticle(paramsId()).then(data => {
    assignFields(data)
    layout.value.banner = data?.banner;
    // 二级栏目分类
    if (data.parentId) {
      listCmsNavigation({parentId: data.parentId}).then(list => {
        category.value = list;
      })
    }

    if(form.permission === 1){
      console.log('登录可见')
      return;
    }
    if(form.permission === 2){
      console.log('需要密码')
      showPassword.value = true;
      return;
    }
  })

  // seo
  useSeoMeta({
    description: form.comments || form.title,
    keywords: form.title,
    titleTemplate: `${form?.title}` + ' - %s',
  })

  // 面包屑
  breadcrumb.value = form
}

watch(
    () => route.path,
    () => {
      reload();
    },
    {immediate: true}
);
</script>
<template>
  <Banner :layout="layout"/>

  <!-- 主体部分 -->
  <div class="clearfix p-5">

    <div class="m-page">
      <div class="sitemp h-[32px] flex justify-between">
        <h2>
          {{ form.categoryName || '分类名称' }}
        </h2>
      </div>
      <div class="detail-container">

        <!-- 产品详细 -->
        <div class="product_detail" id="pd1">
          <div class="allcontent clearfix">
            <div style="float: left;">
              <div class="img clearfix" id="play">
                <el-carousel v-if="form.files" :interval="4000">
                  <el-carousel-item v-for="item in JSON.parse(form.files)" :key="item" style="display: flex; align-items: center; justify-content: center">
                    <el-image fit="cover" :src="item" />
                  </el-carousel-item>
                </el-carousel>
              </div>
            </div>
            <div class="list mb-10">
              <ul class="list_p">
                <h1 class="title">{{ form.title }}</h1>
                <li><h2>{{ $t('categoryName') }}：<a :href="navTo(form,`/product/${form.categoryId}`)"><strong>{{ form.categoryName }}</strong></a></h2></li>
                <li class="text-left">{{ $t('createTime') }}：<span>{{ form.createTime }}</span></li>

                <li class="text-left">{{ $t('overview') }}：</li>
                <div class="bg-gray-100 p-2 w-[360px]">{{ form.comments }}</div>

                <li class="inquiry"><a :href="navTo(form,`/m/order/${form.articleId}.html}`)">{{ $t('onlineInquiry') }}</a></li>
              </ul>
            </div>
          </div>

          <div class="clearboth"></div>
          <div class="p_detail">
            <ul id="product-tab" class="product-tab clearfix">
              <li class="cur">{{ $t('show.detail') }}</li>

            </ul>
            <div class="content tab-content text-sm" v-html="form.content"></div>
          </div>

          <h3 class="tag">{{ $t('articleUrl') }}：{{ locationUrl() }} </h3>
          <Tags :data="form.tags" />
          <NextArticle />
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss">
.content {
  padding-top: 15px;
  overflow: hidden;
}
.content p{
  text-indent: 0;
}
.content img{
  padding: 5px 0;
  max-width: 100%;
  height: auto;
}
.content video {
  width: 100%;
  height: auto;
}
.product_detail{
  padding: 5px 0 !important;
}
.product_detail .list{
  margin-left: 0 !important;
}
.product_detail .img{
  width: 90vw !important;
}
</style>
