// pages/fault-detail/fault-detail.js
const util = require('../../utils/util');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        disabled: false,
        showSubmit: true,
        height: '90%',
        endTime: new Date(),
        showPicker: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.item) {
            let item = JSON.parse(options.item);
            this.setData({
                title: item.title,
                proposePersonnel: item.proposePersonnel,
                content: item.content,
                treatmentMethod: item.treatmentMethod,
                treatmentPersonnel: item.treatmentPersonnel,
                updateTime: item.updateTime,
                disabled: true,
                showSubmit: false,
                height: '100%',
                showPicker: false
            });
        }
    },

    changeTreatmentTime: function (e) {
        let that = this;
        that.setData({
            updateTime: e.detail.value
        });
    },

    /**
     * 保存故障处理
     */
    formSubmit: function (e) {
        let values = e.detail.value;
        if (!values.title) {
            util.toast('请输入故障标题');
            return false;
        }
        if (!values.content) {
            util.toast('请输入故障内容');
            return false;
        }
        values.status = 20;

        util.post({
            url: '/api/fault/handling/record/addBean',
            data: values,
            success: function (res) {
                wx.hideLoading();
                if (res.data.code == 200) {
                    util.toast(res.data.msg, 'success');

                    //获取页面栈
                    let pages = getCurrentPages();
                    if (pages.length > 1) {
                        //上一个页面实例对象
                        var prePage = pages[pages.length - 2];
                        //调用上一个页面的onShow方法
                        prePage.onLoad();
                    }

                    setTimeout(() => {
                        wx.navigateBack({
                            delta: -1,
                        });
                    }, 2000);
                } else {
                    util.toast(res.data.msg);
                }
            }
        });
    }

})