<template>
  <div v-wechat-title="$route.meta.title" style="height:100%;background-color:#FAFAFA;">
    <div style="height:1px;"></div>    
        <div class="housebackgroundcolor">
           <div class="houselist" >
                <Icon  type="ios-arrow-back" style="color:#fff;" @click="openYiBan"/>
                <span style="color:#fff;" @click="openYiBan">返回</span>
                <Icon @click="openMain" size="18" style="color:#fff;float:right;margin-right:10px;" type="md-home" />
            </div>
            <hr style="border:0.5px solid #DDDDDD;" align="center">
        </div>
        <div style="height:10px;"></div>
        <!-- 流向 -->
        <hr style="border:0.5px solid #DDDDDD;" align="center">
        <div style="background-color:#fff;height:50px;">
            <p class="bodyone"><Icon type="md-cloud-upload" size="18" style="margin-right:5px;"/>流向:</p>
            <p style="display:inline-block;margin-left:10px;">{{taskActorName}}</p>
        </div>
        <hr style="border:0.5px solid #DDDDDD;" align="center">
        <div style="height:10px;"></div>
        <!-- 处理人 -->
        <hr style="border:0.5px solid #DDDDDD;" align="center">
        <div style="background-color:#fff;">
            <p class="bodyone"><Icon type="md-contact" size="18" style="margin-right:5px;"/>处理人:</p>
            <div style="display:inline-block;margin-left:5px;">
                <Button v-for="i in users" :key="i.userCode" 
                    :style="i.selected?'margin:10px 10px 10px 10px;background-color:blue;color:#fff;':'margin:10px 10px 10px 10px;'" 
                    icon="ios-contact-outline" 
                    @click="updateSelected(i)" 
                    >{{i.userName}}</Button>
            </div>
        </div>
        <hr style="border:0.5px solid #DDDDDD;" align="center">
        <div style="height:10px;"></div>
        <!-- 处理意见 -->
        <hr style="border:0.5px solid #DDDDDD;" align="center">
        <div style="background-color:#fff;">
            <p class="bodyone"><Icon type="ios-create" size="18" style="margin-right:5px;"/>处理意见</p>
            <div style="width:90%;margin:10px 0px 10px 20px;">
                 <Input v-model="text" type="textarea" :rows="4" placeholder="请输入您的处理意见"/>
            </div>
        </div>
        <hr style="border:0.5px solid #DDDDDD;" align="center">
        <div style="height:10px;"></div>
         <Button @click="openSend()" long style="height:40px;" type="primary">确定</Button>
    </div>
