<template>
    <!--事项选票-->

    <div class="matterVote">
        <div class="voteTable">
<!--            表决Title-->
            <div class="matterTitle">
               <span>{{ mylocale=='zh'?'事项表决':'Vote' }}</span>
            </div>

            <div class="matterContent">
                <el-row :gutter="20">
                    <el-col :span="2"><div class="grid-content bg-purple">{{ mylocale=='zh'?'序号':'No. ' }}</div></el-col>
                    <el-col :span="17"><div class="grid-content bg-purple">{{ mylocale=='zh'?'标题':'Content' }}</div></el-col>
                    <el-col :span="5"><div class="grid-content bg-purple">{{ mylocale=='zh'?'意见':'opinion' }}</div></el-col>
                </el-row>
                <div class="voteMessage">
                    <el-row :gutter="20">
                        <el-col :span="2"><div class="grid-content bg-purples">1</div></el-col>
                        <el-col :span="17"><div class="grid-content bg-purples bg-title">{{meetingContent.title}}</div></el-col>
                        <el-col :span="5">
                            <div class="grid-content bg-purples">
                                <!-- 投票按钮 -->
                                <div class="voteBtn" v-if="meetingContent.voted === false">
                                    <el-button size="small" type="info" @click="statistical(meetingContent,'0')" :class="activeIndex.a0.indexOf(meetingContent.id) != -1 ?'active':''">{{ mylocale=='zh'?'赞成':'Approve'}}</el-button>
                                    <el-button size="small" type="info" @click="statistical(meetingContent,'1')" :class="activeIndex.a1.indexOf(meetingContent.id) != -1 ?'active':''">{{ mylocale=='zh'?'反对':'Oppose' }}</el-button>
                                    <el-button size="small" type="info" @click="statistical(meetingContent,'2')" :class="activeIndex.a2.indexOf(meetingContent.id) != -1 ?'active':''">{{ mylocale=='zh'?'弃权':'waiver' }}</el-button>
                                </div>
                                <div class="voteBtn" v-if="meetingContent.voted === true">
                                    {{$t('common.AreadyVote')}}
                                </div>
                            </div>
                        </el-col>
                    </el-row>
                </div>
                <!-- 事项投票内容展示-->
                <el-dialog
                        :title="'投票内容'"
                        :visible.sync="dialogVisible"
                        width="50%">
                    <div v-html="showIntroduction"></div>
                </el-dialog>
            </div>

            <!-- ==================== -->
            <div class="submitVote" v-if="meetingContent.voted !== true">
               <!-- <el-button class="voteSty" type="danger"  @click="statistical('','4')">{{ mylocale=='zh'?'全部赞成':'Approve All'}}</el-button> -->
               <el-button type="primary" @click="submitVote" style="padding:0">{{ mylocale=='zh'?'提交投票':'Submit to vote'}}</el-button>
            </div>

            <!-- 进入下一页的探窗 -->
            <el-dialog
              :show-close="false"
              :visible.sync="OverFlag"
              :close-on-click-modal="false"
              width="700px">
              <div class="category-body submit-success">
                <p style="font-size: 24px; color: #666; margin-bottom: 20px;text-align:center">{{$i18n.locale=='en'? 'The vote was successful.Click Confirm to go to the next page': '投票成功,点击确定进入下一页.'}}</p>
                <p style="text-align:center">
                  <!-- <el-button style="background: #2c558a; color: #fff" @click="toView('home')">{{$i18n.locale=='en'? 'Back Home': '返回首页'}}</el-button> -->
                  <el-button type="primary" @click="Iknow">{{$i18n.locale=='en'? 'Confirm': '确定'}}</el-button>
                </p>
              </div>
            </el-dialog>
        </div>

    </div>
