<template>
  <a-row>
    <a-col class="theme-wrap">
      <a-col class="tag-wrap" />
      <a-list
        :grid="{ gutter: 16, column: 4 }"
        :data-source="dataSource"
        :loading="loading"
        class="j-table-force-nowrap template_list"
        :style="{ height: '100%' }"
      >
        <template #loadMore>
          <div v-if="!loading && !moreLoading" :style="{ textAlign: 'center', marginTop: '12px', height: '32px', lineHeight: '32px' }">
            <a-button @click="onLoadMore">loading more</a-button>
          </div>
        </template>

        <template #renderItem="{ item, index }">
          <a-list-item>
            <a-card :title="item.name" class="text-center">
              <div class="item" @mouseover="hoverThis">
                <div class="Pic">
                  <a class="img">
                    <img v-if="checkUrl(item.images)" :key="item.code" :alt="item.name" :src="domainUrl + '/sys/common/static/' + item.images" />
                    <img v-else alt="item.name" :src="domainUrl + '/sys/common/static/' + item.images" />
                  </a>
                  <div class="Content">
                    <a-button class="fl" block @click="visit(item, index)">模板预览</a-button>
                    <a-button type="primary" block class="fr" @click="createSite(item)">创建站点</a-button>
                  </div>
                </div>
                <div class="clear"></div>
              </div>
            </a-card>
          </a-list-item>
        </template>
      </a-list>

      <adweb-create-site ref="modelRef" />
      <a-modal v-model:open="visible" width="80%" @cancel="visitImageCancel" :footer="null" class="self_buy_modal">
        <template #title>
          <div>
            <div>
              <p class="ant-modal-title">
                模板预览
                <template
                  v-if="
                    (userRole.indexOf('admin') > -1 ||
                      userRole.indexOf('adweb_admin') > -1 ||
                      userRole.indexOf('adweb_seo_manager') > -1 ||
                      userRole.indexOf('adweb_site_manager') > -1) &&
                    modelText != null &&
                    modelText != ''
                  "
                >
                  <a :href="modelText" target="_blank" style="font-size: 14px; text-decoration: underline"><span>购买链接</span></a>
                </template>
              </p>
            </div>
          </div>
        </template>

        <img alt="example" style="width: 100%" :src="theImage" />
      </a-modal>
    </a-col>
  </a-row>
</template>

