<template>
    <div class="mini-type-list">
        <common-list
                :table_data="table_data"
                @handel_search="handel_search"
                @handel_delete="handel_delete"
                @handel_update="handel_update"
        >
        </common-list>
    </div>
</template>

<script>
    import CommonList from '@/components/CommonList'
    import {mapActions} from 'vuex'
    import miniTypeApi from '@/api/mini_type';

    export default {
        name: "MiniTypeList",
        data() {
            return {
                head_list: [
                    {
                        type: 'select',
                        model: 'classId',
                        placeholder: '请选择类别',
                        list: 'classList',
                        prop: {
                            value: 'id',
                            text: 'className'
                        }
                    },
                ],
                action_list: [
                    {
                        type: 'primary',
                        icon: 'el-icon-search',
                        value: '搜索',
                        click: 'handel_search',
                        params: 'params'
                    },

                    {
                        type: 'success',
                        icon: 'el-icon-circle-plus-outline',
                        value: '导入',
                        upload: true
                    },
                ],
                table_props: {},
                table_list: [
                    {label: 'id', prop: 'id'},
                    {label: '题型名称', prop: 'typeName', type: 'text'},
                    {label: '测试人数', prop: 'amount', type: 'text'},
                    {label: '真实测试人数', prop: 'realAmount', type: 'text'},
                    {label: '轮播', prop: 'carousel', type: 'bool_tag'},
                    {label: '猜你喜欢', prop: 'love', type: 'bool_tag'},
                    {label: '每日推荐', prop: 'recommend', type: 'bool_tag'},
                    {label: '热点', prop: 'hot', type: 'bool_tag'},
                ],
                table_data: [],
                dialog_list: [{label: 'id', prop: 'id', placeholder: '请输入id'}],
                dialog_data: {}
            }
        },
        components: {
            CommonList
        },
        mounted() {
            this.init();
        },
        methods: {
            ...mapActions([
                'set_head_list',
                'set_table_list',
                'set_head_data_map',
                'set_table_data',
                'set_action_list',
                'set_dialog_list',
                'set_dialog_data',
            ]),
            init() {
                this.set_head_list(this.head_list);

                this.set_action_list(this.action_list);

                this.set_table_list(this.table_list);

                this.handel_search();

                this.get_head_data();
            },
            get_head_data() {
                let that = this;
                miniTypeApi.class_list().then(res => {
                    this.set_head_data_map({classList: res});
                })
            },
            handel_search(params) {
                let that = this;
                miniTypeApi.list(params).then(res => {
                    this.set_table_data(res);
                })

            },

            convert_tree(list) {
                list.forEach(f => {
                    if (f.question || f.option) {
                        f.children = f.question || f.option;
                        this.convert_tree(f.question || f.option)
                    }
                })
            },

            handel_delete(id) {
                console.log(id)
            },
            handel_update(id, callback) {
                let dto = {id: id};
                this.set_dialog_list(this.dialog_list);
                this.set_dialog_data(dto);
                callback()
            },

        }
    }
</script>

<style lang="less" scoped>
    .mini-type-list {
        height: 100%;
    }
</style>
