<template>
    <div class="common-container my-open-img">
        <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.name"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-input placeholder="请输入英文名" v-model="searchForm.ename"></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="6">
                            <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="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">{{this.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="name" label="品牌名称" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="ename" label="英文名" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="logo" label="logo">
                        <template slot-scope="scope">
                            <!-- <span @click="imgOpen(scope.row.logo)"></span> -->
                            <el-image
                                :src="scope.row.logo | pathPipe"
                                style="width:40px;height:40px;cursor:pointer"
                                fit="cover"
                                :preview-src-list="[scope.row.logo]"
                            ></el-image>
                        </template>
                    </el-table-column>
                    <el-table-column prop="descr" label="描述" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="createTime" label="创建时间" :show-overflow-tooltip="true">
                        <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="hotFlag" label="热门推荐">
                        <template slot-scope="scope">
                            <el-switch
                                v-model="scope.row.hotFlag"
                                active-text="开"
                                inactive-text="关"
                                :active-value="1"
                                :inactive-value="0"
                                @change="changeHotFlag(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="160px;">
                        <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>
                            <el-button
                                size="mini"
                                type="warning"
                                plain
                                round
                                @click="goDetails(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 {
    queryLifeBrandList,
    updateLifeBrandStatus,
    deleteBrandsSingle,
    deleteBrandsBatch,
    batchCloseBrand,
} from '@/apps/mobile/api/databank/index';
export default {
    name: 'brand-manage',
    components: {
        TitleBar,
        AddEditDialog,
    },
    data() {
        return {
            searchForm: {}, // 搜索表单
            tableData: [],
            page: {
                pageNum: 1,
                pageSize: 5,
            },
            total: 10,
            listLoading: false, // table是否展示loading
            addEditDialogVisible: false,
            addEditDialogType: 'add',
            tableSelection: [],
        };
    },
    created() {
        this.queryListData();
    },
    methods: {
        queryListData() {
            this.listLoading = true;
            let obj = {
                ...this.page,
                ...this.searchForm,
                sort: {
                    order: 'desc',
                    orderBy: 'create_time',
                },
            };
            queryLifeBrandList(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();
        },
        querySearch() {
            this.page.pageNum = 1;
            this.queryListData();
        },
        reset() {
            this.searchForm = {};
            this.queryListData();
        },
        changeHotFlag(data) {
            let obj = {
                hotFlag: data.hotFlag ? 1 : 0,
                id: data.id,
            };
            updateLifeBrandStatus(obj)
                .then(res => {
                    this.queryListData();
                    this.$message({
                        type: 'success',
                        message: '修改成功',
                    });
                })
                .catch();
        },
        changeSwitch(data) {
            let obj = {
                statuz: data.statuz ? 1 : 0,
                id: data.id,
            };
            updateLifeBrandStatus(obj)
                .then(res => {
                    this.queryListData();
                    this.$message({
                        type: 'success',
                        message: '修改成功',
                    });
                })
                .catch();
        },
        deleteData(data) {
            if (data) {
                this.$confirm('确定要删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(() => {
                        deleteBrandsSingle(data.id)
                            .then(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;
                        });
                        deleteBrandsBatch(ids)
                            .then(res => {
                                this.queryListData();
                                this.$message({
                                    type: 'success',
                                    message: '删除成功',
                                });
                            })
                            .catch();
                    })
                    .catch(() => {});
            }
        },
        selectColumn(selection) {
            this.tableSelection = selection;
        },
        // 页码变化时
        handlePagination(val) {
            this.page = val;
            this.queryListData();
        },
        goDetails(data) {
            this.$router.push({
                path: `brand-details`,
                query: { 
                    id: data.id
                },
            });
        },
        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>
.my-open-img{
    .TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
        width: 95%;
    }
    ::v-deep .el-image{
        img{
            pointer-events: auto;
        }

        .el-icon-circle-close {
            font-size: 50px;
            color: #fff;
        }
    }
}

</style>
