import { Component, OnInit, ViewChild, Input, Output, EventEmitter } from '@angular/core';
import { CommonCompModule } from '../../../../services/common.modules';
import { ActivatedRoute, RouterOutlet, Router } from '@angular/router';
import { CompTableListComponent } from '../../../../app/comp-table/comp-table-list/comp-table-list.component';
import { Profile } from '../../../../schemas/Profile';
import * as Parse from 'parse';
import { CommonModule } from '@angular/common';
import { NzAvatarModule } from 'ng-zorro-antd/avatar';
import { NzEmptyModule } from 'ng-zorro-antd/empty';
import { NzMessageService } from 'ng-zorro-antd/message';
@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.scss'],
  imports: [CommonModule, RouterOutlet, CompTableListComponent,NzEmptyModule, CommonCompModule,NzAvatarModule],
  standalone: true,
})
export class ProfileComponent  implements OnInit {
  @Input('maxWidth') maxWidth:any //最大宽度
  @Input('maxChecked') maxChecked:number = 1 //最大可操作数量
  @Input('idList') idList:Array<string> = [] //已指向id
  @Input('identity') identity:any //指定身份类型
  @Input('depart') depart:any //指定单位
  @Input('disabled') disabled:boolean = false //只查询传入id
  @Input('hideBtn') hideBtn:boolean = false//隐藏编辑

  @Output() change: EventEmitter<any> = new EventEmitter<any>();

  profiles:Array<Parse.Object> = []
  
  @ViewChild(CompTableListComponent) list: CompTableListComponent | undefined;

  Profile = Profile;
  user: Parse.User | undefined;
  className: string | undefined;
  queryParams: any | undefined;
  fieldsArray: Array<any> | undefined;
  //是否存在已选中
  checked(id:string):boolean{
    return this.idList.some((item:string)=> item == id)
  }
  constructor(
    private msg: NzMessageService,
  ) { 
    this.user = Parse.User.current();
    this.className = this.Profile.className;
    this.fieldsArray = this.Profile.fieldsArray;
    this.queryParams = {
      where: {
        isDeleted: { $ne: true },
      },
    };
  }

  ngOnInit() {
    this.getProfile()
  }
  loading:boolean = false
  async getProfile(){
    this.loading = true
    let query = new Parse.Query('Profile')
    if(this.depart){
      let queryParams = {
        where : {
          "$or": [{
            "user": {
              "$inQuery": {
                "where": {
                  "$or": [{
                    "department": { "$eq": this.depart},
                    },
                  ]
                },
                "className": "_User"
              }
            }
          }]
        }
      }
      query = Parse.Query.fromJSON('Profile',queryParams);
    }
    this.identity && query.equalTo('identity',this.identity)
    this.disabled && query.containedIn('objectId',this.idList)
    query.notEqualTo('isDeleted',true)
    query.include('user','user.department')
    this.profiles = await query.find()
    this.loading = false
  }
  onChange(id:string, type:string){
    let index = this.idList.findIndex(item=> item == id)
    if(type == 'delete'){
      this.idList.splice(index,1)
    }else{
      if(index == -1){
        if(this.idList.length >= this.maxChecked){
          // this.msg.warning('超出数量限制')
          this.idList.shift()
          this.idList.push(id)
          return
        }
        this.idList.push(id)
      }else{
        this.idList.splice(index,1)
      }
    }
    this.change.emit(this.idList)
  }
}
