import { unionBy,unionWith, remove as _remove, cloneDeep } from "lodash";

export default {
    data() {
        return {
            selectItemList: []
        };
    },
    props: {
        select: {
            type: Boolean,
            default: false
        },
        // 如果需要选择, 提供选择的字段名
        selectField: {
            default: "selected"
        },
        // 选择限制
        selectLimit: {
            default: 0
        },
        // 默认选中的去重字段
        selectUnionKey: {
            default: "id"
        },
        // 默认选中的去重字段是多个时使用该字段
        multipleSelectUnionKey: {
            default() {
                return ["id"];
            }
        },
        // 默认选中哪些
        selectDefault: {
            default() {
                return [];
            }
        }
    },
    watch: {
        selectDefault: {
            immediate: true,
            handler(v) {
                this.selectItemList = cloneDeep(v || []);
                this.dataList.forEach((item) => {
                    const isS =
                        this.selectItemList.filter((v) => {
                            if (this.multipleSelectUnionKey.length > 1) {
                                let result = true;
                                this.multipleSelectUnionKey.forEach(e => {
                                    result = result && v[e] == item[e]
                                });
                                return result;
                            }
                            else {
                                return v[this.selectUnionKey] == item[this.selectUnionKey]
                            }
                        }).length != 0;
                    item[this.selectField] = isS;
                });
            }
        }
    },
    computed: {
        // 全选
        selectAll: {
            get() {
                return this.dataList.length != 0 && this.dataList.filter((v) => !v[this.selectField]).length == 0;
            },
            set(check) {
                this.onSelectAll(check);
            }
        },
        //是否可以显示全选
        showSelectAll() {
            return this.selectLimit == 0 || this.selectLimit >= this.dataList.length;
        },
        //已选中的长度
        selectLength() {
            return this.selectItemsList.length;
        }
    },
    methods: {
        /**
         * 把数据转换成已选择
         */
        checkSelect(dataList) {
            if (!this.select) return dataList;
            // debugger
            const newData = dataList.map((item) => {
                const isS =
                    this.selectItemList.filter((v) => {
                        if (this.multipleSelectUnionKey.length > 1) {
                            let result = true;
                            this.multipleSelectUnionKey.forEach(e => {
                                if(v[e] != item[e])
                                {
                                    result=false;
                                }
                            });
                            return result;
                        }
                        else {
                            return v[this.selectUnionKey] == item[this.selectUnionKey]
                        }
                        // v[this.selectUnionKey] == item[this.selectUnionKey]
                    }).length != 0;
                return {
                    ...item,
                    [this.selectField]: isS
                };
            });
            return newData;
        },
        /**
         * 非选中剔除
         * @param {*} item
         */
        onUnSelectItem(item) {
            item[this.selectField] = false;
            _remove(this.selectItemList, (v) => {
                if (this.multipleSelectUnionKey.length > 1) {
                    let result = true;
                    this.multipleSelectUnionKey.forEach(e => {
                        if(v[e] != item[e])
                        {
                            result=false;
                        }
                    });
                    return result;
                }
                else {
                    return v[this.selectUnionKey] == item[this.selectUnionKey]
                }
                // return v[this.selectUnionKey] == item[this.selectUnionKey];
            });
        },
        /**
         * 选中项目
         * @param {*} item
         */
        onSelectItem(item) {
            item[this.selectField] = true;
            this.selectItemList = unionWith(this.selectItemList, [item], this.comparator);
        },
        /**
         *
         */
        onSelectEmit() {
            this.$emit("select", cloneDeep(this.selectItemList));
        },
        /**
         * 全部选中
         */
        onSelectAll(isSelect) {
            // 非选中不管
            if (!isSelect) {
                this.dataList.forEach(this.onUnSelectItem);
                this.onSelectEmit();
                return;
            }
            // 判断数目, 需要和已选择的一起判断
            if (
                this.selectLimit != 0 &&
                unionWith(this.dataList, this.selectDefault, this.comparator).length > this.selectLimit
            ) {
                this.onError(`最多选择${this.selectLimit}项数据`);
                return;
            }
            this.dataList.forEach(this.onSelectItem);
            this.onSelectEmit();
        },
        comparator(arrVal, othVal)
        {
            if(!arrVal || !othVal) return false;
            if (this.multipleSelectUnionKey.length > 1) {
                let result = true;
                this.multipleSelectUnionKey.forEach(e => {
                    if(arrVal[e] != othVal[e])
                    {
                        result=false;
                    }
                });
                return result;
            }
            else {
                return arrVal[this.selectUnionKey] == othVal[this.selectUnionKey]
            }
        },
        /**
         * 选中操作
         * @param {*} row
         */
        onSelect(row) {
            // 如果是非选中, 不管
            const isSelect = row[this.selectField];
            if (!isSelect) {
                this.onUnSelectItem(row);
                this.onSelectEmit();
                return;
            }
            switch (this.selectLimit) {
                // 只能选1个, 那么别的全部干掉
                case 1:
                    this.dataList.forEach(this.onUnSelectItem);
                    this.onSelectItem(row);
                    break;
                // 任意就不管了
                case 0:
                    this.onSelectItem(row);
                    break;
                // 有限制的话
                default:
                    if (this.selectLength > this.selectLimit) {
                        this.onUnSelectItem(row);
                        this.onError(`最多选择${this.selectLimit}项数据`);
                        return;
                    }
                    break;
            }
            this.onSelectEmit();
        }
    }
};
