import { Params } from '@angular/router';
import { Component, OnInit, ViewChild, Type } from '@angular/core';
import { SFComponent, SFSchema, SFUISchema, SFUploadWidgetSchema, SFTextareaWidgetSchema } from '@delon/form';
import { EAEnvironmentService, CaptchaComponent, EAUserService } from '@shared';
import { Observable, Observer } from 'rxjs';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { apiConf } from '@conf/api.conf';
import { VehicleService } from '../../../services/vehicle.service';
@Component({
  selector: 'app-setting-components-info-edit',
  templateUrl: './edit.component.html',
})
export class VehicleComponentsListEditComponent implements OnInit {
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  record: any = {};
  i: any = {};
  infoData: any = {};
  schema!: SFSchema;
  ui!: SFUISchema;
  formData: any = {};
  interval$: any;
  phone!: string;

  constructor(
    private modal: NzModalRef,
    public msgSrv: NzMessageService,
    public service: VehicleService,
    private envSrv: EAEnvironmentService,
  ) {}

  ngOnInit(): void {
    console.log('init:', this.i, this.infoData);
    // 设置初始值
    this.initInfo();
    this.initSF();
  }
  initInfo() {
        if (this.infoData.carProtocal) {
          this.formData = {
            carProtocal: [
              {
                uid: 'logo',
                name: 'LOGO',
                status: 'done',
                url: this.infoData.carProtocal,
                response: {
                  url: this.infoData.carProtocal,
                },
              },
        ],
          };
        }
  }
  initSF() {
    // 依据类型初始表单
    this.schema = {
      properties: {
        carProtocal: {
          type: 'string',
          title: '车主申明/挂靠协议',
          ui: {
            widget: 'upload',
            action: apiConf.fileUpload,
            fileType: 'image/png,image/jpeg,image/jpg,image/gif',
            limit: 1,
            limitFileCount: 1,
            resReName: 'data.fullFileWatermarkPath',
            urlReName: 'data.fullFileWatermarkPath',
            descriptionI18n: '图片支持jpg、jpeg、png、gif格式，大小不超过5M',
            data: {
              appId: this.envSrv.env.appId,
            },
            name: 'multipartFile',
            beforeUpload: (file: any, fileList: any) => {
              return new Observable((observer: Observer<boolean>) => {
                const isLt1M = file.size / 1024 / 1024 < 5;
                const fileType = 'image/png,image/jpeg';
                if (fileType.indexOf(file.type) === -1) {
                  this.service.msgSrv.warning('图片格式不正确！');
                  observer.complete();
                  return;
                }
                if (!isLt1M) {
                  this.service.msgSrv.warning('图片大小超过5M!');
                  observer.complete();
                  return;
                }
                observer.next(isLt1M);
                observer.complete();
              });
            },
            multiple: false,
            listType: 'picture-card',
            showRequired: true,
          } as SFUploadWidgetSchema,
        },
      },
      required: ['carProtocal'],
    };
    this.ui = {
      '*': {
        spanLabelFixed: 200,
        grid: { span: 24 },
      },
    };
  }

  close() {
    this.modal.destroy();
  }
  sure() {
   const params ={
    carProtocal: this.sf.value.carProtocal?.data?.fullFilePath,
    id: this.i.id
   }
   this.service.request(this.service.$api_get_upLoadCarProtocal, params).subscribe((res) => {
     if(res) {
      this.modal.destroy(true);
      this.service.msgSrv.success('上传协议成功！')
     }
   })
  }
}
