/*
 * @Description  :
 * @Version      : 1.0
 * @Author       : Shiming
 * @Date         : 2022-01-10 16:06:17
 * @LastEditors  : Shiming
 * @LastEditTime : 2022-04-20 10:00:02
 * @FilePath     : \\tms-obc-web\\src\\app\\shared\\components\\dynamic-setting\\dynamic-setting-modal\\dynamic-setting-modal.component.ts
 * Copyright (C) 2022 huzhenhong. All rights reserved.
 */
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/compiler';
import { Component, Input, OnInit } from '@angular/core';
import { ModalHelper } from '@delon/theme';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { BaseService } from 'src/app/shared/services';

@Component({
  selector: 'app-dynamic-setting-modal',
  templateUrl: './dynamic-setting-modal.component.html'
})
export class DynamicSettingModalComponent implements OnInit {
  tabs: any[] = [];
  selectedTab: any = null;
  configFullKey = 'sys.config';
  configList: any = [];

  // 配置类型数组重组函数
  @Input()
  formatTypeList = (item: any[]) => {
    return [...item];
  };
  @Input()
  extendType!: string;
  @Input()
  spareBusinessId!: string;
  @Input()
  businessId!: string;
  @Input()
  configvalue!: string;
  @Input()
  labelWidth = 200;

  constructor(public service: BaseService, private modalHelp: NzModalRef) {}

  ngOnInit() {
    if (this.configvalue) {
      this.configFullKey = this.configvalue;
    }
    this.getTypeList();
  }

  getTypeList() {
    this.service
      .request('/api/mdc/pbc/sysConfigItemExtend/getSysConfigExtend', {
        configFullKey: this.configFullKey,
        extendType: this.extendType,
        businessId: this.businessId,
        spareBusinessId: this.spareBusinessId,
      })
      .pipe(
        map((res: Array<any>) => {
          if (res?.length > 0) {
            const typeData = res.find(config => config.configFullKey === this.configFullKey);
            if (typeData) {
              return typeData.children || [];
            }
          }
          return [];
        }),
        map(this.formatTypeList)
      )
      .subscribe((res: Array<any>) => {
        this.tabs = res;
        if (res?.length > 0) {
          this.selectedTab = res[0];
          this.configList = this.formatItems(this.selectedTab.items);
          const hiddenType = this.configList.find((item: any) => item.itemType === 7 || item.itemType === 999);
          this.labelWidth = hiddenType ? 0 : 200;
        }
      });
  }

  changeType(type: any): void {
    this.selectedTab = type;
    this.configList = this.formatItems(type.items);
    const hiddenType = this.configList.find((item: any) => item.itemType === 7 || item.itemType === 999);
    this.labelWidth = hiddenType ? 0 : 200;
  }

  saveAction(params: Array<any>) {
    const p = params.map(config => ({
      businessId: this.businessId,
      configId: config.configId,
      extendType: this.extendType,
      id: config.extendId,
      configItemId: config.id,
      parentId: config.parentId,
      itemData: config.itemData,
      itemValue: config.itemValue,
      remark: config.remark
    }));
    this.service.request('/api/mdc/pbc/sysConfigItemExtend/saveBatch', p).subscribe(res => {
      if (res) {
        this.service.msgSrv.success('修改配置成功');
        this.modalHelp.destroy(true);
      }
    });
  }

  private formatItems(items: Array<any>): Array<any> {
    if (items?.length > 0) {
      return items
        .map(item => ({
          ...item,
          itemData: item.extendItemData || item.itemData,
          itemValue: item.extendItemValue || item.itemValue
        }))
        .map(item => ({
          ...item,
          remark: item?.remark ? JSON.parse(item?.remark) : null,
          extend: item?.extend ? JSON.parse(item?.extend) : [],
          // itemData: item.itemData ? JSON.parse(item.itemData) : item.itemData,
          itemValue: item?.itemValue ? (item?.itemType !== 8 ? JSON.parse(item?.itemValue) : item?.itemValue) : item?.itemValue
        }));
    }
    return [];
  }
}
