import { AfterViewInit, ChangeDetectorRef, Component, OnChanges, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { apiConf } from '@conf/api.conf';
import { SFComponent, SFRadioWidgetSchema, SFSchema, SFSchemaEnumType, SFSelectWidgetSchema, SFTextareaWidgetSchema, SFUISchema, SFUploadWidgetSchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { Observable, Observer } from 'rxjs';
import { map } from 'rxjs/operators';
import { AmapPoiPickerComponent } from 'src/app/shared/components/amap';
import { ChannelSalesService } from '../../services/channel-sales.service';

@Component({
  selector: 'app-parter-article-management-edit',
  templateUrl: './edit.component.html'
})
export class ParterArticleManagementEditComponent 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,
    public service: ChannelSalesService,
  ) {}

  ngOnInit(): void {
    this.initSF();
  }
  initSF() {
    this.schema = {
      properties: {
        id: {
          type: 'string',
          title: '',
          ui: { hidden: true }
        },
        name1: {
          type: 'string',
          title: '文章标题',
          maxLength: 50,
          ui: {
            widget: 'textarea',
            autosize: { minRows: 3, maxRows: 6 },
            placeholder:'请输入50字符'
          } as SFTextareaWidgetSchema,
        },
        name2: {
          type: 'string',
          title: '文章简介',
          maxLength: 50,
          ui: {
            widget: 'textarea',
            autosize: { minRows: 3, maxRows: 6 },
            placeholder:'请输入50字符'
          } as SFTextareaWidgetSchema,
        },
        name3: {
          type: 'string',
          title: '封面图',
          ui: {
            action: apiConf.fileUpload,
            accept: 'image/png,image/jpeg,image/jpg',
            limit: 1,
            limitFileCount: 1,
            resReName: 'data.fullFilePath',
            urlReName: 'data.fullFilePath',
            widget: 'upload',
            descriptionI18n: '支持JPG、PNG格式，文件小于2M（建议尺寸 280px  *  180 px）',
            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();
              });
            }
          } as SFUploadWidgetSchema
        },
        name: {
          title: '分类',
          type: 'string',
          enum: [
            { label: '管理员', value: '1'},
          ],
          ui: {
            widget: 'select',
          } as SFSelectWidgetSchema,
        },
        name4: {
          type: 'number',
          title: '排序',
          minimum: 0,
          maximum: 99,
          ui: {
            widgetWidth: 300 ,
            placeholder:'请输入0~99，数字越大，排序越靠前'
          }
        },
        name5: {
          type: 'string',
          title: '跳转路径',
          enum: [
            { label: '图文', value: '1'},
            { label: '视频', value: '2'},
          ],
          ui: {
            widget: 'radio',
          } as SFRadioWidgetSchema,
          default: '1',
        },
        content: {
          type: 'string',
          title: '正文',
          ui: {
            widget: 'tinymce',
            loadingTip: 'loading...',
            config: {
              height: 450
            },
            visibleIf: { name5: (value: string) => value === '1' }
          },
        },
        name6: {
          type: 'string',
          title: '视频',
          ui: {
            action: apiConf.fileUpload,
            accept: 'video/mp4,video/avi,video/mkv,video/vob',
            limit: 1,
            limitFileCount: 1,
            resReName: 'data.fullFilePath',
            urlReName: 'data.fullFilePath',
            widget: 'upload',
            descriptionI18n: '支持MP4、AVI、DAT、MKV、FLV、VOB格式，文件小于20M。',
            name: 'multipartFile',
            multiple: false,
            listType: 'picture-card',
            beforeUpload: (file: any, _fileList: any) => {
              return new Observable((observer: Observer<boolean>) => {
                const isLt2M = file.size / 1024 / 1024 < 20;
                if (!isLt2M) {
                  this.service.msgSrv.warning('视频大小超过20M!');
                  observer.complete();
                  return;
                }
                observer.next(isLt2M);
                observer.complete();
              });
            },
            visibleIf: { name5: (value: string) => value === '2' }
          } as SFUploadWidgetSchema
        },
      },
      required: ['name1', 'name2']
    };
    this.ui = {
      '*': {
        spanLabelFixed: 150,
        grid: { span: 20 }
      },

    };
  }

  close() {
    
  }
  save() {
    this.sf.validator({ emitError: true });
    if(!this.sf.valid) return;
    // this.service.request('', { ...this.sf.value }).subscribe(res => {
    //   if (res) {
    //     this.modalRef.destroy(true);
    //   } else {
    //     this.service.msgSrv.error(res.msg);
    //   }
    // });
  }
}
