<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.name"></el-input>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="4">
                            <el-form-item>
                                <el-select placeholder="请选择电梯品牌" v-model="searchForm.brandId" clearable>
                                    <el-option :label="item.name" :value="1" v-for="item in brandList" :key="item.id"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>-->
                        <el-col :span="4">
                            <el-form-item>
                                <!--  -->
                                <BrandList @brandChange="brandChange" ref="brand"></BrandList>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-input placeholder="输入供应商" v-model="searchForm.manufacturer"></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" plain round @click="query">查询</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"
                                    plain
                                    round
                                    icon="el-icon-plus"
                                    @click="create"
                                >新建</el-button>
                                <el-button type="primary" plain round @click="putaway">批量启用</el-button>
                                <el-button type="primary" plain round @click="soldOut">批量停止</el-button>
                                <!-- <el-button type="primary" plain round @click="showBatch">批量展示</el-button> -->
                                <!-- <el-button type="primary" plain round @click="cancleBatch">批量取消</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="name" label="商品名" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="brandName" label="品牌" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="imgs" label="图集">
                        <template slot-scope="scope">
                            <span v-if="scope.row.imgs[0]" @click="imgOpen(scope.row.imgs[0])">
                            <el-image
                                :src="scope.row.imgs[0] | pathPipe"
                                style="width:40px;height:40px;cursor:pointer"
                                fit="cover"
                            ></el-image>
                            </span>
                            <span v-else>暂无图片</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="price" label="推荐价格" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="manufacturer" label="供应商" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="telephone" label="联系电话" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="contactPerson" label="联系人" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="descr" label="描述" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="hotFlag" label="置顶" min-width="110px;">
                        <template slot-scope="scope">
                            <el-switch
                                v-model="scope.row.hotFlag"
                                active-text="开"
                                inactive-text="关"
                                :active-value="1"
                                :inactive-value="0"
                                @change="hotFlagChange(scope.row.id, scope.row.hotFlag)"
                            ></el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column prop="statuz" 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="statuzChange(scope.row.id, scope.row.statuz)"
                            ></el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" min-width="220px">
                        <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>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="showDetailsDialog(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 @queryListData="queryListData" ref="detailsDialog"></DetailsDialog>
    </div>
</template>
<script>
import DetailsDialog from './components/detailsDialog';
import TitleBar from '@/apps/mobile/components/common/TitleBar';
import AddEditDialog from './components/addEditDialog';
import BrandList from '@/apps/mobile/components/brandLongSerch/index'; // 电梯品牌远程搜索
import { queryPage, removeData, editData, useBath, noUseBath, showBath, noshowBath } from '@/apps/mobile/api/shop/index';
export default {
    components: {
        TitleBar,
        AddEditDialog,
        BrandList,
        DetailsDialog,
    },
    data() {
        return {
            searchForm: {}, // 搜索表单
            tableData: [], // 列表数据
            page: {
                // 页码
                pageNum: 1,
                pageSize: 5,
            },
            total: 5,
            listLoading: false, // table是否展示loading
            ids: [], // 选中的列
        };
    },
    methods: {
        create() {
            this.$refs.addEditDialog.open('add');
        },
        edit(data) {
            this.$refs.addEditDialog.open('edit', data);
        },
        showDetailsDialog(data) {
            this.$refs.detailsDialog.open(data);
        },
        /**
         * table选择列被改变时
         * @param {Array} selection 选中的列
         */
        selectionChange(data) {
            this.ids = data.map(item => {
                return item.id;
            });
        },
        handlePagination(val) {
            // console.log(val, 'handlePagination');
            this.page = val;
            this.queryListData();
        },

        queryListData() {
            this.listLoading = true;
            let obj = {
                ...this.page,
                ...this.searchForm,
                platform: 'web',
                sort: {
                    order: 'desc',
                    orderBy: 'create_time',
                },
            };
            queryPage(obj)
                .then(res => {
                    if(res.statusCode == "10"){
                        this.$router.replace("/login");
                    }
                    this.tableData = res.data.records;
                    this.tableData.forEach(item => {
                        item.imgs = item.imgs.split(',');
                    });
                    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('请至少选中一项 !');
                this.delete({ids: this.ids});
            } else {
                this.delete({ids: [id]});
            }
        },
        delete(id) {
            this.$confirm('确定要删除该数据吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                closeOnClickModal: false,
            })
                .then(() => {
                    removeData(id)
                        .then(() => {
                            this.$message.success('删除成功');
                            this.queryListData();
                        })
                        .catch(() => {});
                })
                .catch(() => {});
        },

        // 批量上架
        putaway() {
            if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
            useBath({ids: this.ids})
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 批量下架
        soldOut() {
            if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
            noUseBath({ids: this.ids})
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 批量首页展示
        showBatch() {
            if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
            showBath({ids: this.ids})
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 批量取消首页展示
        cancleBatch() {
            if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
            noshowBath({ids: this.ids})
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        hotFlagChange(id, hotFlag) {
            let obj = {
                id,
                hotFlag,
            };
            // console.log(obj,'hotFlag');
            this.editformData(obj);
        },
        statuzChange(id, statuz) {
            let obj = {
                id,
                statuz,
            };
            this.editformData(obj);
        },
        // 编辑
        editformData(data) {
            editData(data)
                .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.page.pageNum = 1;
            this.queryListData();
        },
        // 重置
        reset() {
            this.searchForm = {};
            this.$refs.brand.reset(); //清空品牌框内容
            this.page.pageNum = 1;
            this.queryListData();
        },
        brandChange(data) {
            console.log(data, 'diant');
            this.searchForm.brandId = data.id;
        },
        imgOpen(imgs){
            window.open(imgs);
        }
    },
    created() {
        this.queryListData();
    },
    mounted() {},
};
</script>
<style lang="stylus" scoped>
.TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
    width: 95%;
}
</style>
