// other/pages/unableAccept/unableAccept.js
const {
  canNotAcceptance,
  canNotAcceptanceDetail,
  API_BASE_URL,
  IMG_BASE_URL,
} = require('../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    API_BASE_URL: API_BASE_URL,
    IMG_BASE_URL: IMG_BASE_URL,
    capture: ['camera', 'album'], //拍摄模式
    reason: '',
    imgPath: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id,
      type: options.type,
    });
    this.getDetail()
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  /** 上传图片 **/
  afteruploadPhotoRead(e) {
    var that = this;
    const choseType = Array.isArray(e.detail.file) ? (e.detail.file[0].choseType || undefined) : (e.detail.file.choseType || undefined); //camera相机  album相册
    const name = e.currentTarget.dataset.name
    console.log("上传后", this.data.direction, choseType, e);
   
    if (Array.isArray(e.detail.file)) { //多张
      var count = 0;
      this.afteruploadPhotoReadS(e, count)
      return
    }
    const {
      file
    } = e.detail;
    wx.showLoading({
      title: '上传中',
    })
    wx.uploadFile({
      url: that.data.API_BASE_URL + 'common/weChat/uploadImage',
      filePath: file.url,
      name: 'file',
      complete: function (res) {
        console.log("上传图片", res)
        wx.hideLoading()
        if (res.statusCode == 200) {
          wx.showToast({
            title: '选择成功',
            icon: 'none',
          })
          var item = {
            url: "",
            name: "",
          }
          item.url = file.url;
          item.name = res.data;
          that.data[name].push(item)
          that.setData({
            [`${name}`]: that.data[name]
          })
        } else {
          wx.showToast({
            title: '选择失败',
            icon: 'error'
          })
        }
      }
    })
  },
  /** 上传多张图片递归 **/
  afteruploadPhotoReadS(e, count) {
    let ob = {
      name: '',
      url: '',
    }
    const name = e.currentTarget.dataset.name
    wx.showLoading({
      title: '加载第' + (count + 1) + '张照片中',
      mask: true,
    })
    const element = e.detail.file[count];
    var that = this;
    wx.uploadFile({
      url: that.data.API_BASE_URL + 'common/weChat/uploadImage',
      filePath: element.url,
      name: 'file',
      success(res) {
        wx.hideLoading()
        if (res.statusCode == 200) {
          count = count + 1;
          ob.name = res.data
          ob.url = element.url
          that.data[name].push(ob);
          that.setData({
            [`${name}`]: that.data[name]
          })
          if (count == e.detail.file.length) {
            wx.showToast({
              title: '选择成功',
              icon: 'none'
            })
          } else {
            that.afteruploadPhotoReadS(e, count)
          }
        } else {
          wx.showToast({
            title: '选择失败',
            icon: 'error'
          })
        }
      }
    })
  },
  /** 删除图片 **/
  deleteImg(e) {
    const name = e.currentTarget.dataset.name
    const index = e.detail.index //删除图片的下标
    this.data[name].splice(index, 1);
    this.setData({
      [`${name}`]: this.data[name]
    })
  },
  /** 取消 **/
  cancel(e) {
    wx.showModal({
      title: '提示',
      content: '确定取消？',
      complete: (res) => {
        if (res.cancel) {}
        if (res.confirm) {
          wx.navigateBack()
        }

      }
    })
  },
  //详情
  getDetail(){
    wx.showLoading({
      title: "加载中"
    });
    canNotAcceptanceDetail({     
      detailId: this.data.id     
    }).then(res=>{     
      if(res.code == 200 && res.data){
        this.setData({
          reason: res.data.unAcceptReason,
          imgPath: res.data.unAcceptFile?res.data.unAcceptFile.split(",").map(itm => {return {name: itm, url: IMG_BASE_URL + itm}}) : [],
        })
      }
      wx.hideLoading();
    });
  },
  //提交
  submitFrom(){
    if (!this.data.reason) {
      wx.showToast({
        title: '请输入无法验收原因',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.showLoading({
      title: '提交中',
      mask: true
    })
    canNotAcceptance({
      detailId: this.data.id,
      reason: this.data.reason,
      imgPath: this.data.imgPath.length > 0 ? this.data.imgPath.map(itm => itm.name).join(",") : "", //凭证
    }).then((res) => {
      wx.hideLoading()
      if (res.code == 200) {
        wx.showToast({
          title: "操作成功",
          icon: 'none',
          success: function () {
            setTimeout(function () {
              //延时返回上一页面
              wx.navigateBack()
            }, 1800) //延迟时间
          }
        })
      } else {
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
      }
    })
  }
})