import {
  get,
  post
} from "./../http/http.js";
export const pathUrl = uni.getStorageSync('path');
export function photographTool(data, deptObj) {
  console.log(data, deptObj, 1111);
  let id_seimin;
  let asTypeValue_seimin;
  let gdStateValue_seimin;
  if (data.status != 200 && data.status !== undefined) {
    id_seimin = data.id;
    asTypeValue_seimin = data.type;
    gdStateValue_seimin = JSON.parse(data.model).gdstate2; //type改成gdstate
  } else {
    id_seimin = data.id;
    asTypeValue_seimin = data.taskType.associationType.value;
    gdStateValue_seimin = data.gdState.value;
  }
  //#ifdef H5
  uni.chooseImage({
    count: 1,
    sourceType: ['camera'],
    success: (chooseImageRes) => {
      console.log(chooseImageRes, pathUrl)
      const tempFilePaths = chooseImageRes.tempFilePaths;
      const tempFiles = chooseImageRes.tempFiles;
      let tp = tempFilePaths[0];
      uni.showLoading({
        mask: true,
        title: '上传中'
      });
      post('/workerOrder/findPatientCodeByWorkOrderId', {
        WorkOrderId: id_seimin
      }).then(result => {
        console.log(result)
        if (result.state == 200) {
          uni.getImageInfo({
            src: tp,
            success(res) {
              console.log('压缩前', res)
              let canvasWidth = res.width //图片原始长宽
              let canvasHeight = res.height
              let img = new Image()
              img.src = res.path
              let canvas = document.createElement('canvas');
              let ctx = canvas.getContext('2d')
              canvas.width = canvasWidth
              canvas.height = canvasHeight
              ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight)
              canvas.toBlob(function (fileSrc) {
                tp = window.URL.createObjectURL(fileSrc)
                console.log('压缩后', tp);
                uni.uploadFile({
                  url: pathUrl + '/common/common/uploadAttachment/inspectAndPatient/' + id_seimin + '/' + result.recordId,
                  filePath: tp,
                  name: 'file',
                  formData: {
                    'filename': tempFiles[0].name ? tempFiles[0].name.split('.')[0] + '.jpg' : tempFiles[
                      0].path.split(
                        /[\\/]/).reverse()[0].split('.')[0] + '.jpg'
                  },
                  success: (uploadFileRes) => {
                    console.log(uploadFileRes, 10086, asTypeValue_seimin);
                    if (asTypeValue_seimin === "patientTransport") {
                      // 转运，扫描患者腕带码
                      if (gdStateValue_seimin == 4) { //转运待到达
                        post('/workerOrder/handleTrans', {
                          "code": result.patientBarCode,
                          "ids": [id_seimin]
                        }).then(result1 => {
                          console.log('result1=====',result1);
                          uni.hideLoading();
                          if (result1.status == 200) {
                            uni.showToast({
                              title: '上传成功',
                              duration: 2000
                            });
                            // 小扫描
                            uni.navigateTo({
                              url: `../scanning_Result/scanning_Result?type=${asTypeValue_seimin
                                }&type1=${result1.type}&id=${id_seimin}&deptCode=${result.patientCode
                                }&patient=${result1.patient}&patientCode=${result1.patientCode
                                }&deptName=${result1.deptName}&bedNum=${result1.bedNum
                                }&status=${result1.status}&msg=${result1.msg
                                }&model=${encodeURIComponent(JSON.stringify(result1))}`,
                            });
                          } else {
                            uni.showToast({
                              icon: "none",
                              title: result1.msg || "接口获取数据失败！",
                            });
                          }
                        })
                      } else if (gdStateValue_seimin == 5) { //转运待送达
                        post('/workerOrder/handleTrans', {
                          "code": result.patientBarCode,
                          "ids": [id_seimin],
                          "type": deptObj.infoType,
                          "deptCode": deptObj.DEPTCode,
                        }).then(result1 => {
                          console.log(result1);
                          uni.hideLoading();
                          if (result1.status == 200) {
                            uni.showToast({
                              title: '上传成功',
                              duration: 2000
                            });
                            // 小扫描
                            uni.navigateTo({
                              url: `../scanning_Result/scanning_Result?type=${asTypeValue_seimin}&type1=${result1.type1}&infoData=${encodeURIComponent(JSON.stringify(result1.data))}&status=${result1.status}&msg=${result1.msg}&patient=${result1.patient}&patientCode=${result1.patientCode}&deptName=${result1.deptName}&bedNum=${result1.bedNum}&deptCode=${deptObj.DEPTCode}&dept=${deptObj.DEPT}&id=${id_seimin}&model=${encodeURIComponent(JSON.stringify(result1))}`
                            });
                          } else {
                            uni.showToast({
                              icon: "none",
                              title: result1.msg || "接口获取数据失败！",
                            });
                          }
                        })
                      }

                    } else if (asTypeValue_seimin === "inspect") {
                      // 陪检，扫描患者腕带码
                      if (gdStateValue_seimin == 4) { //陪检待到达
                        console.log('怎么了')
                        post('/workerOrder/handleIns', {
                          "code": result.patientBarCode,
                          "ids": [id_seimin]
                        }).then(result1 => {
                          console.log(result1);
                          uni.hideLoading();
                          if (result1.status == 200) {
                            uni.showToast({
                              title: '上传成功',
                              duration: 2000
                            });
                            // 小扫描
                            uni.navigateTo({
                              url: `../scanning_Result/scanning_Result?type=${asTypeValue_seimin
                                }&type1=${result1.type}&id=${id_seimin}&deptCode=${result.patientCode
                                }&patient=${result1.patient}&patientCode=${result1.patientCode
                                }&deptName=${result1.deptName}&bedNum=${result1.bedNum
                                }&status=${result1.status}&msg=${result1.msg
                                }&model=${encodeURIComponent(JSON.stringify(result1))}`,
                            });
                          } else {
                            uni.showToast({
                              icon: "none",
                              title: result1.msg || "接口获取数据失败！",
                            });
                          }
                        })
                      } else if (gdStateValue_seimin == 5 || gdStateValue_seimin == 8) { //陪检待送达，执行中
                        post('/workerOrder/handleIns', {
                          "code": result.patientBarCode,
                          "ids": [id_seimin],
                          "type": deptObj.infoType,
                          "deptCode": deptObj.DEPTCode,
                        }).then(result1 => {
                          console.log(result1);
                          uni.hideLoading();
                          if (result1.status == 200) {
                            uni.showToast({
                              title: '上传成功',
                              duration: 2000
                            });
                            // 小扫描
                            uni.navigateTo({
                              url: `../scanning_Result/scanning_Result?type=${asTypeValue_seimin}&type1=${result1.type1}&infoData=${encodeURIComponent(JSON.stringify(result1.data))}&status=${result1.status}&msg=${result1.msg}&patient=${result1.patient}&patientCode=${result1.patientCode}&deptName=${result1.deptName}&bedNum=${result1.bedNum}&deptCode=${deptObj.DEPTCode}&dept=${deptObj.DEPT}&id=${id_seimin}&model=${encodeURIComponent(JSON.stringify(result1))}`
                            });
                          } else {
                            uni.showToast({
                              icon: "none",
                              title: result1.msg || "接口获取数据失败！",
                            });
                          }
                        })
                      }
                    }
                  },
                  fail: err => {
                    console.error(err);
                    uni.hideLoading();
                    uni.showToast({
                      icon: 'none',
                      title: '上传失败',
                      duration: 2000
                    });
                  }
                });
              }, 'image/jpeg', 0.3)
            },
            fail: function () {
              uni.hideLoading();
              uni.showToast({
                icon: 'none',
                title: '上传失败',
                duration: 2000
              });
            }
          })
        } else {
          uni.hideLoading();
          uni.showToast({
            icon: "none",
            title: result.msg || "接口获取数据失败！",
          });
        }
      })
    }
  });
  //#endif
  // #ifdef APP-PLUS
  uni.chooseImage({
    count: 1,
    sourceType: ['camera'],
    sizeType: ['compressed'],
    success: (chooseImageRes) => {
      console.log(chooseImageRes, pathUrl)
      const tempFilePaths = chooseImageRes.tempFilePaths;
      const tempFiles = chooseImageRes.tempFiles;
      let tp = tempFilePaths[0];
      uni.showLoading({
        mask: true,
        title: '上传中'
      });
      post('/workerOrder/findPatientCodeByWorkOrderId', {
        WorkOrderId: id_seimin
      }).then(result => {
        console.log(result)
        if (result.state == 200) {
          uni.uploadFile({
            url: pathUrl + '/common/common/uploadAttachment/inspectAndPatient/' + id_seimin + '/' + result.recordId,
            filePath: tp,
            name: 'file',
            formData: {
              'filename': tempFiles[0].name ? tempFiles[0].name.split('.')[0] + '.jpg' : tempFiles[0].path
                .split(
                  /[\\/]/).reverse()[0].split('.')[0] + '.jpg'
            },
            success: (uploadFileRes) => {
              console.log(uploadFileRes, 10086, asTypeValue_seimin);
              if (asTypeValue_seimin === "patientTransport") {
                // 转运，扫描患者腕带码
                if (gdStateValue_seimin == 4) { //转运待到达
                  post('/workerOrder/handleTrans', {
                    "code": result.patientBarCode,
                    "ids": [id_seimin]
                  }).then(result1 => {
                    console.log(result1);
                    uni.hideLoading();
                    if (result1.status == 200) {
                      uni.showToast({
                        title: '上传成功',
                        duration: 2000
                      });
                      // 小扫描
                      uni.navigateTo({
                        url: `../scanning_Result/scanning_Result?type=${asTypeValue_seimin
                          }&type1=${result1.type}&id=${id_seimin}&deptCode=${result.patientCode
                          }&patient=${result1.patient}&patientCode=${result1.patientCode
                          }&deptName=${result1.deptName}&bedNum=${result1.bedNum
                          }&status=${result1.status}&msg=${result1.msg
                          }&model=${encodeURIComponent(JSON.stringify(result1))}`,
                      });
                    } else {
                      uni.showToast({
                        icon: "none",
                        title: result1.msg || "接口获取数据失败！",
                      });
                    }
                  })
                } else if (gdStateValue_seimin == 5) { //转运待送达
                  post('/workerOrder/handleTrans', {
                    "code": result.patientBarCode,
                    "ids": [id_seimin],
                    "type": deptObj.infoType,
                    "deptCode": deptObj.DEPTCode,
                  }).then(result1 => {
                    console.log(result1);
                    uni.hideLoading();
                    if (result1.status == 200) {
                      uni.showToast({
                        title: '上传成功',
                        duration: 2000
                      });
                      // 小扫描
                      uni.navigateTo({
                        url: `../scanning_Result/scanning_Result?type=${asTypeValue_seimin}&type1=${result1.type1}&infoData=${encodeURIComponent(JSON.stringify(result1.data))}&status=${result1.status}&msg=${result1.msg}&patient=${result1.patient}&patientCode=${result1.patientCode}&deptName=${result1.deptName}&bedNum=${result1.bedNum}&deptCode=${deptObj.DEPTCode}&dept=${deptObj.DEPT}&id=${id_seimin}&model=${encodeURIComponent(JSON.stringify(result1))}`
                      });
                    } else {
                      uni.showToast({
                        icon: "none",
                        title: result1.msg || "接口获取数据失败！",
                      });
                    }
                  })
                }

              } else if (asTypeValue_seimin === "inspect") {
                // 陪检，扫描患者腕带码
                if (gdStateValue_seimin == 4) { //陪检待到达
                  console.log('怎么了')
                  post('/workerOrder/handleIns', {
                    "code": result.patientBarCode,
                    "ids": [id_seimin]
                  }).then(result1 => {
                    console.log(result1);
                    uni.hideLoading();
                    if (result1.status == 200) {
                      uni.showToast({
                        title: '上传成功',
                        duration: 2000
                      });
                      // 小扫描
                      uni.navigateTo({
                        url: `../scanning_Result/scanning_Result?type=${asTypeValue_seimin
                          }&type1=${result1.type}&id=${id_seimin}&deptCode=${result.patientCode
                          }&patient=${result1.patient}&patientCode=${result1.patientCode
                          }&deptName=${result1.deptName}&bedNum=${result1.bedNum
                          }&status=${result1.status}&msg=${result1.msg
                          }&model=${encodeURIComponent(JSON.stringify(result1))}`,
                      });
                    } else {
                      uni.showToast({
                        icon: "none",
                        title: result1.msg || "接口获取数据失败！",
                      });
                    }
                  })
                } else if (gdStateValue_seimin == 5 || gdStateValue_seimin == 8) { //陪检待送达，执行中
                  post('/workerOrder/handleIns', {
                    "code": result.patientBarCode,
                    "ids": [id_seimin],
                    "type": deptObj.infoType,
                    "deptCode": deptObj.DEPTCode,
                  }).then(result1 => {
                    console.log(result1);
                    uni.hideLoading();
                    if (result1.status == 200) {
                      uni.showToast({
                        title: '上传成功',
                        duration: 2000
                      });
                      // 小扫描
                      uni.navigateTo({
                        url: `../scanning_Result/scanning_Result?type=${asTypeValue_seimin}&type1=${result1.type1}&infoData=${encodeURIComponent(JSON.stringify(result1.data))}&status=${result1.status}&msg=${result1.msg}&patient=${result1.patient}&patientCode=${result1.patientCode}&deptName=${result1.deptName}&bedNum=${result1.bedNum}&deptCode=${deptObj.DEPTCode}&dept=${deptObj.DEPT}&id=${id_seimin}&model=${encodeURIComponent(JSON.stringify(result1))}`
                      });
                    } else {
                      uni.showToast({
                        icon: "none",
                        title: result1.msg || "接口获取数据失败！",
                      });
                    }
                  })
                }
              }
            },
            fail: err => {
              console.error(err);
              uni.hideLoading();
              uni.showToast({
                icon: 'none',
                title: '上传失败',
                duration: 2000
              });
            }
          });
        } else {
          uni.hideLoading();
          uni.showToast({
            icon: "none",
            title: result.msg || "接口获取数据失败！",
          });
        }
      })
    }
  });
  // #endif
}
