<template>
  <div class="container-fluid">
    <div class="row">
      <admin-title-bar :title="title">
        <nuxt-link :to="{name: 'admin-products-new'}">
          <el-button type="success">جدید</el-button>
        </nuxt-link>
      </admin-title-bar>
      <div class="col-lg-12">
        <admin-panel>
          <el-button-group class="filterBtns">
            <el-button :type="filter ? 'success' : 'primary'" @click="filter = true">محصولات صفحه اصلی</el-button>
            <el-button :type="!filter ? 'success' : 'primary'" @click="filter = false">تمام محصولات</el-button>
          </el-button-group>
          <el-divider></el-divider>
          <el-table
              :data="filteredProducts"
              style="width: 100%">
            <el-table-column
                type="index"
                label="#">
            </el-table-column>

            <el-table-column
                label="تصویر"
                width="230">
              <template slot-scope="scope">
                <el-image
                    style="width: 100%; height: 100%"
                    :src="scope.row.thumb"
                    fit="fit">
                </el-image>
              </template>
            </el-table-column>

            <el-table-column
                prop="product_details.fa.name"
                label="نام"
                width="">
            </el-table-column>

            <el-table-column
                prop="category"
                label="دسته بندی"
                width="">
              <template slot-scope="scope">
                {{ categoryName(scope.row.category) }}
              </template>
            </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: 'لیست محصولات',
      products: null,
      productCategories: null,
      filter: false
    }
  },
  head() {
    return {
      title: this.title,
    }
  },
  computed: {
    filteredProducts() {
      if (this.filter) return this.products.filter(item => item.favorite)
      else return this.products

    }
  },
  methods: {
    categoryName(id) {
      let category = this.productCategories.filter(item => {
        return item._id === id
      })
      return category[0].category_details.fa.name
    },
    edit(id) {
      this.$router.push(`/admin/products/${id}`)

    },
    del(id) {
      this.$confirm('این محصول حذف شود؟', 'هشدار', {
        confirmButtonText: 'بله',
        cancelButtonText: 'لغو',
        type: 'warning'
      }).then(() => {
        this.$axios.delete(`/api/private/products/${id}`)
            .then(response => {
              this.products = this.products.filter(item => {
                return 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: 'عملیات لغو شد'
        });
      });
    }
  },
  layout: 'admin',
  async asyncData({$axios, store, error}) {
    try {
      let products = await $axios.get(`/api/public/products`)
      let productCategories = await $axios.get(`/api/public/productCategories`)
      return {
        products: products.data,
        productCategories: productCategories.data
      }
    } catch (e) {
      error({status: 404, message: 'There is a problem here'})
    }
  }
}
</script>


<style lang="scss">
.filterBtns {
  span {
    color: #fff !important;
  }
}
</style>
