import { Component, OnInit, ViewChild } from '@angular/core';
import { STComponent, STColumn, STRequestOptions, STChange } from '@delon/abc/st';
import { SFComponent, SFSchema } from '@delon/form';
import { Menu, ModalHelper } from '@delon/theme';
import { EAEnvironmentService } from '@shared';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzModalService } from 'ng-zorro-antd/modal';
import { SettingRoleEditComponent } from 'src/app/routes/sys-setting/components/role-management/edit/edit.component';
import { MenuManagerMenusortComponent } from '../menusort/menusort.component';
import { MenuManagerService } from './../../services/menu-manager.service';
import { MenuModalComponent } from './menu-modal/menu-modal.component';

@Component({
  selector: 'app-menu-manager-components-index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.less', '../../../commom/less/box.less']
})
export class MenuManagerComponentsIndexComponent 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 modal: NzModalService, private modalHelper: ModalHelper,) {
    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);
  }

  menuAction(nzTitle: string, item?: any, parentId?: string, isDisabled = false) {
    const modal = this.modal.create({
      nzTitle,
      nzContent: MenuModalComponent,
      nzWidth: 900,
      nzComponentParams: item
        ? { formData: { ...item }, isDisabled, params: { parentId, appId: this.selectedPlatform.appId } }
        : { formData: { id: null }, params: { parentId, appId: this.selectedPlatform.appId } },
      nzFooter: null
    });
    modal.afterClose.subscribe(res => {
      if (res) {
        this.loadMemu(this.selectedPlatform.appId);
      }
    });
  }

  deleteAction(item: any) {
    this.modal.error({
      nzTitle: '确认删除？',
      nzClosable: false,
      nzCancelText: '取消',
      nzOnOk: () => {
        this.service.request(this.service.$api_del_many, [item.id]).subscribe(res => {
          if (res) {
            this.service.msgSrv.success('删除菜单成功');
            this.loadMemu(this.selectedPlatform.appId);
          }
        });
      }
    });
  }

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

  menuImport(index: number) {
    if (this.listOfMapData?.length > 0) {
      this.service.msgSrv.warning('请先清空菜单');
      return;
    }
    if (!this.selectedPlatform) {
      return;
    }

    this.service.menuImport(this.selectedPlatform.enName, this.selectedPlatform.appId);
  }

  delMenu(type: number) {
    this.modal.confirm({
      nzTitle: '<i>删除确认</i>',
      nzContent: `是否确认删除？`,
      nzOnOk: () => {
        this.service.getMenuByAppID(type === 0 ? 'A48F72F0A304427F921794BAD86B3522' : this.envSrv.env.appId);
      }
    });
  }
  menuSort(){
    const dialogData = {
      appId: this.selectedPlatform.appId
    };
    this.modalHelper.create(MenuManagerMenusortComponent, { i: dialogData }, { size: 900 }).subscribe((res:any) => {
      if(res) {
        console.log('a')
        this.loadMemu(this.selectedPlatform.appId);
      }
    });
  }
}
