<template>
<div style="display: inline-block;">
  <div tabindex="0" @blur="selectClihide()" style="display: inline-block;" v-if="!wxCope">
    <!-- <div :style="'width:' + selectWidth + 'px;height:' + selectHeight + 'px'" :class="classDiv ? 'select selectDiv' : 'select'" @click="selectCli" :ref="disabled ? '' : 'selectDiv'"> -->
    <div :class="disabled ? 'disabledTrue' : 'disabledFalse'" @mouseenter="moveIonDiv" @mouseleave="outIonDiv">
        <div :style="`width:${selectWidth}px;height:${selectHeight}px`" :class="classDiv ? 'select selectDiv' : 'select'" @click="selectCli" :ref="disabled ? '' : 'selectDiv'">
            <div v-if="!multiSelect" :style="'line-height: '+selectHeight+'px;font-size: '+selectFontSize+'px;'" :class="selectName == $t('defaultText.pleaseChoose') ? 'selecttex selecttexXuan' : 'selecttex'">
                <ww-open-data type='userName' :openid='selectName'></ww-open-data>
                <!-- {{selectName}} -->
            </div>
            <div v-if="multiSelect" :style="'line-height: '+selectHeight+'px;margin-left: -10px'" :class="selectName == $t('defaultText.pleaseChoose') ? 'selecttex selecttexXuan' : 'selecttex'">
                <span v-if="multiSelectList.length > 0">
                    <span v-if="!tile" style="width: 100%">
                        <span class="all">
                            <ww-open-data type='userName' :openid='multiSelectList[0].name'></ww-open-data>
                            <!-- {{multiSelectList[0].name}} -->
                            <i class="el-icon-error" v-if="!disabled" @click.stop="deleteMultiSelectList('-1')"></i>
                        </span>
                        <span class="all" v-if="multiSelectList.length > 1"> + {{multiSelectList.length - 1}}</span>
                    </span>
                    <span v-if="tile" style="width: 100%">
                        <span class="all" style="margin-right: 6px" v-for="(items, indexs) in multiSelectList" :key="indexs">
                            <ww-open-data type='userName' :openid='items.name'></ww-open-data>
                            <!-- {{items.name}} -->
                            <span v-if="items.jobNumber" class="spanFllat">{{items.jobNumber}}</span>
                            <i class="el-icon-error" @click.stop="deleteMultiSelectList(indexs)" v-if="!disabled"></i>
                        </span>
                    </span>
                </span>
                <span v-else class="allTwo">{{$t('defaultText.pleaseChoose')}}</span>
            </div>
            <i :class=" move ? 'el-icon-arrow-down iostu iostuHover' : 'el-icon-arrow-down iostu'" v-if="!moveIon"></i>
            <i v-if="moveIon" class="el-icon-circle-close iostu" @click.stop="clearDelete"></i>
        </div>
    </div>
    <transition name="el-zoom-in-top">
      <div v-show="show" style="position: relative;z-index: 999;"> 
        <!-- 搜索框 -->
        <!-- <div class="searchBox">
            <el-input :placeholder="$t('peaseenterthe')" size="mini" v-model="searchTex" style="width: 150px" @focus="searchBox()">
                <el-button slot="append" icon="el-icon-search" size="mini" @click="searchLick()"></el-button>
            </el-input>
        </div> -->
        <div class="transitionBox" :style="filterable ? 'margin: 30px 0;' : ''">
            <ul class="transitionBoxUl">
                <li :class="transitionBoxLiIdx == index ? 'liHover' : ''" v-for="(item, index) in options" :key="index" @mouseover="liMouseOver(index)" @click="liClick(item, index)"> 
                    <span v-if="!multiSelect" style="width: 100%">
                        <span v-if="item.name">
                            <ww-open-data type='userName' :openid='item.name'></ww-open-data>
                        </span> 
                        <span v-if="item.auditorName">
                            <ww-open-data type='userName' :openid='item.auditorName'></ww-open-data>
                        </span>
                        <!-- {{item.name || item.auditorName}} -->
                        <span v-if="item.jobNumber" class="spanFllat">{{item.jobNumber}}</span>
                    </span>

                    <span v-if="multiSelect" style="width: 100%">
                        <span :class="item.flg ? 'hoverSpan hoverSpanHover' : 'hoverSpan'">
                            <span style="width: 100%;display: inline-block;">
                                <ww-open-data type='userName' :openid='item.name'></ww-open-data>
                                <!-- {{item.name}} -->
                                <span v-if="item.jobNumber" class="spanFllat">{{item.jobNumber}}</span>
                            </span>
                            <span v-if="item.flg"><i class="el-icon-check"></i></span>
                        </span>
                    </span>
                </li>
            </ul>
        </div>
      </div>
    </transition>
  </div>
  <div v-if="wxCope" class="waihez">
    <!-- <div @click="dianjis()">点击</div> -->
    <div :style="`width:${selectWidth}px;height:${selectHeight}px;line-height:${selectHeight}px;background:#fff`" class="select selectDandu" @click="dianjis()">
        <span v-if="!multiSelect"><ww-open-data type='userName' :openid='selectName'></ww-open-data></span>
        <!-- <span>你好</span> -->
    </div>
  </div>
