<template>
	<div class="detail">
        <mt-header class="detail_head" fixed title="TA的建议">
            <router-link to="" slot="left">
                <mt-button icon="back" v-on:click="jumpBack()"></mt-button>
            </router-link> 
            <mt-button class="timePicker" slot="right" id="dateTime" v-on:click="timePicker()"><span class="dateTime">{{chooseTime}}</span> <img src="../../assets/image/pull_down.png"></mt-button>
        </mt-header>
        <div class="detail_body">
            <div class="noList" v-if="ideaList.length==0">
                <img src="../../assets/image/noList.png">
            </div> 
            <ul v-else class="recordBox">
                <li v-for="(item,index) in ideaList" class="one_recordBox detailBox">
                    <div>
                        <div class="one_suggest_title">
                            {{item.name}}
                            <span v-if="item.ideaComment != null" class="areaday">已打分</span>
                            <span v-if="power.indexOf('scoring')>-1 && item.isEvaluated == 0" class="edit" v-on:click="openSugPop(index)">打分</span>
                        </div>
                        <div style="color: #aaa;text-align:left;font-size:0.13rem;margin-bottom:0.12rem;">
                            {{item.indate}}
                        </div>
                        <div class="one_suggest_body">
                            {{item.content}}
                        </div>
                        <div class="one_suggest_reply" v-if="item.ideaComment != null">
                            <div class="reply_title">来自领导的回复</div>
                            <div class="reply_body">
                                {{item.ideaComment.content}}
                                <div>{{item.ideaComment.responder}} <span class="score">得分：<span>{{item.score==null?'0分':item.score + '分'}}</span></span></div>
                                <div>{{item.ideaComment.indate}}
                                    <div v-if="power.indexOf('scoring')>-1" class="showMoreBtn" v-on:click.stop="showBtnList(item.id)">
                                        <img src="../../assets/image/loadMore.png">
                                        <div :id="'btn' + item.id" class="btn_list hide">
                                            <div v-on:click="openSugPop(index)"><img src="../../assets/image/register.png"><span>编辑</span></div>
                                            <div v-on:click="deleteScore(index)"><img src="../../assets/image/delete.png"><span>删除</span></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </li>
                <div class="order" v-if="haveMore">
                    <span class="line"></span>
                    <span class="txt">没有更多了</span>
                    <span class="line"></span>
                </div>
            </ul>
        </div>

        <mt-popup v-model="sugScorePop" position="middle" popup-transition="popup-fade">
            <div class="mint-popup-div">
                <h4>打分</h4>
                <mt-field label="" class="input" placeholder="请输入建议分" type="number" disableClear v-model="sugScore"></mt-field>
                <mt-field label="" class="input" placeholder="请输入得分原因" type="textarea" rows="4" v-model="sugContent"></mt-field>
                <div class="btns">
                    <mt-button class="btn" size="small" v-on:click.native="submit(3)">确定</mt-button>
                    <mt-button class="btn" size="small" v-on:click.native="closePop()">取消</mt-button>
                </div>
            </div>
        </mt-popup>
    </div>
</template>

