import { Component, OnInit } from '@angular/core';
import { apiConf } from '@conf/api.conf';
import { SFSchema, SFUISchema } from '@delon/form';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { Observable, Observer } from 'rxjs';
import { ClassificationService } from '../../services/classification.service';

@Component({
  selector: 'app-partner-edit',
  templateUrl: './edit.component.html',
})
export class PartnerEditComponent implements OnInit {
  record: any = {};
  i: any;
  schema!: SFSchema;
  ui!: SFUISchema;
  status = 'add';

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

  ngOnInit(): void {
    if (this.i) {
      this.i.icon = [
        {
          uid: -1,
          name: 'xxx.png',
          status: 'done',
          url: this.i.url,
          response: {
            resource_id: 1,
          },
        },
      ]
    }
    this.initSF();

  }
  initSF() {
    this.schema = {
      properties: {
        abnormalCause: {
          title: '分类名称',
          type: 'string',
          maxLength: 5,
          ui: {
            placeholder: '请输入',
          },
        },
        icon: {
          type: 'string',
          title: '图标',
          ui: {
            action: apiConf.fileUpload,
            fileType: 'image/png,image/jpeg,image/jpg',
            limit: 1,
            resReName: 'url',
            urlReName: 'url',
            widget: 'upload',
            descriptionI18n: '支持JPG、PNG格式，文件小于2M（建议尺寸  88px * 88px）',
            name: 'multipartFile',
            multiple: false,
            listType: 'picture-card',
            beforeUpload: (file: any, _fileList: any) => {
              return new Observable((observer: Observer<boolean>) => {
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isLt2M) {
                  this.service.msgSrv.warning('图片大小超过2M!');
                  observer.complete();
                  return;
                }
                observer.next(isLt2M);
                observer.complete();
              });
            }
          }
        },
        abnormalCause2: {
          title: '排序',
          type: 'number',
          maximum: 99,
          minimum: 0,
          ui: {
            placeholder: '请输入',
            widgetWidth: 350
          }
        },
      },
      required: ['abnormalCause', 'icon', 'abnormalCause2']

    }
    this.ui = { '*': { spanLabelFixed: 90, grid: { span: 20, gutter: 4 } }, };

  }

  save(value: any): void {
    this.service.request(`/user/${this.record.id}`, value).subscribe(res => {
      if (res) {
        this.service.msgSrv.success('保存成功');
        this.modal.close(true);
      }

    });
  }

  close(): void {
    this.modal.destroy();
  }
}
