<template>
  <div>
    <CustomSubHeader>
      <CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
      <CButton size="sm" color="success" :to="{ name: 'admin-downloads-new' }" class="mr-auto">افزودن</CButton>
    </CustomSubHeader>

    <CCard>
      <CCardHeader :dir="paginate ? 'ltr' : 'rtl'" :style="{ textAlign: paginate ? 'center' : 'right' }">
        <el-pagination
          v-if="paginate"
          background
          layout="prev, pager, next"
          :current-page="Number($route.params.page)"
          :page-count="Number(downloads.totalPages)"
          @current-change="pageNumber"
        >
        </el-pagination>

        <slot v-else name="header">
          <CIcon name="cil-grid" />
          {{ list_title }}
        </slot>
      </CCardHeader>
      <CCardBody>
        <el-table :data="downloads.docs" style="width: 100%">
          <el-table-column type="index" label="#"> </el-table-column>

          <el-table-column label="کاور" width="170">
            <template slot-scope="scope">
              <el-image style="width: 100%; height: 100%" :src="scope.row.image" fit="fit"> </el-image>
            </template>
          </el-table-column>

          <el-table-column label="دسته بندی">
            <template slot-scope="scope">
              {{ categoryName(scope.row.category) }}
            </template>
          </el-table-column>

          <el-table-column prop="title" label="عنوان" width=""> </el-table-column>

          <el-table-column prop="short_description" label="توضیح کوتاه" width=""> </el-table-column>

          <el-table-column label="ویرایش" width="105" align="left">
            <template slot-scope="scope">
              <CButton
                :key="scope.row._id"
                color="warning"
                variant="outline"
                :to="`/admin/downloads/${$route.params.page}/${scope.row._id}`"
              >
                <i class="fal fa-pencil"></i>
              </CButton>
              <CButton color="danger" variant="outline" @click="del(scope.row._id)">
                <i class="fal fa-trash-alt"></i>
              </CButton>
            </template>
          </el-table-column>
        </el-table>
      </CCardBody>
    </CCard>
  </div>
</template>

<script>
export default {
  name: 'AddminDownloadsList',
  layout: 'admin',
  async asyncData({ $axios, params, error }) {
    try {
      const dCategories = await $axios.get(`/api/public/dCategory`)
      const downloads = await $axios.get(`/api/public/downloads/page/all/${params.page}`)
      return {
        dCategories: dCategories.data,
        downloads: downloads.data
      }
    } catch (err) {
      error({ status: 404, message: 'There is a problem here' })
    }
  },
  data() {
    return {
      title: 'دانلود ها',
      list_title: 'لیست',
      dCategories: null,
      downloads: null
    }
  },
  head() {
    return {
      title: this.title
    }
  },
  computed: {
    paginate() {
      return this.downloads.totalPages > 1
    }
  },
  methods: {
    categoryName(id) {
      return this.dCategories.filter(item => item._id === id)[0].name
    },
    pageNumber(val) {
      this.$router.push({ name: 'admin-downloads-page', params: { page: val } })
    },
    del(id) {
      this.$confirm('این مورد حذف شود؟', 'هشدار', {
        confirmButtonText: 'بله',
        cancelButtonText: 'لغو',
        type: 'warning'
      })
        .then(() => {
          this.$axios
            .delete(`/api/admin/downloads/${id}`)
            .then(response => {
              this.downloads.docs = this.downloads.docs.filter(item => item._id !== id)
              this.$message({
                type: 'success',
                message: 'آیتم حذف شد'
              })
            })
            .catch(err => {
              this.$message({
                type: 'error',
                message: err.response.data.message
              })
            })
        })
        .catch(() => {
          this.$message({
            type: 'warning',
            message: 'عملیات لغو شد'
          })
        })
    }
  }
}
</script>