<script>
    import { MessageBox } from 'mint-ui';
	export default {
		data() {
			return {
                user: JSON.parse(sessionStorage.getItem("user")),
                power: sessionStorage.getItem("power"),
                ideaList: [],
                pageNum: 1,
                pages: 1,
                haveMore: false,

                date: [],
                chooseTime: '',

                sugScore: 0,
                sugContent: '',
                sugScorePop: false,
                chooseSugNum: 0,

                canEdit: true,
			}
		},
		methods: {
            // 时间筛选
            timePicker() {
                var _this = this;
                var dataShippingSpace = this.date;
                var pickerDiv = document.getElementById("dateTime");
                var pickerView = new PickerView({
                    bindElem: pickerDiv, 
                    data: dataShippingSpace, 
                    title: "时间选择", 
                    leftText: "取消", 
                    rightText: "确定", 
                    rightFn: function(selectArr) {
                        _this.chooseTime = selectArr[0].name;
                        _this.getList();
                        if(_this.chooseTime == _this.date[0].name) {
                            _this.canEdit = true
                        } else {
                            _this.canEdit = false
                        }
                    }
                });
            },

            // 消息列表
            getList() {
                this.$indicator.open();
                this.http.post(this.port.my.ideaListByUid, {
                    'uid': this.user.id,
                    'time': this.chooseTime,
                    // 'pageNum': 1,
                } , 
                res => {
                    this.$indicator.close();
                    if (res.code == "ok") {
                        // this.ideaList = res.data.list;
                        this.ideaList = res.data;
                        this.pages = res.data.pages==0?1:res.data.pages;
                    } else {
                        this.$toast({
                            message: res.msg,
                            duration: 2000
                        });
                    }
                }, error => {
                    this.$indicator.close();
                    this.$toast({
                        message: error,
                        duration: 2000
                    });
                })
            },

            loadMore() {
                if(this.pageNum == this.pages) {
                    this.haveMore = true;
                } else if(this.pageNum < this.pages) {
                    this.$indicator.open();
                    this.http.post(this.port.my.ideaListByUid, {
                        'uid': this.user.id,
                        'time': this.chooseTime,
                        'pageNum': ++this.pageNum
                    } , 
                    res => {
                        this.$indicator.close();
                        if (res.code == "ok") {
                            this.pages = res.data.pages==0?1:res.data.pages;
                            if(res.data.ideaList.length != 0) {
                                for(var i in res.data.ideaList) {
                                    this.ideaList.push(res.data.list[i]);
                                }
                            }
                        } else {
                            this.$toast({
                                message: res.msg,
                                duration: 2000
                            });
                        }
                    }, error => {
                        this.$indicator.close();
                        this.$toast({
                            message: error,
                            duration: 2000
                        });
                    })
                }
            },

            jumpBack() {
                this.$router.go(-1);
            },

            getDate() {
                const dateData = [];
                const date1 = new Date();
                for (let i = 0; i < 36; i++) {
                    let cur = '';
                    let tempYear = date1.getFullYear();
                    let tempMonth = date1.getMonth() - i + 1;
                    if (tempMonth <= 0) {
                        tempYear = date1.getFullYear() - Math.floor(tempMonth / -12) - 1; 
                        tempMonth += (Math.floor(tempMonth / -12) + 1) * 12;
                    }
                    if (tempMonth < 10) {
                        cur = tempYear+ '-0' + tempMonth;
                    } else {
                        cur = tempYear+ '-' + tempMonth;
                    }
                    dateData.push({
                        name: cur
                    });
                    if(i == 0) {
                        this.chooseTime = cur;
                    }
                }
                this.date = dateData;
                this.getList();
            },

            openPop(type) {
                if(type == 1) {
                    this.personalScore = this.detail.personalScore==null?0:this.detail.personalScore;
                    this.score = true;
                } else if(type == 2) {
                    this.reason = this.detail.reason==null?'':this.detail.reason;
                    this.reasonPop = true;
                }
            },

            submit(type) {
                var reg = /^([0-9]|10)(\.\d)?$/;
                var scoreNum = 0;
                for(var i in this.ideaList) {
                    if(this.ideaList[this.chooseSugNum].uid == this.ideaList[i].uid && this.ideaList[this.chooseSugNum].id != this.ideaList[i].id) {
                        scoreNum += this.ideaList[i].score;
                    }
                }
                if(!reg.test(this.sugScore)) {
                    this.$toast({
                        message: '分数区间为0 ~ 10',
                        duration: 2000
                    });
                    return false;
                } else if((parseFloat(scoreNum) + parseFloat(this.sugScore)) > 10){
                        this.$toast({
                            message: '当前人员建议分总分不能超过10分',
                            duration: 2000
                        });
                        return false;
                } else {
                    var from = {};
                    if(this.ideaList[this.chooseSugNum].ideaComment != null) {
                        from = {
                            "ideaId": this.ideaList[this.chooseSugNum].id,
                            "score": this.sugScore,
                            "content": this.sugContent,
                            "responderId": this.user.id,
                            "scoreId": this.ideaList[this.chooseSugNum].scoreId,
                            "id": this.ideaList[this.chooseSugNum].ideaComment.id,
                        }
                    } else {
                        from = {
                            "ideaId": this.ideaList[this.chooseSugNum].id,
                            "score": this.sugScore,
                            "content": this.sugContent,
                            "responderId": this.user.id,
                            "scoreId": this.ideaList[this.chooseSugNum].scoreId,
                        }
                    }
                    this.$indicator.open();
                    this.http.post(this.port.my.leaderScore, from , 
                    res => {
                        this.$indicator.close();
                        if (res.code == "ok") {
                            this.$toast({
                                message: "打分成功",
                                duration: 2000
                            });
                            this.sugScorePop = false;
                            this.getList();
                        } else {
                            this.$toast({
                                message: res.msg,
                                duration: 2000
                            });
                        }
                    }, error => {
                        this.$indicator.close();
                        this.sugScorePop = false;
                        this.$toast({
                            message: error,
                            duration: 2000
                        });
                    })
                }
            },

            closePop() {
                this.sugScorePop = false;
            },

            openSugPop(i) {
                this.sugScore = this.ideaList[i].score == null? 0 : this.ideaList[i].score;
                this.sugContent = this.ideaList[i].ideaComment == null? '' : this.ideaList[i].ideaComment.content;
                this.sugScorePop = true;
                this.chooseSugNum = i;
            },

            showBtnList(id) {
                if($("#btn" + id).attr("class").indexOf("hide") == -1) {
                    $("#btn" + id).slideUp().addClass("hide");
                } else {
                    $(".one_suggest_reply .showMoreBtn .btn_list").addClass("hide").slideUp();
                    $("#btn" + id).slideDown().removeClass("hide");
                }
            },

            deleteScore(index) {
                MessageBox.confirm('', { 
                    message: '是否删除该分数、评论？', 
                        title: '', 
                        confirmButtonText: '确定', 
                        cancelButtonText: '取消' 
                 }).then(action => { 
                    if (action == 'confirm') {     //确认的回调
                        this.http.post(this.port.my.delScore, {
                            'id': this.ideaList[index].ideaComment.id,
                        } , 
                        res => {
                            if (res.code == "ok") {
                                this.$toast({
                                    message: "删除成功",
                                    duration: 2000
                                });
                                this.getList();
                            } else {
                                this.$toast({
                                    message: res.msg,
                                    duration: 2000
                                });
                            }
                        }, error => {
                            this.$toast({
                                message: error,
                                duration: 2000
                            });
                        })
                    }
                 }).catch(err => { 
                 });
            },

            globalClick(callback) {
                document.getElementById('allocation').onclick = function () {
                    $(".one_suggest_reply .showMoreBtn .btn_list").addClass("hide").slideUp();
                }
            }
        },
        created() {
            this.getDate();
        },
		mounted() {
            
		}
	}

