<template>
    <div class="admin-hangyezixun-page">
        <div class="admin-hangyexuzhi">
            <div class="top">
                <div>
                    <mySelect
                            label="name"
                            id="newsClassifyId"
                            placeholder="请选择新闻分类"
                    ></mySelect>
                    <el-button class="btn-search" size="mini" type="primary" @click="handleSearch">查询</el-button>
                </div>
                <div>
                    <el-button size="mini" type="primary" @click="handleAddHangYeZiXun">新增</el-button>
                    <el-button size="mini" type="primary" @click="handleDeleteHangYeZiXun">删除
                    </el-button>
                </div>
            </div>
            <myTable
                    @editor="handleEditor"
                    @delete="handleDelete"
                    @select-change="handleSelectChange"
            />
            <myPagination
                    class="my-Pagination"
                    :pageSizes="defaultPageSizes"
                    @size-change="handleSizeChange"
                    @current-page="handleCurrentPage"
            />
        </div>
    </div>
</template>

<script>
    import myPagination         from './components/Pagination';
    import myTable              from './components/Table';
    import * as HYZXApi         from '@/api/news';
    import * as NewsClassifyApi from '@/api/newsClassify';
    import mySelect             from './components/Select';
    import Vue                  from 'vue';

    const CHANGEPAGECODE = {
        add:    'add',
        editor: 'editor',
    };
    const selectData = {
        options:        [],
        newsClassifyId: null,
        placeholder:    '请选择新闻资讯分类',
    };
    const tableData = {
        myData: [],
    };

    const pagination = {
        page:  1,
        size:  10,
        total: 0,
    };

    const defaultPageSizes = [10, 20, 30, 40];
    const tableServiceData = Vue.observable(tableData);
    const selectServiceData = Vue.observable(selectData);
    const paginationServiceData = Vue.observable(pagination);

    export default {
        name:       'Hangyexuzhi',
        provide:    {
            tableServiceData,
            paginationServiceData,
            selectServiceData,
        },
        data() {
            return {
                defaultPageSizes: defaultPageSizes,
                curIiId:          null,
                tableSelectData:  [],
            };
        },
        components: {
            myPagination,
            myTable,
            mySelect,
        },
        computed:   {
            myPage() {
                return paginationServiceData.page;
            },
            mySize() {
                return paginationServiceData.size;
            },
            myTotal() {
                return paginationServiceData.total;
            },
            newsClassifyId() {
                return selectServiceData.newsClassifyId;
            },
        },
        methods:    {

            handleSearch() {
                this.getHangYeZiXunData();
            },
            handleEditor(data) {
                this.curIiId = data.iiId;
                this.changePage(CHANGEPAGECODE.editor);
            },
            handleDelete(data) {
                const opt = {
                    iiIds: [data.iiId],
                };
                this.getHangYeZiXunDelete(opt);
            },
            handleSelectChange(data) {
                this.tableSelectData = data;
            },
            handleAddHangYeZiXun() {
                this.changePage(CHANGEPAGECODE.add);
            },
            handleSizeChange(size) {
                paginationServiceData.size = size;
                this.getHangYeZiXunData();
            },
            handleCurrentPage(page) {
                paginationServiceData.page = page;
                this.getHangYeZiXunData();
            },
            async handleDeleteHangYeZiXun() {
                if (!this.tableSelectData.length) {
                    this.$message.warning('请选择至少一条需要删除的数据');
                    return;
                }
                const opt = {
                    iiIds: [...this.tableSelectData],
                };
                const loading = this.$loading({ background: 'rgba(0, 0, 0, 0.7)' });
                try {
                    const res = await HYZXApi.getNewsDelete(opt);
                    loading.close();
                    if (res.code === 0 && res.data) {
                        // 删除成功重置选择状态
                        this.tableSelectData = [];
                        const newOpt = this.getOption();
                        const res = await HYZXApi.getNewsList(newOpt);
                        if (res.code === 0) {
                            this.initPageData(res.data);
                        }
                        loading.close();
                    }
                } catch (err) {
                    loading.close();
                }
            },
            getOption() {
                const opt = {
                    page:           this.myPage,
                    size:           this.mySize,
                    newsClassifyId: this.newsClassifyId,
                };
                return opt;
            },
            changePage(code) {
                switch (code) {
                    case CHANGEPAGECODE.add:
                        this.$router.push({ name: 'newsConfig', query: { code: 'add' } });
                        break;
                    case CHANGEPAGECODE.editor:
                        this.$router.push({ name: 'newsConfig', query: { code: 'editor', iiId: this.curIiId } });
                        break;
                    default:
                        break;
                }
            },
            // 获取 数据
            async getHangYeZiXunData() {
                const opt = this.getOption();
                const loading = this.$loading({ background: 'rgba(0, 0, 0, 0.7)' });

                try {
                    const res = await HYZXApi.getNewsList(opt);
                    if (res.code === 0) {
                        // 渲染页面
                        this.initPageData(res.data);
                    }
                    loading.close();
                } catch (e) {
                    loading.close();
                }
            },
            // 删除
            async getHangYeZiXunDelete(opt) {
                const loading = this.$loading({ background: 'rgba(0, 0, 0, 0.7)' });
                try {
                    const res = await HYZXApi.getNewsDelete(opt);
                    loading.close();
                    if (res.code === 0 && res.data) {
                        const res = await HYZXApi.getNewsList(opt);
                        if (res.code === 0) {
                            this.initPageData(res.data);
                        }
                        loading.close();
                    }
                } catch (err) {
                    loading.close();
                }
            },
            // 渲染页面
            initPageData(listData) {
                tableServiceData.myData = listData.data;
                paginationServiceData.total = listData.total;
            },
            // 修正初始化
            async initPageService() {
                const loading = this.$loading({ background: 'rgba(0, 0, 0, 0.7)' });
                try {
                    // 新闻资讯分类
                    const { data: newsClassifyArr } = await NewsClassifyApi.getNewsClassifySearch();
                    selectServiceData.options = newsClassifyArr;

                    // 新闻资讯列表
                    const opt = this.getOption();
                    const { code, data } = await HYZXApi.getNewsList(opt);
                    if (code === 0) {
                        // 渲染页面
                        this.initPageData(data);
                    }

                    loading.close();
                } catch (e) {
                    loading.close();
                }
            },
            clearPageData() {
                selectServiceData.options = [];
                selectServiceData.newsClassifyId = null;
                tableServiceData.myData = [];
                paginationServiceData.page = 1;
                paginationServiceData.size = 10;
                paginationServiceData.total = 0;
            }
        },
        created() {
            this.initPageService();
        },
        beforeDestroy() {
            this.clearPageData();

        },
    };
</script>

<style lang="scss" scoped>
    .admin-hangyexuzhi {
        padding: 20px;

        .top {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;

            .btn-search {
                margin-left: 20px;
            }
        }

        .my-Pagination {
            margin-top: 20px;
        }
    }
</style>