import { Component, OnInit, ViewChild, inject } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFSchema } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { SHARED_IMPORTS } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { AppValidators } from 'src/app/core/utils/app-validators';
import { BaseService } from 'src/app/core/utils/base.service';

@Component({
  selector: 'app-sys-organ-manage',
  standalone: true,
  imports: [...SHARED_IMPORTS],
  templateUrl: './organ-manage.component.html'
})
export class SysOrganManageComponent implements OnInit {
  private readonly http = inject(_HttpClient);
  private readonly modal = inject(ModalHelper);
  private readonly fb = inject(FormBuilder);
  private readonly baseService = inject(BaseService);
  private readonly modalService = inject(NzModalService);

  url = `/api/main/organ-info`;
  searchSchema: SFSchema = {
    properties: {
      type: {
        type: 'number',
        title: '状态',
        enum: [
          { label: '全部', value: 99 },
          { label: '未审核', value: 0 },
          { label: '审核未通过', value: 1 },
          { label: '审核通过', value: 9 }
        ],
        default: 99
      },
      filter: {
        type: 'string',
        title: '关键字过滤'
      }
    }
  };

  @ViewChild('st') private readonly st!: STComponent;
  columns: STColumn[] = [
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          click: (item: any) => {
            this.edit(item);
          }
        },
        {
          text: '删除',
          pop: true,
          click: (item: any) => {
            this.delete(item);
          }
        }
      ]
    },
    { title: '机构名称', index: 'organName' },
    { title: '管理员账户', index: 'adminName' },
    { title: '管理员邮箱', index: 'adminEmail' },
    { title: '管理员电话', index: 'adminTel' },
    { title: '申请时间', index: 'begntime', type: 'date', dateFormat: 'yyyy-MM-dd', width: 120 },
    // { title: '结束时间', index: 'endtime', type: 'date', dateFormat: 'yyyy-MM-dd' },
    { title: '状态', index: 'organStatusDisplay' }
  ];

  editVisible = false;
  title = '';
  loading = false;
  organs: any;
  form: FormGroup = this.fb.group({
    id: [],
    organName: [, AppValidators.required('E0001', '机构名称')],
    adminTel: [, AppValidators.required('E0001', '管理员电话')],
    adminEmail: [, AppValidators.required('E0001', '管理员邮箱')]
  });

  ngOnInit(): void {}

  save() {
    if (this.baseService.hasError(this.form)) {
      return;
    }
    const requestModel = this.form.getRawValue();

    if (!requestModel.id) {
      this.http.post(this.url, requestModel).subscribe(res => {
        this.reset();
      });
    } else {
      this.http.put(`${this.url}/${requestModel.id}`, requestModel).subscribe(res => {
        this.reset();
      });
    }
  }

  reset() {
    this.editVisible = false;
    this.st.reload();
  }

  /**
   * ocean 2024-1-18
   * 添加
   */
  add(): void {
    this.editVisible = true;
    this.form.reset();
  }

  /**
   * ocean 2024-1-18
   * 编辑
   */
  edit(item: { id: any }): void {
    this.editVisible = true;
    this.http.get(`${this.url}/${item.id}`).subscribe(res => {
      this.form.patchValue(res);
    });
  }

  /**
   * ocean 2024-1-18
   * 作废
   */
  delete(item: { id: any }): void {
    this.http.delete(`${this.url}/${item.id}`).subscribe(res => {
      this.st.reload();
    });
  }

  /**
   * ocean 2023-7-10
   * 审核机构
   */
  review(item: any): void {
    this.http.put(`/api/main/organ/${item.id}/review-organ`, {}).subscribe(res => {
      this.st.reload();
    });
  }

  close(): void {
    this.editVisible = false;
  }
}
