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

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

          <el-table-column prop="name" 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/download-categories/${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: 'AddminDownloadCategoryList',
  layout: 'admin',
  async asyncData({ $axios, error }) {
    try {
      const dCategories = await $axios.get(`/api/public/dCategory`)
      return {
        dCategories: dCategories.data
      }
    } catch (e) {
      error({ status: 404, message: 'There is a problem here' })
    }
  },
  data() {
    return {
      title: 'دسته بندی دانلود',
      list_title: 'لیست',
      dCategories: null
    }
  },
  head() {
    return {
      title: this.title
    }
  },
  computed: {},
  methods: {
    del(id) {
      this.$confirm('این مورد حذف شود؟', 'هشدار', {
        confirmButtonText: 'بله',
        cancelButtonText: 'لغو',
        type: 'warning'
      })
        .then(() => {
          this.$axios
            .delete(`/api/admin/dCategory/${id}`)
            .then(response => {
              this.$message({
                type: 'success',
                message: 'آیتم حذف شد'
              })
              this.dCategories = this.dCategories.filter(item => item._id !== id)
            })
            .catch(err => {
              this.$message({
                type: 'error',
                message: err.response.data.message
              })
            })
        })
        .catch(() => {
          this.$message({
            type: 'warning',
            message: 'عملیات لغو شد'
          })
        })
    }
  }
}
</script>
