import { AfterViewInit, ChangeDetectorRef, Component, OnChanges, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema, SFSchemaEnumType, SFUISchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { of } from 'rxjs';
import { delay, map } from 'rxjs/operators';
import { SystemService } from '../../../services/system.service';
import { SettingMenuComponent } from '../menu/menu.component';

@Component({
  selector: 'app-cuc-edit',
  templateUrl: './edit.component.html',
  styleUrls: ['./edit.less']
})
export class SettingRoleEditComponent implements OnInit {
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  @ViewChild('menu', { static: false })
  menu!: SettingMenuComponent;
  record: any = {};
  roleInfoData: any = {};
  authorityAssistId: any[] = [];
  appList: any[] = [];
  source = '';
  i: any;
  schema!: SFSchema;
  authority: any[] = [];
  roleTplData: any[] = [];
  appId = '';
  ui!: SFUISchema;
  constructor(private modal: NzModalRef, public service: SystemService, public http: _HttpClient, private cdr: ChangeDetectorRef) {}

  ngOnInit(): void {
    console.log('i', this.i, this.source);
    if (this.i.id) {
      this.getRoleInfo();
    } else {
      this.getTplList();
    }
    if (this.source === '') {
      this.initSF();
    }
  }
  initSF() {
    this.schema = {
      properties: {
        roleName: {
          title: '角色名称',
          type: 'string',
          default: this.roleInfoData.roleName,
          maxLength: 20,
          ui: {
            placeholder: '请输入角色名称'
          }
        },
        roleDescription: {
          title: '角色描述',
          type: 'string',
          maxLength: 100,
          default: this.roleInfoData.roleDescription,
          ui: {
            autosize: { minRows: 3 },
            hidden: this.i.lookType === 'detail',
            placeholder: '请输入角色描述',
            widget: 'textarea'
          }
        }
      },
      required: ['roleName']
    };
    this.ui = {
      '*': {
        spanLabelFixed: 120,
        grid: { span: 24 }
      }
    };
  }
  getAppList() {
    const params = {
      pageSize: 10000,
      pageIndex: 1
    };
    return this.service.request(this.service.$api_getAppList, params).pipe(
      map(res => {
        this.appList = res;
        const versionArr: any[] = [];
        const resArr = res;
        if (resArr && resArr.length) {
          resArr.forEach((item: any) => {
            versionArr.push({
              label: item.appName,
              value: item.appId
            });
          });
        }
        return versionArr;
      })
    );
  }
  getTplList() {
    // this.menu.getAllFunction();
  }
  getRoleInfo() {
    const params = {
      id: this.i.id
    };
    this.service.request(this.service.$api_getRoleInfo, params).subscribe(res => {
      this.roleInfoData = res;
      this.roleInfoData.authority = res.authority || [];
      this.roleInfoData.authorityAssistId = res.authorityAssistId || [];
      if (this.source === '') {
        this.initSF();
      }
      this.getTplList();
    });
  }
  getData(res: { authority: any[]; authorityAssistId: any[] }) {
    this.authority = res.authority;
    this.authorityAssistId = res.authorityAssistId;
  }
  close() {
    this.modal.destroy();
  }
  sure() {
    this.menu.washTree();
    if (this.authorityAssistId.length === 0) {
      this.service.msgSrv.warning('请选择权限！');
      return;
    }
    const params: any = {
      id: this.i.id,
      ...this.sf.value,
      authority: this.authority,
      authorityAssistId: this.authorityAssistId
    };
    if (this.sf) {
      this.appList.forEach(item => {
        if (item.appId === this.sf.value.appId) {
          params.tenantId = item.tenantId;
        }
      });
    }
    delete params.tplId;
    if (this.i.id === 0) {
      delete params.id;
    }
    this.service.request(this.service.$api_updateRoleInfo, params).subscribe(res => {
      this.modal.close(true);
    });
  }
}
