const _my = require("../../../__antmove/api/index.js")(my);
const wx = _my;
// pages/wx_airport/problem_report/index.js
var upload_image = require("../../../utils/upload/upload_image.js");
var upload_video = require("../../../utils/upload/upload_video.js");
var {
  addProblemReport
} = require("../../../utils/api/xiaosha");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    adminId: null,
    anonymityFlag: true,
    //状态
    desc: "",
    //问题详情
    pictureVOList: [],
    //图片
    video: {} //视频
  },

  // 是否匿名
  onChange(event) {
    this.setData({
      anonymityFlag: event.detail
    });
  },
  //获得问题详情
  descFun(e) {
    this.setData({
      desc: e.detail.value
    });
  },
  //上报
  report() {
    let {
      anonymityFlag,
      desc,
      pictureVOList,
      video
    } = this.data;
    if (!desc) {
      wx.showToast({
        title: "请填写问题详情",
        icon: "none"
      });
      return;
    }
    let userInfo = wx.getStorageSync("userInfo");
    let form = {
      anonymityFlag,
      desc,
      pictureVOList,
      video,
      adminId: this.data.adminId || parseInt(userInfo.orgId),
      userId: userInfo.userId ? userInfo.userId : null
    };
    addProblemReport(form).then(res => {
      if (res.code == 200) {
        my.alert({
          title: "提示",
          content: "上报成功！",
          success: res => {
            if (userInfo.orgId) {
              wx.reLaunch({
                url: "/pages/wx_airport/rlxf_pass/rlxf_pass?orgId=" + userInfo.orgId
              });
            } else {
              wx.reLaunch({
                url: "/pages/wx_airport/rlxf_index/rlxf_index"
              });
            }
          }
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none"
        });
      }
    });
  },
  //选择照片
  ChooseImage() {
    const that = this;
    wx.chooseImage({
      count: 4,
      //默认9
      sizeType: ["original", "compressed"],
      //可以指定是原图还是压缩图，默认二者都有
      sourceType: ["album"],
      //从相册选择
      success: res => {
        const image = {
          url: res.tempFilePaths[0]
        };
        wx.showLoading({
          title: "上传中~"
        });
        upload_image(image).then(img => {
          let data = {
            url: img.url
          };
          that.setData({
            pictureVOList: that.data.pictureVOList.concat(data)
          });
          wx.hideLoading();
        });
      },
      fail(res) {
        wx.hideLoading();
        wx.showToast({
          title: "照片上传失败,请重试",
          icon: "none"
        });
      }
      // complete() {
      //     wx.hideLoading();
      // }
    });
  },

  //查看照片
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.pictureVOList,
      current: e.currentTarget.dataset.url
    });
  },
  //删除照片
  DelImg(e) {
    wx.showModal({
      title: "提示",
      content: "确定删除？",
      cancelText: "取消",
      confirmText: "确定",
      success: res => {
        if (res.confirm) {
          this.data.pictureVOList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            pictureVOList: this.data.pictureVOList
          });
        }
      }
    });
  },
  //选择视频
  ChooseVideo() {
    const that = this;
    wx.chooseVideo({
      sizeType: ["compressed"],
      sourceType: ["album", "camera"],
      //从相册选择
      maxDuration: 15,
      success: res => {
        wx.showLoading({
          title: "视频上传中",
          mask: true
        });
        const video = {
          url: res.tempFilePath
        };
        upload_video(video).then(video => {
          wx.showToast({
            title: "视频上传成功"
          });
          let data = {
            url: video.url
          };
          this.setData({
            video: data
          });
          wx.hideLoading();
        });
      }
      // complete() {
      //     wx.hideLoading();
      // }
    });
  },

  //删除视频
  DelVideo() {
    wx.showModal({
      title: "提示",
      content: "确定删除？",
      cancelText: "取消",
      confirmText: "确定",
      success: res => {
        if (res.confirm) {
          this.setData({
            video: {}
          });
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options) {
      this.setData({
        adminId: parseInt(options.adminId)
      });
    }
    this.setData({
      userInfo: wx.getStorageSync("userInfo")
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  antmoveAction: function () {
    //执行时动态赋值，请勿删除
  }
});