// pages/meeting-inspection-abnormal/meeting-inspection-abnormal.js
const app = getApp();
const util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: app.globalData.imgPath,
        path: app.globalData.path,
        uploadList: [],
        ids: [],
        desc: null,
        inspectionProjectId: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            inspectionProjectId: options.inspectionProjectId
        });
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },
    /**
     * 上传图片、选择图片
     */
    chooseImage: function () {
        let that = this;
        wx.chooseImage({
            count: 1,
            success: function (res) {
                wx.uploadFile({
                    filePath: res.tempFilePaths[0],
                    name: 'file',
                    url: that.data.path + '/api/upload/record/upload',
                    header: {
                        "Content-Type": "multipart/form-data",
                        "Authorization": app.globalData.Authorization
                    },
                    formData: {
                        fileType: 20
                    },
                    success(re) {
                        let datas = JSON.parse(re.data);
                        if (datas.code != 200) {
                            util.toast(datas.msg);
                        } else {
                            let arr = that.data.uploadList;
                            let ids = that.data.ids;
                            arr.push(res.tempFilePaths[0]);
                            ids.push(datas.data.id);
                            that.setData({
                                uploadList: arr,
                                ids: ids
                            });
                        }
                    }
                });
            }
        });
    },

    /**
     * 移除上传图片
     * @param {*} e 
     */
    removeImg: function (e) {
        let that = this;
        let arr = that.data.uploadList;
        let ids = that.data.ids;
        arr.splice(e.target.dataset.index, 1);
        ids.splice(e.target.dataset.index, 1);
        that.setData({
            uploadList: arr,
            ids: ids
        });
    },

    /**
     * 获取描述内容
     * @param {*} e 
     */
    forDescription: function (e) {
        this.setData({
            desc: e.detail.value
        });
    },

    /**
     * 巡检结果不正常描述提交
     */
    submitAbnormal: function () {
        let that = this;
        let desc = that.data.desc;
        if (!desc) {
            util.toast('请输入结果描述');
            return;
        }
        let obj = {
            fileIds: that.data.ids,
            inspectionProjectId: that.data.inspectionProjectId,
            remark: desc,
            result: 20,
        }
        wx.setStorageSync('inspectionResultItem', JSON.stringify(obj));
        wx.navigateBack({
            delta: -1,
        });
    }
})