<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;">
            <p class="bodyone"><Icon type="md-cloud-upload" size="18" style="margin-right:5px;"/>流向:</p>
            <br>
            <Button v-for="i in liuXiang" :key="i.RouterCode" 
                :style="liuXiangCode==i.RouterCode?'margin:10px 5px 10px 10px;background-color:#2D8CF0;color:#fff;width:45%;height:40px;':'margin:10px 5px 10px 10px;background-color:#A1A1A1;color:#fff;width:45%;height:40px;'" 
                icon="ios-contact-outline" 
                @click="updateliuXiang(i)" 
                >{{i.Description}}</Button>
        </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;min-height:30px;"/>处理人:</p>
            <div style="display:inline-block;margin-left:5px;">
                <Button v-for="i in users" :key="i.userCode" 
                    style="margin:10px 10px 10px 10px;background-color:blue;color:#fff;" 
                    icon="ios-contact-outline" 
                    >{{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')),
            users:[],
            taskActor1:[],
            taskActor2:[],
            liuXiang:[],
            liuXiangCode:'',
            approve:'',
            title:'',
            url:'',
            taskActorCode:'',
            ress:'',
        }
    },
    created() {
        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+'/'+"GetWorkFlowBackRouter?userCode="+this.user+"&actCode="+this.code,
                method:'get',
                //发送格式为json
                headers:{
                'Content-Type': 'application/json'
                }
            }).then(r=>{
                if(r.data.data.length!=0){
                    this.liuXiang = r.data.data;
                }
            })
            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('/zaibandetauils');
        },
        //返回主页
        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);
            taskActors += this.taskActorCode;
            this.$axios({
                url: '/'+this.ress+'/'+"WorkFlowBack?userCode=" + this.user +
                "&actCode=" + this.code +
                "&routerCode=" + this.liuXiangCode +
                "&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 = '';
                    for(let opop of this.taskActor2){
                        // 取下个人的往OA传过去的待办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;
                                    titleS = o.title;
                                    lastActCode = o.actCode
                                }
                            }
                            // 第一个参数:加密的字.第二个参数:加密需要用到的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');
            })  
                     
        },
        // 选择流向
        updateliuXiang(i){
            this.taskActor1 = [];
            this.liuXiangCode = i.RouterCode;
            this.$axios({
                url: '/'+this.ress+'/'+"GetWorkFlowBackTaskActor?userCode="+this.user+"&actCode="+this.code+"&routerCode="+i.RouterCode,
                method:'get',
                //发送格式为json
                headers:{
                'Content-Type': 'application/json'
                }
            }).then(r1=>{
                this.users = r1.data.data[0].users;//获取处理人(数组)
                this.taskActorCode = r1.data.data[0].taskActorCode;
                for(let i of this.users){
                    let a = {
                        name:i.userCode
                    }
                    this.taskActor1.push(a)
                }
                this.taskActor2 = this.taskActor1;
            })
        },
    },
}
</script>

<style>
    .housebackgroundcolor{
    background-color:rgb(55, 150, 223);
    }
    .housebackgroundcolor:hover{
    cursor: pointer;  
    }
    .houselist{
    padding:13px 0px 0px 10px;height:45px;font-size:13px;
    }
    .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>
