<template>
	<div class="container-fluid">
		<div class="row">
			<admin-title-bar :title="title">
				<nuxt-link :to="{name: 'admin-blog-category-new'}">
					<el-button type="success">جدید</el-button>
				</nuxt-link>
			</admin-title-bar>
			<div class="col-12">
				<admin-panel>
					<el-table
						:data="blogCategories"
						style="width: 100%">
						<el-table-column
							type="index"
							label="#">
						</el-table-column>
						
						<el-table-column
							prop="blogCategory_details.fa.name"
							label="نام دسته بندی"
							width="">
						</el-table-column>
						
						<el-table-column
							label="ویرایش"
							width="150"
							align="left">
							<template slot-scope="scope">
								<el-button type="warning" plain icon="el-icon-edit" @click="edit(scope.row._id)"></el-button>
								<el-button type="danger" plain icon="el-icon-delete" @click="del(scope.row._id)"></el-button>
							</template>
						</el-table-column>
					
					</el-table>
				</admin-panel>
			</div>
		</div>
	</div>
</template>


<script>
   export default {
      data() {
         return {
            title: 'دسته بندی بلاگ',
            blogCategories: null
         }
      },
      head() {
         return {
            title: this.title,
         }
      },
      methods: {
         edit(id) {
            this.$router.push(`/admin/blog/category/${id}`)
         },
         del(id) {
            this.$confirm('این آیتم حذف شود؟', 'هشدار', {
               confirmButtonText: 'بله',
               cancelButtonText: 'لغو',
               type: 'warning'
            }).then(() => {
               this.$axios.delete(`/api/private/blogCategories/${id}`)
                  .then(response => {
                     this.blogCategories = this.blogCategories.filter(item => {
                        return item._id !== id
                     })
                     this.$message({
                        type: 'success',
                        message: 'آیتم حذف شد'
                     });
                  })
                  .catch(err => {
                     this.$alert(err.response.data.message, 'خطا', {
                        confirmButtonText: 'OK'
                     })
                  })
            }).catch(() => {
               this.$message({
                  type: 'warning',
                  message: 'عملیات لغو شد'
               });
            });
         }
      },
      layout: 'admin',
      async asyncData({$axios}) {
         let blogCategories = await $axios.get(`/api/public/blogCategories`)
         return {
            blogCategories: blogCategories.data
         }
      }
   }
</script>
