<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.title"></el-input>
                            </el-form-item>
                        </el-col>-->
                        <el-col :span="4">
                            <el-form-item>
                                <el-input placeholder="请输入专家" v-model="searchForm.expertName"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-input placeholder="请输入评论人" v-model="searchForm.userName"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item>
                                <el-date-picker
                                    type="daterange"
                                    range-separator="至"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                                    v-model="time"
                                    format="yyyy-MM-dd"
                                    value-format="timestamp"
                                    class="time"
                                ></el-date-picker>
                            </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 @click="batchPutaway(1)">批量显示</el-button>
                                <el-button type="primary" plain round @click="batchUnshelve(0)">批量屏蔽</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="title" label="标题" :show-overflow-tooltip="true"></el-table-column> -->
                    <el-table-column prop="expertName" label="专家" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="averageScore" label="评论星级" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="expression" label="关联诊单" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="userName" label="评论人" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="userUrl" label="头像">
                        <template slot-scope="scope">
                            <span @click="imgOpen(scope.row.userUrl)">
                            <el-image
                                :src="scope.row.userUrl | pathPipe"
                                style="width:60px;height:60px;cursor:pointer"
                                fit="cover"
                            ></el-image>
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="comment" label="评论内容" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column label="评论时间" :show-overflow-tooltip="true">
                        <template slot-scope="scope">{{ scope.row.createTime | formatTimePipe }}</template>
                    </el-table-column>
                    <el-table-column 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="操作" min-width="200px;">
                        <template slot-scope="scope">
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="deleteData(scope.row.id)"
                            >删除</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="showDetailDialog(scope.row)"
                            >详情</el-button>
                            <!-- <el-button size="mini" type="warning" plain round @click="shield(scope.row.id)"
                                >屏蔽</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>
        <!-- 详情弹层 -->
        <DetailsDialog ref="detailsDialog"></DetailsDialog>
    </div>
</template>
<script>
import TitleBar from '@/apps/mobile/components/common/TitleBar';
import DetailsDialog from './components/detailsDialog';
import { queryPage, removeData, editData, useBath, noUseBath } from '@/apps/mobile/api/comment/index';
export default {
    components: {
        TitleBar,
        DetailsDialog,
    },
    data() {
        return {
            searchForm: {},
            tableData: [],
            page: {
                pageNum: 1,
                pageSize: 5,
            },
            total: 5,
            listLoading: false,
            ids: [],
            time: [],
        };
    },
    methods: {
        // 页码变化时
        handlePagination(val) {
            // console.log(val, 'handlePagination');
            this.page = val;
            this.queryListData();
        },
        queryListData() {
            this.listLoading = true;
            let obj = {
                ...this.page,
                ...this.searchForm,
                sort: {
                    order: 'asc',
                    orderBy: 'createTime',
                },
            };
            if (this.time && this.time.length >= 2) {
                obj.starDate = this.time[0];
                obj.endDate = this.time[1];
            }

            queryPage(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;
                });
        },
        /**
         * table选择列被改变时
         * @param {Array} data 选中的列
         */
        selectionChange(data) {
            this.ids = data.map(item => {
                return item.id;
            });
        },
        /**
         * 删除功能
         * @param {String} type 授权操作类型:'batch'批量授权  'single'单个授权
         * @param {Number} id 当前需要删除的id或集合
         */
        deleteData(id, type = 'single') {
            let obj = {};
            if (type === 'batch') {
                if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
                // console.log(this.ids, "ids");
                obj = {
                    ids: this.ids
                }
                this.delete(obj);
            } else {
                obj = {
                    ids: [id]
                }
                this.delete(obj);
            }
        },
        delete(obj) {
            this.$confirm('确定要删除该数据吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                closeOnClickModal: false,
            })
                .then(() => {
                    removeData(obj)
                        .then(() => {
                            this.$message.success('删除成功');
                            this.queryListData();
                        })
                        .catch(() => {});
                })
                .catch(() => {});
        },
        // 启用状态改变时
        switchChange(val, id) {
            console.log(val, id);
            let obj = {
                statuz: val,
                id,
            };
            editData(obj)
                .then(() => {
                    this.$message.success('修改成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 批量显示
        batchPutaway(status) {
            if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
            useBath({ids: this.ids, status})
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 批量屏蔽
        batchUnshelve(status) {
            if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
            noUseBath({ids: this.ids, status})
                .then(() => {
                    this.$message.success('操作成功');
                    this.queryListData();
                })
                .catch(err => err);
        },
        // 查询
        query() {
            for (let k in this.searchForm) {
                if (this.searchForm[k] === '') {
                    delete this.searchForm[k];
                }
            }
            this.page.pageNum = 1;
            this.queryListData();
        },
        // 重置
        reset() {
            this.searchForm = {};
            this.time = [];
            this.page.pageNum = 1;
            this.queryListData();
        },
        showDetailDialog(data) {
            this.$refs.detailsDialog.open(data);
        },
        // shield(id) {
        //     let obj = {
        //         id,
        //         statuz: 1,
        //     };
        //     editData(obj).then(() => {
        //         this.$message.success('操作成功');
        //         this.queryListData();
        //     });
        // },
        imgOpen(img){
            window.open(img);
        }
    },
    created() {
        this.queryListData();
    },
};
</script>
<style lang="stylus" scoped>
.TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
    width: 95%;
}

::v-deep .el-date-editor .el-range-separator {
    width: auto;
}

.main-container {
    padding: 0 15px 15px;
}
</style>
