<template>
    <div class="common-container">
        <TitleBar></TitleBar>
        <!-- 搜索栏与表格区域 -->
        <div class="main-container">
            <div class="search-container TableSelectMain">
                <el-form
                    class="table-select-main widthAuto"
                    :model="searchForm"
                    label-position="left"
                >
                    <el-row :gutter="15">
                        <el-col :span="4">
                            <el-form-item>
                                <el-input placeholder="请输入品牌名称" v-model="searchForm.brandName"></el-input>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="4">
                            <el-form-item>
                                <el-select placeholder="文件分类" v-model="searchForm.dataTable">
                                    <el-option label="tsg" :value="1"></el-option>
                                    <el-option label="品牌资料" :value="2"></el-option>
                                    <el-option label="系统资料" :value="3"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col> -->
                        <el-col :span="4">
                            <el-form-item>
                                <el-input placeholder="请输入文件名" v-model="searchForm.name"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-select placeholder="启用状态" v-model="searchForm.statuz">
                                    <el-option label="开" :value="1"></el-option>
                                    <el-option label="关" :value="0"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="4">
                            <el-form-item>
                                <el-select placeholder="请选择资料分类" v-model="searchForm.catagoryId">
                                    <el-option
                                        v-for="item in fileCategoryList"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col> -->
                        <el-col :span="4">
                            <el-form-item>
                                <el-button type="primary" round @click="querySearch">查询</el-button>
                                <el-button type="primary" plain round @click="reset">重置</el-button>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item>
                                <el-button
                                    type="primary"
                                    round
                                    icon="el-icon-plus"
                                    @click="showAddEditDialog(null)"
                                >新建</el-button>
                                <el-button type="primary" plain round @click="openBatch">批量启用</el-button>
                                <el-button type="primary" plain round @click="batchClose(null)">批量关闭</el-button>
                                <el-button type="primary" plain round @click="deleteData(null)">批量删除</el-button>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>

            <div class="SearchTitle">
                已选择
                <span class="subject-color">{{tableSelection.length}}</span> 项
            </div>
            <div class="MaintenanceTable">
                <el-table
                    ref="multipleTable"
                    :data="tableData"
                    style="width: 100%"
                    tooltip-effect="dark"
                    @selection-change="selectColumn"
                    class="OutTable"
                    border
                    v-loading="listLoading"
                >
                    <el-table-column type="selection"></el-table-column>
                    <el-table-column prop="brandName" label="品牌名称"></el-table-column>
                    <el-table-column prop="name" label="文件名" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="dataTable" label="文件类型">
                        <template slot-scope="scope">
                            <span>{{scope.row.dataTable | fileTypePipe}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="catagoryName" label="资料分类"></el-table-column>
                    <el-table-column prop="browseNum" label="浏览量">
                        <template slot-scope="scope">
                            <span>{{scope.row.browseNum || 0}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="downloadNum" label="下载量">
                        <template slot-scope="scope">
                            <span>{{scope.row.downloadNum || 0}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="statuz" label="启用状态">
                        <template slot-scope="scope">
                            <el-switch
                                v-model="scope.row.statuz"
                                active-text="开"
                                inactive-text="关"
                                :active-value="1"
                                :inactive-value="0"
                                @change="changeSwitch(scope.row)"
                            ></el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" min-width="200px;">
                        <template slot-scope="scope">
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="deleteData(scope.row)"
                            >删除</el-button>
                            <el-button
                                size="mini"
                                type="success"
                                plain
                                round
                                @click="showAddEditDialog(scope.row)"
                            >编辑</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagenation">
                    <PageComponent
                        :total="total"
                        @pagination="handlePagination"
                        :pageSize.sync="page.pageSize"
                        :pageNum.sync="page.pageNum"
                    ></PageComponent>
                </div>
            </div>
        </div>
        <!-- 添加与编辑弹层 -->
        <AddEditDialog ref="addEditDialog" @queryListData="queryListData"></AddEditDialog>
    </div>
</template>
<script>
import TitleBar from '@/apps/mobile/components/common/TitleBar';
import AddEditDialog from './components/addEditDialog';
import {
    queryLifeFilesList,
    updateLifeFilesStatus,
    openFilesBatch,
    deleteFilesSingle,
    deleteFilesBatch,
    queryLiftCategorysList,
    closeFilesBatch
} from '@/apps/mobile/api/databank/index';
export default {
    components: {
        TitleBar,
        AddEditDialog,
    },
    data() {
        return {
            searchForm: {},
            tableData: [],
            page: {
                pageNum: 1,
                pageSize: 5,
            },
            total: 10,
            listLoading: false,
            tableSelection: [],
            fileCategoryList: [],
        };
    },
    created() {
        this.queryListData();
        // this.queryFileCategorysList();
    },
    methods: {
        queryListData() {
            this.listLoading = true;
            let obj = {
                ...this.page,
                ...this.searchForm,
                dataTable: 2,
                sort: {
                    order: 'desc',
                    orderBy: 'create_time',
                },
            };
            queryLifeFilesList(obj)
                .then(res => {
                    console.log(res);
                    if(res.statusCode == "10"){
                        this.$router.replace("/login");
                    }
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                    this.listLoading = false;
                })
                .catch();
        },
        // queryFileCategorysList() {
        //     let obj = {
        //         pageNum: 0,
        //         pageSize: 5000,
        //     };
        //     queryLiftCategorysList(obj)
        //         .then(res => {
        //             console.log(res);
        //             this.fileCategoryList = res.data.records;
        //         })
        //         .catch();
        // },
        changeSwitch(data) {
            console.log(data);
            let obj = {
                statuz: data.statuz ? 1 : 0,
                id: data.id,
            };
            console.log(obj);
            updateLifeFilesStatus(obj)
                .then(res => {
                    console.log(res);
                    this.queryListData();
                    this.$message({
                        type: 'success',
                        message: '修改成功',
                    });
                })
                .catch();
        },
        openBatch() {
            if (!this.tableSelection.length) {
                this.$message({
                    type: 'warning',
                    message: '请先选择一项！',
                });
                return false;
            }
            this.$confirm('确定要启用吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                closeOnClickModal: false,
            })
                .then(() => {
                    const ids = this.tableSelection.map(item => {
                        return item.id;
                    });
                    console.log(ids);
                    openFilesBatch({ids: ids})
                        .then(res => {
                            this.queryListData();
                            this.$message({
                                type: 'success',
                                message: '修改成功',
                            });
                        })
                        .catch();
                })
                .catch(() => {});
        },
        deleteData(data) {
            if (data) {
                this.$confirm('确定要删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(() => {
                        deleteFilesSingle({id: data.id})
                            .then(res => {
                                console.log(res);
                                this.queryListData();
                                this.$message({
                                    type: 'success',
                                    message: '删除成功',
                                });
                            })
                            .catch();
                    })
                    .catch(() => {});
            } else {
                if (!this.tableSelection.length) {
                    this.$message({
                        type: 'warning',
                        message: '请先选择一项！',
                    });
                    return false;
                }
                this.$confirm('确定要删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    closeOnClickModal: false,
                })
                    .then(() => {
                        const ids = this.tableSelection.map(item => {
                            return item.id;
                        });
                        console.log(ids);
                        deleteFilesBatch({ids: ids})
                            .then(res => {
                                this.queryListData();
                                this.$message({
                                    type: 'success',
                                    message: '删除成功',
                                });
                            })
                            .catch();
                    })
                    .catch(() => {});
            }
        },
        querySearch() {
            this.queryListData();
        },
        reset() {
            this.searchForm = {};
            this.page.pageNum = 1;
            this.queryListData();
        },
        selectColumn(selection) {
            this.tableSelection = selection;
        },
        // 页码变化时
        handlePagination(val) {
            // console.log(val, 'handlePagination');
            this.page = val;
            this.queryListData();
        },
        showAddEditDialog(data) {
            if (data) {
                this.$refs.addEditDialog.open('edit', data);
            } else {
                this.$refs.addEditDialog.open('add');
            }
        },
        batchClose() {
            let ids = this.tableSelection.map(item => {
                return item.id;
            });
            if (ids.length === 0) {
                this.$message.warning('请先选择一项！');
                return false;
            }
            closeFilesBatch({ids: ids})
                .then(res => {
                    if (res.statusCode === '1') {
                        this.$message.success('操作成功');
                    } else {
                        this.$message.warning(res.message);
                    }
                    this.queryListData();
                })
                .catch();
        },
    },
};
</script>
<style lang="stylus" scoped>
.TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
    width: 95%;
}
</style>
