<template>
    <filter-list
        v-model="list"
        ref="list"
        url="/logoDemand/all"
        :filters="filters"
        :sortKey="sortKey"
        isDemand
        @btnEvent="btnEvent"
        @init="init"
    >
        <template v-for="item in list">
            <logo-demand :key="item.id" :info="item"></logo-demand>
        </template>
        <logo-demand-add ref="public" @refreash="refreash"></logo-demand-add>
    </filter-list>
</template>

<script>
import FilterList from '../../components/page/FilterList.vue';
import { tradingMethodOption, commissionTypeOptions, moneyOptions } from '../../utils/variables';
import LogoDemandAdd from '../../components/popup/LogoDemandAdd.vue';
import LogoDemand from '../../components/list/LogoDemand.vue';
export default {
    components: { FilterList, LogoDemand, LogoDemandAdd },
    data() {
        return {
            tradingMethodOption,
            commissionTypeOptions,
            moneyOptions,
            categoryOptions: [], //分类,
            list: [],
            sortKey: [
                {
                    name: '发布时间',
                    key: 'createdAt'
                },
                {
                    name: '预算',
                    key: 'expectedPrice'
                },
                {
                    name: '浏览量',
                    key: 'view'
                }
            ]
        };
    },
    created() {
        this.init();
    },
    computed: {
        filters() {
            return [
                {
                    name: '所属分类',
                    key: 'category',
                    list: [...this.categoryOptions]
                },
                {
                    name: '交易方式',
                    key: 'modeId',
                    list: [...this.tradingMethodOption]
                },
                {
                    name: '预算区间',
                    key: 'expectedPrice',
                    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: 2 })
                .then(res => {
                    if (res.length > 0) {
                        this.categoryOptions = 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);
                });
        },
        btnEvent() {
            this.checkLogin(true).then(() => {
                this.$refs.public.init();
            });
        },
        refreash() {
            this.$refs.list.getData();
        }
    }
};
</script>

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