import {ListComponent} from '../../../classes/base_components/list/list.component';
import {Dialog} from '../../../classes/dialog';
import {ServicesService} from './services.service';
import {Component, OnInit, TemplateRef, ViewChild} from '@angular/core';
import {EditServicesComponent} from './edit-services/edit-services.component';
import {FormControl} from '@angular/forms';
import {TypesService} from '../types/types.service';
import 'rxjs/add/operator/debounceTime';
import {Http} from '@angular/http';
import {ProductsService} from '../products/products.service';
import {IParamsResource} from 'ngx-jsonapi';

@Component({
  selector: 'app-services',
  templateUrl: './services.component.html',
  styleUrls: ['./services.component.scss']
})
export class ServicesComponent extends ListComponent implements OnInit {
  @ViewChild('filter_id') public filter_id_ref: TemplateRef<any>;
  @ViewChild('filter_name') public filter_name_ref: TemplateRef<any>;
  @ViewChild('filter_price') public filter_price_ref: TemplateRef<any>;
  @ViewChild('filter_types') public filter_types_ref: TemplateRef<any>;
  typesControl = new FormControl();
  types;

  includes: ['products'];

//  har doe in parametr ha baray ers bary az listcomponent hast
  constructor(
    protected listService: ServicesService,
    protected productService: ProductsService,
    protected typeService: TypesService,
    protected dialog: Dialog,
    protected http: Http,
  ) {
    super(listService, dialog, EditServicesComponent, http);
    this.typeService.register();
    this.productService.register();
    this.listService.all().subscribe(x => {
    });
    const basics = JSON.parse(localStorage.getItem('basics'));
    this.types = basics['types'];
    this.typesControl.valueChanges
      .debounceTime(400)
      .subscribe(word => {
        this._filter(this.typeService, word, 'types');
      });
  }

  ngOnInit() {
    this.columns = [

      {
        prop: 'name',
        name: 'خدمات',
        filter: this.filter_name_ref,
      },
      {
        prop: 'price',
        name: 'قیمت پایه',
        filter: this.filter_price_ref,
      },
      {
        prop: 'types',
        name: 'نوع سرویس',
        isRelationship: true,
        mustIncluded: true,
        filter: this.filter_types_ref,
      },
      {
        prop: 'id',
        name: 'شناسه',
        filter: this.filter_id_ref,
      },

    ];
    super.ngOnInit();
  }

  //
  // replaceTypes(types: string) {
  //     console.log(types, 'salam');
  //     const serviceTypes = JSON.parse(localStorage.getItem('basics'))['types'];
  //     const result = serviceTypes.find(obj => {
  //         return obj.id === types;
  //     });
  //     return result.name;
  // }
  // edit(id?, params?: IParamsResource | null) {
  //   super.edit(id, params);
  //   if (!id) {
  //     const resource = this.listService.new();
  //     let requiredPermission = this.listService.type + '.create';
  //     const dialogRef = this.dialog.openWithData(EditServicesComponent, resource);
  //     dialogRef.afterClosed().subscribe(
  //       (ok) => {
  //         this.fetchList(null);
  //       }
  //     );
  //     this.dialog.openForAlert('access denied');
  //   }
  //
  //   let requiredPermission = this.listService.type + '.update';
  //   this.listService.get(id, params)
  //     .subscribe(
  //       resource => {
  //         if (resource.is_loading) {
  //           return;
  //         }
  //         console.log('id', id, params);
  //         const dialogRef = this.dialog.openWithData(this.editComponent, resource);
  //         dialogRef.afterClosed().subscribe(
  //           (ok) => {
  //             this.fetchList(null);
  //           }
  //         );
  //       }
  //     );
  // }


  edit(id: string) {
    const params: IParamsResource = {include: ['products']};
    super.edit(id, params);
  }
}