</template>
<script>
import CryptoJS from "crypto-js";
export default {
    data() {
        return {
            text:'同意',
            user:JSON.parse(sessionStorage.getItem('usercode')),
            code:JSON.parse(sessionStorage.getItem('actCode')),
            userName:JSON.parse(sessionStorage.getItem('un')),
            taskActorName:'',
            url:'',
            users:[],
            value5:true,
            taskActor1:[],
            taskActor2:[],
            roterCode:'',
            approve:'',
            title:'',
            taskActorCode:'',
            multiSelect:false,
            ress:'',
        }
    },
    created() {
        if(this.user==null){
            this.$router.push('/');
        }
        let a = {
            ApplicationCode:'Webservice接口地址',
            url:'移动审批地址',
        }
        this.$store.dispatch('PostSystemURL',a).then(rurles=>{
            this.ress = rurles.applicationCode;
            this.url = rurles.url;
            this.$axios({
                url: '/'+this.ress+'/'+"GetWorkFlowSendRouter?userCode="+this.user+"&actCode="+this.code,
                method:'get',
                //发送格式为json
                headers:{
                'Content-Type': 'application/json'
                }
            }).then(r=>{
            this.roterCode = r.data.data[0].RouterCode;//获取routerCode
                    this.$axios({
                        url: '/'+this.ress+'/'+"GetWorkFlowSendTaskActor?userCode="+this.user+"&actCode="+this.code+"&routerCode="+this.roterCode,
                        method:'get',
                        //发送格式为json
                        headers:{
                        'Content-Type': 'application/json'
                        }
                    }).then(r1=>{
                    this.multiSelect = r1.data.data[0].multiSelect;
                    this.users = r1.data.data[0].users;//获取处理人(数组)
                    this.taskActorCode = r1.data.data[0].taskActorCode;
                    for(let i of this.users){
                        if(i.selected==true){
                            let a = {
                                name:i.userCode
                            }
                            this.taskActor1.push(a)
                        }
                        if(this.taskActor1.find(f=>f.name==i.userCode)==undefined){
                            if(i.selected==true){
                                let a = {
                                    name:i.userCode
                                }
                                this.taskActor1.push(a)
                            }
                        }
                }
                    this.taskActor2 = this.taskActor1;
                this.taskActorName = r1.data.data[0].taskActorName;//获取流向
            })
            })
            this.$axios({
                url: '/'+this.ress+'/'+"GetWorkFlowPage?userCode="+this.user+"&actCode="+this.code,
                method:'get',
                //发送格式为json
                headers:{
                'Content-Type': 'application/json'
                }
            }).then(r=>{
                this.title = r.data.data.title;
                if(r.data.data.auditResult.items==null){
                    this.approve = '';
                }
                else{
                    this.approve = 'Approve';
                }
            })
        })
    },
    methods: {
        //返回上一层
        openYiBan(){
            this.$router.push('/detauils');
        },
        //返回主页
        openMain(){
            this.$router.push('/main');
        },
        //发送
        openSend(){    
            let procedureName = JSON.parse(sessionStorage.getItem('pname'));  
            let flowNumber = JSON.parse(sessionStorage.getItem('flowNumber'));    
            let taskActors = '';
            let txt = this.text;
            if(this.user==null || this.code==null){
                this.$Message.error('userCode/actCode丢失!');
                return;
            }
            if(this.taskActor2.length==0){
                this.$Message.info('请选择处理人!');
                return;
            }
            let tackCCode = '';
            for(let i of this.taskActor2){
                let tasktn = i.name+','+this.taskActorCode+';'
                tackCCode+=tasktn;
            }
            let taskActorLength = tackCCode.length-1;
            taskActors = tackCCode.substring(0,taskActorLength);
            this.$axios({
                url: '/'+this.ress+'/'+"WorkFlowSend?userCode=" + this.user +
                "&actCode=" + this.code +
                "&routerCode=" + this.roterCode +
                "&taskActors=" + taskActors +
                "&opinion=" + this.text +
                "&auditValue=" + this.approve,
                method:'get',
                //发送格式为json
                headers:{
                'Content-Type': 'application/json'
                }
            }).then(r=>{
                //下面操作往钉钉发送消息
                if(r.data.msg=='发送成功'){
                    this.$Message.success(r.data.msg);
                    let titleS= '';
                    let lastActCode = '';//下一个人actCode
                    for(let opop of this.taskActor2){
                        // 取下个人待办id
                        this.$axios({
                            url: '/'+this.ress+'/'+"GetWorkFlowActListNotSigned?userCode="+opop.name+"&procedureName="+encodeURIComponent(procedureName),
                            method:'get',
                            //发送格式为json
                            headers:{
                            'Content-Type': 'application/json'
                            }
                        }).then(r1=>{
                            for(let o of r1.data.data){
                                if(o.flowNumber == flowNumber){
                                    let ksid = o.actCode+o.fromDate;
                                    lastActCode = o.actCode
                                    titleS = o.title;
                                }
                            }
                            // 第一个参数:加密的字.第二个参数:加密需要用到的16位字符串的key;
                            let key = CryptoJS.enc.Utf8.parse('absoietlj32fai12');
                            let encrypted1 = CryptoJS.AES.encrypt(opop.name, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});//加密userCode
                            let encrypted2 = CryptoJS.AES.encrypt(lastActCode, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});//加密actCode
                            let encrypted3 = CryptoJS.AES.encrypt(this.userName, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});//加密userName
                            this.$store.dispatch('GetUser',opop.name).then(pwds=>{
                                let SystemUser ={ //钉钉消息接口数据参数
                                    PWD:'',
                                    UserID:pwds,
                                    UserName:'发送测试',
                                    UserCode:titleS,
                                    URL:`${this.url}reception?&type=0&userCode=${encrypted1.toString()}&actCode=${encrypted2.toString()}&un=${encrypted3.toString()}&PName=${encodeURIComponent(procedureName)}`, 
                                } 
                                this.$store.dispatch('GetInformation',SystemUser).then(r=>{
                                    this.$router.push('/main');
                                })
                            })
                        })
                    }
                }else{
                    this.$Message.error(r.data.msg);
                }
                //放开下面代码需要注释掉上方(操作往钉钉发送消息)
                //this.$router.push('/daiban');
            })  
                     
        },
        //选择处理人  multiSelect T多 F单
        updateSelected(i){
            if(this.multiSelect==true){
                if(i.selected == false)
                {
                    i.selected = true;
                    if(this.taskActor1.find(f=>f.name==i.userCode)==undefined){
                        let a = {
                            name:i.userCode
                        }
                        this.taskActor1.push(a)
                    }
                    this.taskActor2 = this.taskActor1;
                }
                else
                {
                    this.taskActor1 = [];
                    i.selected = false
                    for(let i of this.users){
                    if(i.selected==true){
                            let a = {
                                name:i.userCode
                            }
                            this.taskActor1.push(a)
                    }
                }
                this.taskActor2 = this.taskActor1;
                }
            }
            else{
                if(i.selected == false)
                {
                    this.taskActor1 = [];
                    i.selected = true;
                    for(let l of this.users){
                        if(l.userCode!=i.userCode){
                            l.selected = false;
                        }
                        else{
                            if(this.taskActor1.find(f=>f.name==i.userCode)==undefined){
                                let a = {
                                    name:i.userCode
                                }
                            this.taskActor1.push(a)
                        }
                            this.taskActor2 = this.taskActor1;
                        }
                    }
                }
                else
                {
                    i.selected = false
                    this.taskActor2 =[]
                }
            }
        }
    },
}
</script>

<style>
    .housebackgroundcolor{
    background-color:rgb(55, 150, 223);
    }
    .housebackgroundcolor:hover{
    cursor: pointer;  
    }
    .houselist{
    padding:13px 0px 0px 10px;height:45px;font-size:13px;
    }
    .headdone{
    width:25%;display: inline-block;text-align:center;height:40px;
    }
    .headdtwo:hover{
    cursor: pointer;
    }
    .headdtwo{
    margin-top:10px;font-weight:bold;
    }
    .bodyone{
        padding:10px 0px 0px 10px;font-weight:bold;font-size:15px;display:inline-block;
    }
    .bodyone:hover{
        cursor: pointer;
    }
     input{
        border: none;
        outline: none;
    }
</style>
