<template>
  <div class="banner m-auto relative sm:flex">
    <svg viewBox="0 0 1440 181" fill="none" xmlns="http://www.w3.org/2000/svg"
         class="pointer-events-none absolute w-full top-[-2px] transition-all text-green-5 flex-shrink-0 opacity-100 duration-[400ms] opacity-80 -z-10">
      <mask id="path-1-inside-1_414_5526" fill="white">
        <path d="M0 0H1440V181H0V0Z"></path>
      </mask>
      <path d="M0 0H1440V181H0V0Z" fill="url(#paint0_linear_414_5526)" fill-opacity="0.22"></path>
      <path d="M0 2H1440V-2H0V2Z" fill="url(#paint1_linear_414_5526)" mask="url(#path-1-inside-1_414_5526)"></path>
      <defs>
        <linearGradient id="paint0_linear_414_5526" x1="720" y1="0" x2="720" y2="181" gradientUnits="userSpaceOnUse">
          <stop stop-color="currentColor"></stop>
          <stop offset="1" stop-color="currentColor" stop-opacity="0"></stop>
        </linearGradient>
        <linearGradient id="paint1_linear_414_5526" x1="0" y1="90.5" x2="1440" y2="90.5" gradientUnits="userSpaceOnUse">
          <stop stop-color="currentColor" stop-opacity="0"></stop>
          <stop offset="0.395" stop-color="currentColor"></stop>
          <stop offset="1" stop-color="currentColor" stop-opacity="0"></stop>
        </linearGradient>
      </defs>
    </svg>
    <div class="md:w-screen-xl m-auto">
      <Breadcrumb :data="form"/>
      <div class="py-8 sm:py-16" _path="/templates" _dir="" _draft="false" _partial="false" _locale=""
           _id="content:4.templates.yml" _type="yaml" _source="content" _file="4.templates.yml" _stem="4.templates"
           _extension="yml">
        <div class="gap-8 sm:gap-y-16 lg:items-center" v-if="form">
          <div class="w-full sm:px-0 px-4">
            <div class="flex flex-1">
              <template v-if="form.companyLogo">
                <el-avatar :src="form.companyLogo" shape="square" :size="180"
                           class="hidden-sm-and-down rounded-avatar shadow-sm hover:shadow mr-4"/>
                <el-avatar :src="form.companyLogo" shape="square" :size="80"
                           class="hidden-sm-and-up rounded-avatar-xs shadow-sm hover:shadow mr-4"/>
              </template>
              <div class="title flex flex-col">
                <h1
                    class="text-2xl font-bold tracking-tight text-gray-900 dark:text-white sm:text-3xl lg:text-4xl">
                  <span v-if="form.tenantName">{{ form.tenantName }}</span>
                </h1>
                <div class="my-1 text-sm text-gray-500 w-auto sm:max-w-3xl max-w-xs flex-1 dark:text-gray-400">
                  {{ form?.comments || desc }}
                </div>
                <!--                <a class="company-name text-sm my-1">-->
                <!--                  {{ form.companyName || 'WebSoft Inc.' }}-->
                <!--                </a>-->
                <el-rate v-model="form.rate" disabled />
                <div class="btn">
                  <el-space class="mt-4">

                    <!-- 1. 未购买 -->
<!--                    <el-button-->
<!--                        v-if="!form.isBuy && !form.price"-->
<!--                        type="success"-->
<!--                        @click="onBuy(form)"-->
<!--                    >-->
<!--                      立即开通-->
<!--                    </el-button>-->

                    <!-- 2. 已购买  @click="installPlug"-->
                    <el-button type="primary">
                      产品控制台
                    </el-button>

                    <!-- 3. 其他按钮 -->
                    <template v-for="(item,index) in form.links" :key="index">
                      <div v-if="item.qrcode">
                        <el-popover
                            placement="top-start"
                            :width="200"
                            trigger="hover"
                        >
                          <template #default>
                            <div class=" p-2 flex justify-center">
                              <el-image :src="item.qrcode" :size="160"/>
                            </div>
                          </template>
                          <template #reference>
                            <el-button :icon="ElIconFullScreen">{{ item.type }}</el-button>
                          </template>
                        </el-popover>
                      </div>
                      <el-button
                          v-else
                          @click="openSpmUrl(`${item.domain}`,item,item.tenantId,true)"
                      >
                        {{ item.type }}
                      </el-button>
                    </template>

                    <!--                    <template v-else>-->
                    <!-- 1.1 已安装 -->
                    <!--                      <template v-if="form.installed">-->
                    <!--                        <el-button-->
                    <!--                            type="primary"-->
                    <!--                            @click="openSpmUrl(`https://${form.domain}`,form,form.companyId)"-->
                    <!--                        >-->
                    <!--                          产品控制台-->
                    <!--                        </el-button>-->
                    <!--                      </template>-->
                    <!-- 1.2 未安装 -->

                    <!--                    </template>-->


                  </el-space>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import {FullScreen} from '@element-plus/icons-vue'
import Breadcrumb from "~/components/Breadcrumb.vue";
import type {ApiResult} from "~/api";
import type {Company} from "~/api/system/company/model";

const token = useToken();

const props = withDefaults(
    defineProps<{
      title?: string;
      desc?: string;
      buyUrl?: string;
      form?: Company;
      value?: number;
    }>(),
    {
      title: 'Templates',
      desc: 'Explore community templates to get up and running in a few seconds.',
      demoUrl: '/product/website',
      buyUrl: 'https://github.com/websoft9/ansible-templates',
      value: 4.2
    }
);

const emit = defineEmits<{
  (e: 'done'): void
}>()

const onBuy = (item: Company) => {
  // if(item.type === 1){
  //   // 插件
  //   openSpmUrl(`/product/checkout`,item,item.productId)
  // }else {
  //   // 产品
  //   openSpmUrl(`/product/create`,item,item.productId)
  // }
  if (!token.value || token.value == '') {
    ElMessage.error('请先登录');
    setTimeout(() => {
      openSpmUrl(`/product/create`, item, item.companyId)
    }, 500)

  }
}

// 安装插件
const installPlug = () => {
  const loading = ElLoading.service({
    lock: true,
    text: '安装中...'
  })
  useClientRequest<ApiResult<any>>(`/system/menu/install`, {
    method: 'POST',
    body: {
      companyId: getIdBySpm(5)
    }
  }).then(res => {
    if (res.code === 0) {
      setTimeout(() => {
        ElMessage.success(res.message);
        loading.close()
        emit('done')
      }, 500)
    }
  })
}

</script>
<style scoped lang="less">
.rounded-avatar {
  border-radius: 30px;
}

.rounded-avatar-xs {
  border-radius: 20px;
}
</style>
