import { Component, OnInit } from '@angular/core';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { ItemForSaleService } from '../services/service.service';
import { deepCopy } from '@angular-devkit/core/src/utils/object';
import * as moment from 'moment';
import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-items',
  templateUrl: './items.component.html',
  styleUrls: ['./items.component.scss'],
})
export class ItemsComponent implements OnInit {
  segment: any = '1';
  items: any;
  categories: any = [];
  direction: string;
  activeClass: any;
  pageNo: number = 1;
  tempSearch: any = [];
  itemsByEmployee: any = [];
  constructor(public ts: TranslatorService, public route: ActivatedRoute, private sanitizer: DomSanitizer, public cs: CommonService, private itemService: ItemForSaleService) {
    this.route
      .params.subscribe(val => {
        setTimeout(() => {
          this.cs.startLoading();
          this.getItemsCategories();
          this.getItemsByEmployee();
        }, 100);
      });

  }


  ngOnInit() {
    this.direction = TranslatorService.getCurrentDirection();

  }
  getItemsCategories() {

    this.itemService.getCategories({}, () => { }, this.ts.trPK('general', 'retry')).subscribe((result) => {
      this.categories = this.itemService.parser(result);
      this.cs.sharedService.setSharedData(this.categories, ItemForSaleService.CATEGORIES);
    })
    this.itemService.getItems({ pageNo: this.pageNo }, () => { }, this.ts.trPK('general', 'retry')).subscribe((result) => {
      this.items = [];
      this.items = this.itemService.parser(result);
      this.tempSearch = deepCopy(this.items);
      this.cs.stopLoading();
    })
  }
  openDetails(selected) {
    this.cs.sharedService.setSharedData(selected, ItemForSaleService.ITEMS_SELECTED);
    this.cs.navigateForward('/itemforsale/items-details');
  }
  segmentChanged(value) {
    this.segment = value.detail.value;
  }
  openSale() {
    this.cs.openCreateitems();
  }
  selectCategory(item) {
    this.cs.startLoading();
    this.activeClass = item.categoryID;
    this.itemService.getItems({ pageNo: this.pageNo, ItgCategoryID: item.categoryID }, () => { }, this.ts.trPK('general', 'retry')).subscribe((result) => {
      this.items = [];
      this.items = this.itemService.parser(result);
      this.tempSearch = deepCopy(this.items);
      this.cs.stopLoading();
    })
  }
  getDotted(temp: string, valueCount: number) {
    temp = this.stripHtml(temp);
    return temp.substring(0, valueCount) + " ...";
  }
  stripHtml(html) {
    var temporalDivElement = document.createElement("div");

    temporalDivElement.innerHTML = html;

    return temporalDivElement.textContent || temporalDivElement.innerText || "";
  }
  loadData(event) {
    this.pageNo = this.pageNo + 1;
    this.itemService.getItems({ pageNo: this.pageNo, categoryID: this.activeClass }, () => { }, this.ts.trPK('general', 'retry')).subscribe((result) => {
      this.items.concat(this.itemService.parser(result));
      this.tempSearch = deepCopy(this.items);
      event.target.complete();

    })
  }
  search(t) {
    if (t === '') {
      this.items = this.tempSearch;
    } else {
      this.items = this.tempSearch.filter((post) => {
        if (this.direction == 'ltr')
          return (post.title.toLowerCase().indexOf(t.toLowerCase()) > -1);
        else
          return (post.title_Ar.indexOf(t) > -1);
      });
    }

  }
  getItemsByEmployee() {

    this.cs.startLoading();
    this.itemService.getItemsByEmployee({}, () => { }, this.ts.trPK('general', 'retry')).subscribe((result) => {
      this.itemsByEmployee = this.itemService.parser(result);

      this.cs.stopLoading();
      console.log(this.itemsByEmployee);
    })
  }

  getDate(date) {
    return moment(date, "YYYY-MM-DD HH:mm:ss").format("DD-MMM-YYYY");
  }

  getImgContent(imgFile): SafeUrl {
    return this.sanitizer.bypassSecurityTrustUrl(imgFile);
  }
  editItems(items) {
    this.cs.sharedService.setSharedData(items, ItemForSaleService.EDIT_ITEMS);
    this.cs.openCreateitems();
  }

}
