import {
  uploadFile
} from "../../utils/upload"
Page({
  data: {
    avatar: "https://6875-huihenduo-2gx127w7f837b584-1255802371.tcb.qcloud.la/miniapp-static/avatar-icon.jpg?sign=f5c66c94d189436f82353eb48cb01f08&t=1634538864",
    cameraIcon: "https://6875-huihenduo-2gx127w7f837b584-1255802371.tcb.qcloud.la/miniapp-static/camera-icon.png?sign=11a65871a2800cd04ecaa8991687fccd&t=1634607415",
    userName: "",
    phone: "",
    shipName: "",
    shipMmsi: "",
    authModal: false,
    modalText: "位置"
  },
  openSetting() {
    this.setData({
      authModal: false,
    })
    wx.openSetting({
      complete: e => {
        console.log(e)
        if (e.authSetting["scope.writePhotosAlbum"]) {
          if (wx.getStorageSync('cacheImage')) {
            wx.saveImageToPhotosAlbum({
              filePath: wx.getStorageSync('cacheImage'),
              success: e => {
                wx.showToast({
                  title: '保存成功!',
                })
                wx.removeStorageSync('cacheImage')
              },
              complete: e => {
                console.log(e)
              }
            })
          }

        }
      }
    })
  },
  takePhoto() {
    wx.getLocation({
      success: e => {
        let {
          latitude,
          longitude
        } = e
        console.log("获取定位成功!", e)
        this.data.latitude = latitude
        this.data.longitude = longitude
        wx.setStorageSync('latitude', latitude)
        wx.setStorageSync('longitude', longitude)
        wx.chooseMedia({
          mediaType: ["image"],
          sourceType: ["camera"],
          success: e => {
            console.log("获取媒体成功!", e)
            let src = e.tempFiles[0].tempFilePath
            if (e.type == "video") {
              wx.showLoading({
                title: '正在压缩...',
              })
              wx.compressVideo({
                src,
                quality: "high",
                bitrate: "",
                fps: "",
                resolution: "",
                success: async e => {
                  if (wx.getStorageSync('userName')) {
                    wx.showLoading({
                      title: '正在上传...',
                    })
                    let res = await uploadFile(e.tempFilePath, {
                      type: 4,
                      userId: wx.getStorageSync('userId'),
                      location: `${this.data.longitude},${this.data.latitude}`
                    })
                    if (res.status == 0) {
                      console.log(res)
                      wx.showToast({
                        title: res.msg
                      })
                      wx.navigateTo({
                        url: '/pages/takePhoto/success/success',
                      })
                    } else {
                      wx.showToast({
                        title: res.msg
                      })
                    }
                  } else {
                    // 新用户视频
                    wx.hideLoading({
                      success: (res) => {},
                    })
                    console.log("新用户视频", e)
                    wx.setStorageSync('type', 2)
                    wx.setStorageSync('file', e.tempFilePath)
                    wx.navigateTo({
                      url: `/pages/cachePage/cachePage`,
                    })
                  }

                },
                fail: e => {
                  console.log(e)
                }
              })
            } else {
              wx.compressImage({
                src,
                quality: 80, // 压缩质量
                success: async e => {
                  console.log("图片压缩成功!", e)
                  wx.hideLoading({
                    success: (res) => {},
                  })
                  if (wx.getStorageSync('userName')) {
                    wx.showLoading({
                      title: '正在上传...',
                    })
                    let res = await uploadFile(e.tempFilePath, {
                      type: 3,
                      userId: wx.getStorageSync('userId'),
                      location: `${this.data.longitude},${this.data.latitude}`
                    })
                    console.log("上传结束", res)
                    if (res.status == 0) {
                      wx.showToast({
                        title: res.msg
                      })
                      wx.setStorageSync('shareImageUrl', res.result.viewUrl)
                      console.log(wx.getStorageSync('shareImageUrl'))
                      wx.downloadFile({
                        url: res.result.viewUrl,
                        success: e => {
                          console.log("下载调用!", e)
                          wx.setStorageSync('cacheImage', e.tempFilePath)
                          wx.saveImageToPhotosAlbum({
                            filePath: e.tempFilePath,
                            success: e => {
                              if (e.errMsg == "saveImageToPhotosAlbum:ok") {
                                wx.showToast({
                                  title: '保存成功!',
                                })
                                wx.removeStorageSync('cacheImage')
                              }
                            },
                            fail: e => {
                              console.log("失败4", e)
                              this.setData({
                                authModal: true,
                                modalText: "文件存储"
                              })
                            }
                          })
                          wx.navigateTo({
                            url: '/pages/takePhoto/success/success',
                          })
                        },
                        fail: e => {
                          console.log("失败3", e)
                        }
                      })


                    } else {
                      wx.showToast({
                        title: res.msg
                      })
                    }
                  } else {
                    // 新用户图片
                    console.log("新用户图片", e)
                    wx.setStorageSync('type', 1)
                    wx.setStorageSync('file', e.tempFilePath)
                    wx.navigateTo({
                      url: `/pages/cachePage/cachePage`,
                    })
                  }
                },
                fail: e => {
                  console.log("失败2", e)
                }
              })
            }
          },
          fail: e => {
            console.log("失败1", e)
          }
        })
      },
      fail: e => {
        this.setData({
          authModal: true,
          modalText: "位置信息"
        })
      }
    })

  },
  onLoad() {
    let userName = wx.getStorageSync('userName')
    let phone = wx.getStorageSync('phone')
    this.setData({
      userName,
      phone
    })
  }
})