</div>
</template>

<script>
export default {
    props: {
        subject:{
            type: Array
        },
        size: {
            type: String,
        },
        subjectId: {
            type: [String, Number, Array]
        },
        wxCope: {
            type: Boolean,
            default: false,
        },
        // 是否支持多选
        multiSelect: {
            type: Boolean,
            default: false
        },
        // 是否平铺 (需要 multiSelect 为 true)
        tile: {
            type: Boolean,
            default: false
        },
        // 当前页面用到的第几个
        distinction: {
            type: String,
            default: '1',
        },
        // 真对填写日报单独处理
        idx: {
            type: String
        },
        flg: {
            type: Boolean,
            default: false, // 默认值，不是填写日报
        },
        // 宽度
        widthStr: {
            type: String,
            default: false
        },
        // 剩下统一索引
        index: {
            type: String
        },
        // 是否禁用
        disabled: {
            type: Boolean,
            default: false
        },
        // 是否可清空
        clearable: {
            type: Boolean,
            default: false
        },
        // 是否可搜索
        filterable: {
            type: Boolean,
            default: false
        },
        // 其他数据
        other: {
            type: [String, Number, Boolean],
            default: false
        },
        // 是否执行到日报单独的函数
        flgs: {
            type: Boolean,
            default: false
        },
    },
    components: {
        selectWidth: '150',
        selectHeight: '28'
    },
    data() {
        return {
            selectWidth: '150',
            selectHeight: '28',
            selectFontSize: '12',
            show: false, // 下拉框
            options: [], // 列表数据
            transitionBoxLiIdx: '', // hover 背景色
            selectName: this.$t('defaultText.pleaseChoose'), // 显示的文字
            classDiv: false, // 获得焦点样式
            optionsOId: '', // 选中人的id
            dailyListObj: null, // 填写日报的数据
            dailyListIndex: null, // 日报点的索引
            move: false,
            moveIon: false,
            multiSelectList: [],
            searchTex: '', // 搜索文字
            cursor: '', // 搜索的标记（需传给后端）
        };
    },
    computed: {},
    watch: {
        subject: {
            handler(newValue, oldValue) {
                this.options = newValue
                if(this.flg) {
                    if(newValue) {
                        this.selectName = newValue[0].name || newValue[0].auditorName
                        this.optionsOId = newValue[0].id || newValue[0].auditorId
                    }
                }
            },
        },
        // 日报点的索引, 真对填写的日报
        idx: {
            handler(newValue, oldValue) {
                console.log(newValue, oldValue)
                this.dailyListIndex = newValue
            },
        },
        subjectId: {
            handler(newValue, oldValue) {
                console.log(newValue, oldValue)
                this.optionsOId = newValue
                this.multiSelectList = []
                if(!this.multiSelect) {
                    if(this.optionsOId) {
                        for(let i in this.options) {
                            if(this.options[i].id == this.optionsOId || this.options[i].auditorId == this.optionsOId) {
                                this.selectName = this.options[i].name || this.options[i].auditorName
                            }
                        }
                    } else {
                        this.selectName = this.$t('defaultText.pleaseChoose')
                    }
                }
                if(this.multiSelect) {
                    for(var i in this.options) {
                        for(var j in this.optionsOId) {
                            if(this.options[i].id == this.optionsOId[j] || this.options[i].auditorId == this.optionsOId[j]) {
                                this.multiSelectList.push(this.options[i])
                                this.options[i].flg = true
                            }
                        }
                    }
                }
            },
        },
        widthStr: {
            handler(newValue, oldValue) {
                this.selectWidth = newValue
            },
        }
    },
    created() {},
    mounted() {
        if(this.size == 'mini') {
            this.selectWidth = '150'
            this.selectHeight = '28'
        } else if(this.size == 'small') {
            this.selectWidth = '191'
            this.selectHeight = '32'
        } else if(this.size == 'medium') {
            this.selectWidth = '205'
            this.selectHeight = '40'
            this.selectFontSize = '14'
        }
        if(this.widthStr) {
            this.selectWidth = this.widthStr
        }
        
        if(this.subject) {
            this.options = JSON.parse(JSON.stringify(this.subject))
        }
        if(this.subjectId) {
            this.multiSelectList = []
            if(!this.multiSelect) {
                this.optionsOId = JSON.parse(JSON.stringify(this.subjectId))
                for(let i in this.options) {
                    if(this.options[i].id == this.optionsOId || this.options[i].auditorId == this.optionsOId) {
                        this.selectName = this.options[i].name || this.options[i].auditorName
                    }
                }
            }
            if(this.multiSelect) {
                for(var i in this.options) {
                    for(var j in this.optionsOId) {
                        if(this.options[i].id == this.optionsOId[j] || this.options[i].auditorId == this.optionsOId[j]) {
                            this.multiSelectList.push(this.options[i])
                            this.options[i].flg = true
                        }
                    }
                }
            }
        }
        console.log(this.subject)
        this.dailyListIndex = this.idx
    },
    methods: {
        dianjis() {
            if(!this.disabled) {
                let modes = this.multiSelect ? 'multi' : 'single'
                let that = this
                wx.invoke("selectEnterpriseContact", {
                    "fromDepartmentId": 0,// 必填，表示打开的通讯录从指定的部门开始展示，-1表示自己所在部门开始, 0表示从最上层开始
                    "mode": modes,// 必填，选择模式，single表示单选，multi表示多选
                    "type": ["user"],// 必填，选择限制类型，指定department、user中的一个或者多个
                    "selectedDepartmentIds": [],// 非必填，已选部门ID列表。用于多次选人时可重入，single模式下请勿填入多个id
                    "selectedUserIds": []// 非必填，已选用户ID列表。用于多次选人时可重入，single模式下请勿填入多个id
                        },function(res){
                            console.log(res)
                            if (res.err_msg == "selectEnterpriseContact:ok"){
                                console.log(res, '数据来源')
                                if(typeof res.result == 'string'){
                                    res.result = JSON.parse(res.result) //由于目前各个终端尚未完全兼容，需要开发者额外判断result类型以保证在各个终端的兼容性
                                }
                                var selectedUserList = res.result.userList; // 已选的成员列表
                                var userId = ''
                                var userName = ''
                                for (var i = 0; i < selectedUserList.length; i++) {
                                        var user = selectedUserList[i];
                                        userId = user.id; // 已选的单个成员ID
                                        userName = user.name;// 已选的单个成员名称
                                        console.log(userId, userName)
                                }
                                for(var s in that.options) {
                                    if(that.options[s].name == userId) {
                                        that.selectName = userId
                                        that.wxCoper(that.options[s].id)
                                    }
                                }
                            }
                        }
                );
            }
        },
        wxCoper(id) {
            let obj = {
                id: id,
                distinction: this.distinction,
                index: this.index, // 选中的索引
                other: this.other,
                name: this.selectName
            }
            console.log(obj)
            this.$emit("selectCal", obj)
        },
        selectCli() {
            if(!this.disabled) {
                this.$refs.selectDiv.focus()
                this.classDiv = !this.classDiv
                this.show = !this.show
                this.move = !this.move
                this.searchTex = ''
            }
        },
        selectClihide() {
            if(this.classDiv) {
                this.transitionBoxLiIdx = ''
                this.show = !this.show
                this.classDiv = false
                this.move = false
                if(this.multiSelect) {
                    let obj = {
                        // id: nameId,
                        distinction: this.distinction,
                        index: this.index, // 选中的索引
                        other: this.other,
                        arrUserList: this.multiSelectList,
                        name: this.selectName
                    }
                    this.$emit("selectCal", obj)
                }
            }
        },
        liMouseOver(index) {
            this.transitionBoxLiIdx = index
        },
        liClick(item, itemIndex) {
            let nameId = item.id || item.auditorId
            this.selectName = item.name || item.auditorName
            if(!this.multiSelect) {
                console.log('我进来了', this.flg)
                if(this.flgs) {
                    let obj = {
                        id: nameId,
                        idx: this.dailyListIndex
                    }
                    this.$emit("selectCatCli", obj);
                } else {
                    let obj = {
                        id: nameId,
                        distinction: this.distinction,
                        index: this.index, // 选中的索引
                        other: this.other,
                        name: this.selectName
                    }
                    this.$emit("selectCal", obj)
                }
                this.transitionBoxLiIdx = ''
                this.show = false
                this.classDiv = false
                this.move = false
            }
            if(this.multiSelect) {
                if(this.options[itemIndex].flg) {
                    this.options[itemIndex].flg = !this.options[itemIndex].flg
                    for(var i in this.multiSelectList) {
                        if(this.multiSelectList[i].id == item.id) {
                            this.multiSelectList.splice(i, 1)
                        }
                    }
                } else {
                    this.options[itemIndex].flg = false
                    this.options[itemIndex].flg = !this.options[itemIndex].flg
                    this.multiSelectList.push(item)
                }
            }
        },
        moveIonDiv() {
            if(this.clearable) {
                this.moveIon = true
            }
        },
        outIonDiv() {
            if(this.clearable) {
                this.moveIon = false
            }
        },
        clearDelete() {
            if(!this.multiSelect) {
                this.selectName = this.$t('defaultText.pleaseChoose')
                let obj = {
                    name: this.$t('defaultText.pleaseChoose'),
                    id: ''
                }
                this.liClick(obj)
            }
            if(this.multiSelect) {
                this.multiSelectList = []
                let obj = {
                    distinction: this.distinction,
                    index: this.index, // 选中的索引
                    other: this.other,
                    arrUserList: []
                }
                for(var i in this.options) {
                    if(this.options[i].flg) {
                        this.options[i].flg = false
                    }
                }
                this.$emit("selectCal", obj)
            }
        },
        deleteMultiSelectList(str) {
            let userId
            if(str == '-1') {
                userId = this.multiSelectList[0].id
                this.multiSelectList.splice(0, 1)
            } else {
                userId = this.multiSelectList[str].id
                this.multiSelectList.splice(str, 1)
            }
            for(var i in this.options) {
                if(this.options[i].id == userId) {
                    if(this.options[i].flg) {
                        this.options[i].flg = false
                    }
                }
            }
            
            let obj = {
                distinction: this.distinction,
                index: this.index, // 选中的索引
                other: this.other,
                arrUserList: this.multiSelectList,
                name: this.selectName
            }
            this.$emit("selectCal", obj)
        },
        searchBox() {
            this.selectCli()
        },
        // 搜索
        searchLick() {
            console.log('文字')
            this.getSimpleActiveUserList()
        },
        // 针对  getSimpleActiveUserList 获取所有人员接口
        getSimpleActiveUserList() {
            this.http.post('/user/getSimpleActiveUserList', {
                keyword: this.searchTex,
                cursor: this.cursor
            },
            res => {
                if (res.code == "ok") {
                    console.log('数据')
                    this.options = res.data.retUser
                    this.cursor = res.data.nextCursor
                } else {
                    this.$message({
                        message: res.msg,
                        type: "error"
                    });
                }
            },
            error => {
                this.$message({
                    message: error,
                    type: "error"
                });
            });
        }
    },
    triggerOption(){

    },
    choose(item,value){
      
    },

};
</script>

