<template>
  <div class="guns-layout">
    <div class="guns-layout-content">
      <div class="guns-layout">
        <div class="guns-layout-content-header portal-config-header">
          <a-space :size="16">
            <span class="back-btn" @click="back">
              <left-outlined />
              <span>返回</span>
            </span>
            <a-divider type="vertical" />
            <span class="title">{{ currentComponentData?.elementName }}</span>
          </a-space>
        </div>
        <div class="guns-layout-content-application">
          <div class="content-mian">
            <div class="content-mian-header p-t-12" v-if="showTab">
              <div class="header-content">
                <a-tabs v-model:activeKey="activeKey" class="devops-tabs" @change="activeKeyChange">
                  <a-tab-pane :tab="cItem.categoryName" v-for="cItem in categoryList" :key="cItem.categoryId" />
                </a-tabs>
              </div>
            </div>
            <div :class="['content-mian-body', { 'p-t-12': !showTab }]">
              <div class="table-content">
                <common-table
                  :columns="columns"
                  :where="where"
                  :showToolTotal="false"
                  showTableTool
                  :isInit="false"
                  rowId="appId"
                  ref="tableRef"
                  :rowSelection="false"
                  url="/component/news/pageList"
                >
                  <template #toolLeft>
                    <a-input
                      v-model:value="where.searchText"
                      placeholder="请输入名称（回车搜索）"
                      @pressEnter="reload"
                      :bordered="false"
                      style="width: 240px"
                      class="search-input"
                    >
                      <template #prefix>
                        <icon-font iconClass="icon-opt-search"></icon-font>
                      </template>
                    </a-input>
                  </template>
                  <template #bodyCell="{ column, record }">
                    <template v-if="column.dataIndex == 'newsTitle'">
                      <a @click="newClick(record)">{{ record.newsTitle }}</a>
                    </template>
                  </template>
                </common-table>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRoute, useRouter } from 'vue-router';
import { ref, onMounted, computed } from 'vue';
import { PortalDesignApi } from '../../portal/backend/design/api/PortalDesignApi';
import { NewApi } from '../../portal/backend/config_detail/new/api/NewApi';

const route = useRoute();
const router = useRouter();

const categoryId = ref('');
const currentDesignId = ref('');
const widgetId = ref('');

const activeKey = ref('');

const loading = ref(false);

const currentComponentData = ref(null);

// 新闻分类列表
const categoryList = ref([]);

const where = ref({});

// 表格配置
const columns = ref([
  {
    key: 'index',
    title: '序号',
    width: 40,
    align: 'center',
    isShow: true,
    hideInSetting: true
  },
  {
    dataIndex: 'newsTitle',
    title: '标题名称',
    ellipsis: true,
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'showPublishUser',
    title: '发布人',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'showPublishTime',
    title: '发布时间',
    width: 100,
    isShow: true
  }
]);
// ref
const tableRef = ref(null);

const showTab = computed(() => {
  return categoryList.value?.length > 1 ? true : false;
});

onMounted(() => {
  const { newsCategoryId, designId, elementId } = route.query;
  if (!(newsCategoryId || designId || elementId)) return back();
  currentDesignId.value = designId;
  categoryId.value = newsCategoryId;
  widgetId.value = elementId;
  getDesignConfig();
});

// 获取门户配置
const getDesignConfig = () => {
  if (!currentDesignId.value) return;
  loading.value = true;
  PortalDesignApi.getDesignConfig({ designId: currentDesignId.value })
    .then(res => {
      if (res.data?.designLayoutJson?.widgets?.length) {
        const data = res.data?.designLayoutJson?.widgets.find(item => item.widget.id == widgetId.value);
        if (data) {
          currentComponentData.value = data.widget?.param;
          getCategoryList();
        }
      }
    })
    .finally(() => (loading.value = false));
};

/**
 * 获取分类列表
 */
const getCategoryList = () => {
  if (!currentComponentData.value) return;
  if (currentComponentData.value?.selectCateList?.length) {
    loading.value = true;
    NewApi.summary({ designId: currentDesignId.value, categoryIdList: currentComponentData.value?.selectCateList.map(item => item.id) })
      .then(res => {
        if (res.data && res.data.length > 0) {
          categoryList.value = res.data;

          if (categoryId.value) {
            activeKey.value = categoryId.value;
          } else {
            activeKey.value = res.data[0].categoryId;
          }
          where.value.categoryId = activeKey.value;
          where.value.designId = currentDesignId.value;
          reload();
        }
      })
      .finally(() => {
        loading.value = false;
      });
  }
};

const reload = () => {
  tableRef.value.reload();
};

// 返回
const back = () => {
  router.push('/home');
};

// 新闻点击
const newClick = async item => {
  // 判断是否是外链新闻
  let _linkFlag = item.linkFlag;
  if (_linkFlag && _linkFlag === 'Y') {
    let _linkUrl = item.linkUrl;
    if (_linkUrl) {
      // 判断是否是以指定字符串结尾 需要拼接gunsToken
      const result = _linkUrl.endsWith('&grantType=token&gunsToken=');
      if (result) {
        let token = await HomeApi.jwtAaToken();
        redirectUrl(_linkUrl + token, 2);
      } else {
        redirectUrl(_linkUrl, 2);
      }
    }
  } else {
    const { href } = router.resolve({
      path: '/home/newsDetail',
      query: {
        newsId: item.newsId
      }
    });
    window.open(href, '_blank');
  }
  item.clickStatusFlag = 'Y';
};

// 分类切换
const activeKeyChange = () => {
  where.value.categoryId = activeKey.value;
  reload();
};
</script>

<style scoped lang="less">
.back-btn {
  cursor: pointer;
  font-size: 16px;
  color: #60666b;
  font-weight: 400;

  .anticon {
    font-size: 14px;
    margin-right: 4px;
    color: #60666b;
  }
}
.ant-divider {
  border-left: 1.5px solid #c5cfd1;
}
.title {
  color: #43505e;
  font-size: 18px;
  font-weight: 700;
}
.portal-config-header {
  height: auto !important;
  border-bottom: 1px solid rgba(197, 207, 209, 0.4);
  margin-bottom: 0 !important;
  padding-bottom: 12px;
}
</style>
