<template>
    <div class="common-container">
        <TitleBar></TitleBar>
        <div class="main-container">
            <div class="search-container TableSelectMain">
                <el-form class="table-select-main" :model="searchForm" label-position="left">
                    <el-row :gutter="15">
                        <el-col :span="4">
                            <el-form-item>
                                <el-input placeholder="请输入名称" v-model="searchForm.imgName"></el-input>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="4">
                            <el-form-item>
                                <el-select placeholder="启用状态" v-model="searchForm.statuz" clearable>
                                    <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" round @click="query">查询</el-button>
                                <el-button type="info" plain round @click="reset">重置</el-button>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item>
                                <el-button
                                    type="primary"
                                    plain
                                    round
                                    icon="el-icon-plus"
                                    @click="create(null)"
                                >新建</el-button>
                                <!-- <el-button type="primary" plain round @click="batchUse">批量启用</el-button>
                                <el-button type="primary" plain round @click="batchClose">批量关闭</el-button> -->
                                <el-button
                                    type="primary"
                                    plain
                                    round
                                    @click="deleteData(ids, 'batch')"
                                >批量删除</el-button>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>

            <div class="SearchTitle">
                已选择
                <span class="subject-color">{{ ids.length }}</span> 项
            </div>

            <div class="MaintenanceTable">
                <el-table
                    ref="multipleTable"
                    :data="tableData"
                    style="width: 100%"
                    tooltip-effect="dark"
                    @selection-change="selectionChange"
                    class="OutTable"
                    border
                    v-loading="listLoading"
                >
                    <el-table-column type="selection"></el-table-column>
                    <!-- <el-table-column prop="ProjectCode" label="项目编号"></el-table-column> -->
                    <el-table-column prop="imgName" label="名称" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="planDate" label="图片" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <span @click="imgOpen(scope.row.image)">
                            <el-image
                                :src="scope.row.image | pathPipe"
                                style="width:40px;cursor:pointer"
                                fit="cover"
                                :preview-src-list="['https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg']"
                            ></el-image>
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="url" label="跳转链接" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="jumpType" label="跳转类型">
                        <template slot-scope="scope">
                            <span v-show="scope.row.jumpType == 1">pdf</span>
                            <span v-show="scope.row.jumpType == 2">网页链接</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="type" label="显示位置">
                        <template slot-scope="scope">
                            <span v-show="scope.row.type == 1">电梯管家端banner图</span>
                            <span v-show="scope.row.type == 2">物业端banner图</span>
                            <span v-show="scope.row.type == 3">4.0首页banner图</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="serialNo" label="排序"></el-table-column>
                    <!-- <el-table-column prop="planDate" label="启用状态" min-width="110px;">
                        <template slot-scope="scope">
                            <el-switch
                                v-model="scope.row.statuz"
                                active-text="开"
                                inactive-text="关"
                                :active-value="1"
                                :inactive-value="0"
                                @change="switchChange(scope.row.statuz, scope.row.id)"
                            ></el-switch>
                        </template>
                    </el-table-column> -->

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="deleteData(scope.row.id)"
                            >删除</el-button>
                            <el-button
                                size="mini"
                                type="success"
                                plain
                                round
                                @click="edit(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 @queryListData="queryListData" ref="addEditDialog"></AddEditDialog>
    </div>
</template>
<script>
import TitleBar from '@/apps/mobile/components/common/TitleBar';
import AddEditDialog from './components/addEditDialog';
import { queryPageBanner, deleteBanner, editBanner, noUseBathBanner, useBathBanner } from '@/apps/mobile/api/banner/index';
export default {
    components: {
        TitleBar,
        AddEditDialog,
    },
    data() {
        return {
            searchForm: {}, // 搜索表单
            tableData: [], // 列表数据
            page: {
                pageNum: 1,
                pageSize: 5,
            },
            total: 0,
            listLoading: false,
            ids: [],
        };
    },
    methods: {
        selectionChange(data) {
            this.ids = data.map(item => {
                return item.id;
            });
        },
        /**
         * 新增
         */
        create() {
            this.$refs.addEditDialog.open('add');
        },
        /**
         * 编辑
         */
        edit(data) {
            this.$refs.addEditDialog.open('edit', data);
        },
        queryListData() {
            this.listLoading = true;
            let obj = {
                ...this.page,
                ...this.searchForm,
                sort: {
                    order: 'desc',
                    orderBy: 'create_time',
                },
            };
            queryPageBanner(obj)
                .then(res => {
                    if(res.statusCode == "10"){
                        this.$router.replace("/login");
                    }
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                    this.listLoading = false;
                })
                .catch(() => {
                    this.listLoading = false;
                });
        },
        /**
         * 删除功能
         * @param {String} type 授权操作类型:'batch'批量授权  'single'单个授权
         * @param {Number} id 当前需要删除的id或集合
         */
        deleteData(id, type = 'single') {
            if (type === 'batch') {
                if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
                // console.log(this.ids, "ids");

                this.delete({ids: this.ids});
            } else {
                this.delete({ids: [id]});
            }
        },
        delete(id) {
            this.$confirm('确定要删除该数据吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                closeOnClickModal: false,
            })
                .then(() => {
                    deleteBanner(id)
                        .then(() => {
                            this.$message.success('删除成功');
                            this.queryListData();
                        })
                        .catch(() => {});
                })
                .catch(() => {});
        },
        // 页码变化时
        handlePagination(val) {
            console.log(val, 'handlePagination');
            this.page = val;
            this.queryListData();
        },
        // 启用状态改变时
        switchChange(val, id) {
            console.log(val, id);
            let obj = {
                statuz: val,
                id,
            };
            editBanner(obj)
                .then(() => {
                    this.$message.success('修改成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 查询
        query() {
            for (let k in this.searchForm) {
                if (this.searchForm[k] === '' || this.searchForm[k] === null || this.searchForm[k] === undefined) {
                    delete this.searchForm[k];
                }
            }
            this.queryListData();
        },
        // 重置
        reset() {
            this.searchForm = {};
            this.queryListData();
        },
        // 批量关闭
        batchClose() {
            if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
            noUseBathBanner(this.ids)
                .then(() => {
                    this.$message.success('修改成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 批量开启
        batchUse() {
            if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
            useBathBanner(this.ids)
                .then(() => {
                    this.$message.success('修改成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        imgOpen(imgs){
            window.open(imgs);
        }
    },
    created() {
        this.queryListData();
    },
};
</script>