</template>
<script>
    import {getMeetingGeidcoMatters,saveOrUpdateMatter,getCouncilVoteSituation} from "@/api/vote/council";
    import { getDicts } from "@/api/dict";
    import Base from "@/views/base/Base";
    // import vote from "../AddFeatures/vote/vote";
    export default {
        name:'MatterVote',
        extends: Base,
        props:[
        //   'vote',
        //   'showLink'
        ],
        data () {
            return {
                mylocale:this.$i18n.locale,
                // positionDic:'',
                // candidateList:[],
                num:0,
                meetingContent:'',
                allVoteList:new Map(),
                endVote:[],
                mattersList:[],
                activeIndex:{
                    a0:[],
                    a1:[],
                    a2:[]
                },
                votedLeader:'',
                votedMatter:'',
                dialogVisible:false,
                showIntroduction:'',
                // showLink:'',
                isRefresh:'',
                trueVote:[],
                falseVote:[],
                flalseLength:'',
                meetingGeidcoMatterNumDetail:[],
                OverFlag:false,
            }
        },
        mounted() {
            // this.getDictsList();
            this.getMatters();
            console.log('啪啪啪啪啪啪铺铺铺铺铺',this.mylocale)
        },
        destroyed(){
            clearInterval(this.isRefresh)
        },
        methods:{
            //将投票结果统计到数组
            statistical(item,sta){
                // let matterId = item.id;
                let arr0 = this.activeIndex.a0
                let arr1 = this.activeIndex.a1
                let arr2 = this.activeIndex.a2
                if(sta=='0'){
                    if(arr0.indexOf(item.id)==-1) arr0.push(item.id)
                    if(arr1.indexOf(item.id)!=-1) arr1 = arr1.filter(ele=>{return ele!=item.id})
                    if(arr2.indexOf(item.id)!=-1) arr2 = arr2.filter(ele=>{return ele!=item.id})
                    this.activeIndex.a0 = arr0
                    this.activeIndex.a1 = arr1
                    this.activeIndex.a2 = arr2
                    this.meetingGeidcoMatterNumDetail=[]
                    this.meetingGeidcoMatterNumDetail.push({
                        matterId:item.id,
                        type:sta
                    })
                }
                if(sta=='1'){
                    if(arr0.indexOf(item.id)!=-1) arr0 = arr0.filter(ele=>{return ele!=item.id})
                    if(arr1.indexOf(item .id)==-1) arr1.push(item.id)
                    if(arr2.indexOf(item.id)!=-1) arr2 = arr2.filter(ele=>{return ele!=item.id})
                    this.activeIndex.a0 = arr0
                    this.activeIndex.a1 = arr1
                    this.activeIndex.a2 = arr2
                    this.meetingGeidcoMatterNumDetail=[]
                    this.meetingGeidcoMatterNumDetail.push({
                        matterId:item.id,
                        type:sta
                    })
                }
                if(sta=='2'){
                    if(arr0.indexOf(item.id)!=-1) arr0 = arr0.filter(ele=>{return ele!=item.id})
                    if(arr1.indexOf(item.id)!=-1) arr1 = arr1.filter(ele=>{return ele!=item.id})
                    if(arr2.indexOf(item.id)==-1) arr2.push(item.id)
                    this.activeIndex.a0 = arr0
                    this.activeIndex.a1 = arr1
                    this.activeIndex.a2 = arr2
                    this.meetingGeidcoMatterNumDetail=[]
                    this.meetingGeidcoMatterNumDetail.push({
                        matterId:item.id,
                        type:sta
                    })
                }

            },
             // 获取事项列表信息
            getMatters(){
                // getMeetingGeidcoMatters().then((res)=>{
                //     this.meetingContent = res.data.meetingGeidcoMatters
                // })
                this.meetingContent = [];
                this.trueVote = [];
                this.falseVote = [];
                let params = {
                    language: this.mylocale
                }
                getMeetingGeidcoMatters(params).then((res)=>{
                    this.meetingContent = res.data.meetingGeidcoMatters[1];
                    if( this.mylocale =='en'){
                        this.meetingContent.title = this.meetingContent.titleEn
                        this.meetingContent.introduction = this.meetingContent.introductionEn;
                    }
                    this.$emit('pushFlag',this.meetingContent.voted);
                    this.statistical(this.meetingContent,'0')
                    // if(this.meetingContent.voted == true){
                    //     this.trueVote.push(position)
                    // }else {
                    //     this.falseVote.push(position)
                    // }
                    //============================================================
                    // this.mattersList.forEach((position)=>{
                    //     if( this.mylocale =='en'){
                    //         position.title = position.titleEn
                    //         position.introduction = position.introductionEn;
                    //     }
                    //     if(position.voted == true){
                    //         this.trueVote.push(position)
                    //     }else {
                    //         this.falseVote.push(position)
                    //     }
                    //     this.meetingContent.push(position)
                    // });
                    this.flalseLength =  this.falseVote.length;

                })
            },
            // test(){
            //     this.submitHandler((token) => {
	        //         saveOrUpdateMatter(JSON.stringify(this.meetingGeidcoMatterNumDetail), token).then(result => {
	        //             //提示投票成功(提示信息要区分中英文)
    
	        //         }).catch((error) => {
	        //             // 此处你的业务代码
	        //             // this.resetToken();
	        //         });
    		//     });

            // },
            //提交投票结果
            submitVote(){
                // this.OverFlag = true;
                this.submitHandler((token) => {
	                saveOrUpdateMatter(JSON.stringify(this.meetingGeidcoMatterNumDetail), token).then(res => {
	                    //提示投票成功(提示信息要区分中英文)
                        if(res.status=="200") {
                        //   let msgTxt =this.$i18n.locale == 'en' ? 'Submitted successfully. Thank you for your feedback' : '投票成功';
                        //   this.$message({
                        //       message: msgTxt,
                        //       type: 'success',
                        //   });
                        this.OverFlag = true;
                           this.resetToken();
                        }
                        //重新查询
                        this.refreshMatter()
	                }).catch((error) => {
	                    // 此处你的业务代码
	                    this.resetToken();
	                });
    		    });
            },
            // 查询投票状态
            getVoteState(){
                const that = this;
                getCouncilVoteSituation().then((res)=>{
                    let sta = res.data.councilVoteSituation;
                    this.votedLeader=sta.votedLeader;
                    this.votedMatter=sta.votedMatter
                    // this.showLink = sta.showCouncilLiveingLink;
                    this.refreshPage()

                })

            },
            // 事项详情弹窗
            dialogVisibleChange(item){
                this.dialogVisible = true;
                this.showIntroduction = item.introduction
                console.log('1234321232123')
            },
            // 刷新按钮事件
            refreshMatter(){
                this.getMatters()
            },
            Iknow(){
                this.$emit('getVotepage',2)
                this.OverFlag = false
            }
            
        }
    }
