<template>
    <filter-list
        v-model="list"
        ref="list"
        url="/technologyProduct/all"
        :filters="filters"
        :sortKey="sortKey"
        @btnEvent="addInfo"
        @init="init"
    >
        <template v-for="item in list">
            <product-row :key="item.id" :info="item"></product-row>
        </template>
        <technology-product-add ref="public" @refreash="refreash"></technology-product-add>
    </filter-list>
</template>

<script>
import FilterList from '../../components/page/FilterList.vue';
import {
    stageOptions,
    tradingMethodOptionsDemand,
    commissionTypeOptions,
    moneyOptions,
    tradingMethodOptionsDemands
} from '../../utils/variables';
import ProductRow from '../../components/list/ProductRow.vue';
import TechnologyProductAdd from '../../components/popup/TechnologyProductAdd.vue';
export default {
    components: { FilterList, ProductRow, TechnologyProductAdd },
    data() {
        return {
            stageOptions,
            tradingMethodOptionsDemand,
            commissionTypeOptions,
            moneyOptions,
            tradingMethodOptionsDemands,
            fieldOptions: [], //领域,
            list: [],
            sortKey: [
                {
                    name: '发布时间',
                    key: 'createdAt'
                },
                {
                    name: '价格区间',
                    key: 'expectedPrice'
                },
                {
                    name: '浏览量',
                    key: 'view'
                }
            ]
        };
    },
    created() {
        this.init();
    },
    computed: {
        filters() {
            const type = [this.TECHNOLOGY, this.PRODUCT];
            return type[this.$route.query.tpType == 'TECHNOLOGY' ? 0 : 1];
        },
        TECHNOLOGY() {
            return [
                {
                    name: '所属领域',
                    key: 'field',
                    list: [...this.fieldOptions]
                },
                {
                    name: '所处阶段',
                    key: 'stage',
                    list: [...this.stageOptions]
                },
                {
                    name: '交易方式',
                    key: 'tradingMethodId',
                    list: [...this.tradingMethodOptionsDemand]
                },
                {
                    name: '价格区间',
                    key: 'money',
                    list: [...this.moneyOptions]
                },
                {
                    name: '发布时间',
                    key: 'createdAt',
                    list: [...this.createdAtOptions]
                }
            ];
        },
        PRODUCT() {
            return [
                {
                    name: '所属领域',
                    key: 'field',
                    list: [...this.fieldOptions]
                },
                {
                    name: '所处阶段',
                    key: 'stage',
                    list: [...this.stageOptions]
                },
                {
                    name: '交易方式',
                    key: 'tradingMethodId',
                    list: [...this.tradingMethodOptionsDemands]
                },
                {
                    name: '价格区间',
                    key: 'money',
                    list: [...this.moneyOptions]
                },
                {
                    name: '发布时间',
                    key: 'createdAt',
                    list: [...this.createdAtOptions]
                }
            ];
        },
        createdAtOptions() {
            return [
                {
                    label: '一周内',
                    value: this.getTimeBefor()
                },
                {
                    label: '一月内',
                    value: this.getTimeOnem()
                },
                {
                    label: '二月内',
                    value: this.getTimeTom()
                },
                {
                    label: '三月内',
                    value: this.getTimeTherrm()
                },
                {
                    label: '三月以上',
                    value: this.getTimeTherrms()
                }
            ];
        }
    },
    methods: {
        init() {
            this.$http
                .post('/setting/byFlag', { flag: 1 })
                .then(res => {
                    if (res.length > 0) {
                        this.fieldOptions = res.map(item => {
                            return {
                                label: item.name,
                                value: item.id
                            };
                        });
                    }

                    this.$nextTick(() => {
                        this.$refs.list.init();
                    });
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        },
        addInfo() {
            this.checkLogin(true).then(() => {
                this.$refs.public.init(this.$route.query.tpType);
            });
        },
        refreash() {
            this.$refs.list.getData();
        }
    }
};
</script>

<style lang="less" scoped></style>
