/*
 * @Description  :
 * @Version      : 1.0
 * @Author       : Shiming
 * @Date         : 2021-12-14 15:02:52
 * @LastEditors  : Shiming
 * @LastEditTime : 2022-01-18 17:20:58
 * @FilePath     : \\tms-obc-web\\src\\app\\routes\\order-management\\modal\\bulk\\confir-receipt\\confir-receipt.component.ts
 * Copyright (C) 2022 huzhenhong. All rights reserved.
 */
import { Component, OnInit } from '@angular/core';
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-confir-receipt',
  templateUrl: './confir-receipt.component.html',
  styleUrls: ['./confir-receipt.component.less']
})
export class ConfirReceiptComponent implements OnInit {
  i: any;
  Status: any;
  detailList: any;
  data: any = {};
  driverList: any;
  payeeList: any;
  carList: any;
  listImagUrls: any[] = [];
  previewImage1 = '';
  previewVisible1 = false;
  constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    public http: _HttpClient,
    public service: OrderManagementService
  ) {}

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

  save(): void {
    // 大宗
    console.log(this.listImagUrls);
    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
    };
    console.log(params);
    this.service.request(this.service.$api_get_signBulkOrder, params).subscribe((res: any) => {
      console.log(res.success);
      if (res) {
        this.service.msgSrv.success('确认签收成功！');
        this.modal.destroy(true);
      }
    });
  }
  initData() {
    // 大宗
    this.service.request(this.service.$api_get_getBulkSignForDetail, { id: this.i?.id }).subscribe((res: any) => {
      console.log(res);
      this.detailList = res;
      let arr: any = [];
      res.filePathList.forEach((element: any, index: any) => {
        console.log(index);
        arr.push({
          url: element,
          status: 'done',
          uid: index
        });
      });
      this.listImagUrls = arr;
    });
  }
  close(): void {
    this.modal.destroy(true);
  }
  userAction() {}
  handlePreview1 = async (file: NzUploadFile) => {
    if (!file.url && !file.preview) {
      file.preview = await getBase64(file.originFileObj!);
    }
    this.previewImage1 = file.url || file.preview;
    this.previewVisible1 = true;
  };
  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;
    }
  }
  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();
    });
  };
}
