/*
 * @Description  :
 * @Version      : 1.0
 * @Author       : Shiming
 * @Date         : 2022-01-25 13:10:49
 * @LastEditors  : Shiming
 * @LastEditTime : 2022-01-26 17:45:11
 * @FilePath     : \\tms-obc-web\\src\\app\\routes\\sys-setting\\components\\basic-setting\\basic-setting.component.ts
 * Copyright (C) 2022 huzhenhong. All rights reserved.
 */
import { Component, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { dateTimePickerUtil } from '@delon/util';
import { SystemService } from '../../services/system.service';
const NOJSONTYPE = new Set([8, 12, 13]);
@Component({
  selector: 'app-basic-setting',
  templateUrl: './basic-setting.component.html',
  styleUrls: ['./basic-setting.component.less']
})
export class BasicSettingComponent implements OnInit {
  tabs: any[] = [];
  selectedTab: any = null;
  labelWidth = 250;
  configList: any = [];
  constructor(public service: SystemService) {}

  ngOnInit() {
    this.getTypeList();
  }

  getTypeList() {
    this.service.request(this.service.$api_get_config_tree, { configFullKey: 'sys.config', extendType: 0 }).subscribe((res: Array<any>) => {
      if (res?.length > 0) {
        const typeData = res.find(config => config.configFullKey === 'sys.config');
        if (typeData) {
          this.tabs = typeData?.children;
          this.selectedTab = typeData?.children?.[0];
          this.getConfigList(this.selectedTab);
        }
      }
    });
  }

  getConfigList(selectedTab: any) {
    this.selectedTab = selectedTab;
    this.service.request(this.service.$api_get_config_by_parent_id, { id: selectedTab?.id }).subscribe((res: Array<any>) => {
      if (res?.length > 0) {
        res = res.map(item => ({
          ...item,
          remark: item.remark ? JSON.parse(item.remark) : null,
          itemValue: item?.itemValue ? (NOJSONTYPE.has(item?.itemType) ? item?.itemValue : JSON.parse(item?.itemValue)) : item?.itemValue,
          itemData: item.itemData ? JSON.parse(item.itemData) : item.itemData
        }));
        const hiddenType = res.find(item => item.itemType === 7 || item.itemType === 999);
        this.labelWidth = hiddenType ? 0 : 250;
        this.configList = res;
      } else {
        this.configList = [];
      }
    });
  }

  saveAction(params: any) {
    this.service.request(this.service.$api_update_config_batch, params).subscribe(res => {
      if (res) {
        this.service.msgSrv.success('修改配置成功');
        setTimeout(() => {
          this.getConfigList(this.selectedTab);
        }, 100);
      }
    });
  }
}
