import { AfterViewInit, ChangeDetectorRef, Component, OnChanges, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { SFComponent, SFRadioWidgetSchema, SFSchema, SFSchemaEnumType, SFSelectWidgetSchema, SFTextareaWidgetSchema, SFUISchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
import { map } from 'rxjs/operators';
import { AmapPoiPickerComponent } from 'src/app/shared/components/amap';
import { ChannelSalesService } from '../../services/level-config.service';

@Component({
  selector: 'app-parter-LevelConfig-edit',
  templateUrl: './edit.component.html'
})
export class ParterLevelConfigEditComponent implements OnInit {
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  schema!: SFSchema;
  ui!: SFUISchema;
  i: any;
  type: any;

  constructor(
    public http: _HttpClient,
    private cdr: ChangeDetectorRef,
    private route: ActivatedRoute,
    private modalService: NzModalService,
    public service: ChannelSalesService,
    private modalRef: NzModalRef
  ) {}

  ngOnInit(): void {
    this.initSF();
  }
  initSF() {
    this.schema = {
      properties: {
        id: {
          type: 'string',
          title: '',
          ui: { hidden: true }
        },
        gradeName: {
          title: '等级名称',
          type: 'string',
        },
        sortId: {
          title: '排序',
          type: 'string',
        },
        remark: {
          type: 'string',
          title: '备注',
          maxLength: 50,
          ui: {
            widget: 'textarea',
            autosize: { minRows: 3, maxRows: 6 },
            placeholder:'请输入50字符'
          } as SFTextareaWidgetSchema,
        },
      },
      required: ['gradeName', 'sortId', 'remark']
    };
    this.ui = {
      '*': {
        spanLabelFixed: 120,
        grid: { span: 24 }
      },
    };
  }

  close() {
    this.modalRef.destroy();
  }
  save() {
    this.sf.validator({ emitError: true });
    if(!this.sf.valid) return;
    this.service.request(this.service.$api_save, { ...this.sf.value }).subscribe(res => {
      if (res) {
        this.service.msgSrv.success('保存成功！')
        this.modalRef.destroy(true);
      } else {
        this.service.msgSrv.error(res.msg);
      }
    });
  }
}
