// pages/information-release/information-release.js
const dateTimePicker = require('../../utils/dateTimePicker.js');
const util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        startTime: '',
        endTime: '',
        dateTimeArray: null,
        // dateTimeArray: null,
        dateTime: null,
        dateTime1: null,
        startYear: new Date().getFullYear(),
        endYear: new Date().getFullYear() + 5,
        ids: [],
        templetArr: [],
        inspectionTemplate: '',
        inspectionTemplateId: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        // 获取完整的年月日 时分秒，以及默认显示的数组
        var obj1 = dateTimePicker.dateTimePicker(that.data.startYear, that.data.endYear);
        // console.info(obj1)
        // 精确到分的处理，将数组的秒去掉
        var lastArray = obj1.dateTimeArray.pop();
        var lastTime = obj1.dateTime.pop();
        that.setData({
            dateTimeArray: obj1.dateTimeArray,
            dateTimeArray: obj1.dateTimeArray,
            dateTime: obj1.dateTime,
            dateTime1: obj1.dateTime
        });
        that.setTime();

        //查询巡检模板列表
        util.get({
            url: '/api/inspection/template/getByType',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
                type: 20
            },
            success: (res) => {
                console.info(res);
                wx.hideLoading();
                if (res.data.code != 200) {
                    util.toast(res.data.msg);
                } else {
                    let datas = res.data.data;
                    let ids = [];
                    let arr = [];
                    for (let i in datas) {
                        ids.push(datas[i].id);
                        arr.push(datas[i].name);
                    }
                    that.setData({
                        ids: ids,
                        templetArr: arr
                    });
                }
            }
        });
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },
    changeDateTime(e) {
        this.setData({ dateTime: e.detail.value });
    },
    changeDateTimeColumn(e) {
        var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;

        arr[e.detail.column] = e.detail.value;
        //切换月份
        if (e.detail.column == 1) {
            dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
            this.setData({
                dateTimeArray: dateArr
            });
        }
        this.setTime();
    },
    changeDateTime1(e) {
        this.setData({ dateTime1: e.detail.value });
    },
    changeDateTimeColumn1(e) {
        var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray;

        arr[e.detail.column] = e.detail.value;
        if (e.detail.column == 1) {
            dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
            this.setData({
                dateTimeArray: dateArr
            });
        }
        this.setTime();
    },
    setTime: function (e) {
        this.setData({
            startTime: this.data.dateTimeArray[0][this.data.dateTime[0]] + '-' + this.data.dateTimeArray[1][this.data.dateTime[1]] + '-' + this.data.dateTimeArray[2][this.data.dateTime[2]] + ' ' + this.data.dateTimeArray[3][this.data.dateTime[3]] + ':' + this.data.dateTimeArray[4][this.data.dateTime[4]],
            endTime: this.data.dateTimeArray[0][this.data.dateTime1[0]] + '-' + this.data.dateTimeArray[1][this.data.dateTime1[1]] + '-' + this.data.dateTimeArray[2][this.data.dateTime1[2]] + ' ' + this.data.dateTimeArray[3][this.data.dateTime1[3]] + ':' + this.data.dateTimeArray[4][this.data.dateTime1[4]]
        });
    },

    /**
     * 提交保存会议信息
     * @param {*} e 
     */
    formSubmit: function (e) {
        let that = this;
        console.info(e);
        let values = e.detail.value;
        if (!values.title) {
            util.toast('请输入标题');
            return false;
        }
        if (!values.receptionObject) {
            util.toast('请输入接待对象');
            return false;
        }
        if (!values.receptionTime) {
            util.toast('请选择接待时间');
            return false;
        }
        if (new Date() >= new Date(values.receptionTime)) {
            util.toast('接待时间不能小于当前时间');
            return false;
        }
        if (!values.receptionNumber) {
            util.toast('请输入接待人数');
            return false;
        }
        values.inspectionTemplateId = that.data.inspectionTemplateId;
        if (!values.inspectionTemplateId) {
            util.toast('请选择巡检模板');
            return false;
        }

        util.post({
            url: '/api/meeting/info/addBean',
            data: values,
            success: function (res) {
                console.info(res);
                if (res.data.code == 200) {
                    util.toast(res.data.msg);
                    setTimeout(() => {
                        wx.navigateBack({
                            delta: -1,
                        });
                    }, 1000);
                } else {
                    util.toast(res.data.msg);
                }
            }
        });
    },

    bindPickerChange: function (e) {
        let that = this;
        that.setData({
            inspectionTemplate: that.data.templetArr[e.detail.value],
            inspectionTemplateId: that.data.ids[e.detail.value]
        });
    }
})