import { CommonModule } from '@angular/common';
import { Component, Inject } from '@angular/core';
import { FormControl, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import Parse from "parse";
import { ParseSchema } from '../../../schemas/func-parse';

@Component({
  selector: 'app-comp-edit-object',
  standalone:true,
  imports:[ 
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    MatFormFieldModule,
    MatInputModule,
    MatButtonModule,
    MatDialogModule,
    // MatFormFieldModule,
  ],
  templateUrl: './comp-edit-object.component.html',
  styleUrls: ['./comp-edit-object.component.scss']
})
export class CompEditObjectComponent {
  jsonData:any = {}
  formControlMap:any = {}
  isFormShow:boolean = false;
  constructor(
    public dialogRef: MatDialogRef<CompEditObjectComponent>,
    @Inject(MAT_DIALOG_DATA) public data: {
      schema:ParseSchema|undefined,
      object:Parse.Object|null|undefined,
      fieldsArray:Array<any>|null,
      title:string|null,
      default?:any
    },
  ){
    // 设置数据校验
    this.data?.fieldsArray?.forEach(field=>{
      this.formControlMap[field?.key] = new FormControl({value:field?.key,disabled:field?.isDisabled||false})
      if(field?.require){
        this.formControlMap[field?.key] = new FormControl({value:field?.key,disabled:field?.isDisabled||false}, [Validators.required]);
      }
    })
    if(this.data.object?.toJSON()){
      this.jsonData = this.data?.object?.toJSON();
      console.log(this.jsonData)
    }
    if(!this.data?.object?.id){
      let className:any = this.data?.object?.className || this.data.schema?.className
      let SchemaContructor =  Parse.Object.extend(className)
      this.data.object = new SchemaContructor()
    }
  }
  ngOnInit(){
    setTimeout(() => {
      this.isFormShow = true
    }, 200);
  }
  /**
   * 保存与取消
   */
  close(){
    this.dialogRef.close();
  }
  isSaving:boolean = false;
  requiredErrorMap:any = {}
  async save() {
    if(this.isSaving) return;
    this.isSaving = true
    // 检查必填项
    this.requiredErrorMap = {}
    this.data?.fieldsArray?.forEach(field=>{
      if(field?.require && typeof this.jsonData[field?.key] == "undefined"){
        this.requiredErrorMap[field?.key] = true
      }
    })
    if(Object.keys(this.requiredErrorMap)?.length>=1){
      this.isSaving = false
      return
    }

    // 设置编辑结果
    delete this.jsonData.objectId
    delete this.jsonData.updatedAt
    delete this.jsonData.createdAt
    delete this.jsonData.sessionToken
    delete this.jsonData.ACL
    this.data?.object?.set(this.jsonData);

    // 设置默认值
    if(this.data?.default){
      this.data?.object?.set(this.data?.default)
    }

    // 保存并返回
    this.data.object = await this.data?.object?.save();
    this.dialogRef.close(this.data?.object);
    this.isSaving = false

  }

}
