import { Component, OnInit, ViewChild } from '@angular/core';
import { STComponent, STColumn, STChange } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { SystemService } from '../../services/system.service';

@Component({
  selector: 'app-user-logs',
  templateUrl: './user-logs.component.html',
  styleUrls: ['./user-logs.component.less']
})
export class UserLogsComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;

  url = `/rule?_allow_anonymous=true`;

  searchSchema: SFSchema = {
    properties: {
      expand: {
        type: 'boolean',
        ui: {
          hidden: true
        }
      },
      receiveName: {
        type: 'string',
        title: '登录端口',
        ui: { placeholder: '请输入' }
      },
      phone: {
        type: 'string',
        title: '登录方式',
        ui: { placeholder: '请输入' }
      },
      page: {
        type: 'string',
        title: '位置',
        ui: {
          placeholder: '请输入'
        }
      },
      content: {
        type: 'string',
        title: 'ip',
        ui: {
          placeholder: '请输入',
          visibleIf: {
            expand: (value: boolean) => value
          }
        }
      },
      createTime: {
        title: '登录时间',
        type: 'string',
        ui: {
          widget: 'date',
          mode: 'range',
          format: 'yyyy-MM-dd',
          visibleIf: {
            expand: (value: boolean) => value
          }
        } as SFDateWidgetSchema
      }
    }
  };

  columns: STColumn[] = [
    { title: '登录时间', index: 'updatedAt', type: 'date' },
    { title: '登录端口', index: 'description' },
    { title: '姓名', index: 'description' },
    { title: '登录方式', index: 'description' },
    { title: '位置', index: 'description' },
    { title: 'ip', index: 'description' }
  ];
  selectedRows: any[] = [];

  reqParams = { pageIndex: 1, pageSize: 10 };
  _$expand = false;

  constructor(public service: SystemService, private nzModalService: NzModalService) {}

  ngOnInit(): void {}

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
      case 'filter':
        this.st.load();
        break;
    }
  }

  roleAction(item?: any) {}

 /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
  }

  /**
   * 伸缩查询条件
   */
  expandToggle() {
    this._$expand = !this._$expand;
    this.sf?.setValue('/expand', this._$expand);
  }
}
