// pages/daily-inspection/daily-inspection.js
const util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        inspectionProjectList: [],
        inspectionType: 10,//10：日常巡检，20：会议巡检
        inspectionResultList: [],
        abnormalIndex: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        util.get({
            url: '/api/inspection/template/getDailyInspectionProject',
            header: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            success: (res) => {
                wx.hideLoading();
                console.info(res)
                if (res.data.code != 200) {
                    util.toast(res.data.msg);
                } else {
                    that.setData({
                        inspectionProjectList: res.data.data
                    });
                }
            }
        });
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let that = this;
        let item = wx.getStorageSync('inspectionResultItem');
        if (item) {
            let obj = JSON.parse(item);
            let index = that.data.abnormalIndex;

            that.data.inspectionProjectList[index].result = 20;
            that.data.inspectionProjectList[index].color = '#E95543';
            that.data.inspectionProjectList[index].results = '不正常';
            let list = that.data.inspectionResultList;
            list.push(obj);
            that.setData({
                inspectionProjectList: that.data.inspectionProjectList,
                inspectionResultList: list,
                abnormalIndex: null
            });
            wx.removeStorageSync('inspectionResultItem');
        }
    },

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

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 历史记录
     */
    history: function () {
        wx.navigateTo({
            url: '../meeting-inspection-history/meeting-inspection-history?inspectionType=10&meetingId=',
        });
    },


    /**
     * 巡检结果不正常
     * @param {*} e 
     */
    abnormal: function (e) {
        this.setData({
            abnormalIndex: e.currentTarget.dataset.index
        });
        wx.navigateTo({
            url: '../meeting-inspection-abnormal/meeting-inspection-abnormal?inspectionProjectId=' + e.currentTarget.dataset.id,
        })
    },


    /**
     * 巡检结果正常
     * @param {*} e 
     */
    normal: function (e) {
        let that = this;
        // 修改巡检结果为正常（10）
        that.data.inspectionProjectList[e.currentTarget.dataset.index].result = 10;
        that.data.inspectionProjectList[e.currentTarget.dataset.index].color = '#2EC3CD';
        that.data.inspectionProjectList[e.currentTarget.dataset.index].results = '正常';
        let list = that.data.inspectionResultList;
        list.push({
            inspectionProjectId: e.currentTarget.dataset.id,
            result: 10
        });
        that.setData({
            inspectionProjectList: that.data.inspectionProjectList,
            inspectionResultList: list
        });
    },


    /**
     * 提交巡检结果
     */
    submitInspection: function () {
        let that = this;
        let inspectionResultList = that.data.inspectionResultList;
        if (!inspectionResultList || inspectionResultList.length < 1) {
            util.toast('请选择巡检结果');
            return;
        }

        wx.showModal({
            cancelColor: 'cancelColor',
            title: '巡检工程师',
            showCancel: false,
            editable: true,
            placeholderText: '请输入姓名',
            success: (res) => {
                if (!res.content) {
                    util.toast('请输入姓名');
                    return;
                } else {
                    util.post({
                        url: '/api/inspection/record/addBean',
                        data: {
                            inspectionType: that.data.inspectionType,
                            inspectionResultList: inspectionResultList,
                            inspectionPersonnel: res.content
                        },
                        success: (res) => {
                            wx.hideLoading();
                            if (res.data.code != 200) {
                                util.toast(res.data.msg);
                            } else {
                                util.toast(res.data.msg);
                                setTimeout(() => {
                                    wx.navigateBack({
                                        delta: -1,
                                    });
                                }, 1000);
                            }
                        }
                    });
                }
            }
        });
    }

})