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

<script>
import FilterList from '../../components/page/FilterList.vue';
import { tradingMethodOption, moneyOptions } from '../../utils/variables';
import CopyrightDemandRow from '../../components/list/CopyrightDemandRow.vue';
import CopyrightDemandAdd from '../../components/popup/CopyrightDemandAdd.vue';
export default {
    components: { FilterList, CopyrightDemandRow, CopyrightDemandAdd },
    data() {
        return {
            tradingMethodOption,
            moneyOptions,
            workTypeeOptions: [], //融资目的,
            list: [],
            sortKey: [
                {
                    name: '发布时间',
                    key: 'createdAt'
                },
                {
                    name: '预算',
                    key: 'expectedPrice'
                },
                {
                    name: '浏览量',
                    key: 'view'
                }
            ]
        };
    },
    created() {
        this.init();
    },
    computed: {
        type() {
            return this.$route.query.type;
        },
        filters() {
            return [
                {
                    name: `${this.type === 'COPY' ? '作品类型' : '软著类型'}`,
                    key: 'workType',
                    list: [...this.workTypeeOptions]
                },
                {
                    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: this.$route.query.type == 'COPY' ? 3 : 4 })
                .then(res => {
                    if (res.length > 0) {
                        this.workTypeeOptions = 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(this.$route.query.type);
            });
        },
        refreash() {
            this.$refs.list.getData();
        }
    }
};
</script>

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