<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-input placeholder="请输入英文名" v-model="searchForm.title"></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-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="homeDisplay(null)">批量展示</el-button> -->
                                <el-button type="primary" plain round @click="deleteData(null)">批量删除</el-button>
                                <!-- <el-button type="primary" plain round @click="batchClose(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="系统名称"></el-table-column>
                    <el-table-column prop="logo" label="logo">
                        <template slot-scope="scope">
                            <span @click="imgOpen(scope.row.logo)">
                            <el-image
                                :src="scope.row.logo | pathPipe"
                                style="width:40px;height:40px;cursor:pointer"
                            ></el-image>
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="descr" label="描述" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="sort" label="排序"></el-table-column>
                    <el-table-column prop="topFlag" label="主页展示">
                        <template slot-scope="scope">
                            <el-switch
                                v-model="scope.row.topFlag"
                                active-text="是"
                                inactive-text="否"
                                :active-value="1"
                                :inactive-value="0"
                                @change="homeDisplay(scope.row)"
                            ></el-switch>
                        </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="150">
                        <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 {
    queryOperatingSystemList,
    deleteOperatingSystemSingle,
    deleteOperatingSystemBatch,
    updateOperatingSystemStatus,
    showOperatingSystemBatch,
    batchCloseBrand
} 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: [],
            addEditDialogVisible: false,
            addEditDialogType: 'add',
            addEditDialogForm: {},
        };
    },
    created() {
        this.queryListData();
    },
    methods: {
        queryListData() {
            this.listLoading = true;
            let obj = {
                ...this.page,
                ...this.searchForm,
                sort: {
                    order: 'desc',
                    orderBy: 'sort',
                    // orderBy: 'create_time',
                },
            };
            queryOperatingSystemList(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(() => {
                    this.listLoading = false;
                });
        },
        deleteData(data) {
            if (data) {
                this.$confirm('确定要删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(() => {
                        deleteOperatingSystemSingle({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);
                        deleteOperatingSystemBatch({ids: ids})
                            .then(res => {
                                this.queryListData();
                                this.$message({
                                    type: 'success',
                                    message: '删除成功',
                                });
                            })
                            .catch();
                    })
                    .catch(() => {});
            }
        },
        changeSwitch(data) {
            console.log(data);
            let obj = {
                statuz: data.statuz ? 1 : 0,
                id: data.id,
            };
            console.log(obj);
            this.updateStatus(obj);
        },
        homeDisplay(data) {
            console.log(data);
            if (data) {
                let obj = {
                    topFlag: data.topFlag ? 1 : 0,
                    id: data.id,
                };
                console.log(obj);
                this.updateStatus(obj);
            } else {
                if (!this.tableSelection.length) {
                    this.$message({
                        type: 'warning',
                        message: '请先选择一项！',
                    });
                    return false;
                }
                this.$confirm('确定要主页展示吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    closeOnClickModal: false,
                })
                    .then(() => {
                        let ids = this.tableSelection.map(item => {
                            return item.id;
                        });
                        console.log(ids);
                        showOperatingSystemBatch(ids)
                            .then(res => {
                                this.queryListData();
                                this.$message({
                                    type: 'success',
                                    message: '修改成功',
                                });
                            })
                            .catch();
                    })
                    .catch(() => {});
            }
        },
        updateStatus(obj) {
            updateOperatingSystemStatus(obj)
                .then(res => {
                    console.log(res);
                    this.queryListData();
                    this.$message({
                        type: 'success',
                        message: '修改成功',
                    });
                })
                .catch();
        },
        querySearch() {
            this.queryListData();
        },
        reset() {
            this.searchForm = {};
            this.page.pageNum = 1;
            this.queryListData();
        },
        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');
            }
        },
        batchClose() {
            let ids = this.tableSelection.map(item => {
                return item.id;
            });
            if (ids.length === 0) {
                this.$message.warning('请先选择一项！');
                return false;
            }
            batchCloseBrand(ids)
                .then(res => {
                    if (res.statusCode === '1') {
                        this.$message.success('操作成功');
                    } else {
                        this.$message.warning(res.message);
                    }
                    this.queryListData();
                })
                .catch();
        },
        imgOpen(imgs){
            window.open(imgs);
        }
    },
};
</script>
<style lang="stylus" scoped>
.TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
    width: 95%;
}
</style>
