import { OnChanges } from '@angular/core';
/*
 * @Description  :
 * @Version      : 1.0
 * @Author       : Shiming
 * @Date         : 2022-01-05 11:01:55
 * @LastEditors  : Shiming
 * @LastEditTime : 2022-03-30 10:45:19
 * @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 { ShipperBaseService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { ContractManagementService } from '../../services/contract-management.service';

@Component({
  selector: 'app-contract-management-template-text-complaint',
  templateUrl: './contract-template-detail.component.html',
  styleUrls: ['./contract-template-detail.component.less'],
  providers: [DatePipe]
})
export class ContractManagementTemplateTextComponent implements OnInit {
  constructor(
    private nzModalService: NzModalService,
    public service: ContractManagementService,
    public route: ActivatedRoute,
    private datePipe: DatePipe,
    private router: Router,
    public shipperservice: ShipperBaseService
  ) {}
  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;
  Types: 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',
          enum: [
            { label: '框架合同', value: 'KJ' },
            { label: '明细合同', value: 'MX' },
            { label: '合伙人合同', value: 'HHR' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            change: (tag: any, org: any) => {
              console.log(tag);
              switch (tag) {
                case 'MX':
                  this.Types = [
                    { label: '订单合同', value: '1' },
                    { label: '订单补充协议', value: '2' },
                    { label: '运单合同', value: '3' },
                    { label: '运单补充协议', value: '4' },
                    { label: '委托代收合同', value: '5' },
                    { label: '电子提货单', value: '10' },
                    { label: '电子卸货单', value: '11' },
                  ];
                  this.sf.getProperty('/contractType')!.schema.enum = this.Types;
                  this.sf.getProperty('/contractType')!.widget.reset(this.Types);
                  this.sf.setValue('/contractType', this.Types);
                  return;
                  break;
                case 'KJ':
                  this.Types = [
                    { label: '网络货物运输服务合同', value: '6' },
                    { label: '运输服务承揽合同', value: '7' }
                  ];
                  this.sf.getProperty('/contractType')!.schema.enum = this.Types;
                  this.sf.getProperty('/contractType')!.widget.reset(this.Types);
                  this.sf.setValue('/contractType', this.Types);
                  return;
                  break;
                case 'HHR':
                  this.Types = [
                    { label: '企业合伙人入驻合同', value: '8' },
                    { label: '个人合伙人入驻合同', value: '9' }
                  ];
                  this.sf.getProperty('/contractType')!.schema.enum = this.Types;
                  this.sf.getProperty('/contractType')!.widget.reset(this.Types);
                  this.sf.setValue('/contractType', this.Types);
                  return;
                  break;
                default:
                  break;
              }
            }
          }
        },
        signingObject: {
          type: 'string',
          title: '承包商对象',
          enum: [
            { label: '货主', value: '1' },
            { label: '司机', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          }
        },
        contractType: {
          title: '合同类型',
          type: 'string',
          enum: this.Types,
          ui: {
            widget: 'select',
            placeholder: '请选择'
          }
        },
        resourceType: {
          title: '货源类型',
          type: 'string',
          default: '',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'goodresource:type' },
            containsAllLable: true,
            visibleIf: {
              templateType: value => value === 'MX'
            }
          } as SFSelectWidgetSchema
        },
        enterpriseInfoId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            visibleIf: {
              templateType: value => value === 'KJ' || value === 'HHR'
            },
            asyncData: () => this.shipperservice.getNetworkFreightForwarder()
          }
        }
      },
      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) {
    console.log('编辑');

    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() {
    // 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.detailList.templateName
    };
    console.log(params);
    this.service.request(this.service.$api_save_contractTemplate, params).subscribe((res: any) => {
      if (res) {
        this.service.msgSrv.success('保存成功！');
        this.router.navigate(['/contract-management/template']);
      }
    });
  }
}
