import { Component, OnInit, ViewChild } from '@angular/core';
import { STChange, STColumn, STColumnBadge, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { SupplyManagementService } from '../../services/supply-management.service';
import { SupplyManagementAddDriversComponent } from '../add-drivers/add-drivers.component';
import { CarAddmodalComponent } from '../addmodal/addmodal.component';


const BADGE: STColumnBadge = {
  1: { text: '空闲', color: 'success' },
  2: { text: '未实名', color: 'error' },
  3: { text: '在途', color: 'warning' },
};

@Component({
  selector: 'app-supply-management-assigned-car',
  templateUrl: './assigned-car.component.html',
})
export class SupplyManagementVehicleAssignedCarComponent implements OnInit {
  record: any = {};
  i: any;
  schema: SFSchema = {};
  ui: SFUISchema = {};
  columns: STColumn[] = [];

  @ViewChild('st') st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  status: string = 'anew';
  type: any;
  url = ''; // 请求的api地址
  params: any = {}; // 传进来的参数
  cardBADGE: STColumnBadge | any = {
    0: { text: '空闲', color: 'success' },
    1: { text: '在途', color: 'warning' },
    2: { text: '未认证', color: 'error' },
  };
  selectedRows: any = null; // 已选行
  constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    public service: SupplyManagementService,
    private modalHelper: ModalHelper,
  ) {
    this.initSF();
    this.initSt();
  }

  /**
* 查询参数
*/
  get reqParams() {
    return {
      ...this.sf?.value,
    };
  }


  /**
* 初始化查询表单
*/
  initSF() {
    this.schema = {
      properties: {
        _$expand: { type: 'boolean', ui: { hidden: true } },
        nameOrPhone: {
          type: 'string',
          title: '',
          ui: {
            placeholder: '请输入司机姓名/手机号'
          }
        },
        carNo: {
          type: 'string',
          title: '',
          ui: {
            placeholder: '请输入车牌号'
          }
        },
      },
      type: 'object',
    };
    this.ui = { '*': { spanLabelFixed: 10, grid: { span: 8, gutter: 1 } } };
  }

  /**
   * 初始化数据列表
   */
  initSt() {
    this.columns = [
      { width: 50, type: 'radio', className: 'text-center' },
      { title: '司机姓名', width: 120, index: 'name', className: 'text-center' },
      { title: '手机号', index: 'telephone', width: 200, className: 'text-center' },
      { title: '车队长', render: 'carCaptain', className: 'text-center' },
      { title: '指定车辆', width: 300, render: 'carId', className: 'text-center' },
      { title: '状态', render: 'driverStatus', className: 'text-center', type: 'badge', badge: BADGE },
    ];
  }

  ngOnInit(): void {
    console.log(this.type)
  }

  dataProcess(data: STData[]): STData[] {
    return data.map((i, index) => {
      i.carId = '';
      i.disabled = i.carStatus === '1';
      const defaultCar = i?.userCarLicenseDesensitizationVOList?.find((item: any) => item.isDefault);
      if (defaultCar) {
        i.carId = defaultCar?.carId;
      }
      return i;
    });
  }

  save(): void {
    if (this.selectedRows) {
      const { carId, appUserId: driverId, captainAppUserId: carCaptainId } = this.selectedRows;
      const params: any = { carId, driverId, carCaptainId };
      this.service.request(this.url, { ...params, ...this.params }).subscribe((res: any) => {
        if (res) {
          this.modal.close(res);
        }
      })

    }
  }

  changeSt(e: STChange): void {
    if (e?.type === 'loaded') this.selectedRows = null;
    if (e?.type === 'radio') this.selectedRows = e?.radio;

  }
  /**
   * 添加司机
   * @param item
   */
  addDriver() {
    this.modalHelper.create(CarAddmodalComponent, {}, { size: 900, modalOptions: { nzMaskClosable: false } }).subscribe((res) => {
      if (res) this.st.reload();
    });
  }

  /**
   * 设置车队长
   */
  setCarCaptain(item: any) {
    this.modalHelper.create(SupplyManagementAddDriversComponent, { dirvierInfo: item }, {
      size: 900,
      modalOptions: { nzMaskClosable: false, nzTitle: '设置' }
    }
    ).subscribe((res) => {
      if (res) {
        item.captainName = res?.name;
        item.captainPhone = res?.mobile;
        item.captainAppUserId = res?.appUserId;
      }
    });
  }

  close(): void {
    this.modal.destroy();
  }

  reset() {
    this.sf.reset();
    this.st.load(1);
  }
}
