import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFUISchema } from '@delon/form';
import { DatePipe, _HttpClient } from '@delon/theme';
import { differenceInCalendarDays } from 'date-fns';
import { DataService } from '../../../services/data.service';

@Component({
  selector: 'app-datatable-driver',
  templateUrl: './driver.component.html',
  styleUrls: ['./driver.component.less'],
  providers: [DatePipe]
})
export class DatatableDriverComponent implements OnInit {
  @ViewChild('st', { static: false }) st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  _$expand = false;
  type = 1;
  mode = 'year';
  date: any = null;
  defineDate = [];
  queryTime: any = [new Date().getFullYear()]
  dateFormat = 'yyyy';
  today = new Date();
  ui: SFUISchema = {};
  schema: SFSchema = {};

  columns: STColumn[] = [
    { title: '司机姓名', index: 'driverName', className: 'text-center' },
    { title: '手机号', index: 'driverPhone', className: 'text-center' },
    { title: '注册时间', index: 'driverRegisterTime', className: 'text-center' },
    {
      title: '司机状态', index: 'driverStatus', className: 'text-center', type: 'enum', enum: {
        0: '未激活',
        1: '活跃',
        2: '沉默',
        3: '流失',
      }
    },
    { title: '运单数', index: 'wbAllCount', className: 'text-center' },
    { title: '待接单运单', index: 'wbWaitCount', className: 'text-center' },
    { title: '已完成运单', index: 'wbOverCount', className: 'text-center' },
    { title: '运费金额', index: 'wbAllAmount',  className: 'text-right', width: '100px', type: 'widget', widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.wbAllAmount }) } },
    { title: '已收运费金额', index: 'wbGetAmount', className: 'text-right', width: '100px', type: 'widget', widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.wbGetAmount }) } },
    { title: '待收运费金额', index: 'wbWaitAmount', className: 'text-right', width: '100px', type: 'widget', widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.wbWaitAmount }) } },
  ];
  isLoading: boolean = false;
  /**
   * 查询参数
   */
  get reqParams() {
    if (this.mode === 'year') {
      this.type = 1
    } else if (this.mode === 'month') {
      this.type = 2
    } else if (this.mode === 'date') {
      this.type = 3
    } else {
      this.type = 4
    }
    let params: any = {
      queryTime: this.queryTime,
      ...this.sf?.value
    };

    delete params._$expand;
    return { ...params };
  }
  constructor(public service: DataService, private datePipe: DatePipe) { }
  ngOnInit(): void {
    this.initSF();
  }
  /**
    * 初始化查询表单
    */
  initSF() {
    this.schema = {
      properties: {
        _$expand: { type: 'boolean', ui: { hidden: true } },
        driverName: {
          type: 'string',
          title: '司机姓名',
          ui: {
            placeholder: '请输入',
          }
        },
        driverPhone: {
          type: 'string',
          title: '手机号',
          ui: {
            placeholder: '请选择',
          }
        },
        driverStatus: {
          type: 'string',
          title: '司机状态',
          ui: {
            widget: 'select',
            placeholder: '请选择',
          },
          enum: [
            { label: '未激活', value: 0 },
            { label: '活跃', value: 1 },
            { label: '沉默', value: 2 },
            { label: '流失', value: 3 },
          ]
        },
        driverRegisterTime: {
          title: '注册时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to', type: 'date', format: 'yyyy-MM-dd', visibleIf: {
              _$expand: (value: boolean) => value,
            },
          } as SFDateWidgetSchema,
        }
      },
      type: 'object'
    };
    this.ui = { '*': { spanLabelFixed: 110, grid: { span: 8, gutter: 4 } } };
  }
  changeData() {
    if (this.mode === 'year') {
      this.dateFormat = 'yyyy'
    } else if (this.mode === 'month') {
      this.dateFormat = 'yyyy-MM'
    }
  }
  onChange(result: any) {
    if(result === null) {
      return
    }
    // if (this.mode === 'year') {
    //   this.queryTime = this.datePipe.transform(this.date, 'yyyy')
    // } else if (this.mode === 'month') {
    //   this.queryTime = this.datePipe.transform(this.date, 'yyyy-MM')
    // }

    if(this.mode === 'year') {
      this.queryTime = [this.datePipe.transform(this.date, 'yyyy')]
    } else if(this.mode === 'month') {
      this.queryTime = [this.datePipe.transform(this.date, 'yyyy-MM')]
    } else if(this.mode === 'date') {
      this.queryTime = [this.datePipe.transform(this.date, 'yyyy-MM-dd')]
    } else{
      this.queryTime = [this.datePipe.transform(this.defineDate[0], 'yyyy-MM-dd'), this.datePipe.transform(this.defineDate[1], 'yyyy-MM-dd')]
    }

    this.st.reload({ ...this.reqParams });
  }
  disabledDate = (current: Date): boolean =>
    // Can not select days before today and today
    differenceInCalendarDays(current, this.today) > 0;

  export() {
    // this.service.downloadFile(this.service.$api_exportUploadBill, this.sf.value, {});
  }
  search() {
    this.st?.load(1)

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

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