// pages/measures/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // initData: { text: '请选择你的项目', id: 0 }
        titleName:"测量台账详情",
        szhProjectId:'',
        managementNum:'',
        ledgerType:'',
        measureDevice:{}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this
        const {q} = options
        let urlStr = decodeURIComponent(q);
        let subStrUrl = urlStr.substr(urlStr.indexOf("?"),urlStr.length-1)    
        let params = that.getRequest(subStrUrl)
        let ledgerType = params.ledgerType
        let projectId = params.projectId
        let managementNum = params.managementNum
        var titleName = "台账详情"
        if(ledgerType == 1) {
            titleName = "测量台账详情"
        } else {
            titleName = "实验台账详情"
        }
        that.setData({
            szhProjectId:projectId,
            managementNum:managementNum,
            ledgerType:ledgerType,
            titleName:titleName
        })
        that.getMeasureDeviceInfo()
    },


      /**
      * 获取请求地址信息
      */
     getRequest(url) {
       
        var theRequest = new Object();
        var strs;
        if (url.indexOf("?") != -1) {
            let str = url.substr(1);
            strs = str.split("&");
            for (let i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    },

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

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 关闭
     */
    goGCLB() {
        //在需要退出小程序的地方调用添加下面代码即可(js文件中)
        wx.exitMiniProgram({success: (res) => {

        },fail:(res)=>{
          console.log(res)
        }}) 
    },
    /**
     * 获取台账信息
     */
    getMeasureDeviceInfo() {
        var that = this
        wx.request({
            url:  app.globalData.szhUrl+"/system/deviceCheck/selectListByManagementCode",
            method:"POST",
            data:{
                projectId: that.data.szhProjectId,
                managementNum:that.data.managementNum,
                ledgerType:that.data.ledgerType
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded' //修改此处即可
            },
            success(res){
                if(res.data.code == 0 && res.data.data.length > 0) {
                    var data = res.data.data[0]
                    data.checkCertificateUrl = data.checkCertificateUrl != null ?data.checkCertificateUrl.split(","):''
                    that.setData({
                        measureDevice:data
                    })
                    console.log(that.data.measureDevice)
                }
            }
        })
    },
    /**
     * 打开附件
     * @param {url} e 
     */
    openFile(e) {
        console.log(e)
        var url = e.currentTarget.dataset.url
        wx.downloadFile({
            // 示例 url，并非真实存在
            url: url,
            success: function (res) {
                const filePath = res.tempFilePath
                if(url.indexOf(".png") > 0 || url.indexOf(".jpg") > 0 || url.indexOf(".jpeg") >0|| url.indexOf(".PNG")>0|| url.indexOf(".JPG")> 0) {
                    wx.previewImage({
                    urls: [filePath],
                    success: function (res) {
                        console.log('打开图片成功')
                    },
                    fail:function(res) {
                        console.log(res)
                    }
                    })
                } else {
                    wx.openDocument({
                        filePath: filePath,
                        success: function (res) {
                            console.log('打开文档成功')
                        },
                        fail:function(res) {
                            console.log(res)
                        }
                    })
                }
               
            }
        })
    },
})