import { Component, OnInit, ViewChild } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { cacheConf } from '@conf/cache.conf';
import { STColumn, STComponent, STChange, STData } from '@delon/abc/st';
import { SFUISchema, SFSchema, SFComponent } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { EACacheService } from '@shared';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { SupplyManagementService } from '../../services/supply-management.service';
import { CarAddDriverComponent } from '../add-driver/add-driver.component';

@Component({
  selector: 'app-car-addmodal',
  templateUrl: './addmodal.component.html',
  styleUrls: ['./addmodal.component.less']
})
export class CarAddmodalComponent implements OnInit {
  @ViewChild('st', { static: false }) st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  _$expand = false;
  ui!: SFUISchema;
  schema!: SFSchema;
  columns!: STColumn[];
  tableData: any = [];
  companyData: any = {};
  flag = false
  isLoading: boolean = false;
  constructor(
    private modal: NzModalRef,
    private eaCacheSrv: EACacheService,
    public service: SupplyManagementService, private router: Router, private ar: ActivatedRoute,
    private modalHelper: ModalHelper
  ) { }

  /**
    * 查询字段个数
    */
  get queryFieldCount(): number {
    return Object.keys(this.schema?.properties || {}).length;
  }

  /**
   * 查询参数
   */
  get reqParams() {
    const params = Object.assign({}, this.sf?.value || {});
    delete params._$expand;
    return { ...params };
  }

  /**
   * 选中行
   */
  get selectedRows() {
    return this.st?.list.filter((item: any) => item.checked) || [];
  }

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

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
    this.isLoading = true
  }
  /**
   * 程序初始化入口
   */
  ngOnInit() {
    this.companyData = this.eaCacheSrv.get(cacheConf.env)
    this.initSF();
    this.initST()
    this.initData()
  }

  /**
   * 初始化查询表单
   */
  initSF() {
    this.schema = {
      properties: {
        _$expand: { type: 'boolean', ui: { hidden: true } },
        mobile: { title: '司机手机号', type: 'string', maxLength: 11, ui: { showRequired: false, placeholder: '请输入司机手机号', } },
      },
    };
    this.ui = {
      '*': { spanLabelFixed: 120, grid: { span: 8, gutter: 4 }, enter: () => this.st?.load(1) },
      $time: { grid: { span: 24 } },
    };
  }

  /**
   * 初始化数据列表
   */
  initST() {
    this.columns = [
      { title: '司机头像', type: 'img', index: 'avatar', className: 'text-center' },
      { title: '司机姓名', index: 'name', className: 'text-center' },
      { title: '当前车辆', index: 'carNo', className: 'text-center' },
      { title: '车辆信息', render: 'carModel', className: 'text-center' },
      {
        title: '个人认证状态',
        render: 'certificationStatus',
        className: 'text-center',
      },
      { title: '驾驶证类型', index: 'driverType', className: 'text-center' },
      {
        title: '操作',
        width: '180px',
        className: 'text-center',
        buttons: [
          { text: '添加', click: (_record: any) => this.add(_record) },
        ],
      },
    ];
  }
  initData(flag?: any) {
    if (this.sf?.value.mobile) {
      this.service.request(this.service.$api_getCarCaptainByMobile, { mobile: this.sf?.value.mobile }).subscribe((res: any) => {
        this.flag = flag
        if (res.userId) {
          this.tableData = [res];
          setTimeout(() => {
            this.st.reload()
          })
        } else {
          this.tableData = []
        }
      })
    }
  }
  /**
   * 数据列表状态变化事件
   */
  change(change: STChange) {
    // console.log(change);
  }

  /**
   * 新增
   */
  add(item: any) {
    const params: any = {
      source: 1,
      appUserId: item.appUserId ? item.appUserId : '',
      mobile: item.mobile
    }
    this.service.request(this.service.$api_enterpriseVehicleSave, params).subscribe((res: any) => {
      if (res) {
        this.service.msgSrv.success('添加成功')
        this.modal.close(true)
      }
    })
  }
  addModal() {
    this.modalHelper.create(CarAddDriverComponent, {mobile: this.sf?.value.mobile}, { size: 900 }).subscribe((res) => {
      this.initData()
    });
    //this.router.navigate(['/car/add'])
  }
  goBack() {
    window.history.go(-1);
  }
  close(): void {
    this.modal.close(true);
  }
}
