const _Http = getApp().globalData.http;

Page({
    data: {
        ownerid: 0,
        ownertable: 0,
        list: [], //全部标签
        copy: [], //全部标签拷贝 用来模糊搜索
        forbiddens: [], //相斥禁用
        actives: [], //选中数组
    },
    onLoad(options) {
        if (options.ownertable) {
            _Http.basic({
                "id": 20220929085401,
                "content": {
                    "ownertable": options.ownertable,
                    "ownerid": options.ownerid
                }
            }).then(res => {
                let forbiddens = [];
                res.data.datatag.filter(value => {
                    let item = res.data.option.find(v => value == v.tag);
                    if (!item || item.mutextag.length == 0) return;
                    forbiddens = forbiddens.concat(item.mutextag)
                })
                this.setData({
                    list: res.data.option, //全部标签
                    copy: res.data.option, //全部标签拷贝 用来模糊搜索
                    actives: res.data.datatag, //选中数组
                    forbiddens
                })
            })
            this.setData({
                ...options
            })
        }
    },
    //选中标签
    onChange(e) {
        const {
            tag,
            mutextag
        } = e.currentTarget.dataset.item;
        if (!tag || e.currentTarget.dataset.disabled) return;
        let actives = this.data.actives,
            forbiddens = this.data.forbiddens;
        if (actives.some(v => v == tag)) {
            //已经选中
            actives = actives.filter(v => v != tag)
            if (mutextag.length != 0) mutextag.forEach(v => {
                let index = forbiddens.findIndex(value => value == v);
                if (index != -1) forbiddens.splice(index, 1);
            })
        } else {
            //未选中
            actives.push(tag)
            if (mutextag.length != 0) forbiddens = forbiddens.concat(mutextag);
        }
        this.setData({
            actives,
            forbiddens
        })
    },
    /* 搜索 */
    onInput({
        detail
    }) {
        this.setData({
            list: detail == '' ? this.data.copy : this.data.copy.filter(v => v.tag.includes(detail))
        })
        //用来搜索的时候初始化选中项
        setTimeout(() => {
            this.setData({
                actives: this.data.actives
            })
        }, 100)
    },
    /* 提交 */
    submit() {
        _Http.basic({
            "id": 20220929090901,
            "content": {
                "ownertable": this.data.ownertable,
                "ownerid": this.data.ownerid,
                "datatag": this.data.actives
            }
        }).then(res => {
            console.log("提交标签", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            wx.showToast({
                title: '保存成功!',
                icon: "none"
            });
            setTimeout(() => {
                getCurrentPages().forEach(v => {
                    if (v.getTags) v.getTags();
                })
                wx.navigateBack();
            }, 300)
        })
    },
})