<template>
  <div>
    <CustomSubHeader>
      <CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
    </CustomSubHeader>
    <template v-if="$auth.user.permissions.includes('superAdmin')">
      <CCard>
        <CCardBody>
          <CRow>
            <CCol col="12" sm="3">
              <CCallout color="info">
                <small class="text-muted">تعداد ادمین ها</small><br>
                <strong class="h4">{{ countUser }}</strong>
              </CCallout>
            </CCol>
            <CCol col="12" sm="3">
              <CCallout color="danger">
                <small class="text-muted">تعداد اخبار</small><br>
                <strong class="h4">{{ countNews }}</strong>
              </CCallout>
            </CCol>
            <CCol col="12" sm="3">
              <CCallout color="success">
                <small class="text-muted">تعداد رسانه ها</small><br>
                <strong class="h4">{{ countGallery }}</strong>
              </CCallout>
            </CCol>
            <CCol col="12" sm="3">
              <CCallout color="warning">
                <small class="text-muted">تعداد پرونده خبری</small><br>
                <strong class="h4">{{ countFileNews }}</strong>
              </CCallout>
            </CCol>
          </CRow>
        </CCardBody>
      </CCard>

      <CRow>
        <CCol col="12" sm="6">
          <CCard>
            <CCardHeader>
              <slot name="header">
                <i class="fal fa-bars"></i>
                <span>پر بازدیدترین اخبار</span>
              </slot>
              <slot></slot>
            </CCardHeader>
            <CCardBody>
              <div class="mb-3" v-for="(item , index) in popNews.docs" :key="item._id">
                <nuxt-link class="mainLink" :to="{name: 'admin-news-details',params: { details: item._id }}">
                  <span class="numberMain">{{ index + 1 }}</span>
                  <span class="descMain">{{ item.title }}</span>
                </nuxt-link>
              </div>
            </CCardBody>
          </CCard>
        </CCol>
        <CCol col="12" sm="6">
          <CCard>
            <CCardHeader>
              <slot name="header">
                <i class="fal fa-bars"></i>
                <span>جدیدترین اخبار</span>
              </slot>
              <slot></slot>
            </CCardHeader>
            <CCardBody>
              <div class="mb-3" v-for="(item , index) in newNews.docs" :key="item._id">
                <nuxt-link class="mainLink" :to="{name: 'admin-news-details',params: { details: item._id }}">
                  <span class="numberMain">{{ index + 1 }}</span>
                  <span class="descMain">{{ item.title }}</span>
                </nuxt-link>
              </div>
            </CCardBody>
          </CCard>
        </CCol>
      </CRow>

    </template>
    <template v-else>
      <CCard>
        <CCardBody>
          <CRow>
            <CCol>
              <h1> پرتال استانداری</h1>
            </CCol>
          </CRow>
        </CCardBody>
      </CCard>
    </template>
  </div>
</template>

<script>
import moment from "moment-jalaali";

export default {
  data() {
    return {
      title: 'داشبورد',
      list_title: 'لیست',
      countUser: null,
      countNews: null,
      countGallery: null,
      countFileNews: null,
      popNews: [],
      newNews: []
    }
  },
  computed: {},
  methods: {
    jDate(date) {
      return moment(date).format("jYYYY/jMM/jDD")
    },
  },
  head() {
    return {
      title: this.title
    }
  },
  layout: 'admin',
  async asyncData({$axios, $auth, error}) {
    try {
      // let returnObj = {};

      if ($auth.user.permissions.includes('superAdmin')) {

        const requests = [
          $axios.get(`/api/admin/user`, {progress: false}),
          $axios.get(`/api/admin/news`, {progress: false}),
          $axios.get(`/api/admin/gallery`, {progress: false}),
          $axios.get(`/api/admin/newsFile`, {progress: false}),
          $axios.get(`/api/public/getNews?flag=popular&portal=ostandari`, {progress: false}),
          $axios.get(`/api/public/getNews?flag=new&portal=ostandari`)
        ];

        const fetch = await Promise.all(requests);

        const [countUser, countNews, countGallery, countFileNews, popNews, newNews] = fetch;

        return {
          countUser: countUser.data.totalDocs,
          countNews: countNews.data.totalDocs,
          countGallery: countGallery.data.totalDocs,
          countFileNews: countFileNews.data.length,
          popNews: popNews.data,
          newNews: newNews.data,
        }
      }

    } catch (e) {
      error({
        status: 500,
        message: "مشکلی در گرفتن اطلاعات بوجود آمده است",
      });
    }
  }
}
</script>
