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

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (!getToken()) {
      wx.redirectTo({
        url: '../../../login/login',
      })
    }
    const proUserInfo = getUserInfo();
    this.setData({
      proUserInfo,
      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 state = this.data.active;
        if (res.data.checkState == "0") {
          state = 2;
        } else if (res.data.checkState == "1") {
          state = 3;
        } else if (res.data.checkState == "3") {
          state = 2;
        }
        this.setData({
          active: state
        })
        let urls = [];
        if (res.data.smarkUrl) {
          res.data.smarkUrl.split(',').forEach(item => {
            urls.push(config.baseImgUrl + item);
          });
        }
        this.setData({
          infoData: res.data,
          imageList: urls
        })
        //判断当前能否删除
        if (res.data.checkState != 4 && res.data.createUser == this.data.proUserInfo.userId) {
          this.setData({
            showDel: true
          })
        }
      }
    })
  },

  /**
   * 查详隐患流程
   * @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
            })
          }
        });
      }
    });
  },

  /**
   * 点击删除
   */
  onDelete() {
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否确定删除当前隐患数据?',
      success: function (sm) {
        if (sm.confirm) {
          // 用户点击了确定 可以调用了
          that.submitDelete();
        } else if (sm.cancel) {
          console.log('用户点击取消');
        }
      }
    })
  },

  /**
   * 提交删除
   */
  submitDelete() {
    let {
      infoData
    } = this.data
    remove(infoData.id).then(res => {
      if (res.code == 200) {
        app.toast("删除数据成功!")
        setTimeout(() => {
          wx.redirectTo({
            url: `../list/index?type=${this.data.type}`,
          })
        }, 200);
      }
    });
  },

  //展示图片
  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() {

  },

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

  },

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

  }
})