import { Component, OnInit, ViewChild } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { MainService } from "../../services/main.service";
import { OverlayScrollbarsComponent } from "overlayscrollbars-ngx";
import { NzMessageService } from "ng-zorro-antd";
import { ToolService } from 'src/app/services/tool.service';
import chunk from 'lodash-es/chunk'
@Component({
  selector: "app-inspection-execute-detail",
  templateUrl: "./inspection-execute-detail.component.html",
  styleUrls: ["./inspection-execute-detail.component.less"],
})
export class InspectionExecuteDetailComponent implements OnInit {
  @ViewChild("osComponentRef1", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef1: OverlayScrollbarsComponent;
  @ViewChild("osComponentRef2", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef2: OverlayScrollbarsComponent;
  @ViewChild("osComponentRef4", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef4: OverlayScrollbarsComponent;
  constructor(
    private message: NzMessageService,
    private route: ActivatedRoute,
    private mainService: MainService,
    private tool: ToolService,
  ) {}
  maskFlag: any = false;
  id: number; //id
  orderInfo: any; //详情信息
  hosId;

  ngOnInit() {
    this.hosId = this.tool.getCurrentHospital().id;
    this.tabType = this.route.snapshot.params.type || 1;
    this.getInfo();
  }

  getValuex(value){
    return value.formItemConfigList ? value.formItemConfigList.map(v => v.valuex).toString() : value.valuex;
  }

  // 预览图片
  imgs = [];
  isPreview = false;
  initialViewIndex:number = 0;
  previewImageHandler(data = [], index = 0) {
    this.initialViewIndex = index;
    console.log(index)
    this.isPreview = false;
    data = data || [];
    this.imgs = data.map((v) => location.origin + '/file' + v.relativeFilePath);
    setTimeout(() => {
      this.isPreview = true;
    }, 0)
  }

  // 获取详情
  hsLoading:boolean = false;
  getInfo() {
    this.id = +this.route.snapshot.paramMap.get("id");
    this.maskFlag = this.message.loading("正在加载中..", {
      nzDuration: 0,
    }).messageId;
    this.mainService
      .getFetchData("simple/data", "inspectionTask", this.id)
      .subscribe((data) => {
        this.orderInfo = data.data || {};
        this.list = this.orderInfo.logList || [];
        this.message.remove(this.maskFlag);
        this.maskFlag = false;
      });
  }

  // 获取巡检信息
  valueList:any[] = [];
  getValue() {
    this.id = +this.route.snapshot.paramMap.get("id");
    this.maskFlag = this.message.loading("正在加载中..", {
      nzDuration: 0,
    }).messageId;
    let postData = {
      idx: 0,
      sum: 9999,
      inspectionFormValues: {
        taskId: this.id,
      }
    }
    this.mainService
      .getFetchDataList("simple/data", "inspectionFormValues", postData)
      .subscribe((data) => {
        let valueList = data.list || [];
        let imgFlag = valueList.some(v => v.inspectionFormItemDTO.type.value == 7);

        if(imgFlag){
          valueList.forEach(v => {
            if(v.inspectionFormItemDTO.type.value == 7){
              v.valuex = [];
            }
          })
        }

        this.valueList = chunk(valueList, 3);
        this.message.remove(this.maskFlag);
        this.maskFlag = false;

        if(imgFlag){
          this.getImgList();
        }
      });
  }

  // 获取巡检图片
  getImgList(){
    this.mainService
      .getPreviewImage('inspection', this.id)
      .subscribe((res:any) => {
        res.data = res.data || [];
        res.data.forEach(v => {
          v.previewUrl = location.origin + "/file" + v.relativeFilePath;
          v.thumbFilePath = location.origin + "/file" + v.thumbFilePath;
        })
        let imgList = res.data || [];
        this.valueList.forEach(v => {
          v.forEach(vv => {
            imgList.forEach(item => {
              if(vv.itemId == item.recordId){
                if(vv.valuex && vv.valuex.length){
                  vv.valuex.push(item)
                }else{
                  vv.valuex = [item];
                }
              }
            })
          })
        })
        console.log(this.valueList);
      });
  }

  // 获取列表
  pageIndex: number = 1;//表格当前页码
  pageSize: number = 10;//表格每页展示条数
  total: number = 0;//表格总数据量
  list: any = [];
  // 切换tab栏
  tabType: number = 1; //tab栏
  checkTab(type) {
    this.tabType = type;
    if(type === 2){
      this.getValue();
    }
  }

  // 关闭弹框
  close() {
    history.go(-1);
  }

}
