import {
    ApiModel
} from "../../utils/api";
const _Http = new ApiModel;
import {
    TestVerify
} from "../../utils/verify";
const _Verify = new TestVerify();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        tnoticeid: 0, //通告id
        ftitle: "", //通告标题
        fsummary: "", //概述
        fcontent: "", //通告内容
        ftype: "", //通告类型
        popups: false,
        checkboxShow: false, //多选
        fisspecifiedrange: 0, // 是否指定范围，0否 1 是
        include: [], //包含范围
        partnerList: [], //合作商列表
        coverFiles: [], //封面
        defaultFiles: [], //附件
        errTips: {
            ftitle: false,
            fsummary: false,
            fcontent: false,
            ftype: false
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //修改
        if (options.id) {
            _Http.basic({
                "accesstoken": wx.getStorageSync('userData').token,
                "classname": "customer.noticemag.noticemag",
                "method": "query_noticeMain",
                "content": {
                    "tnoticeid": options.id
                }
            }).then(res => {
                console.log("详情", res)
                if (res.msg != '成功') return wx.showToast({
                    title: res.data,
                    icon: "none"
                });
                let data = res.data[0],
                    attinfos = res.data[0].attinfos,
                    coverFiles = [],
                    defaultFiles = [],
                    ftype = (data.ftype == '商户') ? '合作商通告' : '团队通告';
                for (let i = 0; i < attinfos.length; i++) {
                    let img = {
                        name: attinfos[i].serialnumber,
                        url: attinfos[i].fobsurl,
                        ownerid: attinfos[i].ownerid,
                        tattachmentid: attinfos[i].tattachmentid,
                        ftype: attinfos[i].ftype,
                        ownertable: attinfos[i].ownertable
                    };
                    (img.ftype == "default") ? defaultFiles.push(img): coverFiles.push(img)
                }
                if (data.ftype == '商户' && data.fisspecifiedrange == 1) {
                    //查询指定范围
                    _Http.basic({
                        "accesstoken": wx.getStorageSync('userData').token,
                        "classname": "customer.noticemag.noticemag",
                        "method": "query_noticeuserList",
                        "content": {
                            "tnoticeid": options.id,
                            "getdatafromdbanyway": true
                        }
                    }).then(s => {
                        console.log("查询范围", s)
                        if (s.msg != '成功') return wx.showToast({
                            title: res.data,
                            icon: "none"
                        });
                        let include = [];
                        for (let i = 0; i < s.data.length; i++) {
                            include.push(s.data[i].tagentsid)
                        }
                        this.setData({
                            include
                        })
                    })
                }
                this.setData({
                    tnoticeid: data.tnoticeid,
                    ftitle: data.ftitle,
                    fsummary: data.fsummary,
                    fcontent: data.fcontent,
                    ftype: ftype,
                    coverFiles,
                    defaultFiles,
                    fisspecifiedrange: data.fisspecifiedrange
                })
            })
        }
    },
    //范围选择回调
    scopeCallBack({
        detail
    }) {
        let data = JSON.parse(detail);
        console.log(data)
        if (data.isAll == 0) {
            this.setData({
                fisspecifiedrange: data.isAll,
                checkboxShow: false
            })
        } else {
            this.setData({
                deleteList: data.deleteItem,
                include: data.include,
                fisspecifiedrange: data.isAll,
                checkboxShow: false
            })
        }
    },
    /* 修改图片回调 */
    imageChange(e) {
        const {
            fileList
        } = e.detail;
        this.setData({
            coverFiles: fileList
        })
    },
    /* 修改附加回调 */
    filesChange(e) {
        const {
            fileList
        } = e.detail;
        this.setData({
            defaultFiles: fileList
        })
    },
    /* 提交 */
    submit() {
        if (!this.formVerify()) return wx.showToast({
            title: '请检查表单内容',
            icon: "error"
        });
        let ftype = (this.data.ftype == '团队通告') ? '团队' : '商户';
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.noticemag.noticemag",
            "method": "insertOrModify",
            "content": {
                "tnoticeid": this.data.tnoticeid,
                "ftype": ftype,
                "ftitle": this.data.ftitle,
                "fisspecifiedrange": this.data.fisspecifiedrange,
                "fsummary": this.data.fsummary,
                "fcontent": this.data.fcontent,
            }
        }).then(res => {
            console.log("新增或修改", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            if (this.data.tnoticeid == 0) _Http.basic({
                "accesstoken": wx.getStorageSync('userData').token,
                "classname": "customer.noticemag.noticemag",
                "method": "check",
                "content": {
                    "tnoticeid": res.data[0].tnoticeid,
                    "fischeck": 1
                }
            }).then(s => {
                console.log("修改上架", s)
            })
            //指定范围，删除
            if (ftype == '商户') {
                if (this.data.fisspecifiedrange == 1) {
                    //默认新增
                    let body = {
                        "accesstoken": wx.getStorageSync('userData').token,
                        "classname": "customer.noticemag.noticemag",
                        "method": "adduser",
                        "content": {
                            "tnoticeid": res.data[0].tnoticeid,
                            "tagentsid": this.data.include
                        }
                    }
                    console.log(this.data.include)
                    _Http.basic(body).then(res => {
                        console.log("添加范围", res)
                    });
                    //删除
                    let deleteList = this.data.deleteList;
                    body.method = 'deleteuser';
                    body.content.tagentsid = deleteList;
                    if (deleteList.length >= 1) {
                        _Http.basic(body).then(res => {
                            console.log("删除范围", res)
                        });
                    }
                }
            }
            wx.showToast({
                title: '保存成功',
            });
            let content = {
                ownerid: res.data[0].tnoticeid,
                ownertable: "tnotice",
                tattachmentid: 0
            };
            this.selectComponent("#UploadFiles").saveTheChanges(content);
            this.selectComponent("#UpFiles").saveTheChanges(content);
            setTimeout(() => {
                wx.navigateBack()
            }, 500)
        })
    },
    /* 表单验证 */
    formVerify() {
        let errTips = this.data.errTips,
            verify = true;
        /* 验证标题  */

        if (!_Verify.required(this.data.ftitle)) {
            errTips.ftitle = true;
            verify = false;
        }
        /* 验证概述 */
        if (!_Verify.required(this.data.fsummary)) {
            errTips.fsummary = true;
            verify = false;
        }
        /* 验证内容  */
        if (!_Verify.required(this.data.fcontent)) {
            errTips.fcontent = true;
            verify = false;
        }
        /* 发布范围 */
        if (!_Verify.required(this.data.ftype)) {
            errTips.ftype = true;
            verify = false;
        }
        this.setData({
            errTips
        })
        return verify;
    },
    /* 获取焦点 */
    inputFocus(e) {
        const {
            name
        } = e.currentTarget.dataset;
        let errTips = this.data.errTips;
        errTips[name] = false;
        this.setData({
            errTips
        })
    },
    /* 失去焦点 */
    inputBlur(e) {
        const {
            name
        } = e.currentTarget.dataset;
        const {
            value
        } = e.detail;
        if (value.trim() == "") {
            let errTips = this.data.errTips;
            errTips[name] = true;
            this.setData({
                errTips
            })
        }
    },
    /* input事件剔除特殊字符 */
    eliminate(value) {
        const {
            name
        } = value.target.dataset;
        this.setData({
            [name]: _Verify.Eliminate(value.detail)
        })
    },
    /* 发布范围回调 */
    radioChange({
        detail
    }) {
        let ftype = (detail.length != 0) ? detail : this.data.ftype;
        console.log(ftype)
        this.setData({
            ftype,
            popups: false,
            'errTips.ftype': false
        })
    },
    /* 打开类目选择 */
    showPop() {
        this.setData({
            popups: !this.data.popups
        })
    },
    checkboxShowPop() {
        this.setData({
            checkboxShow: !this.data.checkboxShow
        })
    },
    /* 跳转富文本 */
    toRichText() {
        const that = this;
        const fcontent = encodeURIComponent(this.data.fcontent);
        wx.navigateTo({
            url: '/pages/storeMessage/editor/editor?fintroduction=' + fcontent,
            events: {
                richTextCallBack: function (richText) {
                    let fcontent = null;
                    (richText.richText == '<p><br></p>') ? fcontent = "": fcontent = richText.richText;
                    that.setData({
                        fcontent: fcontent,
                        'errTips.fcontent': false
                    })
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})