/*
 * @Description  : 
 * @Version      : 1.0
 * @Author       : Shiming
 * @Date         : 2022-01-05 11:01:55
 * @LastEditors  : Shiming
 * @LastEditTime : 2022-01-25 14:25:49
 * @FilePath     : \\tms-obc-web\\src\\app\\routes\\contract-management\\components\\contract-template-detail\\contract-template-detail.component.ts
 * Copyright (C) 2022 huzhenhong. All rights reserved.
 */
import { DatePipe } from '@angular/common';
import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { SFComponent, SFSchema, SFSelectWidgetSchema, SFUISchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { ContractManagementService } from '../../services/contract-management.service';

@Component({
  selector: 'app-contract-management-template-detail-complaint',
  templateUrl: './contract-template-detail.component.html',
  styleUrls: ['./contract-template-detail.component.less'],
  providers: [DatePipe]
})
export class ContractManagementTemplateDetailComponent implements OnInit {
  constructor(
    private nzModalService: NzModalService,
    public service: ContractManagementService,
    public route: ActivatedRoute,
    private datePipe: DatePipe,
    private router: Router
  ) {}
  textStatus = '新建模板';
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  schema: SFSchema = {};
  @ViewChild('sf2', { static: false }) sf2!: SFComponent;
  schema2: SFSchema = {};
  ui!: SFUISchema;
  sfdata: any;
  sfdata2: any;
  title: any;
  templateHTML: any;
  detailList: any = {
    templateName: ''
  };
  isUpdate = false;
  ngOnInit() {
    this.initSF();
    this.initSF2();
    if (this.route.snapshot.queryParams.status == 1) {
      // 新建
      this.isUpdate = true;
    } else if (this.route.snapshot.queryParams.status == 2) {
      // 编辑
      this.textStatus = '编辑模板';
      this.isUpdate = true;
      this.initData(this.service.$api_get_contractTemplate);
    } else if (this.route.snapshot.queryParams.status == 3) {
      // 编辑
      this.textStatus = '查看模板';
      this.isUpdate = false;
      this.initData(this.service.$api_get_contractTemplate);
    }
  }
  goBack() {
    window.history.go(-1);
  }
  initSF() {
    this.schema = {
      properties: {
        templateName: {
          type: 'string',
          title: '模版名称'
        },
        templateType: {
          title: '模板类型',
          type: 'string',
          default: '',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'contract:template:type' },
            containAllLable: true,
            visibleIf: {
              _$expand: (value: boolean) => value
            }
          } as SFSelectWidgetSchema
        },
        contractType: {
          title: '单据类型',
          type: 'string',
          default: '',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'contract:document:type' },
            containAllLable: true,
            visibleIf: {
              templateType: value => value === 'MX'
            }
          } as SFSelectWidgetSchema
        }
      },
      required: ['templateName', 'templateType']
    };
    this.ui = {
      '*': {
        spanLabelFixed: 120,
        grid: { span: 8 }
      }
    };
  }
  initSF2(data?: any) {
    this.schema2 = {
      properties: {
        templateContent: {
          type: 'string',
          title: '',
          ui: {
            widget: 'tinymce',
            loadingTip: 'loading...',
            config: {
              height: 650
            }
          },
          default: data?.agreementContent || ''
        }
      }
    };
  }
  initData(url: string) {
    this.service.request(url, { id: this.route.snapshot.params.id }).subscribe(res => {
      if (res) {
        this.detailList = res;
        this.title = this.detailList?.templateName
        this.sfdata = res;
        this.sfdata2 = res;
      }
    });
  }
  cancel() {
    window.history.go(-1);
  }

  save() {
    console.log(this.sf.value)
    console.log(this.sf2.value) 
    console.log(this.detailList.templateName)
    console.log(this.title)
    if (!this.sf.value.templateName || !this.sf.value.templateType || !this.sf2.value.templateContent || !this.title) {
      this.service.msgSrv.error('必填参数为空，请检查再重新保存！');
      return;
    }
    if (this.sf.value.templateType == 'MX') {
      if (this.sf.value.contractType == '') {
        this.service.msgSrv.error('必填参数为空，请检查再重新保存！');
        return;
      }
    }
    const params = {
      ...this.sf.value,
      ...this.sf2.value,
      templateTitle: this.title
    };
    this.service.request(this.service.$api_save_contractTemplate, params).subscribe((res: any) => {
      if (res) {
        this.service.msgSrv.success('保存成功！');
        this.router.navigate(['/contract-management/template']);
      }
    });
  }
}
