import { matchType } from '../../utils/dict';
import WxValidate from '../../utils/wxValidate';
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        frameStyle: { useTop: true, name: '信息维护', leftArrow: true, useBar: false },
        typeList: matchType,
        form: {},
        logo: [],
        id: ''
    },
    initValidate() {
        const rules = { type: { required: true }, name: { required: true }, start_time: { required: true }, end_time: { required: true }, address: { required: true }, sign_end_time: { required: true }, money_remark: { required: true }, money_mode: { required: true }, contact: { required: true }, explain: { required: true }, regular: { required: true } }
        // 验证字段的提示信息，若不传则调用默认的信息
        const messages = { type: { required: '请选择比赛类别' }, name: { required: '请输入比赛名称' }, start_time: { required: '比赛开始时间' }, end_time: { required: '比赛结束时间' }, address: { required: '请输入比赛地点' }, sign_end_time: { required: '报名截止时间' }, money_remark: { required: '请输入报名费用说明' }, money_mode: { required: '请输入付款方式' }, contact: { required: '请输入联系方式' }, explain: { required: '请输入报名说明' }, regular: { required: '请输入赛事规程' } };
        this.WxValidate = new WxValidate(rules, messages)
    },
    // 跳转菜单
    back(e) {
        wx.navigateBack({ delta: 1 })
    },
    // 选择比赛类别
    typeChange: function (e) {
        const that = this;
        let data = that.data.typeList[e.detail.value];
        if (data) that.setData({ 'form.type': data.label })
    },
    // 选择比赛开始时间
    startChange: function (e) {
        const that = this;
        that.setData({ 'form.start_time': e.detail.value });
    },
    // 选择比赛结束时间
    endChange: function (e) {
        const that = this;
        that.setData({ 'form.end_time': e.detail.value });
    },
    // 选择报名截止日期
    dateChange: function (e) {
        const that = this;
        that.setData({ 'form.sign_date': e.detail.value });
    },
    // 选择报名截止时间
    timeChange: function (e) {
        const that = this;
        that.setData({ 'form.sign_time': e.detail.value });
    },
    // 上传图片
    imgUpl: function (e) {
        const that = this;
        let data = that.data.logo;
        data.push(e.detail)
        that.setData({ logo: data })
    },
    // 删除图片
    imgDel: function (e) {
        const that = this;
        let list = that.data.logo;
        let arr = list.filter((i, index) => index != e.detail.index)
        that.setData({ logo: arr })
    },
    onSubmit: async function (e) {
        const that = this;
        const params = e.detail.value;
        params.sign_end_time = params.sign_date + '-' + params.sign_time;
        const logo = that.data.logo;
        if (!this.WxValidate.checkForm(params)) {
            const error = this.WxValidate.errorList[0];
            wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
            return false
        } else {
            if (logo && logo.length > 0) {
                params.logo = logo;
                let arr;
                if (that.data.id) arr = await app.$post(`/newCourt/api/match/${that.data.id}`, params);
                else arr = await app.$post('/newCourt/api/match', params);
                if (arr.errcode == '0') {
                    wx.showToast({ title: `信息维护成功`, icon: 'success', duration: 2000 });
                    that.back()
                } else {
                    wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 });
                }
            } else { wx.showToast({ title: `请上传logo`, icon: 'fail', duration: 2000 }); }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const that = this;
        that.setData({ id: options.id || '62e388d4fd61d871861b80af' })
        //验证规则函数
        that.initValidate();
        // 监听用户是否登录
        that.watchLogin();
    },
    watchLogin: function () {
        const that = this;
        wx.getStorage({
            key: 'user',
            success: async (res) => {
                if (that.data.id) {
                    const arr = await app.$get(`/newCourt/api/match/${that.data.id}`);
                    if (arr.errcode == '0') {
                        if (arr.data.sign_end_time) {
                            arr.data.sign_date = arr.data.sign_end_time.substring(0, 10);
                            arr.data.sign_time = arr.data.sign_end_time.substring(11, arr.data.sign_end_time.length);
                        }
                        that.setData({ form: arr.data })
                        that.setData({ logo: arr.data.logo })
                    } else { wx.showToast({ title: `${res.errMsg}`, icon: 'fail', duration: 2000 }); }
                }
            },
            fail: async (res) => {
                wx.redirectTo({ url: '/pages/index/index' });
            },
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})