<template>
    <div class="app-container">
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    size="mini"
                    @click="handleBatchAdd()"
                >添加分组</el-button>
            </el-col>
            <right-toolbar @queryTable="getList" :showSearch="false"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="list" row-key="groupId">
            <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="添加分组" v-if="dialogVisible" :visible.sync="dialogVisible" width="610px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-col :span="24">
                    <el-form-item label="分组名称" prop="name">
                        <el-input v-model="form.name" placeholder="请输入分组名称"/>
                    </el-form-item>
                    <!-- <el-form-item label="公众号" prop="channel_ids">
                        <el-input
                            type="textarea"
                            :rows="2"
                            placeholder="请输入公众号ID,例: 公众号ID1,公众号ID2"
                            v-model="form.channel_ids">
                        </el-input>
                    </el-form-item> -->
                    <el-form-item label="公众号" prop="channel_ids">
                        <VirtualList :data='data' v-model="form.channel_ids"></VirtualList>
                    </el-form-item>
                </el-col>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="dialogVisible = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getChannelGroup, getAllAccount, addGroup, delGroup } from '@/api/data'
import VirtualList from '@/components/VirtualList'
export default {
    components: { VirtualList },
    data () {
        return {
            list: [],
            loading: false,
            form: {
                name: '',
                channel_ids: ''
            },
            dialogVisible: false,
            rules: {
                name: [
                    {required: true, message: "分组名称不能为空", trigger: "blur"}
                ],
                channel_ids: [
                    {required: true, message: "请输入公众号ID", trigger: "blur"}
                ],
            },
            data: [],
            allData: [],
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 获取数据 */
        getList() {
            this.loading = true
            getChannelGroup({}).then(res => {
                this.loading = false
                if(res.status.RetCode === 200 && res.data) {
                    this.list = res.data
                }
            }).catch(e => {
                this.loading = false
            })

            getAllAccount({}).then(res => {
                if(res.status.RetCode === 200 && res.data) {
                    this.allData = res.data.map(item => ({
                        key: item.id,
                        label: item.nick_name,
                    }));
                    this.data = this.allData //.splice(0, 100)
                }
            })
        },
        /** 添加分组 */
        handleBatchAdd() {
            this.dialogVisible = true
        },
        /** 删除 */
        handleDelete(row) {
            console.log(row);
            delGroup({id: row.id}).then(res => {
                if(res.status.RetCode === 200) {
                    this.$message.success('删除成功')
                    this.getList()
                }
            })
        },
        /** 提交添加分组 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    let { channel_ids } = this.form
                    channel_ids = channel_ids.join(',')
                    addGroup({...this.form, channel_ids}).then(res => {
                        console.log(res);
                        if(res.status.RetCode === 200) {
                            this.form = {
                                name: '',
                                channel_ids: ""
                            }
                            this.$message.success('添加成功')
                            this.getList()
                            this.dialogVisible = false
                        }
                    })
                }
            })
        },
        /** 自定义搜索 */
        filterMethod(query, item) {
            // console.log(item);
            if(query === ''){
                return item
            }else {
                let querys = query.split(',')
                return querys.indexOf(item.label) !== -1 && item;
            } 
        },
        /** 自定义渲染 */
        renderFunc(h, option) {
            return <span>{ option.label }</span>;
        }
    }
}
</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>