import searchLayout from "@/components/grid/searchLayout";
import tableList from "@/components/grid/TableList";

export default {
    components: {
        searchLayout,
        tableList
    },
    data() {
        return {
            pagination: {
                //分页使用
                currentPage: 1,
                pageSize: 10,
                position: "right",
                pageSizes: [10, 30, 50, 100, 200],
                layout: "sizes, total, prev, pager, next, jumper",
            },
            selected: false, // 是否选中展示已选择项
            checkedArr: [], // 列表当前页已选
            checkedNames: [], // 列表所有已选
        };
    },
    props: {
        sceneType: {
            type: String,
            default: "",
        },
        nameType: {
            type: String,
            default: "",
        },
    },
    methods: {
        doSearch() {
            let data = {
                ...this.searchParams
            };
            if (this.selected) {
                data.allSceneNames = this.checkedNames;
            } else {
                data.allSceneNames = null;
            }

            this.refreshList(data);
        },
        //刷新table
        refreshList(param) {
            param
                ?
                this.$refs["table"].loadData(param) :
                this.$refs["table"].loadData();
        },
        // 数据加载后的回调
        loadedCallBack() {
            this.checkedArr = [];
            this.$refs.table.tableData.forEach((i) => {
                if (this.checkedNames.includes(i[this.sceneType])) {
                    // 显示已选中的项
                    this.$refs.table.$refs.ListTable.toggleRowSelection(
                        i,
                        true
                    );
                    this.checkedArr.push(i);
                }
            });
        },
        // 用户勾选完执行函数后的回调
        selectedCallBack(row, type) {
            if (type === 1) {
                if (!this.checkedNames.includes(row[this.sceneType])) {
                    this.checkedNames.push(row[this.sceneType]);
                }
            } else {
                let index = this.checkedNames.indexOf(row[this.sceneType]);
                if (index > -1) {
                    this.checkedNames.splice(index, 1);
                }
            }

            if (this.sceneType === "sceneName") {
                this.$emit("selectedData", this.sceneType, this.checkedNames, this.nameType);
            } else {
                this.$emit("selectedData", this.sceneType, this.checkedNames);
            }
        },
        // 用户勾选全选执行函数后的回调
        selectedAllCallBack(selection, type) {
            if (type === 1) {
                selection.forEach((i) => {
                    if (!this.checkedNames.includes(i[this.sceneType])) {
                        this.checkedNames.push(i[this.sceneType]);
                    }
                });
            } else {
                this.$refs.table.tableData.forEach((i) => {
                    let index = this.checkedNames.indexOf(i[this.sceneType]);
                    if (index > -1) {
                        this.checkedNames.splice(index, 1);
                    }
                });
            }

            if (this.sceneType === "sceneName") {
                this.$emit("selectedData", this.sceneType, this.checkedNames, this.nameType);
            } else {
                this.$emit("selectedData", this.sceneType, this.checkedNames);
            }
        },
        selectedShow() {
            this.doSearch();
        },
    },
}