<template>
    <div class="app-container">
        <el-row :gutter="10" class="mb8">
            <el-col :span="15">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
                    <el-form-item label="" prop="userId">
                        <el-select
                            v-model="queryParams.userId"
                            placeholder="用户"
                            filterable
                            style="width: 140px"
                            size="small"
                            :loading="userLoading"
                        >
                            <el-option
                                v-for="item in userAll"
                                :key="item.id"
                                :label="item.nickName"
                                :value="item.id"
                            />
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <!-- <el-button type="cyan" icon="el-icon-search" @click="handleQuery">查询</el-button> -->
                        <el-button type="warning" icon="el-icon-edit-outline" @click="distributeGroup" :disabled="single">分配分组</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
            <right-toolbar @queryTable="getList" :isshowSearch="false"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="list" row-key="groupId" :empty-text="userName + '暂无分组信息'">
            <el-table-column label="ID" prop="id" width="45"></el-table-column>
            <el-table-column label="分组名称" prop="name" width="200"></el-table-column>
            <el-table-column label="公众号" prop="channel_names">
                <template slot-scope="scope">
                    <el-tooltip placement="top" effect="light">
                        <div slot="content" class="tooltip">{{scope.row.channel_names}}</div>
                        <div class="name-wrapper">
                            <p>{{ scope.row.channel_names }}</p>
                        </div>
                    </el-tooltip>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="120px" align="center">
                <template slot-scope="scope">
                    <el-popconfirm
                        title="确定删除？"
                        @confirm="handleDelete(scope.row)"
                    >
                        <el-button slot="reference" type="text" size="mini" style="color: red" icon="el-icon-delete">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog :title="'分配分组给' + userName" v-if="groupVisible" :visible.sync="groupVisible" width="610px" append-to-body>
            <el-form ref="groupForm" :model="groupForm" :rules="grouprules" label-width="80px">
                <el-col :span="24">
                    <el-form-item label="公众号" prop="groupIds">
                        <el-select
                            v-model="groupForm.groupIds"
                            placeholder="选择分组"
                            filterable
                            size="small"
                        >
                            <el-option
                                v-for="item in groupData"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitGroupForm">确 定</el-button>
                <el-button @click="groupVisible = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getChannelGroup, delFpGroup, AllUser, fpGroup } from '@/api/data'
export default {
    data () {
        return {
            queryParams: {
                userId: ''
            },
            allList: [],
            list: [],
            loading: false,
            form: {
                name: '',
                channel_ids: []
            },
            rules: {
                name: [
                    {required: true, message: "分组名称不能为空", trigger: "blur"}
                ],
                channel_ids: [
                    {required: true, message: "请选择公众号", trigger: "blur"}
                ],
            },
            data: [],
            allData: [],
            userAll: [], //所有用户
            single: true,
            groupVisible: false,
            userName: '',
            userLoading: false,
            groupForm: {
                groupIds: "",
            },
            groupData: [],
            grouprules: {
                groupIds: [
                    {required: true, message: "请选择分组", trigger: "blur"}
                ],
            },
        }
    },
    created() {
        this.getList()
        this.getAll()
    },
    watch: {
        'queryParams.userId': {
            handler(val) {
                if(val) {
                    this.single = false
                    this.userName = this.userAll.find(item => item.id === val).nickName
                    this.handleQuery()
                }else{
                    this.single = true
                }
            },
            deep: true
        }
    },
    methods: {
        /** 分配分组 */
        distributeGroup() {
            this.groupVisible = true
        },
        /** 查询 */
        handleQuery() {
            this.userLoading = true
            this.loading = true
            getChannelGroup(this.queryParams.userId ? {user_id: this.queryParams.userId } : {}).then(res => {
                this.userLoading = false
                if(res.status.RetCode === 200 && res.data) {
                    this.loading = false
                    if(res.status.RetCode === 200 && res.data) {
                        this.list = res.data
                    }
                }
            }).catch(e => {
                this.userLoading = false
                this.loading = false
            })
        },
        /** 获取数据 */
        getList() {
            getChannelGroup({}).then(res => {
                if(res.status.RetCode === 200 && res.data) {
                    this.groupData = res.data
                }
            })
        },
        /**  */
        getAll() {
            AllUser({}).then(res => {
                if(res.status.RetCode === 200 && res.data) {
                    this.userAll = res.data.map((item) => {
                        return { id: item.user_id, nickName: item.nick_name }
                    })
                    this.queryParams = {
                        userId: res.data[0].user_id
                    }
                    this.handleQuery()
                }
            })
        },
        /** 删除 */
        handleDelete(row) {
            delFpGroup({user_id: this.queryParams.userId, group_id: row.id}).then(res => {
                if(res.status.RetCode === 200) {
                    this.$message.success('删除成功')
                    this.handleQuery()
                    this.groupVisible = false
                }
            })
        },
        /** 提交分配分组 */
        submitGroupForm() {
            console.log(this.groupForm);
            this.$refs["groupForm"].validate(valid => {
                if(valid) {
                    fpGroup({user_id: this.queryParams.userId, group_id: this.groupForm.groupIds}).then(res => {
                        if(res.status.RetCode === 200) {
                            this.$message.success('分配成功')
                            this.handleQuery()
                            this.groupVisible = false
                        }
                    })
                }
            })
            
        },
    }
}
</script>

<style lang="scss">
    .transfer{
        &>.el-transfer__buttons{
            display: inline-flex !important;
            flex-direction: column !important;
            padding: 0 10px !important;
            .el-button + .el-button{
                margin-left: 0;
            }
        }
    }
    .tooltip{
        max-width: 300px;
        max-height: 200px;
        overflow-y: scroll;
    }
    .name-wrapper{
        overflow: hidden;
        text-overflow:ellipsis;
        white-space: nowrap;
    }
</style>