import { observable, action, computed } from 'mobx';

export default class Help {
    
        @observable list = [];
        @observable selected = {};
        @observable istLoading = false;
        @observable isSearching = false;
        @observable filtered = [];

        constructor(context) {
            this.http = context.http;
            this.context = context;
        }
    
        @action
        getList(){
            this.isLoading = true;
            return this.http.get("posts/by_tag/a571f41f-c9d2-4fe1-9638-320c589cffe3")
            .then(res => {
              this.list = res;
              this.isLoading = false;
            })
            .catch(err => {
              this.isLoading = false;
              throw err;
            })
        }

        @action
        getDetail(id){
            this.isLoading = true;
            return this.http.get(`posts/${id}`)
            .then(res => {
              console.log(res,'ini res store')
              this.selected = res;
              this.isLoading = false;
            })
            .catch(err => {
              this.isLoading = false;
              throw err;
            })
        } 
    
        @action
        post(data){
            this.isLoading = true;
            return this.http.post("posts", data)
              .then(res => {
                console.log('rest men',res);
                this.isLoading = false;
                this.getList();
                return res;
              })
              .catch(err => {
                console.log(err,'err men')
                this.isLoading = false;
                throw err;
              })
        }
    
        @action
        put(id,data){
            this.isLoading = true;
            return this.http.put(`posts/${id}`, data)
              .then(res => {
                this.isLoading = false;
                this.getList();
                return res;
              })
              .catch(err => {
                this.isLoading = false;
                throw err;
              })
        }
    
        @action
        delete(id){
            this.isLoading = true;
            return this.http.delete(`posts/${id}`)
              .then(res => {
                this.isLoading = false;
                this.getList();
                return res;
              })
              .catch(err => {
                this.isLoading = false;
                throw err;
              })
        }

        filterItems = (query) => {
          return this.list.filter((el) =>
            el.title.toLowerCase().indexOf(query.toLowerCase()) > -1 
          );
        }

        @action
        search(text){
          this.filtered = this.filterItems(text);
        }
    }
