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 { 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.enterpriseLogo) {
          this.formData = {
            enterpriseLogo: [
              {
                uid: 'logo',
                name: 'LOGO',
                status: 'done',
                url: this.infoData.enterpriseLogo,
                response: {
                  url: this.infoData.enterpriseLogo,
                },
              },
        ],
          };
        }
  }
  initSF() {
    // 依据类型初始表单
    this.schema = {
      properties: {
        enterpriseLogo: {
          type: 'string',
          title: 'Logo',
          ui: {
            widget: 'upload',
            action: `/cms/upload/multipartFile/fileModel`,
            // fileType: 'image/png,image/jpeg,image/gif,image/bmp',
            limit: 1,
            limitFileCount: 1,
            resReName: 'url',
            urlReName: 'url',
            descriptionI18n: '支持JPG、GIF、PNG、JPEG格式，图片小于2M',
            data: {
              appId: this.envSrv.env.appId,
            },
            name: 'multipartFile',
            change: (args) => {
              if (args.type === 'success') {
                const avatar = [
                  {
                    uid: 'logo',
                    name: 'LOGO',
                    status: 'done',
                    url: args.fileList[0].response.url,
                    response: {
                      url: args.fileList[0].response.url,
                    },
                  },
                ];
                this.sf?.setValue('/enterpriseLogo', avatar);
              }
            },
            beforeUpload: (file: any, fileList: any) => {
              return new Observable((observer: Observer<boolean>) => {
                const isLt1M = file.size / 1024 / 1024 < 2;
                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('图片大小超过2M!');
                  observer.complete();
                  return;
                }
                observer.next(isLt1M);
                observer.complete();
              });
            },
            multiple: false,
            listType: 'picture-card',
            showRequired: true,
          } as SFUploadWidgetSchema,
        },
      },
      required: ['enterpriseLogo'],
    };
    this.ui = {
      '*': {
        spanLabelFixed: 120,
        grid: { span: 24 },
      },
    };
  }

  close() {
    this.modal.destroy();
  }
  sure() {
   
  }
}
