// pages/Construction-Log/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        buildList: [],
        floorList: [],
        //侧边
        show: false,
        projectId:'',
        projectName:'' ,
        loginName:'',
        userName:'',
        remark:'',
        initData:{},
        projectData:{},
        logList:[],
        buildingNum:'',
        floorNum:'',
        params:{
            startTime:'',
            endTime:''
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this
        wx.getStorage({
            key: 'userinfo',
            success: function (res) {
                that.setData({
                    remark: res.data.remark,
                    loginName: res.data.loginName,
                    userName: res.data.userName,
                    projectId: app.globalData.projectId,
                    projectName: app.globalData.projectName,
                    initData: {
                        text: app.globalData.projectName,
                        id: app.globalData.projectId
                    }
                })
            }
        })
        that.getProjectCorrespondence(app.globalData.projectId);
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },
     /**
     * 项目对应关系
     */
    getProjectCorrespondence(projectId) {
        var that = this;
        wx.request({
            url: app.globalData.reqUrl + '/weixin/training/getProjectCorrespondence',
            data: {
                "projectId": projectId,
            },
            method: "get",
            success: function (res) {
                that.setData({
                    projectData: res.data
                })
                that.getLogList(that.data.projectData.szh)
                // that.getBulidingFloorList(res.data.szh) 
            }
        })
    },
    /**
     * 获取施工日志列表
     */
    getLogList(projectId) {
        var that = this;
        wx.request({
            url: app.globalData.szhUrl + '/weixin/constructionLog/getLoglist',
            data: {
                "projectId": projectId,
                "buildingNum":that.data.buildingNum,
                "floorNum":that.data.buildingNum,
                "params.startTime":that.data.params.startTime,
                "params.endTime":that.data.params.startTime
            },
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
            },
            success: function (res) {
               console.log(res)
               that.setData({
                    logList:res.data.data
               })
            }
        })
    },
    /**
     * 返回上一层
     */
    goGCLB() {
        wx.redirectTo({
            url: '../gengduogongneng/gengduogongneng',
        })
    },
    showPopup() {
        this.setData({ show: true });
    },
    
    onClose() {
    this.setData({ show: false });
    },
    /**
     * 跳转到添加页面
     */
    goAddPage() {
        wx.navigateTo({
          url: '../construction/index',
        })
    },
     /**
     * 获取楼盘信息和楼层信息
     */
    getBulidingFloorList(projectId) {
        if(projectId != '') {
            var that = this
            wx.request({
                url: app.globalData.szhUrl + '/weixin/constructionLog/getBuildingFloorList',
                method: "POST",
                data: {
                    "projectId": parseInt(projectId)
                },
                header: {
                    "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
                },
                success: (res => {
                    if(res.data.code == 0) {
                        var data = res.data.data
                        if (data.length > 0) {
                            var list = [];
                            list = data.map(item => {
                                var floorList = []
                                if (item.buildingFloorList.length > 0) {
                                    floorList = item.buildingFloorList.map(floor => {
                                        return {
                                            id: floor.floorNum,
                                            text: floor.floorName
                                        }
                                    })
                                }
        
                                return {
                                    id: item.id,
                                    text: item.buildingName,
                                    floorList: floorList
                                }
                            })
                            that.setData({
                                buildList: list,
                                buildingNum:list[0].id,
                                floorList: list[0].floorList,
                                floorNum:list[0].floorList[0].id
                            })
                          
                        }
                    }    
                })
            })
        }
       
    },
    //筛选关闭
    screenCondition(e) {
        console.log(e.detail)
        const {buildingNum,endDate,floorNum,startDate} = e.detail
        this.setData({
            buildingNum:buildingNum,
            floorNum:floorNum,
            'params.startTime':startDate + " 00:00:00",
            'params.endTime':endTime + " 23:59:59"
        })
        this.getLogList(this.data.projectData.szh)
    },
    onProjectSelect(e) {
        var that = this
        var id = e.detail.id
        that.getProjectCorrespondence(id)
    },

    checkDetails(e){
        let item = e.currentTarget.dataset.item;
        wx.navigateTo({
          url: '../construction-details/index?id='+item.id,
        })
    }


})