import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STColumn, STColumnBadge, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { format } from 'path';
import { VehicleService } from '../../../vehicle/services/vehicle.service';
@Component({
  selector: 'app-Vehicle-components-list',
  templateUrl: './list.component.html',
})
export class VehicleComponentsListComponent implements OnInit {
  _$expand = false;
  url = `/rule?_allow_anonymous=true`;

  ui!: SFUISchema;
  schema!: SFSchema;
  columns!: STColumn[];
  @ViewChild('st', { static: false }) st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;

  constructor(public service: VehicleService, private modal: NzModalService, private router: Router, private ar: ActivatedRoute) {}
  /**
   * 查询字段个数navigate
   */
  get queryFieldCount(): number {
    return Object.keys(this.schema?.properties || {}).length;
  }

  /**
   * 查询参数
   */
  get reqParams() {
    const params: any = {
      ...(this.sf && this.sf.value),
    };
    if (this.sf?.value.effectiveDate) {
      params.effectiveDateStart = this.sf?.value.effectiveDate[0];
      params.effectiveDateEnd = this.sf?.value.effectiveDate[1];
    }
    delete params.effectiveDate;
    delete params.expand;
    return params;
  }

  get selectedRows() {
    return this.st?.list.filter((item) => item.checked) || [];
  }

  ngOnInit() {
    this.initSF();
    this.initST();
    this.ar.url.subscribe((params) => {
      this.st?.load(1);
    });
  }
  dataProcess(data: STData[]): STData[] {
    return data.map((i, index) => {
      i.showSortFlag = false;
      return i;
    });
  }
  initSF() {
    this.schema = {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true,
          },
        },
        carNo: { title: '车牌号', type: 'string', ui: { showRequired: false } },
        carNoColor: {
          type: 'string',
          title: '车牌颜色',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'CarColor' },
          },
        },
        carLength2: {
          title: '运营状态',
          type: 'string',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'CarColor' },
            visibleIf: {
              expand: (value: boolean) => value,
            },
          },
        },
        carModel: {
          title: '车型',
          type: 'string',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'CarColor' },
            visibleIf: {
              expand: (value: boolean) => value,
            },
          },
        },
        carLength: {
          title: '车长',
          type: 'string',
           ui: {
            widget: 'dict-select',
            params: { dictKey: 'CarColor' },
            visibleIf: {
              expand: (value: boolean) => value,
            },
          },
        },
        carLoad: {
          title: '载重',
          type: 'string',
          visibleIf: {
            expand: (value: boolean) => value,
          },
        },
        isTrailer: {
          type: 'string',
          title: '是否挂靠',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'Whether' },
            visibleIf: {
              expand: (value: boolean) => value,
            },
          },
        },
        isDriverLicenseExpire: {
          type: 'string',
          title: '行驶证到期状态',
          enum: [
            { label: '正常', value: 0 },
            { label: '冻结', value: 1 },
            { label: '废弃', value: 2 },
          ],
          default: '',
          ui: {
            widget: 'select',
            visibleIf: {
              expand: (value: boolean) => value,
            },
          },
        },
        isRoadTransportExpire: {
          type: 'string',
          title: '驾驶证到期状态',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'CarColor' },
            visibleIf: {
              expand: (value: boolean) => value,
            },
          },
        },
      },
    };
    this.ui = { '*': { spanLabelFixed: 130, grid: { span: 8, gutter: 4 }, enter: () => this.st.load() } };
  }

  initST() {
    this.columns = [
      // { title: '', type: 'checkbox', className: 'text-center' },
      { title: '车牌号', className: 'text-center', index: 'carNo' },
      { title: '车牌颜色', className: 'text-center', index: 'carNoColor' },
      { title: '车型-车长-载重', className: 'text-center', render: 'carLength' },
      { title: '运营状态', className: 'text-center', index: 'effectiveDateStr',
      type: 'badge',
      badge: {
        正常: { text: '空闲', color: 'success' },
        冻结: { text: '运输中', color: 'warning' },
        废弃: { text: '废弃', color: 'default' },
      },
     },
      {
        title: '行驶证到期状态',
        className: 'text-center',
        index: 'isDriverLicenseExpire',
        type: 'badge',
        badge: {
          false: { text: '否', color: 'success' },
          true: { text: '是', color: 'warning' },
        },
      },
      {
        title: '道运证到期状态',
        className: 'text-center',
        index: 'isRoadTransportExpire',
        type: 'badge',
        badge: {
          false: { text: '否', color: 'success' },
          true: { text: '是', color: 'warning' },
        },
      },
      { title: '所有人', className: 'text-center', index: 'carOwner' },
      { title: '是否挂靠', className: 'text-center', index: 'isTrailer', },
      { title: '挂靠协议', className: 'text-center', index: 'carNo' },

      {
        title: '操作',
        width: '170px',
        className: 'text-center',
        buttons: [
          {
            text: '查看',
            click: (item) => {
              this.router.navigate(['./detail', item.id], { relativeTo: this.ar });
              // this.router.navigate(['./view', item.id], { relativeTo: this.ar, queryParams: { tenantId: item.tenantId } });
            },
          },
        ],
      },
    ];
  }
  daoyun(item: any) {
    this.router.navigate(['./view', item.id], { relativeTo: this.ar });
  }
  expandToggle() {
    this._$expand = !this._$expand;
    this.sf?.setValue('/expand', this._$expand);
  }
  creat() {
    this.router.navigate(['./new',], { relativeTo: this.ar });
  }
  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
  }
}
