import { Component, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { ReportingService } from '../../services/reporting.service';

@Component({
  selector: 'app-datatable-upload-setting',
  templateUrl: './upload-setting.component.html',
})
export class DatatableReportingUploadSettingComponent implements OnInit {
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  record: any = {};
  i: any;
  schema!: SFSchema;
  ui!: SFUISchema;

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

  ngOnInit(): void {
    this.initSF();
    this.loadData();
  }

  /**
 * 初始化查询表单
 */
  initSF() {
    this.schema = {
      properties: {
        orderPushType: {
          type: 'string',
          title: '订单数据',
          enum: [
            {
              label: '手动上传',
              value: 1
            },
            {
              label: '自动上传',
              value: 2
            }
          ],
          description: '开启自动上传后，订单将在支付完成且风险单校验通过后自动上传',
          ui: {
            widget: 'radio',

          }
        },
        capitalPushType: {
          type: 'string',
          title: '资金数据',
          enum: [
            {
              label: '手动上传',
              value: 1
            },
            {
              label: '自动上传',
              value: 2
            }
          ],
          description: '开启自动上传后，订单将在支付完成且风险单校验通过后自动上传',
          ui: {
            widget: 'radio',

          }
        },
      },
      required: ['orderPushType', 'capitalPushType'],
    }
    this.ui = {
      '*': {
        spanLabelFixed: 100,
        grid: { span: 24 },
      },
    };
  }

  /**
   * 获取设置数据
   */
  loadData() {
    this.service.request(this.service.$api_get_upload_setting, {}).subscribe(res => {
      if (res) {
        this.i = res;
      }
    })
  }

  /**
   * 修改
   * @param value
   */
  save(value: any): void {
    if (this.sf?.valid)
      this.service.request(this.service.$api_upload_setting_save, { ...value }).subscribe(res => {
        if (res) {
          this.service.msgSrv.success('保存成功');
          this.modal.close(true);
        }
      })
  }

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