// pageage/safetyManagement/addSafetyInspect/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        deptId:"",
        projectId:"",
        projectName:"",
        loginName:"",
        servicePersonnel:0,
        supervisorPersonnel:0,
        contractorPersonnel:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let {projectId,projectName} = options
        //获取缓存数据
        wx.getStorage({
        key: 'userinfo',
        success:res=>{
            this.setData({
            projectId,
            projectName,
            deptId:res.data.deptId,
            loginName:res.data.loginName
            })
            this.getLastData(); 
        }  
        }) 
    },

    //获取数据
    getLastData(){
        let that = this
        wx.request({
          url: app.globalData.reqUrl+'/wechat/projectDeptWroks/list',
          method:"get",
          data:{
              projectId:this.data.projectId,
              deptId:this.data.deptId
          },
          success(res){
            res = res.data
            if(res.code == 200){
                if(res.data){
                    this.setData({
                        servicePersonnel:res.data[0].servicePersonnel,
                        supervisorPersonnel:res.data[0].servicePersonnel,
                        contractorPersonnel:res.data[0].contractorPersonnel
                    });
                }
            }
          }
        })
    },

    //取消页面
    cancelSaveView(){
         /*关闭当前页面，跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面*/
         wx.redirectTo({
            url: '../list/index'
        })
    },

    //保存
    onSave(){
        this.setData({
            loadShow:true
        })
        let that = this
        let {projectId,deptId,loginName,servicePersonnel,
            supervisorPersonnel,
            contractorPersonnel} =  that.data;
        //数据效验
        if(projectId==""||loginName==""||deptId==""){
            app.toast("数据异常，请刷新页面重试!")
            that.setData({
                loadShow:false
            })
            return;
        }
        if(servicePersonnel==""){
            app.toast("请填写劳务人员数!")
            that.setData({
                loadShow:false
            })
            return;
        }
        if(supervisorPersonnel==""){
            app.toast("请填写监理人员数!")
            that.setData({
                loadShow:false
            })
            return;
        }
        if(contractorPersonnel==""){
            app.toast("请填写总包人员数!")
            that.setData({
                loadShow:false
            });
            return;
        }
        let params = {
            projectId,
            deptId,
            servicePersonnel,
            supervisorPersonnel,
            contractorPersonnel,
            createBy:loginName
        }
        wx.request({
            url: app.globalData.reqUrl + "/wechat/projectDeptWroks/add",
            method:"POST",
            data:params,
            header: {
                "Username": loginName,
                "Content-Type": "application/json"
            },
            success(res){
                that.setData({
                    loadShow:false
                })
                res = res.data
                if(res.code == 200){
                    if(res.data==-1){
                        app.toast("办理失败，当前单位已办理此类保险!")
                    }else{
                        app.toast("添加成功!")
                        setTimeout(()=>{
                        wx.redirectTo({
                            url: '../list/index',
                        })
                        },200)
                    }
                }
            }
            })
    },

    servicePersonnelAction: function (options) {
        let value = options.detail.value;
        value = value.replace(/[^0-9]/g, ''); // 正则表达式替换非数字为空
        this.data.servicePersonnel = value;
    },

    supervisorPersonnelAction: function (options) {
        let value = options.detail.value;
        value = value.replace(/[^0-9]/g, ''); // 正则表达式替换非数字为空
        this.data.supervisorPersonnel = value;
    },

    contractorPersonnelAction: function (options) {
        let value = options.detail.value;
        value = value.replace(/[^0-9]/g, ''); // 正则表达式替换非数字为空
        this.data.contractorPersonnel = value;
    },

    returnToPage: function () {
        /*关闭当前页面，跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面*/
        wx.redirectTo({
            url: '../list/index',
        })
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})