export default {
    data() {
        return {
            page: 1,
            pageSize: Number(localStorage.getItem('pageSize') || 20),
            totalPages: 0,
            totalElements: 0,
            tableData: [],
            typeStr: [],
            sort: {},
            sortStr: 'createdAt,desc',
            tableHeight: 200,
            yylyList: [],
            sblxList: [],
            rzjdList: []
        };
    },
    created() {
        this.page = Number(this.$route.query.page) || 1;
        if (this.$route.query.sort) {
            let sort = {};
            for (let s of this.$route.query.sort.split(';')) {
                if (s) {
                    let [prop, order] = s.split(',');
                    sort[prop] = order;
                }
            }
            this.sort = sort;
        }
        this.getData();
    },
    mounted() {
        //this.tableHeight = document.querySelector('.el-table').getBoundingClientRect().height;
    },
    methods: {
        getType(strType, id) {
            // debugger;
            if ('融资类型' === strType) {
                //this.query.resSnDPropertyList.push({ name: type, value: id });
                this.show.industryType.id = id;
            } else if ('应用领域' === strType) {
                //this.query.resSnDPropertyList.push({ name: type, value: id });
                this.show.applicationField.id = id;
            } else if ('融资阶段' === strType) {
                //this.query.resSnDPropertyList.push({ name: type, value: id });
                this.show.stageType.id = id;
            } else if ('技术类型' === strType) {
                this.show.techType.id = id;
            } else if ('设备类型' === strType) {
                this.show.dataType.id = id;
            }

            let strTypeIndex = this.query.resSnDPropertyList.indexOf(strType);
            let valueIndex = this.query.resSnDPropertyList.indexOf(id);
            if (strTypeIndex == -1 && valueIndex == -1) {
                this.typeStr.push('name:' + strType + ',value:' + id);
            } else {
                // debugger;
                this.typeStr.forEach((item, index) => {
                    let strIndex = item.indexOf(strType);
                    if (strIndex != -1) {
                        this.typeStr.splice(index, 1);

                        console.log(this.typeStr);
                    }
                });
                this.typeStr.push('name:' + strType + ',value:' + id);
            }
            // debugger;
            // this.typeStr = [];
            // this.typeStr.push(id);
            // this.query.contact = this.typeStr.join(';');
            this.query.resSnDPropertyList = this.typeStr.join(';');
            this.getData();
        },
        getDataType(data, list) {
            this.$http
                .get('/dataType/all', data)
                .then(res => {
                    if (res.content.length > 0) {
                        res.content.forEach(item => {
                            list.push({
                                label: item.name,
                                value: item.id
                            });
                        });
                    }
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        },
        getData() {
            let urlQuery = { ...this.$route.query };
            delete urlQuery.sort;
            delete urlQuery.page;
            delete urlQuery.size;
            let data = {
                page: this.page - 1,
                size: this.pageSize,
                sort: this.sortStr,
                query: urlQuery
            };
            if (this.beforeGetData) {
                let mergeData = this.beforeGetData();
                if (mergeData) {
                    data = { ...data, ...mergeData };
                }
            }
            this.$http
                .get(this.url, data)
                .then(res => {
                    if (this.url == '/conversation/all') {
                        this.tableData = res.data.content;
                        this.totalPages = res.data.totalPages;
                        this.totalElements = res.data.totalElements;
                    } else {
                        this.tableData = res.content;
                        this.totalPages = res.totalPages;
                        this.totalElements = res.totalElements;
                    }
                    if (this.afterGetData) {
                        this.afterGetData(res);
                    }
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        },
        changeSort(prop, order) {
            let newOrder = '';
            if (order) {
                if (this.sort[prop] !== order) {
                    newOrder = order;
                } else {
                    newOrder = '';
                }
            } else {
                if (this.sort[prop] === 'asc') {
                    newOrder = 'desc';
                } else if (this.sort[prop] === 'desc') {
                    newOrder = '';
                } else {
                    newOrder = 'asc';
                }
            }
            this.sort = { ...this.sort, [prop]: newOrder };
        },
        onSortChange(e) {
            this.sort = e;
            this.getData();
        },
        onSizeChange(e) {
            localStorage.setItem('pageSize', e);
            this.page = 1;
            this.pageSize = e;
            this.getData();
        },
        onCurrentChange(e) {
            this.$router
                .replace({
                    query: {
                        ...this.$router.query,
                        page: e
                    }
                })
                .catch(_ => {});
            this.page = e;
            this.getData();
        },
        adjustColumnWidth() {
            let canvas = document.createElement('canvas');
            let ctx = canvas.getContext('2d');
            let measureTxt = text => {
                return parseInt(ctx.measureText(text).width);
            };
            let rowWidth = [];
            let headWidth = [];
            document.querySelectorAll('.el-table__body-wrapper .table-row').forEach(row => {
                row.querySelectorAll('.table-cell').forEach((cell, index) => {
                    let children = cell.querySelector('.cell').children;
                    if (children && children.length) {
                        let width = 0;
                        console.log(children);
                        for (let child of children) {
                            width +=
                                child.clientWidth +
                                parseInt(window.getComputedStyle(child).marginLeft.replace('px', '')) +
                                parseInt(window.getComputedStyle(child).marginRight.replace('px', ''));
                        }
                        rowWidth[index] = width;
                    } else {
                        console.log(cell.querySelector('.cell').innerHTML);
                        rowWidth[index] = measureTxt(cell.querySelector('.cell').innerHTML);
                    }
                });
            });
            this.$refs.table.columns.forEach(({ label }, index) => {
                headWidth[index] = measureTxt(label) + 50;
            });
            console.log(rowWidth);
            console.log(headWidth);
        }
    },
    watch: {
        sort() {
            let sortStr = [];
            for (let [key, value] of Object.entries(this.sort)) {
                if (value) {
                    sortStr.push(key + ',' + value);
                }
            }
            sortStr = sortStr.join(';');
            this.$router
                .replace({
                    query: {
                        ...this.$route.query,
                        sort: sortStr
                    }
                })
                .catch(_ => {});
            this.sortStr = sortStr;
            this.getData();
        }
    }
};
