<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="10">
                        <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.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>
                                <div>
                                    <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>
                                </div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="3" style="margin-left:100px;">
                            <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="enableBatch(null)"
                                >批量启用</el-button>
                                <el-button type="primary" plain round @click="deleteData(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="gender" label="协议类型">
                        <template slot-scope="scope">
                            <span v-if="scope.row.type ===1">用户协议</span>
                            <span v-if="scope.row.type ===2">专家协议</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="url" label="文件">
                        <template slot-scope="scope">
                            <a
                                :href="staticUrl+scope.row.url"
                                target="_blank"
                                style="color:#606266;"
                            >{{scope.row.url}}</a>
                        </template>
                    </el-table-column>
                    <el-table-column prop="createTime" label="创建时间">
                        <template slot-scope="scope">
                            <span>{{ scope.row.createTime | formatTimePipe }}</span>
                        </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="操作">
                        <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 { staticUrl } from '@/config.js';
import TitleBar from '@/apps/mobile/components/common/TitleBar';
import AddEditDialog from './components/addEditDialog';
import {
    queryExpertAgreementList,
    deleteExpertAgreementSingle,
    deleteExpertAgreementBatch,
    updateExpertAgreement,
    useExpertAgreementBatch,
    queryAllExpert,
} from '@/apps/mobile/api/expert/index';
export default {
    components: {
        TitleBar,
        AddEditDialog,
    },
    data() {
        return {
            searchForm: {},
            tableData: [],
            page: {
                pageNum: 1,
                pageSize: 5,
            },
            total: 10,
            listLoading: false,
            tableSelection: [],
            time: [],
            expertList: [],
            staticUrl: staticUrl,
        };
    },
    created() {
        this.queryListData();
        this.queryAllExpert();
    },
    methods: {
        queryListData() {
            this.listLoading = true;
            let obj = {
                ...this.page,
                ...this.searchForm,
                sort: {
                    order: 'desc',
                    orderBy: 'create_time',
                },
            };
            queryExpertAgreementList(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;
                });
        },
        queryAllExpert() {
            queryAllExpert()
                .then(res => {
                    console.log(res);
                    this.expertList = res.data;
                })
                .catch();
        },
        deleteData(data) {
            console.log(data);
            if (data) {
                this.$confirm('是否删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(() => {
                        deleteExpertAgreementSingle({id: data.id})
                            .then(res => {
                                console.log(res);
                                this.reset();
                                this.$message({
                                    type: 'success',
                                    message: '删除成功',
                                });
                            })
                            .catch();
                    })
                    .catch(() => {});
            } else {
                if (this.tableSelection.length === 0) {
                    this.$message({
                        type: 'warning',
                        message: '请先选择一个',
                    });
                    return false;
                }
                this.$confirm('是否删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                    .then(() => {
                        let ids = this.tableSelection.map(item => {
                            return item.id;
                        });
                        console.log(ids);
                        deleteExpertAgreementBatch({ids: ids})
                            .then(res => {
                                console.log(res);
                                this.reset();
                                this.$message({
                                    type: 'success',
                                    message: '删除成功',
                                });
                            })
                            .catch();
                    })
                    .catch(() => {});
            }
        },
        changeSwitch(data) {
            let obj = {
                statuz: data.statuz ? 1 : 0,
                id: data.id,
            };
            updateExpertAgreement(obj)
                .then(res => {
                    console.log(res);
                    this.queryListData();
                    this.$message({
                        type: 'success',
                        message: '修改成功',
                    });
                })
                .catch();
        },
        enableBatch() {
            if (this.tableSelection.length === 0) {
                this.$message({
                    type: 'warning',
                    message: '请先选择一个',
                });
                return false;
            }
            this.$confirm('是否启用?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    let ids = this.tableSelection.map(item => {
                        return item.id;
                    });
                    console.log(ids);
                    useExpertAgreementBatch({ids: ids})
                        .then(res => {
                            console.log(res);
                            this.queryListData();
                            this.$message({
                                type: 'success',
                                message: '启用成功',
                            });
                        })
                        .catch();
                })
                .catch(() => {});
        },
        selectColumn(selection) {
            this.tableSelection = selection;
        },
        handSizeChange() {},
        // 页码变化时
        handlePagination(val) {
            this.page = val;
            this.queryListData();
        },
        querySearch() {
            if (this.time && this.time.length >= 2) {
                this.$set(this.searchForm, 'startTime', this.time[0]);
                this.$set(this.searchForm, 'endTime', this.time[1]);
            }
            this.page.pageNum = 1;
            this.queryListData();
        },
        reset() {
            this.searchForm = {};
            this.time = [];
            this.page.pageNum = 1;
            this.queryListData();
        },
        showAddEditDialog(data) {
            if (data) {
                this.$refs.addEditDialog.open('edit', data);
            } else {
                this.$refs.addEditDialog.open('add');
            }
        },
    },
};
</script>
<style lang="stylus" scoped>
.TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
    width: 95%;
}

.el-date-editor {
    border-radius: 20px;
    overflow: hidden;
    height: 30px;
    line-height: 30px;
}

.el-range-editor.el-input__inner {
    margin-top: 5px;
}
</style>
