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

    /**
     * 页面的初始数据
     */
    data: {
        integral: 0, 
        moneyNum: null, 
        buildList: [],
        floorList: [],
        initData: [],
        projectData: {},
        buildingNum:'',
        floorNum:'',
        time:'',
        loginName:'',
        activeState:1,
        oneData:{},
        twoData:{},
        threeData:{},
        fourData:{},
        fiveData:{},
        loadShow:false,
    },
    
    /**
     * 标签切换
     */
    trainingTypeJump(e){
        this.setData({
            activeState:e.currentTarget.dataset.index,
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this;
        that.setData({
            time: that.getTimeFormat(new Date())
        })
       
        //获取缓存数据
        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);
    },

    getIntegral: function(e) {
        var integral = e.detail.value;
        if(integral<=100) { // 判断value值是否小于等于100, 如果大于100限制输入100
          if(integral == '') { // 判断value值是否等于空,为空integral默认0,
            this.setData({
              integral: 0
            })
          } else {
            this.setData({
              integral: integral,
            })
          }
        } else {
          wx.showToast({
            title: '输入的最大数为100，请慎重填写！',
            icon: 'none',
          })
        //   this.setData({
        //     integral: 100,
        //     moneyNum: 100,
        //   })
        }
    },

    vmodel(e) {
        this.setData({
          [e.currentTarget.dataset.value]:e.detail.value
        })
    },

    /**
     * 获取项目对应关系
     */
    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.getConstructionPlanOne()
            }
        })
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

    
    /**
     * 项目变化
     */
    onProjectSelect(e) {
        var that = this
        var id = e.detail.id
        that.getProjectCorrespondence(id)
    },

    /**
     * 选择一级区域
     */
    buildChange(e) {
        var that = this
        that.setData({
            buildingNum:e.detail.id,
        })
        that.getConstructionPlanTwo();
    },

    /**
     * 选择二级区域
     */
    floorChange(e) {
        var that = this;
        that.setData({
            floorNum:e.detail.id
        })
    },

    /**
     * 选择填报日期
     */
    onPlaceTime(e) {
        this.setData({
            time:e.detail
        })
    },

    /**
     * 格式化时间
     */
    getTimeFormat(date) {
        var date = new Date(date);
        var year = date.getFullYear();
        var month = date.getMonth()+1;
        var day = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
         }
         if (day >= 0 && day <= 9) {
            day = "0" + day;
         }
         var time = year + "-" + month + "-" + day;
         return time;
    },

    /**
     * 获取施工计划一级区域
     */
    getConstructionPlanOne(){
        var that = this
        wx.request({
            url: app.globalData.szhUrl + '/api/getConstructionPlanOne',
            method: "get",
            data: {
                "projectId":that.data.projectData.szh
            },
            success: (res => {
                that.setData({
                    buildList:res.data,
                    buildingNum:res.data[0].id
                })
                that.getConstructionPlanTwo();
            })
        })
    },

    /**
     * 获取施工计划二级区域
     */
    getConstructionPlanTwo(){
        var that = this
        wx.request({
            url: app.globalData.szhUrl + '/api/getConstructionPlanTwo',
            method: "get",
            data: {
                "projectId":that.data.projectData.szh,
                "buildingNum":that.data.buildingNum
            },
            success: (res => {
                that.setData({
                    floorList:res.data,
                    floorNum:res.data[0].id
                })
            })
        })
    },
    
    /**
     * 提交地基与基础
     */
    doSubmit1(e) {
        var that = this

        if (isNaN(that.data.projectData.szh)) {
            app.toast("请选择项目！");
            return;
        }
        if (!isNaN(that.data.buildingNum)) {
            app.toast("请选择一级区域！");
            return;
        }
        if (!isNaN(that.data.floorNum)) {
            app.toast("请选择二级区域！");
            return;
        }
        if (!isNaN(that.data.time)) {
            app.toast("请选择填报日期！");
            return;
        }
        that.setData({
            loadShow: true
        })

        var data = e.detail.value;
        data.projectId = that.data.projectData.szh;
        data.buildingNum = that.data.buildingNum;
        data.floorNum = that.data.floorNum;
        data.branch = that.data.activeState;
        data.fillTime = that.data.time;
        data.createUserid = that.data.loginName;
        data.oneData = that.data.oneData;

        wx.request({
            url: app.globalData.szhUrl + '/api/addConstructionLogOne',
            method: "post",
            data: data,
            success: (res => {
                that.setData({
                    loadShow: false
                })
                if (res.data.code == 200) {
                    app.toast("提交成功！");
                } else {
                    app.toast("提交失败，请重新提交！");
                }
            })
        })
    },

    doSubmit2(e){
        var that = this

        if (isNaN(that.data.projectData.szh)) {
            app.toast("请选择项目！");
            return;
        }
        if (!isNaN(that.data.buildingNum)) {
            app.toast("请选择一级区域！");
            return;
        }
        if (!isNaN(that.data.floorNum)) {
            app.toast("请选择二级区域！");
            return;
        }
        if (!isNaN(that.data.time)) {
            app.toast("请选择填报日期！");
            return;
        }
        that.setData({
            loadShow: true
        })

        var data = e.detail.value;
        data.projectId = that.data.projectData.szh;
        data.buildingNum = that.data.buildingNum;
        data.floorNum = that.data.floorNum;
        data.branch = that.data.activeState;
        data.fillTime = that.data.time;
        data.createUserid = that.data.loginName;
        data.twoData = that.data.twoData;

        wx.request({
            url: app.globalData.szhUrl + '/api/addConstructionLogTwo',
            method: "post",
            data: data,
            success: (res => {
                that.setData({
                    loadShow: false
                })
                if (res.data.code == 200) {
                    app.toast("提交成功！");
                } else {
                    app.toast("提交失败，请重新提交！");
                }
            })
        })
    },

    doSubmit3(e){
        var that = this

        if (isNaN(that.data.projectData.szh)) {
            app.toast("请选择项目！");
            return;
        }
        if (!isNaN(that.data.buildingNum)) {
            app.toast("请选择一级区域！");
            return;
        }
        if (!isNaN(that.data.floorNum)) {
            app.toast("请选择二级区域！");
            return;
        }
        if (!isNaN(that.data.time)) {
            app.toast("请选择填报日期！");
            return;
        }
        that.setData({
            loadShow: true
        })

        var data = e.detail.value;
        data.projectId = that.data.projectData.szh;
        data.buildingNum = that.data.buildingNum;
        data.floorNum = that.data.floorNum;
        data.branch = that.data.activeState;
        data.fillTime = that.data.time;
        data.createUserid = that.data.loginName;
        data.threeData = that.data.threeData;

        wx.request({
            url: app.globalData.szhUrl + '/api/addConstructionLogThree',
            method: "post",
            data: data,
            success: (res => {
                that.setData({
                    loadShow: false
                })
                if (res.data.code == 200) {
                    app.toast("提交成功！");
                } else {
                    app.toast("提交失败，请重新提交！");
                }
            })
        })
    },

    doSubmit4(e){
        var that = this

        if (isNaN(that.data.projectData.szh)) {
            app.toast("请选择项目！");
            return;
        }
        if (!isNaN(that.data.buildingNum)) {
            app.toast("请选择一级区域！");
            return;
        }
        if (!isNaN(that.data.floorNum)) {
            app.toast("请选择二级区域！");
            return;
        }
        if (!isNaN(that.data.time)) {
            app.toast("请选择填报日期！");
            return;
        }
        that.setData({
            loadShow: true
        })

        var data = e.detail.value;
        data.projectId = that.data.projectData.szh;
        data.buildingNum = that.data.buildingNum;
        data.floorNum = that.data.floorNum;
        data.branch = that.data.activeState;
        data.fillTime = that.data.time;
        data.createUserid = that.data.loginName;
        data.fourData = that.data.fourData;

        wx.request({
            url: app.globalData.szhUrl + '/api/addConstructionLogFour',
            method: "post",
            data: data,
            success: (res => {
                that.setData({
                    loadShow: false
                })
                if (res.data.code == 200) {
                    app.toast("提交成功！");
                } else {
                    app.toast("提交失败，请重新提交！");
                }
            })
        })
    },

    doSubmit5(e){
        var that = this

        if (isNaN(that.data.projectData.szh)) {
            app.toast("请选择项目！");
            return;
        }
        if (!isNaN(that.data.buildingNum)) {
            app.toast("请选择一级区域！");
            return;
        }
        if (!isNaN(that.data.floorNum)) {
            app.toast("请选择二级区域！");
            return;
        }
        if (!isNaN(that.data.time)) {
            app.toast("请选择填报日期！");
            return;
        }
        that.setData({
            loadShow: true
        })

        var data = e.detail.value;
        data.projectId = that.data.projectData.szh;
        data.buildingNum = that.data.buildingNum;
        data.floorNum = that.data.floorNum;
        data.branch = that.data.activeState;
        data.fillTime = that.data.time;
        data.createUserid = that.data.loginName;
        data.fiveData = that.data.fiveData;

        wx.request({
            url: app.globalData.szhUrl + '/api/addConstructionLogFive',
            method: "post",
            data: data,
            success: (res => {
                that.setData({
                    loadShow: false
                })
                if (res.data.code == 200) {
                    app.toast("提交成功！");
                } else {
                    app.toast("提交失败，请重新提交！");
                }
            })
        })
    },
})