import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { SFComponent, SFRadioWidgetSchema, SFSchema, SFSelectWidgetSchema, SFUISchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { EAEnvironmentService } from '@shared';
import differenceInCalendarDays from 'date-fns/differenceInCalendarDays';
import format from 'date-fns/format';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzUploadFile } from 'ng-zorro-antd/upload';
import { Observable, Observer, of } from 'rxjs';
import { apiConf } from '@conf/api.conf';
import { UsermanageService } from 'src/app/routes/usercenter/services/usercenter.service';
import { NzModalRef } from 'ng-zorro-antd/modal';

@Component({
  selector: 'app-ad-components-partner',
  templateUrl: './editPartner.component.html',
  styleUrls: ['./editPartner.component.less']
})
export class EditPartnerComponentsAddComponent implements OnInit {
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  record: any = {};
  i: any;
  schema: SFSchema = {};
  detailData: any = {}
  ui: SFUISchema = {};
 
  constructor(
    public msgSrv: NzMessageService,
    public http: _HttpClient,
    private route: ActivatedRoute,
    private router: Router,
    public service: UsermanageService,
    private envSrv: EAEnvironmentService,
    private modal: NzModalRef,
  ) { }


  ngOnInit(): void {
    this.initDetailData()
    this.initSF();
  }
  initDetailData() {
    const params = {
      id: this.i.id
    }
    this.service.request(this.service.$api_partnerChannelUpdateDetaiList, params).subscribe(res => {
      if(res) {
        this.detailData = res
      } else {
        this.service.msgSrv.error(res.msg)
      }
    })
  }
  initSF() {
    this.schema = {
      properties: {
        channelId: {
          type: 'string',
          title: '合伙人修改为',
          ui: {
            widget: 'radio',
            showRequired: true,
          } as SFRadioWidgetSchema,
          enum: [
            { label: '全部可见', value: 1 },
            { label: '合伙人可见', value: 2 },
            { label: '销售渠道可见', value: 3 },
          ],
        },
        remark: {
          type: 'string',
          title: '备注',
          ui: {
            widget: 'textarea',
            placeholder: '请不要超过50个字',
            maxLength: 50,
            autosize: { minRows: 2, maxRows: 6 },
          },
        },
        effectiveNode: {
          type: 'string',
          title: '生效节点',
          ui: {
            widget: 'radio',
            showRequired: true,
          } as SFRadioWidgetSchema,
          enum: [
            { label: '修改成功后立即生效', value: 1 },
            { label: 'CRM流程审核通过后生效', value: 2 }
          ],
        },
      },
      required: ['channelId', 'remark', 'effectiveNode'],
    };
    this.ui = {
      '*': {
        spanLabelFixed: 180,
        grid: { span: 18 },
        width: 600,
      }
    };
  }
  get reqParams() {
    return {};
  }
  close(): void {
    this.modal.close(true)
  }
  save() {
    const params = {
      ...this.sf.value
    }
    this.service.request(this.service.$api_batchUpdateEnterpricePartner, params).subscribe(res => {
      if(res) {
        this.service.msgSrv.success('修改成功')
      } else {
        this.service.msgSrv.error(res.msg)
      }
    })
  }
}
