import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { apiConf } from '@conf/api.conf';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFUISchema } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { NzUploadFile } from 'ng-zorro-antd/upload';
import { ImageViewComponent } from 'src/app/shared/components/imagelist';
import { VehicleService } from '../../../services/vehicle.service';
// import { VehicleComponentsListEditComponent } from '../edit/edit.component';
// import { VehicleImgViewComponent } from '../img-view/img-view.component';

@Component({
  selector: 'app-Vehicle-components-Audit-detail',
  templateUrl: './detail.component.html',
  styleUrls: ['./detail.component.less']
})
export class VehicleComponentsAuditDetailComponent implements OnInit {
  @ViewChild('st', { static: false }) st!: STComponent;
  @ViewChild('redectModal', { static: false }) redectModal!: any;
  columns!: STColumn[];
  detailData: any = this.initData();
  tempalateData = { ...this.detailData };

  isEdit = false;

  approvalOpinion = '';

  uploadURl = apiConf.waterFileUpload;
  disabledUpload = false;
  constructor(public service: VehicleService, private route: ActivatedRoute, private nzModalService: NzModalService) {}

  ngOnInit() {
    this.getDetailList();
    this.initST();
  }

  initST() {
    this.columns = [
      { title: '认证司机', index: 'name', width: 300, className: 'text-center' },
      { title: '司机手机号', index: 'mobile', width: 300, className: 'text-center' },
      { title: '是否挂靠', render: 'isSelf', width: 300, className: 'text-center' },
      { title: '录入人员', index: 'totalPrice', width: 300, className: 'text-center' },
      {
        title: '车主申明/挂靠协议',
        fixed: 'right',
        width: '200px',
        className: 'text-left',
        buttons: [
          {
            text: '查看协议'
            // click: _record => this.viewEvaluate(_record)
          },
          {
            text: '上传协议'
            // click: _record => this.updateEvaluate(_record)
          }
        ]
      }
    ];
  }

  getDetailList() {
    console.log(this.route.snapshot?.params?.id);
    const params = {
      id: this.route.snapshot?.params?.id
    };
    this.service.request(`${this.service.$api_get_operate_get}`, params).subscribe(res => {
      console.log(res);
      // this.detailData = res;
    });
  }

  approveDriver() {
    this.nzModalService.confirm({
      nzTitle: '审核通过',
      nzContent: `<p>车牌号：${this.detailData?.licenseNo}</p><p>是否确认通过审核`,
      nzOnOk: () => {
        this.adjuctUser(
          {
            auditStatus: 20
          },
          '审核成功'
        );
      }
    });
  }

  rejectedDriver() {
    this.approvalOpinion = '';
    this.nzModalService.create({
      nzTitle: '审核驳回',
      nzContent: this.redectModal,
      nzOnOk: () => {
        if (!this.approvalOpinion) {
          return false;
        }
        this.adjuctUser(
          {
            auditStatus: 30,
            approvalOpinion: this.approvalOpinion
          },
          '审核驳回成功'
        );
        return;
      }
    });
  }

  reset() {
    this.detailData = { ...this.tempalateData };
    this.isEdit = false;
  }

  save() {
    this.isEdit = false;
  }

  ratify() {
    this.isEdit = true;
  }

  changeUpload({ file, fileList, type }: any, key: string) {
    console.log({ file, fileList, type });
    if (type === 'success') {
      this.detailData[key] = file.response.data.fullFileWatermarkPath;
    }
  }

  goBack() {
    window.history.go(-1);
  }

  showImg(url: any) {
    const params = {
      imgList: [url],
      index: 0
    };
    this.nzModalService.create({ nzContent: ImageViewComponent, nzComponentParams: { params } });
  }

  deleteImg(key: string) {
    this.nzModalService.warning({
      nzTitle: '是否确认删除该图片',
      nzOnOk: () => {
        this.disabledUpload = true;
        this.detailData[key] = '';
        setTimeout(() => {
          this.disabledUpload = false;
        }, 100);
      }
    });
  }

  private adjuctUser(params: any, msg: string) {
    this.service.request(this.service.$api_get_operate_get, { ...params }).subscribe(res => {
      if (res) {
        this.service.msgSrv.success(msg);
      }
      this.getDetailList();
    });
  }

  private initData() {
    return {
      carNo: '',
      carNoColor: '',
      carModel: '',
      carLength: '',
      archivesNo: '',
      driverLicenseSigningOrg: '',
      carDistinguishCode: '',
      carLoad: '',
      curbWeight: '',
      roadTransportNo: '',
      roadTransportLicenceNo: '',
      carOwner: '',
      isTrailer: null,
      useNature: null,
      driverLicenseRegisterTime: null,
      driverLicenseGetTime: null,
      driverLicenseEndTime: null,
      roadTransportStartTime: null,
      roadTransportEndTime: null,
      carFrontPhotoWatermark: ''
    };
  }
}
