/*
 * @Description  :
 * @Version      : 1.0
 * @Author       : Shiming
 * @Date         : 2022-01-25 16:03:45
 * @LastEditors  : Shiming
 * @LastEditTime : 2022-02-17 14:44:14
 * @FilePath     : \\tms-obc-web\\src\\app\\shared\\components\\dynamic-setting\\dynamic-setting-h5\\dynamic-setting-h5.component.ts
 * Copyright (C) 2022 huzhenhong. All rights reserved.
 */
import { Component, OnInit, EventEmitter, Input, Output } from '@angular/core';
import { BaseService } from '@shared';
import { NzUploadChangeParam } from 'ng-zorro-antd/upload';

const JSONTYPE = new Set([5, 6, 9]);
@Component({
  selector: 'app-dynamic-setting-h5',
  templateUrl: './dynamic-setting-h5.component.html',
  styleUrls: ['./dynamic-setting-h5.component.less']
})
export class DynamicSettingH5Component implements OnInit {
  @Input()
  tabs: any[] = [];
  @Input()
  selectedTab: any = null;

  @Input()
  configList: any = [];
  @Output()
  selectedEvent = new EventEmitter<any>();
  @Output()
  saveEvent = new EventEmitter<any>();

  @Input()
  tabSpan = 4;
  @Input()
  labelWidth = 250;
  @Input()
  itemValue = 'itemValue';
  listUrls: any;
  constructor(public service: BaseService) {}

  ngOnInit() {
    console.log(this.tabs);
  }

  changeType(type: any): void {
    this.selectedTab = type;
    console.log(type);

    this.selectedEvent.emit(this.selectedTab);
  }
 
  saveAction() {
    if (this.configList?.length < 0) {
      return;
    }
    console.log(this.configList);
    let params = [...this.configList];
    params = params.map((item: any) => {
      console.log(item);
      if (item.itemType == 9) {
        const files = item.itemValue?.map(({ response, name }: any) => ({ url:response?.data?.fullFilePath, name }));
        return {
          ...item,
          remark: item.remark ? JSON.stringify(item.remark) : null,
          itemData: item.itemData ? JSON.stringify(item.itemData) : null,
          itemValue: item.itemValue ? JSON.stringify(files) : null
        };
      }
      if (JSONTYPE.has(item.itemType)) {
        item.itemValue = item.itemValue ? JSON.stringify(item.itemValue) : null;
      }
      return {
        ...item,
        remark: item.remark ? JSON.stringify(item.remark) : null,
        itemData: item.itemData ? JSON.stringify(item.itemData) : null
      };
    });
    console.log(params);
    this.saveEvent.emit(params);
  }
}
// [0]?.response?.data.fullFilePath
