import { Component, OnInit, ViewChild } from '@angular/core';
import { apiConf } from '@conf/api.conf';
import { cacheConf } from '@conf/cache.conf';
import { SFUISchema, SFSchema, SFUploadWidgetSchema, SFComponent, SFSelectWidgetSchema, SFSchemaEnum } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { EACacheService, EAEnvironmentService } from '@shared';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { NzUploadFile } from 'ng-zorro-antd/upload';
import { Observable, Observer, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { UsermanageService } from 'src/app/routes/usercenter/services/usercenter.service';

@Component({
  selector: 'app-car-showService',
  templateUrl: './showService.component.html',
  styleUrls: ['./showService.component.less']
})
export class ShowServiceComponent implements OnInit {
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  appUserId = ''
  record: any = {};
  i: any;
  ui!: SFUISchema;
  schema!: SFSchema;

  constructor(
    private modal: NzModalRef,
    public service: UsermanageService,
  ) { }

  ngOnInit(): void {
    this.initSF()
  }

  initSF() {
    this.schema = {
      properties: {
        _$expand: { type: 'boolean', ui: { hidden: true } },
        customerServiceId: {
          title: '客服人员',
          type: 'string',
          ui: {
            widget: 'select',
            asyncData: () => {
              const params = {
               // enterpriseId: 1,
	              // enterpriseProjectId: this.i.mainProjectId,
              }
              return this.service.request(this.service.$api_getStaffList, params).pipe(
                map((res: any) => {
                  return res.map((i: any) => {
                    let name = i.name ? `${i.name} / ` : '';
                    return { label:  `${name}${i.telephone}`, value: i.appUserId };
                  });
                }),
              );
              // serverSearch: true,
              // searchDebounceTime: 300,
              // searchLoadingText: '搜索中...',
              // onSearch: (q: any) => {
              //   if (!!q) {
              //     return this.service
              //       .request(this.service.$api_getStaffList, {
              //         nameOrPhone: q
              //       })
              //       .pipe(map((res: any) => (res?.records as any[]).map(i => ({ name: i.name, value: i.appUserId } as SFSchemaEnum))))
              //       .toPromise();
              //   } else {
              //     return of([]);
              //   }
            },
          } as SFSelectWidgetSchema
        }
      },
      required: ['customerServiceId']
    };
  }
  close(): void {
    this.modal.close(true)
  }
  submitForm() {
    const params: any = {
      ...this.sf.value,
      enterpriseIdList: [this.i.id]
    };
    this.service.request(this.service.$api_distributionCusService, params).subscribe(res => {
      if (res) {
        this.service.msgSrv.success('分配成功')
        this.modal.close(true)
      }
    })
  }
}
