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

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

  searchSchema: SFSchema = {
    properties: {
      expand: {
        type: 'boolean',
        ui: {
          hidden: true
        }
      },
      operator: {
        type: 'string',
        title: '操作人',
        ui: { placeholder: '请输入' }
      },
      telephone: {
        type: 'string',
        title: '手机号码',
        ui: { placeholder: '请输入' }
      },
      operatePage: {
        type: 'string',
        title: '操作页面',
        ui: {
          placeholder: '请输入'
        }
      },
      operationContent: {
        type: 'string',
        title: '操作内容',
        ui: {
          placeholder: '请输入',
          visibleIf: {
            expand: (value: boolean) => value
          }
        }
      },
      time: {
        title: '操作时间',
        type: 'string',
        ui: {
          widget: 'sl-from-to-search',
          format: 'yyyy-MM-dd HH:mm:ss',
          nzShowTime: true,
          visibleIf: {
            expand: (value: boolean) => value
          }
        } as SFDateWidgetSchema
      }
    }
  };

  columns: STColumn[] = [
    { title: '操作时间', index: 'operatorTimestamp' },
    { title: '操作人', index: 'operator' },
    { title: '操作人手机号码', index: 'telephone' },
    { title: '操作页面', index: 'operatePage' },
    { title: '操作内容', index: 'operationContent' }
  ];

  _$expand = false;

  constructor(public service: LogsService) {}

  ngOnInit(): void {
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    requestOptions.body.operateType = '1';
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf.value,
        operateType: '1',
        endTime: this.sf.value.time?.[1] || null,
        startTime: this.sf.value.time?.[0] || null
      });
    }
    return requestOptions;
  };

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

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