<template>
  <div class="bg-bottom">
    <div class="bg-top">
      <div class="content-1180">
        <div class="d-flex pt-4">
          <LeftNav :code="code" :list="list" class="mr-4" @chanegCode="chanegCode" />
          <div class="flex-1 flex-column">
            <Location :title="defaultTitle" />
            <div class="content-detail p-4" :style="{ 'min-height': list.children.length * 56 + 'px' }">
              <ListPage v-if="!introId" :code="code" @changeYear="changeYear" :content="content" @getData="getData" />
              <RichText v-else :rich-obj="introContent" />
            </div>
            <div v-if="introContent.relations && introContent.relations.length" class="content-detail p-4">
              <Relevant :data="introContent" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapActions } from 'vuex';
import LeftNav from '@/components/LeftNav/LeftNav.vue';
import ListPage from '@/components/ListPage/ListPage.vue';
import RichText from '@/components/RichText/RichText.vue';
import Location from '@/components/Location/Location.vue';
import Relevant from '@/components/RichText/Relevant.vue';

export default {
  components: { LeftNav, ListPage, Location, RichText, Relevant },
  data() {
    return {
      code: '0501',
      defaultTitle: '通知',
      list: {
        title: '通知公告',
        url: '/notice',
        children: [
          {
            title: '通知',
            code: '0501',
          },
          {
            title: '通报',
            code: '0502',
          },
          {
            title: '公告',
            code: '0503',
          },
          {
            title: '值班安排',
            code: '0504',
          },
          {
            title: '浮窗',
            code: '0505',
          },
        ],
      },
      year: '2022',
      content: {},
      pageNum: 1,
      introId: '',
      introContent: {},
    };
  },
  watch: {
    '$route.query.code'(val) {
      if (this.$route.path === this.list.url) {
        this.code = val;
        this.getDetault(val);
        this.getContentData();
        this.introContent = {};
      }
    },
  },
  created() {
    this.code = this.$route.query.code;
    this.getDetault(this.code);
    if (this.$route.query.introId) {
      this.introId = this.$route.query.introId;
      this.getIntroContent();
    }
    this.getContentData();
  },
  methods: {
    ...mapActions('home', ['getContent', 'getDetail']),
    // 获取默认显示标题
    getDetault(val) {
      for (let i = 0; i < this.list.children.length; i++) {
        if (val === this.list.children[i].code) {
          this.defaultTitle = this.list.children[i].title;
        }
      }
    },
    async getContentData() {
      try {
        const param = {
          showPage: this.code,
          year: this.year,
          showType: '0',
          pageNum: this.pageNum,
          pageSize: 10,
        };
        const data = await this.getContent(param);
        this.content = data;
      } catch (error) {}
    },
    // 获取文章信息
    async getIntroContent() {
      try {
        const param = {
          showPage: '',
          introId: this.introId,
        };
        const data = await this.getDetail(param);
        this.introContent = data || {};
      } catch (error) {
        console.error('error: ', error);
      }
    },
    chanegCode(code) {
      this.code = code;
      this.introId = '';
    },
    changeYear(year) {
      this.year = year;
      this.getContentData();
    },
    getData(pageNum) {
      console.log('pageNum: ', pageNum);
      this.pageNum = pageNum;
      this.getContentData();
    },
  },
};
</script>
<style scoped>
.content-detail {
  background: #fff;
}
</style>
