<template>
  <a-list :class="$style.home" :data-source="data">
    <a-skeleton active :paragraph="{ rows: xwdt ? 3 : 6 }" :loading="loading" />
    <a-list-item slot="renderItem" slot-scope="record, index" :class="$style.items">
      <div
        custom-div
        :style="{ width: newsId === 'xwdt' && index === 0 ? '100%' : 'auto' }"
        style="display: flex; flex-flow: row wrap"
      >
        <div v-if="index === 0 && xwdt" :class="$style.firstNotice">
          <a-row v-if="imgdata.length > 0" :gutter="24" :class="$style.maint" style="height: 200px">
            <a-col v-for="(img, index) in imgdata" :key="index" :span="12">
              <img
                :class="['previewimg', $style.tpxwimg]"
                :src="
                  `api/flow-mobile/v1/task-form-process/download-attachments/${img[0].code}`
                    | sdResource
                "
                @click="
                  () => {
                    openUrl(getArticleUrl('', img))
                  }
                "
              />
            </a-col>
          </a-row>
        </div>
        <template v-if="index === 0 && xwdt">
          <div :class="$style.list" style="padding-top: 20px">
            <a-list-item-meta>
              <a
                slot="title"
                :href="getArticleUrl(type, record)"
                target="_blank"
                rel="opener"
                :title="record.title"
              >
                <a-badge color="blue" :text="record.title" />
              </a>
            </a-list-item-meta>
            <div :class="$style.dates">{{ moment(record.finishTime).format('YYYY-MM-DD') }}</div>
          </div>
        </template>
      </div>
      <template v-if="index !== 0 || !xwdt">
        <div :class="$style.list">
          <a-list-item-meta>
            <a
              slot="title"
              :href="getArticleUrl(type, record)"
              target="_blank"
              rel="opener"
              :title="record.title"
            >
              <a-badge color="blue" :text="record.title" />
            </a>
          </a-list-item-meta>
          <div :class="$style.dates">{{ moment(record.finishTime).format('YYYY-MM-DD') }}</div>
        </div>
      </template>
    </a-list-item>
    <a-empty v-if="isempty" :class="$style.emptySize">
      <a-icon slot="image" type="sd-notebook3" />
    </a-empty>
  </a-list>
</template>