</script>
<style scoped>
    .matterVote{
        margin: auto;
        padding: 0;
        width: 90%;
        background: #fff;
    }
    .voteTable{
        width: 100%;
        padding: 42px 0 0px 0;
        /* background:#F0F3F7 url("../../assets/img/electionBallot/DirectorVotes/1.png") repeat; */
    }
    .matterTitle{
        width: 1200px;
        margin: auto;
        height: 45px;
        text-align: center;
        font-size: 28px;
        font-weight: 600;
        color: #666666;
    }
    .el-row {
        margin-bottom: 20px;
        text-align: center;
        line-height: 42px;
    }
    .el-col {
        border-radius: 4px;
    }
    .bg-purple-dark {
        background: #99a9bf;
    }
    .bg-purple {
        background: #d3dce6;
        font-size: 18px;
    }
    .bg-purples {
        background: #fff;
        font-size: 16px;
    }
    .bg-title{
        text-align: left;
        padding-left:10px
    }
    .bg-purple-light {
        background: #e5e9f2;
    }
    .grid-content {
        height: 42px;
        border-radius: 4px;
        min-height: 36px;
    }
    .row-bg {
        padding: 10px 0;
        background-color: #f9fafc;
    }
    .matterContent{
        width: 1200px;
        margin:20px auto;
    }
    .voteMessage{
        /*width: 100%;*/
        height: 65px;
        line-height: 65px;
        /* padding: 0 20px; */
        border-radius: 8px;
        background: #fff;
        /* margin-bottom: 15px; */
        position: relative;
        cursor: pointer;
    }
    /*.moreMessage{*/
    /*    display: none;*/
    /*}*/

    /*.voteMessage:hover .moreMessage{*/
    /*    display: block;*/
    /*    padding: 30px;*/
    /*    background: #ffffff;*/
    /*    box-shadow: 0px 2px 10px 0px rgba(167, 167, 167, 0.5);*/
    /*    width:60%;*/
    /*    line-height: 40px;*/
    /*    position: absolute;*/
    /*    border-radius: 6px;*/
    /*    left:-10%;*/
    /*    top: 60px;*/
    /*    z-index: 999;*/
    /*    overflow: scroll;*/
    /*    max-height:300px;*/
    /*}*/
    .message{
        width: 75%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        display: inline-block;
        float: left;
    }
    .voteBtn{
        float: right;
        display: inline-block;
        text-align: center;
        height: 100%;
        width: 100%;
    }
    .voteBtn > .el-button{
        border-radius: 6px;
    }
    .submitVote{
        height: 100px;
        line-height: 100px;
        text-align: center;
    }
    .submitVote > .el-button{
        width: 150px;
        height: 50px;
        border-radius: 5px;
        font-size: 20px;
    }
    .submitVote .el-button:nth-child(1){
        background-color: #6dd400;
    }
    .submitVote .el-button:nth-child(2){
        background-color: #6f9fe8;
    }
    .el-button{
        border: none;
        /* padding: 0; */
    }
    .active{
        background: #6dd400;
        border: none;
    }
    .refreshBtn{
        float: right;

        height: 30px;
        margin-top: 10px;
        padding: 5px 5px;
        border-radius: 5px;
        background: #409EFF;
        border: none;
        color: #ffffff;
        cursor: pointer;
    }
    .notActive{
        background: #A6A9AD;
    }

</style>