<style scoped lang="scss">
    .selectDandu {
        font-size: 16px;
    }
    .waihez {
        // padding-top: 7px;
        // box-sizing: border-box;
        // background: #FFF;
        display: inline-block;
    }
    .selectDiv {
        border-color: #409EFF !important;
    }
    .disabledTrue {
        background: #F5F7FA !important;
        border-radius: 4px;
        cursor: not-allowed !important;
        position: relative;
    }
    .disabledFalse .select {
        background: #FFF !important;
        border-radius: 4px;
    }
    .select {
        -webkit-appearance: none;
        // background-color: #FFF;
        background-image: none;
        border-radius: 4px;
        border: 1px solid #DCDFE6;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        color: #606266;
        display: inline-block;
        font-size: inherit;
        height: 40px;
        line-height: 40px;
        outline: 0;
        padding: 0 15px;
        -webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1);
        transition: border-color .2s cubic-bezier(.645,.045,.355,1);
        width: 100%;
        position: relative;
        cursor: pointer;
    }
    .selecttex {
        height: 28px;
        line-height: 28px;
        text-overflow: ellipsis;
        font-size: 12px;
    }
    .iostu {
        position: absolute;
        top: 50%;
        margin-top: -4px;
        right: 8px;
        color: #C0C4CC;
        transition: All 0.2s ease-in-out;
    }
    .iostuHover {
        transform: rotate(-180deg);
    }
    .searchBox {
        position: absolute; 
        width: 100%;
    }
    .transitionBox {
        background: #FFF;
        position: absolute; 
        width: 100%;
        border-radius: 2em;
        border: 1px solid #E4E7ED;
        border-radius: 4px;
        background-color: #FFF;
        box-sizing: border-box;
        margin: 5px 0;
        box-shadow: 0 2px 12px #dfdfdf;
        max-height: 274px;
        overflow: auto;
        z-index: 500 !important;
    }
    .transitionBoxUl {
        list-style: none;
        padding: 6px 0;
        margin: 0;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
    }
    .transitionBoxUl li {
        font-size: 14px;
        padding: 0 20px;
        position: relative;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        color: #606266;
        height: 34px;
        line-height: 34px;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        cursor: pointer;
    }
    .liHover {
        background-color: #F5F7FA;
    }
    .selecttexXuan {
        color: #C0C4CC;;
    }
    .all {
        display: inline-block;
        font-size: 10px;
        background: #f4f4f5;
        height: 20px;
        line-height: 24px;
        padding: 0px 8px;
        color: #909399;
    }
    .allTwo {
        display: inline-block;
        margin-left: 10px;
    }
    .hoverSpan {
        display: flex;
        width: 100%;
        justify-content: space-between;
    }
    .hoverSpanHover {
        color: #409eff;
        font-weight: 700;
    }
    .spanFllat {
        display: inline-block;
        float: right;
    }
</style>
