import {
    ApiModel
} from "../../utils/api"
const _Http = new ApiModel();
Component({
    options: {
        addGlobalClass: true
    },
    /**
     * 组件的属性列表
     */
    properties: {
        /* 类型 */
        type: {
            type: String,
            value: "default" //annunciate
        },
        /* 标题 */
        title: {
            type: String,
        },
        /* 选中列表 */
        saleprodclass: {
            type: Array,
            value: []
        },
        /* 是否全选 */
        isAll: {
            type: Number
        },
        /* 回调 */
        saleprodChange: {
            type: Function
        },
        /* 分类列表 */
        dataList: {
            type: Array,
            value: []
        },
        /* 是否请求 */
        isRequest: {
            type: Boolean,
            value: true
        }
    },
    lifetimes: {
        ready: function () {
            //查询类目列表
            if (this.data.isRequest) _Http.basic({
                "accesstoken": wx.getStorageSync('userData').token,
                "classname": "enterprise.system.prodclass",
                "method": "query_typeselectList",
                "content": {}
            }, false).then(res => {
                if (res.msg != '成功') return;
                let dataList = [];
                for (let i = 0; i < res.data.length; i++) {
                    dataList.push({
                        value: res.data[i],
                        index: i,
                        checked: false
                    })
                }
                //遍历选中数据
                const arr = this.data.saleprodclass;
                for (let i = 0; i < arr.length; i++) {
                    for (let k = 0; k < dataList.length; k++) {
                        if (arr[i] == dataList[k].value) {
                            dataList[k].checked = true;
                            break;
                        }
                    }
                }
                this.setData({
                    dataList
                })
            });
            if (this.data.type == 'annunciate') {
                _Http.basic({
                    "accesstoken": wx.getStorageSync('userData').token,
                    "classname": "customer.tagents.tagents",
                    "method": "query_cooperation",
                    "content": {
                        "getdatafromdbanyway": true,
                        "pageNumber": 1,
                        "pageSize": 999,
                        "where": {
                            "condition": "",
                            "ftype": "",
                            "fstatus": ""
                        }
                    }
                }, false).then(res => {
                    if (res.msg != '成功') return wx.showToast({
                        title: res.data,
                        icon: "none"
                    })
                    let data = res.data,
                        partnerCount = 0;
                    if (this.data.isAll == 0) {
                        for (let i = 0; i < data.length; i++) {
                            data[i].index = i;
                            data[i].checked = true;
                            partnerCount = partnerCount + 1
                        }
                    } else {
                        for (let i = 0; i < data.length; i++) {
                            data[i].index = i;
                            data[i].checked = false;
                        }
                    }
                    this.setData({
                        dataList: data,
                        partnerCount,
                        manual: this.data.isAll
                    });
                })
            }
        },
    },
    observers: {
        'saleprodclass': function (saleprodclass) {
            if (this.data.type != "annunciate") return;
            setTimeout(() => {
                let dataList = this.data.dataList;
                for (let i = 0; i < dataList.length; i++) {
                    if (saleprodclass.some((v) => v == dataList[i].tcooperationagentsid)) {
                        dataList[i].checked = true;
                    }
                }
                this.setData({
                    dataList
                })
            }, 1000);
        }
    },
    /**
     * 组件的初始数据
     */
    data: {
        pitchOnList: [], //选中列表
        manual: 0, //是否手动选择发布范围
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /* 确定 */
        confirm() {
            if (this.data.type == 'default') {
                this.triggerEvent("saleprodChange", this.data.pitchOnList)
            } else if (this.data.type == 'annunciate') {
                let data = {
                    isAll: this.data.isAll
                };
                if (this.data.isAll == 1) {
                    let include = [], //选中的商户id
                        list = this.data.dataList,
                        deleteItem = [], //删除的商户id
                        saleprodclass = this.data.saleprodclass;
                    //选中商户id
                    for (let i = 0; i < list.length; i++) {
                        if (list[i].checked) include.push(list[i].tcooperationagentsid)
                    };
                    //删除商户id
                    if (saleprodclass.length >= 1) {
                        for (let i = 0; i < saleprodclass.length; i++) {
                            if (!include.some((v) => v == saleprodclass[i])) {
                                deleteItem.push(saleprodclass[i])
                            }
                        }
                    };
                    data.include = include;
                    data.deleteItem = deleteItem;
                    if (include.length == 0) return wx.showToast({
                        title: '您还未选择可查看通告的合作伙伴',
                        icon: "none"
                    })
                    if (include.length == this.data.dataList.length) {
                        data.isAll = 0
                    } else {
                        data.isAll = 1
                    }
                };
                this.triggerEvent("saleprodChange", JSON.stringify(data));
            }
        },
        /* 多选框返回数值 */
        checkedChange(e) {
            this.setData({
                pitchOnList: e.detail.value
            });
        },
        /* 添加背景色 */
        pitchOn(e) {
            let dataList = this.data.dataList,
                index = e.currentTarget.dataset.index,
                partnerCount = this.data.partnerCount,
                isAll = 1;
            //计数
            if (dataList[index].checked) {
                partnerCount = partnerCount - 1
            } else {
                partnerCount = partnerCount + 1
            }
            isAll = (partnerCount == dataList.length) ? isAll = 0 : isAll = 1; //是否指定范围
            dataList[index].checked = !dataList[index].checked; //切换状态
            if (this.data.manual == 1) isAll = 1;
            this.setData({
                dataList,
                isAll,
                partnerCount
            })
        },
        /* 商户全选或指定范围 */
        allOrPart(e) {
            const {
                value
            } = e.detail;
            if (value == 0) {
                let dataList = this.data.dataList;
                for (let i = 0; i < dataList.length; i++) {
                    dataList[i].checked = true;
                }
                this.setData({
                    dataList
                })
            };
            this.setData({
                isAll: value,
                manual: value
            })
        }
    }
})