<template>
    <div class="pd-select-wrap">
        <Dropdown class="sort-dropdown" trigger="click" placement="bottom-start" @on-click="function(e){ filterPeriod = e }" @on-visible-change="dropdownStates" v-if="$store.state.user.schoolProfile.school_base">
            <span style="cursor: pointer;">
                <span v-show="showLabel">{{$t('teachContent.uploadTo')}}</span>
                <b class="title">{{ filterPeriodName }}</b>
                <Icon type="ios-arrow-down" style="margin-left:8px;"></Icon>
            </span>
            <DropdownMenu slot="list" v-for="(item,index) in periods" :value="item.id" :key="index">
                <DropdownItem :name="item.id">{{ item.name }}</DropdownItem>
            </DropdownMenu>
            <DropdownMenu slot="list">
                <DropdownItem name="public">{{$t('teachContent.publicRes')}}</DropdownItem>
            </DropdownMenu>
        </Dropdown>
    </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
    name: 'BasePdSelect',
    props: {
        pdId:{
            type:String,
            default:''
        },
        showLabel:{
            type:Boolean,
            default:false
        }
    },
    data() {
        return {
            filterPeriod: '',
        }
    },
    created() {
        this.$store.dispatch('user/getSchoolProfile').then(
            (res) => {
                if (res) {
                    if (this.periods && !this.pdId) {
                        this.filterPeriod = this.periods[0].id
                    }else{
                        this.filterPeriod = this.pdId
                    }
                }
            },
            (err) => {
                this.$Message.error('API error!')
            }
        )
    },
    methods: {
        dropdownStates(flag) {
            // if (!flag) this.filterByPeriod()
        }
    },
    computed: {
        ...mapGetters({
            periods: 'user/getPeriods', // 學制s
        }),
        filterPeriodName(){
            let data = this.periods
            let pId = this.filterPeriod
            let name = ''
            if (pId !== '') {
                let temp = data.find(item => {
                    return pId == item.id
                })
                if (temp) {
                    name = temp.name
                }else{
                    name = this.$t('teachContent.publicRes')
                }
            }
            return name
        },
    },
    watch: {
        filterPeriod() {
            this.$emit('pd-change', this.filterPeriod)
        },
        pdId:{
            handler(){
                this.filterPeriod = this.pdId
            }, 
            immediate:true
        }
    }
}
</script>
<style lang="less">
.pd-select-wrap {
    display: inline-block;
    color: #a5a5a5;
}
.sort-dropdown {
    .title {
        color: white;
        font-size: 14px;
    }
    .ivu-select-dropdown {
        background-color: #2d2d2d;
        border-radius: 2px;
        border: 1px #464646 solid;
        .ivu-dropdown-menu {
            li {
                color: #ccc;
                font-size: 12px !important;
                &:hover {
                    color: #2d2d2d;
                }
            }
        }
    }
}
</style>
