<template>
  <div tabindex="0" @blur="selectClihide()" style="display: inline-block;">  
    <div :style="'width:' + selectWidth + 'px;height:' + selectHeight + 'px'" :class="classDiv ? 'select selectDiv' : 'select'" @click="selectCli" ref="selectDiv">
        <div :class="selectName == $t('defaultText.pleaseChoose') ? 'selecttex selecttexXuan' : 'selecttex'">
            <ww-open-data type='userName' :openid='selectName'></ww-open-data>
            {{selectName}}
        </div>
        <i class="el-icon-arrow-down iostu"></i>
    </div>
    <transition name="el-zoom-in-top">
      <div v-show="show" style="position: relative;z-index: 99;">
        <div class="transitionBox">
            <ul class="transitionBoxUl">
                <li :class="transitionBoxLiIdx == index ? 'liHover' : ''" v-for="(item, index) in options" :key="index" @mouseover="liMouseOver(index)" @click="liClick(item)"> 
                    <span v-if="item.name">
                        <!-- {{item.name || item.auditorName}} -->
                        <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}} -->
                </li>
            </ul>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
    props: {
        subject:{
            type: Array
        },
        size: {
            type: String
        },
        subjectId: {
            type: String
        },
        distinction: {
            type: String,
            default: '1',
        },
        // 真对填写日报单独处理
        idx: {
            type: String
        },
        flg: {
            type: Boolean,
            default: false, // 默认值，不是填写日报
        },
        // 针对查任务的处理
        index: {
            type: String
        }
    },
    components: {
        selectWidth: '150',
        selectHeight: '28'
    },
    data() {
        return {
            selectWidth: '150',
            selectHeight: '28',
            show: false, // 下拉框
            options: [], // 列表数据
            transitionBoxLiIdx: '', // hover 背景色
            selectName: this.$t('defaultText.pleaseChoose'), // 显示的文字
            classDiv: false, // 获得焦点样式
            optionsOId: '', // 选中人的id
            dailyListObj: null, // 填写日报的数据
            dailyListIndex: null, // 日报点的索引
        };
    },
    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
            },
        }
    },
    created() {},
    mounted() {
        if(this.size == 'mini') {
            this.selectWidth = '150'
            this.selectHeight = '28'
        } else if(this.size == 'small') {
            this.selectWidth = '191'
            this.selectHeight = '32'
        } 
        if(this.subject) {
            this.options = JSON.parse(JSON.stringify(this.subject))
        }
        if(this.subjectId) {
            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
                }
            }
        }
        console.log(this.options, this.optionsOId)
        this.dailyListIndex = this.idx
    },
    methods: {
        selectCli() {
            this.$refs.selectDiv.focus()
            this.classDiv = !this.classDiv
            this.show = !this.show
        },
        selectClihide() {
            if(this.classDiv) {
                this.transitionBoxLiIdx = ''
                this.show = !this.show
                this.classDiv = false
            }
        },
        liMouseOver(index) {
            this.transitionBoxLiIdx = index
        },
        liClick(item) {
            let nameId = item.id || item.auditorId
            if(this.flg) {
                let obj = {
                    id: nameId,
                    idx: this.dailyListIndex
                }
                this.$emit("selectCatCli", obj);
            } else {
                let obj = {
                    id: nameId,
                    distinction: this.distinction,
                    index: this.index, // 选中的索引
                }
                this.$emit("selectCal", obj)
            }
            this.selectName = item.name || item.auditorName
            this.transitionBoxLiIdx = ''
            this.show = !this.show
            this.classDiv = false
        },
    },
    triggerOption(){

    },
    choose(item,value){
      
    },

};
</script>

<style scoped lang="scss">
    .selectDiv {
        border-color: #409EFF !important;
    }
    .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;
    }
    .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;
    }
    .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;;
    }
</style>
