/*
 * @Description  :
 * @Version      : 1.0
 * @Author       : Shiming
 * @Date         : 2021-12-15 13:17:42
 * @LastEditors  : Shiming
 * @LastEditTime : 2022-03-31 17:36:46
 * @FilePath     : \\tms-obc-web\\src\\app\\routes\\order-management\\modal\\vehicle\\confir-receipt\\confir-receipt.component.ts
 * Copyright (C) 2022 huzhenhong. All rights reserved.
 */
import { Component, OnInit } from '@angular/core';
import { STColumn } from '@delon/abc/st';
import { _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { NzUploadChangeParam, NzUploadFile } from 'ng-zorro-antd/upload';
import { Observable, Observer } from 'rxjs';
import { OrderManagementService } from '../../../services/order-management.service';
function getBase64(file: File): Promise<string | ArrayBuffer | null> {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
  });
}
@Component({
  selector: 'app-order-management-vehicle-confir-receipt',
  templateUrl: './confir-receipt.component.html',
  styleUrls: ['./confir-receipt.component.less']
})
export class VehicleConfirReceiptComponent implements OnInit {
  record: any = {};
  i: any;
  Status: any;
  costDetail: any; // 费用明细
  columns!: STColumn[];
  previewVisible1 = false;
  dataInfo: any;
  detailList: any;
  data: any = {};
  previewImage1 = '';
  listImagUrls: any[] = [];
  payeeList: any;
  driverList: any;
  carList: any;
  constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    public http: _HttpClient,
    public service: OrderManagementService
  ) {}

  ngOnInit(): void {
    this.initST();
    this.initData();
  }

  save(): void {
    let imgList: any = [];
    if (this.listImagUrls.length > 0) {
      this.listImagUrls.forEach((res: any) => {
        if (res.url) {
          imgList.push(res.url);
        }
      });
    }
    console.log(imgList);
    const params = {
      id: this.i?.id,
      filePathList: imgList
    };
    this.service.request(this.service.$api_get_signWholeOrder, params).subscribe((res: any) => {
      console.log(res);
      if (res) {
        this.service.msgSrv.success('确认签收成功！');
        this.modal.destroy(true);
      }
    });
  }
  handlePreview1 = async (file: NzUploadFile) => {
    if (!file.url && !file.preview) {
      file.preview = await getBase64(file.originFileObj!);
    }
    this.previewImage1 = file.url || file.preview;
    this.previewVisible1 = true;
  };
  close(): void {
    this.modal.destroy();
  }

  beforeUpload = (file: NzUploadFile, _fileList: NzUploadFile[]) => {
    return new Observable((observer: Observer<boolean>) => {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif' || file.type === 'image/bmp';
      if (!isJpgOrPng) {
        this.service.msgSrv.warning('只能上传图片!');
        observer.complete();
        return;
      }
      // tslint:disable-next-line: no-non-null-assertion
      const isLt2M = file.size! / 1024 / 1024 < 3;
      if (!isLt2M) {
        this.service.msgSrv.warning('图片大小超过3兆!');
        observer.complete();
        return;
      }
      observer.next(isJpgOrPng && isLt2M);
      observer.complete();
    });
  };
  initData() {
    // 大宗
    this.service.request(this.service.$api_get_getWholeSignForDetail, { id: this.i?.id }).subscribe((res: any) => {
      console.log(res);
      this.dataInfo = res;
      const cost: any = {};
      res.mybidDetailInfo.forEach((element: any) => {
        if (element.expenseName === '预付' || element.expenseCode === 'PRE') {
          cost.PRE = element.price;
        } else if (element.expenseName === '到付' || element.expenseCode === 'RECE') {
          cost.RECE = element.price;
        } else if (element.expenseName === '回单付' || element.expenseCode === 'BACK') {
          cost.BACK = element.price;
        } else if (element.expenseCode === 'ATT') {
          cost.surcharge = element.price;
        } else if (element.expenseCode === 'TOTAL') {
          cost.traiPrice = element.price;
        }
      });
      let arr: any = [];
      res.filePathList.forEach((element: any, index: any) => {
        console.log(index);
        arr.push({
          url: element,
          status: 'done',
          uid: index
        });
      });
      this.listImagUrls = arr;
      this.costDetail = [cost];
    });
  }

  initST() {
    this.columns = [
      {
        title: '预付',
        index: 'PRE',
        render:'PRE'
      },
      {
        title: '到付',
        index: 'RECE',
        render:'RECE'
      },
      // {
      //   title: '油卡',
      //   index: 'OIL'
      // },
      {
        title: '回单付',
        index: 'BACK',
        render:'BACK'
      },
      {
        title: '附加费',
        index: 'surcharge',
        render:'surcharge'
      },
      {
        title: '总运费',
        index: 'traiPrice',
        render:'traiPrice'
      }
    ];
  }

  handleChange1(info: NzUploadChangeParam): void {
    switch (info.file.status) {
      case 'uploading':
        break;
      case 'done':
        let fileList = [...info.fileList];
        // 2. Read from response and show file link
        console.log(fileList);
        fileList = fileList.map((file: any) => {
          if (file.response) {
            file.url = file.response.data.fullFilePath;
          }
          return file;
        });
        console.log(this.listImagUrls);
        break;
      case 'error':
        this.service.msgSrv.error('网络错误');
        break;
    }
  }
}