<script>
import moment from 'moment'
import homepageService from './homepage-service'
import components from './_import-components/home-xwdt-list-import'
import axios from '@/common/services/axios-instance'
export default {
  name: 'HomeXwdtList',
  components,
  props: {
    type: {
      default: '',
      type: String,
    },
    newsId: {
      default: '',
      type: String,
    },
    xwdt: {
      default: false,
      type: Boolean,
    },
    maxResults: {
      default: 6,
      type: Number,
    },
  },
  data() {
    return {
      loading: false,
      data: [],
      imgdata: [],
      skeletonLoading: true,
      isempty: false,
      moment,
    }
  },
  mounted() {
    this.loading = true
    this.getNewsList({ articleBusinessType: this.type, maxResults: this.maxResults })
  },
  methods: {
    getArticleUrl(type, record) {
      if (type === 'getKmKnowledges') {
        return `?#/km-knowledage-view?id=${record.id}&title=${record.title}`
      } else if (type === 'getLawInstitutions') {
        if (record.attribute === 'laws') {
          // return `?#/iam-audit-auditsource-law-form?record=${record.id}`
          return `?#/iam-audit-home-three?type=laws&id=${record.id}`
        } else {
          // return `?#/iam-audit-auditsource-institution-form?record=${record.id}`
          return `?#/iam-audit-home-three?type=nbzd&id=${record.id}`
        }
      } else {
        // return `?#/sd-webflow/done-pages/${record.instId}`
        return `?#/iam-audit-home-three?type=xwdt&id=${record.instId}`
      }
    },
    // 降序函数  封装
    Desc(arr, StrKey) {
      var desc = function (x, y) {
        return x[StrKey] < y[StrKey] ? 1 : -1
      }
      return arr.sort(desc)
    },
    getNewsList(params) {
      this.loading = true
      homepageService.getNews(params).then((res) => {
        this.loading = false
        if (res) {
          if (this.newsId === 'xwdt') {
            if (res.data['02'].length) {
              res.data['02'].forEach((item) => {
                item.finishTime = item.finishdate
              })
              this.data = res.data['02'].splice(0, this.maxResults)
            } else {
              this.isempty = true
            }
            if (res.data['01']) {
              if (res.data['01'].length) {
                this.imgdata = []
                res.data['01'].forEach((item) => {
                  this.fngetattachment(
                    item.thumbnail,
                    'entity:iamAuditInformation',
                    '',
                    item.instId
                  )
                })
              }
            }
          } else {
            if (res.data.length) {
              if (this.type === 'getLawInstitutions') {
                res.data.forEach((item) => {
                  item.title = item.docTitle
                  item.finishTime = moment(item.publishTime).format('YYYY-MM-DD')
                })
              } else {
                res.data.forEach((item) => {
                  // item.title = item.docTitle
                  item.finishTime = moment(item.lastModifiedTime).format('YYYY-MM-DD')
                })
              }
              this.data = res.data.splice(0, this.maxResults)
            } else {
              this.isempty = true
            }
          }
        } else {
          this.isempty = true
        }
      })
    },
    fngetattachment(id, formId, beanId, instId) {
      return axios
        .get(`api/xcoa-mobile/v1/attachment-extend/attachments-info-perm/${id}`, {
          params: {
            formId: formId,
            beanId: beanId,
            instId: instId,
          },
        })
        .then((res) => {
          this.imgdata.push({ ...res.data, instId: instId })
        })
    },
    openUrl(url) {
      window.open(url)
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
$importcolor: #e02020;
$importsize: 28px;
$lineheight: 28px;
$fonsicon-bg: 48px;
$nodata-icon: #d9d9d9;
.home {
  :global(.ant-table-placeholder) {
    border: 0;
  }
  .first-notice {
    height: 150px;
    width: 100%;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    a {
      overflow: hidden;
      font-size: $importsize;
      font-weight: bold;
      color: $importcolor;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .detail {
      max-height: 60px;
      overflow: hidden;
      line-height: $lineheight;
    }
  }
  .items {
    border-bottom: none;
    .list {
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: 100%;
      :global(.ant-list-item-meta) {
        display: block;
        flex: inherit;
        width: calc(100% - 100px);
      }
      :global(.ant-badge) {
        display: inline-block;
        max-width: calc(100% - 50px);
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        vertical-align: middle;
      }
    }
    .dates {
      min-width: 90px;
    }
  }
  :global(.ant-tag) {
    margin-left: 10px;
  }
  :global(.ant-badge-status-primary) {
    background-color: $primary-color;
  }
  :global(.ant-badge-status-dot) {
    width: 10px;
    height: 10px;
  }
  :global(.ant-badge-status-text) {
    font-size: $font-size-lg;
  }
}
.empty-size {
  margin: 10% 0;
  :global(.ant-empty-image) {
    height: 50px;
    i {
      height: 100%;
      font-size: $fonsicon-bg;
      color: $nodata-icon;
    }
  }
}
.tpxwimg {
  width: 340px !important;
  cursor: pointer;
}
@media screen and (min-width: 1920px) {
  .items {
    padding: 12px 0;
  }
}
@media screen and (min-width: 1440px) and (max-width: 1599px) {
  .items {
    padding: 10px 0;
  }
}
@media screen and (min-width: 1600px) and (max-width: 1919px) {
  .items {
    padding: 10px 0;
  }
}
@media screen and (min-width: 992px) and (max-width: 1439px) {
  .items {
    padding: 8px 0;
  }
}
</style>
