import { Component, OnInit, ViewChild } from '@angular/core';
import { STComponent, STColumn, STChange, STRequestOptions } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema } from '@delon/form';
import { LogsService } from '../../services/logs.service';

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

  searchSchema: SFSchema = {
    properties: {
      expand: {
        type: 'boolean',
        ui: {
          hidden: true
        }
      },
      roleName: {
        type: 'string',
        title: '登录端口',
        ui: { placeholder: '请输入' }
      },
      loginType: {
        type: 'string',
        title: '登录方式',
        enum: [
          { label: '全部', value: null },
          { label: 'PC', value: '1' },
          { label: 'APP', value: '2' },
          { label: '小程序', value: '3' }
        ],
        ui: {
          widget: 'select',
          placeholder: '请选择'
        },
        default: null
      },
      address: {
        type: 'string',
        title: '位置',
        ui: {
          placeholder: '请输入'
        }
      },
      terminalIp: {
        type: 'string',
        title: 'ip',
        ui: {
          placeholder: '请输入',
          visibleIf: {
            expand: (value: boolean) => value
          }
        }
      },
      time: {
        title: '登录时间',
        type: 'string',
        ui: {
          widget: 'sl-from-to-search',
          format: 'yyyy-MM-dd HH:mm:ss',
          placeholder: '请选择',
          nzShowTime: true,
          visibleIf: {
            expand: (value: boolean) => value
          }
        } as SFDateWidgetSchema
      }
    }
  };

  columns: STColumn[] = [
    { title: '登录时间', index: 'createTime', type: 'date' },
    { title: '登录端口', index: 'roleName' },
    { title: '姓名', index: 'userName' },
    { title: '登录方式', index: 'loginType', enum: { '1': 'PC', '2': 'APP', '3': '小程序' }, type: 'enum' },
    { title: '位置', index: 'address' },
    { title: 'ip', index: 'terminalIp' }
  ];

  _$expand = false;

  constructor(public service: LogsService) {}

  ngOnInit(): void {}

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

  roleAction(item?: any) {}

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

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