import { Component, Input, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NzSpaceModule } from 'ng-zorro-antd/space';
import { CommonCompModule } from '../../../services/common.modules';
import { NzTabsModule } from 'ng-zorro-antd/tabs';
import { ProcessComponent } from '../components/process/process.component';
import { CreateCollectionComponent } from '../collection/create-collection/create-collection.component';
import { ActivatedRoute, Router } from '@angular/router';
import Parse from 'parse';
import { NzAvatarModule } from 'ng-zorro-antd/avatar';
import { NzDropDownModule } from 'ng-zorro-antd/dropdown';
import { NzPopoverModule } from 'ng-zorro-antd/popover';
import { NzTagModule } from 'ng-zorro-antd/tag';
import { NzModalModule } from 'ng-zorro-antd/modal';
import { NzMessageService } from 'ng-zorro-antd/message';
@Component({
  selector: 'app-user-edit',
  templateUrl: './user-edit.component.html',
  styleUrls: ['./user-edit.component.scss'],
  imports: [
    CommonModule,
    NzSpaceModule,
    CommonCompModule,
    NzTabsModule,
    ProcessComponent,
    CreateCollectionComponent,
    NzAvatarModule,
    NzDropDownModule,
    NzPopoverModule,
    NzTagModule,
    NzModalModule,
  ],
  standalone: true,
})
export class UserEditComponent implements OnInit {
  inputValue: string = `
  async function pipe(user, context, callback) {
    if (context.connection === "weibo") {
      return callback(new Error("当前系统禁止使用微博登录！"))
    }
    callback(null, user, context)
  }`;
  isVisible: boolean = false;
  user: Parse.Object | any;
  profile: Parse.Object | any;
  password: string = '';

  constructor(
    private activeRoute: ActivatedRoute,
    private router: Router,
    private message: NzMessageService
  ) {}

  ngOnInit() {
    this.activeRoute.paramMap.subscribe(async (params) => {
      let id = params.get('id');
      console.log(id);
      if (id) {
        let query = new Parse.Query('_User');
        query.include('department')
        this.user = await query.get(id);
        let queryProfile = new Parse.Query('Profile');
        queryProfile.equalTo('user', id);
        this.profile = await queryProfile.first();
      }
    });
  }
  async updateUser(type: string) {
    console.log(type);
    switch (type) {
      case '已认证':
        this.user.set('accountState', '已认证');
        break;
      case '已禁用':
        this.user.set('accountState', '已禁用');
        break;
      case '删除':
        this.user.set('isDeleted', true);
        break;
    }
    await this.user.save();
    this.ngOnInit();
  }
  async handleOk() {
    this.password = this.password.trim()
    if(!this.password){
      this.message.warning('密码格式错误');
      return
    }
    try {
      this.user.set('password', this.password);
      await this.user.save();
      this.ngOnInit();
    } catch (err) {
      this.message.warning('保存出错，请注意密码格式');
    }
    this.isVisible = false;
  }
}
