import { DatePipe } from '@angular/common';
import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { SFComponent, SFSchema, SFUISchema, SFUploadWidgetSchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { Observable, Observer } from 'rxjs';
import { Router } from '@angular/router';
import { SystemService } from '../../services/system.service';
import { EnvironmentService } from '@env/environment.service';
@Component({
  selector: 'app-agreement-config-components-base',
  styleUrls: ['./agreement-config.component.less'],
  templateUrl: './agreement-config.component.html'
})
export class AgreementConfigComponentsBaseComponent implements OnInit {
  @ViewChild('sf1', { static: false }) sf1!: SFComponent;

  i: any;
  formDate: any = {};
  schema1!: SFSchema;
  ui1: SFUISchema = {};
  idx: any = 0;
  isUpdate = false;
  type = 0;
  tabItem = {
    content: '',
    id: 0,
    modifyTime: '',
    type: '',
    typeName: ''
  };
  tabs = [
    {
      name: '《货主端用户注册协议》'
    },
    {
      name: '《司机端用户注册协议》'
    },
    {
      name: '《服务协议》'
    },
    {
      name: '《隐私政策》'
    },
    {
      name: '《人脸识别协议》'
    }
  ];

  constructor(private service: SystemService) {}

  ngOnInit() {
    this.loadAgreement();
    this.initSF();
  }
  initSF() {
    this.schema1 = {
      properties: {
        content: {
          type: 'string',
          title: '',
          ui: {
            widget: 'tinymce',
            loadingTip: 'loading...',
            config: {
              height: 650
            }
          }
        }
      }
    };
    this.ui1 = {
      '*': {
        spanLabelFixed: 10,
        grid: { span: 24 }
      }
    };
  }

  changeType(type: number): void {
    this.idx = type;
    if (type > 0) {
      this.isUpdate = false;
      this.type = +type;

      this.loadAgreement();
    }
  }

  loadAgreement() {
    // this.service.request(`${this.service.$api_general_getAgreement}`, { type: this.type }).subscribe((res) => {
    //   console.log('🚀 ~ this.service.request ~ res', res);
    //   if (res) {
    //     this.tabItem = res;
    //   }
    // });
  }

  update() {
    this.isUpdate = true;
    console.log('🚀 ~ update ~ isUpdate', this.tabItem);

    this.formDate.content = this.tabItem.content;
    this.initSF();
  }
  save() {
    const params = {
      id: this.tabItem.id,
      content: encodeURIComponent(this.sf1?.value.content),
      type: this.type
    };
    this.isUpdate = false;
    // this.service.request(`${this.service.$api_general_updateAgreement}`, params).subscribe((res) => {
    //   if (res) {
    //     this.service.msgSrv.success('保存成功');
    //     this.changeType(this.type);
    //     this.isUpdate = false;
    //   }
    // });
  }

  cancel() {
    this.isUpdate = false;
  }
}
