import { Component, OnInit } from '@angular/core';
import { MyService } from '../my.service';
import { LifeCommonService } from '../../common/life-common.service';
import { Router , ActivatedRoute} from "@angular/router";
import {Location} from '@angular/common';

@Component({
  selector: 'ydlife-my-business',
  templateUrl: './my-business.component.html',
  styleUrls: ['./my-business.component.scss'],
})
export class MyBusinessComponent implements OnInit {
  businessList: Array<any>;
  isTaskCome:boolean = false;
  pageType:any;
  showBusinessList: Array<any>;
  isSelectFlag:boolean;
  selectedBusiness:any;
   //控制弹框
   toastDialog: boolean;
   toastInfo: any;
  constructor(private myService: MyService, public lifeCommonService: LifeCommonService, private router: Router,private activatedRoute: ActivatedRoute,private location:Location) { 
    this.pageType = this.activatedRoute.snapshot.data[0]['type'];
  }

  ngOnInit() {
    this.ownOpportunityQuery();
  }

  ownOpportunityQuery() {
    const brokerCustomerId = JSON.parse(localStorage.getItem('lifeCustomerInfo'))['customerId'];
    const brokerPractitionerId = JSON.parse(localStorage.getItem('lifeCustomerInfo'))['practitionerId'];
    this.myService.ownOpportunityQuery({ customerId: brokerCustomerId, practitionerId: brokerPractitionerId }).subscribe((res) => {
      if (res['data']['ownOpportunityInfos']) {
        this.businessList = res['data']['ownOpportunityInfos'];
        this.showBusinessList = res['data']['ownOpportunityInfos'];
      } else {
        this.businessList = [];
      }
    })
  }

  addBussiness() {
    this.router.navigate(['/business/0']);
  }

  isJumpToDetail(opportunityId,orderId,leadsAssignedId,businessItem?:any){
    if(this.pageType == 'business'){
      this.router.navigate([`/business/${opportunityId}`], { queryParams: { orderId: orderId, leadsAssignedId: leadsAssignedId } });
    }else{
      if(!this.selectedBusiness){
        this.selectedBusiness = businessItem; 
      }else{
        this.selectedBusiness = undefined;
      }
      return;
    }
  }

  change(event){
    this.showBusinessList = this.businessList.filter((item)=>{
      return item.name.indexOf(event) !=-1;
    })
  }

  returnAddTask(){
    if(this.selectedBusiness){
      const {leadsAssignedId,opportunityId}=this.selectedBusiness;
      this.router.navigate(['/addtask/'], { queryParams: { customerId:opportunityId , referLeadsId: leadsAssignedId },state:this.selectedBusiness});
    }else{
      this.toastDialog = true;
        this.toastInfo = {
          status: 1,
          msg:'请选择您需要关联的商机！',
          timeout: 3000,
          align: 'center'
        };
      return;
    }
  }
}
