<template>
  <ai-detail class="AppDynamicDetail">
    <template slot="title">
      <ai-title title="详情" isShowBack isShowBottomBorder @onBackClick="cancel(false)">
      </ai-title>
    </template>
    <template slot="content">
      <ai-card title="基本信息">
        <template #content>
          <ai-wrapper
            label-width="120px">
            <ai-info-item label="标题" isLine :value="info.title"></ai-info-item>
            <ai-info-item label="发布地区" isLine :value="info.areaName"></ai-info-item>
            <ai-info-item label="文章类型" isLine :value="info.contentType === '0' ? '文章' : '视频'"></ai-info-item>
            <ai-info-item label="分类" v-if="info.categoryName" isLine :value="info.categoryName"></ai-info-item>
            <ai-info-item label="正文" v-if="info.contentType === '0'" isLine>
              <AiArticle :value="info.content"></AiArticle>
            </ai-info-item>
            <ai-info-item label="附件" isLine>
              <div class="files">
                <div class="file-item" v-for="(item, index) in info.files" :key="index">
                  <img :src="item.url" v-if="item.postfix !== '.mp4'">
                  <video controls v-else :src="item.url"></video>
                </div>
              </div>
            </ai-info-item>
          </ai-wrapper>
        </template>
      </ai-card>
    </template>
  </ai-detail>
</template>

<script>
  export default {
    name: 'Detail',

    props: {
      instance: Function,
      dict: Object,
      params: Object,
      moduleId: String
    },

    data () {
      return {
        info: {},
        id: ''
      }
    },

    created () {
      if (this.params && this.params.id) {
        this.id = this.params.id
        this.getInfo(this.params.id)
      }
    },

    methods: {
      getInfo (id) {
        this.instance.post(`/app/appcontentinfo/queryDetailById?id=${id}`).then(res => {
          if (res.code === 0) {
            this.info = res.data
            this.info.pictureUrl = res.data.pictureUrl ? [{
              url: res.data.pictureUrl
            }] : []
          }
        })
      },

      cancel (isRefresh) {
        this.$emit('change', {
          type: 'List',
          isRefresh: !!isRefresh
        })
      }
    }
  }
</script>

<style scoped lang="scss">
  .AppDynamicDetail {
    .files {
      display: flex;
      align-items: center;
      flex-wrap: wrap;
      .file-item {
        width: 240px;
        height: 240px;
        margin: 0 20px 20px 0;

        img, video {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
    }
  }
</style>
