import config from '../../../config'
import {
  getToken
} from '../../../utils/auth'
import {
  findInfo,
  findAuditInfos,
  checkProblemmodify
} from '../../../api/problemmodify'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    infoData: {},
    loadShow: false,
    auditInfo1: {},
    auditInfo3: {},
    auditInfo4: {},
    form: {},
    imageInfoData: [],
    imageList: [],
    auditImageList: [],
    active: 3,
    flowNodes: [{
      text: '开始'
    }, {
      text: '提交隐患'
    }, {
      text: '隐患整改'
    }, {
      text: '隐患复检'
    }, {
      text: '结束'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (!getToken()) {
      wx.redirectTo({
        url: '../../login/login',
      })
    }
    this.setData({
      type: options.type,
      typeName: options.type == 1 ? "质量" : "安全",
    });
    this.getInfo(options.id);
    this.getAuditInfos(options.id);
  },

  /**
   * 查详隐患详情
   * @param {*} id 
   */
  getInfo(id) {
    findInfo(id).then(res => {
      if (res.code == 200) {
        let urls = [];
        if (res.data.smarkUrl) {
          res.data.smarkUrl.split(',').forEach(item => {
            urls.push(config.baseImgUrl + item);
          });
        }
        this.setData({
          infoData: res.data,
          imageList: urls,
          "form.mainId": res.data.id,
        })
      }
    })
  },

  /**
   * 查详隐患流程
   * @param {*} id 
   */
  getAuditInfos(id) {
    findAuditInfos(id).then(res => {
      if (res.code == 200 && res.data.length > 0) {
        res.data.forEach(item => {
          if (item.processState == "0") {
            let urls = [];
            if (item.files.length > 0) {
              item.files.forEach(_file => {
                urls.push(config.baseImgUrl + _file.fileUrl);
              });
            }
            this.setData({
              auditInfo1: item,
              auditImageList: urls
            })
          } else if (item.processState == "1") {
            this.setData({
              auditInfo4: item
            })
          } else if (item.processState == "2") {
            this.setData({
              auditInfo3: item
            })
          }
        });
      }
    });
  },

  //取消页面
  cancelSaveView() {
    this.returnToPage()
  },

  //保存
  onSubmitSave(status) {
    let _form = {
      ...this.data.form
  };
    _form.processState = status;
    this.setData({
      loadShow: true
    });
    checkProblemmodify(_form).then(res =>{
      this.setData({
        loadShow: false
      });
      if (res.code == 200) {
        app.toast("复检隐患成功!")
        setTimeout(() => {
          wx.redirectTo({
            url: `../list/index?type=${this.data.type}`
          })
        }, 200)
      }
    });
  },

  //审核驳回
  onRejectSave() {
    let that = this;
    if (this.data.form.opinion) {
      //弹出确认
      wx.showModal({
        title: '提示',
        content: '是否确认隐患复检驳回?',
        success: function (sm) {
          if (sm.confirm) {
            // 用户点击了确定 可以调用了
            that.onSubmitSave(2);
          } else if (sm.cancel) {
            console.log('用户点击取消');
          }
        }
      })
    } else {
      app.toast("请填写整改驳回意见!")
      return false;
    }
  },

  //审核通过
  onPassSave() {
    let that = this;
    if (!this.data.form.opinion) {
      this.setData({
        "form.opinion": "审核通过!"
      })
    }
    //弹出确认
    wx.showModal({
      title: '提示',
      content: '是否确认隐患复检通过?',
      success: function (sm) {
        if (sm.confirm) {
          // 用户点击了确定 可以调用了
          that.onSubmitSave(1);
        } else if (sm.cancel) {
          console.log('用户点击取消');
        }
      }
    })
  },

  //展示图片
  showImg: function (e) {
    let img = e.target.dataset.set;
    wx.previewImage({
      urls: img.split(','),
      current: 0
    })
  },

  returnToPage: function () {
    wx.redirectTo({
      url: `../list/index?type=${this.data.type}`
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  // 审核意见
  onInputOpinion(e) {
    this.setData({
      "form.opinion": e.detail.value
    })
  },

  // list  上传图片
  onImagesArr(e) {
    var data = this.data.imageInfoData
    data = e.detail
    this.setData({
      imageInfoData: data
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})