<script lang="ts" setup name="AdwebThemeCList">
  import '/@/assets/less/TableExpand.less';
  import '/@/assets/less/common.less';
  import { getAction } from '/@/api/manage/manage';
  import $ from 'jquery';

  import { nextTick, onMounted, reactive, ref } from 'vue';
  import { useMessage } from '@/hooks/web/useMessage';
  import AdwebCreateSite from '@/views/adweb/theme/components/AdwebCreateSite.vue';

  const url = reactive({
    list: '/adweb/adwebTheme/list',
    delete: '/adweb/adwebThemeC/delete',
    deleteBatch: '/adweb/adwebThemeC/deleteBatch',
    createSiteUrl: 'siteManage/createSite',
    addSiteLanguageUrl: 'siteManage/addSiteLanguage',
    getTemplateTagsUrl: '/adweb/adwebThemeC/getTemplateTags',
    wpSiteCreateUrl: 'wpWebSite/addWebsite',
    getAdwebVipUrl: 'adweb/adwebSiteManage/getAdwebVip',
  });

  const domainUrl = ref(window._CONFIG.domianURL);

  const userRole = ref('');

  const modelText = ref('');
  const theImage = ref('');

  let dataSource = reactive([
    {
      createBy: '',
      createTime: '',
      descirbe: '',
      domain: '',
      id: '',
      images: '',
      name: '',
      putaway: '',
      runStatus: null,
      templateColorId: '',
      templatePrice: 0,
      templateTags: '',
      updateBy: null,
      updateTime: null,
      visible: true,
    },
  ]);
  const loading = ref(true);
  const moreLoading = ref(false);
  const modelRef = ref();
  const visible = ref(false);

  const pageNo = ref(1);

  const { createMessage } = useMessage();

  onMounted(() => {
    getTemplateList();
  });

  function getTemplateList() {
    loading.value = true;
    let listUrl = `${url.list}` + '?pageNo=' + pageNo.value + '&pageSize=8&templateTags=' + 22;
    getAction(listUrl, null).then(function (res) {
      if (res.code == 200) {
        loading.value = false;

        // 第一页数据直接赋值数组，后续合并数组
        if (res.result.current === 1) {
          dataSource = res.result.records.map((item) => ({
            ...item,
            visible: false,
          }));
        } else {
          dataSource = dataSource.concat(
            res.result.records.map((item) => ({
              ...item,
              visible: false,
            }))
          );
        }

        if (res.result.current >= res.result.pages) {
          moreLoading.value = true;
        }
      } else {
        loading.value = false;
        createMessage.error('模板获取失败！');
      }

      nextTick(() => {
        // Resetting window's offsetTop so as to display react-virtualized demo underfloor.
        // In real scene, you can using public method of react-virtualized:
        // https://stackoverflow.com/questions/46700726/how-to-use-public-method-updateposition-of-react-virtualized
        window.dispatchEvent(new Event('resize'));
      });
    });
  }

  function checkUrl(val) {
    if (val != null) {
      if (val.substr(0, 7).toLowerCase() == 'http://' || val.substr(0, 8).toLowerCase() == 'https://') {
        return false;
      } else {
        return true;
      }
    } else {
      return false;
    }
  }

  function hoverThis() {
    $('.item').hover(
      function () {
        if ($(this).height() >= $(this).find('.Pic img').height()) {
          return;
        } else {
          $(this)
            .find('.Pic img')
            .stop()
            .animate({ top: $(this).height() - $(this).find('.Pic img').height() }, 5000);
        }
      },
      function () {
        $(this).find('.Pic img').stop().animate({ top: 0 }, 'fast');
      }
    );
  }
  //预览
  function visit(data, index) {
    if (data.domain != null && data.domain != '' && data.putaway === 1) {
      return window.open(data.domain, '_blank');
    } else {
      visitImage(data);
    }
  }

  function visitImage(data) {
    visible.value = true;
    modelText.value = data.buyLinks;
    theImage.value = domainUrl.value + '/sys/common/static/' + data.images;
  }

  function visitImageCancel() {
    visible.value = false;
    modelText.value = '';
    theImage.value = '';
  }

  function onLoadMore() {
    loading.value = true;
    pageNo.value += 1;
    getTemplateList();
  }

  function createSite(data) {
    modelRef.value.initCreateSiteModel(data);
  }
</script>
<style scoped lang="less">
  .mt-20 {
    margin-top: 20px;
  }

  .fl {
    float: left;
  }

  .fr {
    float: right;
  }

  .template-image {
    width: 100%;
    height: 300px;
  }

  .text-center {
    text-align: center;
  }

  .template_list /deep/ .ant-card-body {
    padding: 0;
  }

  .template_list .item {
    width: 100%;
    display: block;
    height: 467px;
    position: relative;
    transition: all 0.4s ease-out;
  }

  .template_list .item .Pic {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
  }

  .template_list .item .Pic img {
    width: 100%;
    max-height: unset;
    position: absolute;
    top: 0;
    left: 0;
  }

  .template_list .item .Pic .Content {
    display: flex;
    justify-content: center;
    align-items: flex-end;
    opacity: 0;
    position: absolute;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.4);
    top: 0;
    left: 0;
    font-size: 0;
    transition: all 0.4s ease-out;
    padding-bottom: 20px;
    cursor: pointer;
  }

  .template_list .item .Pic .Content button {
    margin: 0 10px;
  }

  .template_list .item:hover {
    box-shadow: 3px 3px 12px 2px #aaaaaad4;
  }

  .template_list .item:hover .Pic .Content {
    opacity: 1;
  }

  .tag-wrap .ant-btn.ant-btn-link.act {
    color: @primary-color!important;
    background-color: #fff !important;
    font-weight: 600;
    border-bottom: 1px solid #fff;
  }

  .tag-wrap {
    margin-bottom: 12px;
  }

  .tag-wrap .ant-btn-link {
    color: #fff !important;
    width: 10%;
    margin: 5px 0.5%;
  }

  .template_list {
    overflow-y: scroll;
    overflow-x: hidden;
  }

  .ant-btn-link {
    margin-right: 10px;
  }

  img[lazy='error'],
  img[lazy='loading'] {
    top: calc(50% - 140px) !important;
    position: relative !important;
    margin: 0 auto;
    width: 240px !important;
  }

  .self_buy_modal {
    /deep/ .ant-modal-body {
      padding: 0;
    }
  }
</style>
