import common from '../../utils/util';
import main from '../../utils/main';
import constant from '../../utils/constant';

const app = getApp();

Page({
  data: {
    Feedback:constant.arrFeedback,
    ImageArr:[],
    ImagePath: app.globalData.uploadImageUrl,
  },
  onLoad: function (options) {
    var that = this;
    var feedbackType=0;
    if (options.type){
      that.data.Feedback[0].CSS="";
      that.data.Feedback[options.type].CSS="Selected";
    }

    that.setData({
      Containnerheight: main.getWindowHeight(),
      IsShow:app.globalData.userInfo.IsShow,
      Feedback:that.data.Feedback,
    });
    main.checkGenerating();
  },
  bindKeyInput:function(e){
    var that=this;
    that.setData({
      InputValue:e.detail.value,
    });
  },
  bindKeyInput2:function(e){
    var that=this;
    that.setData({
      InputValue2:e.detail.value,
    });
  },
  saveFeedback:function(){
    var that=this;
    if (!that.data.InputValue){
      wx.showToast({
        title: '请填写内容',
      });
    }
    else if (that.data.InputValue.length<5){
      wx.showToast({
        title: '请多写点内容',
      });
    }
    else{

      wx.showLoading({
        title: '保存中',
        mask: true,
      });
      

      that.uploadImageAll(that.data.ImageArr,function (success,arrResult) {
        if (success) {
          wx.hideLoading();         
          var param={};
          param.ProgramID=186;
          param.UserID=app.globalData.userInfo.UserID;
          param.Content=that.data.InputValue;
          param.ContactTa=that.data.InputValue2;
          param.FeedbackType=[];
          for(var i=0;i<that.data.Feedback.length;i++){
            if (that.data.Feedback[i].CSS=="Selected")
              param.FeedbackType.push(that.data.Feedback[i].Name);
          }
          param.FeedbackType=param.FeedbackType.join(",");
          
          if (arrResult.length>0)
            param.ImageUrl1=arrResult[0].Target;
          if (arrResult.length>1)
            param.ImageUrl2=arrResult[1].Target;
          if (arrResult.length>2)
            param.ImageUrl3=arrResult[2].Target;
          if (arrResult.length>3)
            param.ImageUrl4=arrResult[3].Target;

          param.FeedbackUrl=app.globalData.FeedbackUrl;
            
          main.postData('MPSFeedback', param, function (data) {
            wx.showModal({
              title: '提醒',
              content: '您的反馈已经收到，我们会尽快处理。感谢您的支持！',
              showCancel: false,
              complete: (res) => {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 1
                  });
                }
              }
            });
          });

        } else {
          wx.showToast({
            title: '图片上传失败',
            image: "../images/universalpic_exclamation_white_120x120.png",
            mask: true,
          });
        }
      });
    }
  },
  //选择图片上传
  uploadImageField: function () {
    //console.log("uploadImageStart");
    var that = this;

    //若是安卓机
    if (app.globalData.IsAndroid) {
      selectImage(that);
    } else {
      wx.showActionSheet({
        itemList: ['拍照', '从手机相册选择'],
        success(res) {
          if (res.tapIndex == 0) {
            wx.chooseImage({
              count: 1,
              sizeType: ['compressed'],
              sourceType: ['camera'],
              success(res2) {
                wx.showLoading({
                  title: '请稍候',
                  mask: true,
                });
                setTimeout(function () {
                  wx.hideLoading();
                  selectImage(that);
                }, 2000);
              },
            });
          } else if (res.tapIndex == 1) {
            selectImage(that);
          }
        },
        fail(res) {
          console.log(res.errMsg)
        }
      });
    }

    function selectImage(that) {
      var sizeType = ['album'];
      //若是安卓机
      if (app.globalData.IsAndroid)
        sizeType = ['album', 'camera'];

      wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: sizeType,
        success(res) {
          // tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          //console.log(tempFilePaths[0]);
          //that.data.FieldContent = that.data.FieldContent + "[图 url='" + tempFilePaths[0] + "']" + tempFilePaths[0] + "[/图]";
          that.data.ImageArr.push(tempFilePaths[0]);
          that.setData({
            ImageArr:that.data.ImageArr,
          });
        },
        fail: function () {

        }
      });
    }
  },
  uploadFileToServer: function (file, callback) {
    var url = common.Encrypt("MiaoguoUploadFile2");
    wx.showLoading({
      title: '上传中',
      mask: true,
    });
    url=app.globalData.serverUrl + url;
    wx.uploadFile({
      url: url,
      filePath: file,
      name: 'file',
      success(res) {
        var err = JSON.parse(res.data);
        if (err.errcode == 10000) {
          wx.hideLoading();
          callback(err.result);
        } else {
          wx.hideLoading();
          wx.showModal({
            title: '上传文件失败1',
            showCancel: false,
            content: JSON.stringify(err.errMsg),
          });
        }
      },
      fail: function (err) {
        wx.hideLoading();
        wx.showModal({
          title: '上传文件失败2',
          showCancel: false,
          content: JSON.stringify(err)+" url:"+url,
        });
      }
    });
  },

  uploadImageAll: function (arr, callback) {
    var that = this;
    var b = false;
    var arrResult = [];
    for (var i = 0; i < arr.length; i++) {
      var source = arr[i];
      that.uploadFileToServer(source, function (data) {
        if (data) {
          var result = data;
          result.Target = app.globalData.uploadImageUrl + result.Target;
          arrResult.push(result);
        }
      });
    }

    //检查是否上传成功
    setTimeout(function () {
      if (arrResult.length==arr.length)
        callback(true, arrResult);
    }, 2000);
  },
  goto: function (e) {
    main.goto(e);
  },
  onSelect:function(event){
    var that=this;
    main.onSelect(that,event,function(obj,e,result){
      if (e.currentTarget.dataset.object=="Feedback"){
        
      }
    });   
  },
  onShareTimeline: function () {
    return this.onShareAppMessage();
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})
