import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { STColumn, STComponent } from '@delon/abc/st';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { UsermanageService } from '../../../../services/usercenter.service';
import {
  SFComponent,
  SFSchema,
  SFDateWidgetSchema,
  SFUISchema,
  SFUploadWidgetSchema,
  SFSelectWidgetSchema,
  SFTextWidgetSchema,
  SFTagWidgetSchema
} from '@delon/form';
import { Observable, Observer } from 'rxjs';
import { NzModalService } from 'ng-zorro-antd/modal';
import { apiConf } from '@conf/api.conf';
import { ImageViewComponent } from 'src/app/shared/components/imagelist';
import { NzCascaderOption } from 'ng-zorro-antd/cascader';
@Component({
  selector: 'app-Freight-components-EnterpriseAudit-view',
  templateUrl: './view.component.html',
  styleUrls: ['./view.component.less']
})
export class FreightComponentsEnterpriseAuditViewComponent implements OnInit {
  @ViewChild('approvedModal', { static: false })
  approvedModal!: any;
  @ViewChild('redectModal', { static: false })
  redectModal!: any;

  approvalOpinion = '';
  networkTransporter = null;

  detailData: any = this.initDefalutData();
  tempalateData = { ...this.detailData };

  statusE: any = {
    10: '待审核',
    20: '审核通过',
    30: '审核失败'
  };

  isEdit = false;

  uploadURl = apiConf.waterFileUpload;
  disabledUpload = false;

  values: string[] | null = null;
  constructor(private nzModalService: NzModalService, public service: UsermanageService, private route: ActivatedRoute) {}

  ngOnInit() {
    this.initData();
    // this.launchSign();
  }

  initData() {
    this.service
      .request(this.service.$api_get_freight_detail, {
        id: this.route.snapshot.params.id
      })
      .subscribe(res => {
        if (res) {
          this.detailData = res;
          this.tempalateData = { ...this.detailData };
        }
      });
  }

  auditPass() {
    this.networkTransporter = null;
    this.nzModalService.create({
      nzTitle: '审核通过',
      nzContent: this.approvedModal,
      nzOnOk: () => {
        if (!this.networkTransporter) {
          return false;
        }
        this.auditEnterprise(20);
        return;
      }
    });
  }
  auditNo() {
    this.approvalOpinion = '';
    this.nzModalService.create({
      nzTitle: '审核驳回',
      nzContent: this.redectModal,
      nzOnOk: () => {
        if (!this.approvalOpinion) {
          return false;
        }
        this.auditEnterprise(30);
        return;
      }
    });
  }

  private auditEnterprise(status: number) {
    this.service
      .request(this.service.$api_audit_freight, {
        approvalStatus: status,
        id: this.detailData.id,
        approvalOpinion: this.approvalOpinion,
        networkTransporter: this.networkTransporter
      })
      .subscribe(res => {
        if (res) {
          this.service.msgSrv.success(status === 20 ? '审核通过' : '驳回成功');
        }
        this.initData();
      });
  }

  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);
      }
    });
  }

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

  changeTime(event: any) {
    console.log(event);
  }

  changeCascader(event: any) {
    console.log(event);
  }

  /*
   * 根据地区code查询地区详情
   * code：请求参数
   * type：参数 name：获取省市区名称，fullcode：获取省市区code
   * num：参数 1：第一个地区选择，2：第二个地区选择
   */
  getRegionDetailByCode(regionCode: any) {
    // 根据地区code查询地区详情
    return this.service.request(this.service.$api_get_region_by_code, { regionCode });
  }

  loadData = (node: any, index: number) => {
    return new Promise(resolve => {
      this.getRegionDetailByCode(node?.regionCode || '').subscribe(
        res => {
          node.children = res.map((item: any) => ({ ...item, isLeaf: index === 1, value: item.regionCode, label: item.name }));
        },
        _ => {},
        () => {
          resolve(node);
        }
      );
    });
  };

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

  save() {
    this.isEdit = false;
  }

  ratify() {
    this.isEdit = true;
  }

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

  private initDefalutData() {
    return {
      adminUserInfo: {
        name: '',
        mobile: '',
        certificatePhotoFrontWatermark: '',
        certificatePhotoBackWatermark: ''
      },
      legalPersonIdentityVO: {
        certificateNumber: ''
      },
      enterpriseName: '',
      enterpriseType: '',
      unifiedSocialCreditCode: '',
      registrationCapital: null,
      enterpriseRegistrationTime: '',

      driverLicenseSigningOrg: '',
      carDistinguishCode: '',
      carLoad: '',
      curbWeight: '',
      roadTransportNo: '',
      roadTransportLicenceNo: '',
      carOwner: '',
      isTrailer: null,
      useNature: null,
      driverLicenseRegisterTime: null,
      driverLicenseGetTime: null,
      driverLicenseEndTime: null,
      roadTransportStartTime: null,
      roadTransportEndTime: null,
      carFrontPhotoWatermark: ''
    };
  }
}
