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

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

  searchSchema: SFSchema = {
    properties: {
      expand: {
        type: 'boolean',
        ui: {
          hidden: true
        }
      },
      publishPort: {
        type: 'string',
        title: '端口',
        enum: [
          { label: '全部', value: null },
          { label: '司机', value: '1' },
          { label: '货主', value: '2' },
          { label: '运营平台', value: '3' }
        ],
        ui: {
          widget: 'select',
          placeholder: '请选择'
        },
        default: null
      },
      publishType: {
        type: 'string',
        title: '端口方式',
        enum: [
          { label: '全部', value: null },
          { label: 'PC ', value: '1' },
          { label: 'APP', value: '2' },
          { label: '小程序', value: '3' }
        ],
        ui: {
          widget: 'select',
          placeholder: '请选择'
        },
        default: null
      },
      publishVersion: {
        type: 'string',
        title: '版本号',
        ui: {
          placeholder: '请输入'
        }
      },
      time: {
        title: '发布时间',
        type: 'string',
        ui: {
          widget: 'sl-from-to-search',
          format: 'yyyy-MM-dd',
          visibleIf: {
            expand: (value: boolean) => value
          }
        } as SFDateWidgetSchema
      }
    }
  };

  columns: STColumn[] = [
    { title: '端口', index: 'publishPort', type: 'enum', enum: { 1: '司机', 2: '货主', 3: '运营平台' } },
    { title: '端口方式', index: 'publishType', type: 'enum', enum: { 1: 'PC', 2: 'APP', 3: '小程序' } },
    { title: '版本号', index: 'publishVersion' },
    {
      title: '发布时间',
      index: 'createTime',
      type: 'date'
    },
    { title: '更新内容', index: 'publicshContext' }
  ];

  _$expand = false;

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

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

  ngOnInit(): void {}

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

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