</script>

<style scoped>
    body {
        background: #EFEFEF!important;
    }

    .context_router {
        background: #EFEFEF;
    }
    
    .detail {
        background: #EFEFEF;
    }

    .detail_head {
        background: #fff;
        color: #333;
        height: 0.4rem;
    }

    .detail_body {
        margin-top: 0.4rem;
        padding-bottom: 0.15rem;
        background: #efefef;
    }

    .detailBox {
        background: #fff;
        margin-bottom: 0.1rem;
    }

    .one_recordBox {
        padding: 0.12rem 0.2rem;
    }

    .record_head {
        padding: 0 0 0.1rem 0;
        line-height: 0.25rem;
        border-bottom: 1px solid #ccc;
    }

    .record_head > div {
        width: 100%;
        display: inline-block;
        vertical-align: middle;
        line-height: 0.4rem;
    }

    .record_head > div img {
        width:0.12rem;
        float:right;
        margin: 0.135rem 0 0 0;
    }

    .record_head > div span {
        color: #5FA1F0;
        margin-left: 0.2rem;
        float: right;
        margin-right: 0.05rem;
    }

    .record_body {
        color: #5FA1F0;
        font-size: 0.14rem;
        margin: 0.12rem 0 0 0;
    }

    .record_body .toDetail {
        float: right;
        margin-right: 0.05rem;
    }

    .record_body .toDetail img {
        width: 0.12rem;
    }

    .one_suggest_title {
        line-height: 0.3rem;
    }

    .one_suggest_title span {
        float: right;
        font-size: 0.13rem;
    }

    .one_suggest_title span.areaday {
        color: #ccc;
    }

    .one_suggest_title span.edit {
        color:#5FA1F0;
    }

    .one_suggest_body {
        font-size: 0.13rem;
        line-height: 0.2rem;
        color: #333;
    }

    .one_suggest_reply {
        margin-top: 0.12rem;
    }

    .reply_title {
        font-size: 0.12rem;
        color: #999;
        margin-bottom: 0.08rem;
    }

    .reply_body {
        padding: 0.1rem;
        background: #EEE;
        color: #333;
    }

    .reply_body > div {
        color: #333;
        line-height: 0.2rem;
        font-size: 0.13rem;
        color: #777;
    }

    .reply_body > div:nth-child(1) {
        margin-top: 0.1rem;
    }

    .reply_body .score {
        float: right;
    }

    .reply_body .score span {
        color: #5FA1F0;
    }

    .mint-popup {
        width: 70%;
        box-sizing: content-box;
    }

    .mint-popup-div {
        padding: 0.12rem;
        background: #fff;
    }

    .mint-popup-div h4 {
        margin: 0;
        padding: 0;
    }

    .mint-popup-div .mint-cell {
        margin: 0.2rem 0;
    }

    .mint-popup-div > div.btns {
        height: 0.3rem;
    }

    .mint-popup-div .btn {
        float: right;
        margin-right: 0.1rem;
        background: #fff;
        box-shadow: none;
        width: 50%;
        margin: 0;
    }

    .mint-popup-div .btn:nth-child(1) {
        color: #f00;
    }

    .allocation {
        width: 100%;
        height: 100%;
    }

    .noList {
        text-align: center;
        padding: 1.5rem 0;
    }

    .noList img {
        width: 1.2rem;
        height: 1.2rem;
    }

    .order {
        height: 0.6rem;
        line-height: 0.6rem;
        text-align: center;
    }

    .order .line {
        display: inline-block;
        width: 1.2rem;
        border-top: 1px solid #ccc ;
        vertical-align: middle;
    }

    .order .txt {
        color: #ccc;
        font-size: 0.13rem;
        vertical-align: middle;
    }

    
    .showMoreBtn {
        display: inline-block;
        float: right;
        position: relative;
    }

    .showMoreBtn img {
        width: 0.23rem;
    }

    .btn_list {
        position: absolute;
        right: -0.04rem;
        top: 0.30rem;
        border:#aaa;
        background: #fcfcfc;
        box-shadow: 5px 5px 10px #ccc;
        z-index: 105;
    }

    .btn_list:before {
        display: inline-block;
        width: 0;
        height: 0;
        border: solid transparent;
        border-width: 10px;
        border-bottom-color: #fcfcfc;
        content: "";
        position: absolute;
        top: -20px;
        right: 0.06rem;
    }

    .btn_list > div {
        width: 0.6rem;
        font-size: 0.13rem;
        padding: 0.1rem 0.15rem;
        border-bottom: 1px solid #efefef;
        text-align: center;
    }

    .btn_list > div img {
        width: 0.14rem;
        height: 0.14rem;
        vertical-align: middle;
        margin-right: 0.05rem;
    }

    .btn_list > div span {
        height: 0.15rem;
        line-height: 0.15rem;
        vertical-align: middle;
    }

    .hide {
        display: none;
    }
</style>
<style>
    .mint-popup-div input {
        border: 1px solid #ddd;
        border-radius: 5px;
        padding: 0.08rem;
    }
</style>