import { Component, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema } from '@delon/form';
import { EAEnvironmentService } from '@shared';
import { NzDrawerService } from 'ng-zorro-antd/drawer';
import { NzModalService } from 'ng-zorro-antd/modal';
import { MenuManagerService } from '../../services/menu-manager.service';
import { MenuModalComponent } from '../index/menu-modal/menu-modal.component';
import { AuthDrawerComponent } from './auth-drawer/auth-drawer.component';

@Component({
  selector: 'app-api-auth',
  templateUrl: './api-auth.component.html',
  styleUrls: ['./api-auth.component.less', '../../../commom/less/box.less']
})
export class ApiAuthComponent implements OnInit {
  selectedPlatform!: { name: string; appId: string; enName: string };
  platforms: Array<any> = [
    { name: '货主PC', appId: 'A48F72F0A304427F921794BAD86B3522', enName: 'tms-smc-web' },
    { name: '运营后台', appId: this.envSrv.env.appId, enName: 'tms-obc-web' }
  ];

  @ViewChild('sf', { static: false })
  sf!: SFComponent;

  searchSchema: SFSchema = {
    properties: {
      roleName: {
        type: 'string',
        title: '菜单名称',
        ui: { placeholder: '请输入' }
      }
    }
  };

  mapOfExpandedData: { [key: string]: any[] } = {};
  listOfMapData: any[] = [];
  constructor(private envSrv: EAEnvironmentService, public service: MenuManagerService, private drawer: NzDrawerService) {
    this.initData();
  }

  ngOnInit(): void {}

  initData(): void {
    this.selectedPlatform = this.platforms[0];
    this.loadMemu(this.selectedPlatform.appId);
  }

  loadMemu(appId: string) {
    this.service.request(this.service.$api_get_all, { appId }, 'POST', false).subscribe(res => {
      if (res) {
        this.listOfMapData = res;
        this.listOfMapData.forEach(item => {
          this.mapOfExpandedData[item.key] = this.service.convertTreeToList(item);
        });
        // console.log(this.listOfMapData, this.mapOfExpandedData);
      }
    });
  }

  changeMemu(key: number) {
    this.selectedPlatform = this.platforms[key];
    this.loadMemu(this.selectedPlatform.appId);
  }

  openDrawer(item: any) {
    this.drawer.create({
      nzTitle: `接口权限配置(${item.text})`,
      nzContent: AuthDrawerComponent,
      nzWidth: 900,
      nzContentParams: { id: item.id, appId: this.selectedPlatform.appId, code: item.keyCode }
    });
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
  }
}
