<!-- TSG官方文件管理 -->
<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.name"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item>
                                <el-date-picker
                                    class="time"
                                    value-format="timestamp"
                                    v-model="time"
                                    type="daterange"
                                    range-separator="-"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                                ></el-date-picker>
                            </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-button type="primary" plain 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="deleteData(null)">批量删除</el-button>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>

            <div class="SearchTitle">
                已选择
                <span class="subject-color">4</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="name" label="文件名" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="url" label="文件">
                        <template slot-scope="scope">
                            <a
                                :href="staticUrl+scope.row.url"
                                target="_blank"
                                style="color:#606266;"
                            >{{scope.row.url}}</a>
                        </template>
                    </el-table-column>
                    <el-table-column prop="createTime" label="创建时间">
                        <template slot-scope="scope">
                            <span>{{ scope.row.createTime | formatTimePipe }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="sort" label="排序"></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="210">
                        <template slot-scope="scope">
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="deleteData(scope.row)"
                            >删除</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="showAddEditDialog(scope.row)"
                            >编辑</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="showDetailDialog(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>
        <!-- 详情弹层 -->
        <DetailsDialog ref="detailsDialog"></DetailsDialog>
    </div>
</template>
<script>
import { staticUrl } from '@/config.js';
import TitleBar from '@/apps/mobile/components/common/TitleBar';
import AddEditDialog from './components/addEditDialog';
import DetailsDialog from './components/detailsDialog';
import { queryLifeFilesList, updateLifeFilesStatus, deleteFilesSingle, deleteFilesBatch } from '@/apps/mobile/api/databank/index';
export default {
    components: {
        TitleBar,
        AddEditDialog,
        DetailsDialog,
    },
    data() {
        return {
            searchForm: {},
            tableData: [],
            page: {
                pageNum: 1,
                pageSize: 5,
            },
            total: 10,
            time: [],
            listLoading: false,
            tableSelection: [],
            staticUrl: staticUrl,
        };
    },
    created() {
        this.queryListData();
    },
    methods: {
        queryListData() {
            this.listLoading = true;
            let obj = {
                dataTable: 1,
                ...this.page,
                ...this.searchForm,
                sort: {
                    order: 'desc',
                    orderBy: 'sort',
                },
            };
            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();
        },
        querySearch() {
            if (this.time && this.time.length >= 2) {
                this.$set(this.searchForm, 'startTime', this.time[0]);
                this.$set(this.searchForm, 'endTime', this.time[1]);
            }
            this.queryListData();
        },
        reset() {
            this.searchForm = {};
            this.time = [];
            this.queryListData();
        },
        changeSwitch(data) {
            let obj = {
                statuz: data.statuz ? 1 : 0,
                id: data.id,
            };
            updateLifeFilesStatus(obj)
                .then(res => {
                    console.log(res);
                    this.queryListData();
                    this.$message({
                        type: 'success',
                        message: '修改成功',
                    });
                })
                .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(() => {});
            }
        },
        selectColumn(selection) {
            this.tableSelection = selection;
        },
        // 页码变化时
        handlePagination(val) {
            this.page = val;
            this.queryListData();
        },
        showAddEditDialog(data) {
            if (data) {
                this.$refs.addEditDialog.open('edit', data);
            } else {
                this.$refs.addEditDialog.open('add');
            }
        },
        showDetailDialog(data) {
            this.$refs.detailsDialog.open(data);
        },
    },
};
</script>
<style lang="stylus" scoped>
.TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
    width: 95%;
}
</style>
