<template>
    <div>
        <div class="title">邀请加入视频通话</div>
        <div class="ui-grid ptb10 plr10">
            <div class="ui-col-0 left-panel">
                <div class="input-search c-position-r">
                    <input type="text" class="search" placeholder="搜索医生" @input="search" v-model.trim="keyword" @click="search" />
                    <span class="search-btn c-position-a">
                        <i class="iconfont iconfont-sousuo" @click="search"></i>
                    </span>
                </div>
                <ul class="c-list">
                    <li v-for='doc in docList' :key='doc.code' @click="chooseDoc(doc)">
                        <div class="ui-col-0 mr20"><img :src="getDocPhoto(doc.photo)" class="c-images-cycle" width="40"></div>
                        <div class="ui-col-1">{{doc.name}}</div>
                        <div class="ui-col-0" v-show="selectedDoc[doc.code]"><img src="../../assets/images/gouxuan_lan_icon.png" width="20"></div>
                    </li>
                </ul>
            </div>
            <div class="ui-col-1 right-panel">
                <div class="header">已选医生（{{selectedLength}}人）：</div>
                <ul class="c-list">
                    <li v-for='doc in selectedDoc' :key='doc.code'>
                        <div class="ui-col-0 mr20"><img :src="getDocPhoto(doc.photo)" class="c-images-cycle" width="40"></div>
                        <div class="ui-col-1">{{doc.name}}</div>
                    </li>
                </ul>
            </div>
        </div>
        <div class="action-panel plr10 c-t-right">
            <a class="c-btn confirm-btn mr20" @click="confirm">确 定</a>
            <a class="c-btn" @click="cancel">取 消</a>
        </div>
    </div>
</template>

<style rel="stylesheet/scss" lang="scss" scoped>
    .title{
        height: 40px;
        line-height: 40px;
        font-size: 14px;
        color: #333;
        padding: 0 10px 0 20px;
        border-bottom: 1px solid #e1e1e1;
    }
    .left-panel{
        margin-right: 38px;
    }
    .left-panel, .right-panel{
        background-color: #F5F5FA;
        width: 220px;
        height: 300px;
    }
    .input-search {
        display: inline-block;
        vertical-align: middle;
        padding: 8px 10px;
    }
    .input-search input.search {
        width: 164px;
        padding-right: 26px;
        padding-left: 10px;
        height: 28px;
        line-height: 26px;
        border: solid 1px #d7dce6;
    }
    .input-search span.search-btn {
        z-index: 2;
        top: 9px;
        right: 11px;
        cursor: pointer;
        background-color: #fff;
        display: inline-block;
        padding: 3px 5px 3px;
    }
    .input-search span.search-btn i.iconfont {
        color: #999;
    }
    .header{
        font-size: 14px;
        color: #333;
        height: 40px;
        line-height: 40px;
        padding: 0 20px;
    }
    .c-list li{
        height: 40px;
        padding: 5px 10px;
        -webkit-box-align: center;
        margin: 0;
    }
    .c-list li:hover{
        background-color: #EBEBF5;
    }
    .left-panel .c-list{
        height: calc(100% - 46px);
        overflow: hidden;
        overflow-y: scroll;
    }
    .right-panel .c-list{
        height: calc(100% - 40px);
        overflow: hidden;
        overflow-y: scroll;
    }
    ::-webkit-scrollbar-thumb {
        background-color: #DCDCE1;
        border-radius: 5px;
    }
    .c-btn{
        display: inline-block;
        height: 28px;
        line-height: 26px !important;
        text-align: center;
        font-size: 14px;
        font-weight: normal;
        font-stretch: normal;
        line-height: 30px;
        letter-spacing: 0px;
        color: #666666;
        border: solid 1px #d7dce6;
        background-color: #fafafa;
        padding: 0 20px;
    }
    .confirm-btn{
        border-color: #12b7f5;
    }
</style>
<script>
import _ from 'lodash'
import common from '../../assets/js/common'
import api from '../../assets/js/apis/api-p2psocket'
export default {
    props:['chatMembers', 'createVideoChat'],
    data(){
        return {
            keyword: '',
            docList: [],
            selectedDoc: {},
            selectArr: []
        }
    },
    computed:{
        selectedLength(){
            return _.size(this.selectedDoc);
        }
    },
    mounted(){
        this.getDocList();
    },
    methods:{
        search(){

        },
        async getDocList(){
            let allDocs = await api.getAllDocMembers(),
                docList = allDocs.data.data
            docList = _.values(docList);
            docList = _.flatten(docList);
            console.log(docList);
            var vm = this;
            if(this.chatMembers.length == 0){
                this.docList = docList;
            }else{
                let doc = [];
                _.each(docList, function(o){
                    if(!_.find(vm.chatMembers, {code: o.code})){
                        doc.push(o);
                    }
                });
                this.docList = doc;
            }
            
        } ,
        getDocPhoto(p){
            if(!p){
                return './images/d-male.png'
            }else{
                return common.setImgUrl(p)
            }
        },
        chooseDoc(doc){
            if(this.selectedDoc[doc.code]){
                delete this.selectedDoc[doc.code];
                this.selectedDoc= Object.assign({}, this.selectedDoc)
                let index = _.findIndex(this.selectArr, {code: doc.code});
                this.selectArr.splice(index, 1);
            }else{
                this.$set(this.selectedDoc, doc.code, doc );
                this.selectArr.push(doc);
            }
        },
        confirm(){
            if(this.selectedLength == 0){
                this.$toast("请选择医生");
                return false;
            }else if(this.selectedLength > 10){
                this.$toast("最多仅支持10人同时视频通话");
                return false;
            }
            if(this.chatMembers.length > 2){
                //从群聊里邀请参加视频通话
                // this.$root.bus.$emit('addChatMember', this.selectedDoc);
                console.log(_.keys(this.selectedDoc));
                this.$root.bus.$emit("joinMemberInChat", _.keys(this.selectedDoc));
            }else{
                //p2p会话邀请，协同视频邀请
                if(this.chatMembers.length == 0){
                    if(this.selectedLength < 2){
                        this.$toast("请选择医生");
                        return false;
                    }
                }
                this.$root.bus.$emit('menuIndex', true)
                let list = this.chatMembers.concat(this.selectArr),
                    arr = _.map(list, "name"),
                    name = arr.join("、"),
                    participants = {};

                _.each(list, function(o){
                    participants[o.code] = o;
                });
                
                //会话的名称，如果长度>10，则用...结尾
                if(name.length > 10){
                    name = name.substr(0, 10)+"...";
                }
                
                this.$root.bus.$emit('infoToMsg', {
                    type: '4', //普通群聊的类型
                    participants: participants,
                    sessionName: name,
                    createVideoChat: this.createVideoChat
                })
                this.$router.push({ path: 'message' })
            }
            this.$root.bus.$emit('addChatMember', this.selectedDoc);
            this.$emit('close');
        },
        cancel(){
            this.$emit('close');
        }
    }
}
</script>
