import { Component, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFRadioWidgetSchema, SFSchema, SFUISchema } from '@delon/form';
import { ShipperBaseService } from '@shared';
import { NzMessageService } from 'ng-zorro-antd/message';
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;
  i: any;
  sts: any;
  rows: any;
  schema: SFSchema = {};
  detailData: any = {};
  ui: SFUISchema = {};

  constructor(
    public msgSrv: NzMessageService,
    public service: UsermanageService,
    private modal: NzModalRef,
    public shipperservice: ShipperBaseService
  ) {}

  ngOnInit(): void {
    if (this.sts == '2') {
      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 },
        //   ],
        // },channelId
        partnerId: {
          type: 'string',
          title: '合伙人修改为',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            visibleIf: {
              _$expand: (value: boolean) => value
            },
            asyncData: () => this.shipperservice.getNetworkEnterpriseName()
          }
        },
        // settStartTime: {
        //   title: '结算起算日期',
        //   type: 'string',
        //   ui: {
        //     widget: 'sl-from-to', type: 'date', format: 'yyyy-MM-dd', visibleIf: {
        //       _$expand: (value: boolean) => value,
        //     },
        //   } as SFDateWidgetSchema,
        // },
        settStartTime: {
          title: '结算起算日期',
          type: 'string',
          format: 'date'
        },
        remark: {
          type: 'string',
          title: '备注',
          maxLength: 50,
          ui: {
            widget: 'textarea',
            placeholder: '请不要超过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: ['partnerId','settStartTime', 'remark', 'effectiveNode']
    };
    this.ui = {
      '*': {
        spanLabelFixed: 180,
        grid: { span: 18 },
        width: 600
      }
    };
  }
  get reqParams() {
    return {};
  }
  close(): void {
    this.modal.close(true);
  }
  save() {
    console.log(this?.rows);
    let enterId: any;
    if (this.sts == '1') {
      enterId = this.rows;
    } else {
      enterId = [this.i.id];
    }
    const params = {
      ...this.sf?.value,
      enterpriceIds: enterId,
      settStartTime: this.sf?.value?.settStartTime + ' 00:00:00'
    };

    this.service.request(this.service.$api_batchUpdateEnterpricePartner, params).subscribe(res => {
      if (res) {
        this.service.msgSrv.success('修改成功');
        this.modal.destroy(true);
      }
    });
  }
}
