<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:'同意',
            liuxiangUser:'',
            user:JSON.parse(sessionStorage.getItem('usercode')),
            code:JSON.parse(sessionStorage.getItem('actCode')),
            userName:JSON.parse(sessionStorage.getItem('un')),
            userid:JSON.parse(sessionStorage.getItem('userid')),
            users:[],
            taskActor1:[],
            taskActor2:[],
            liuXiang:[],
            liuXiangCode:'',
            approve:'',
            title:'',
            url:'',
            taskActorCode:'',
            taskActorName:'',
            opinionText:'',
        }
    },
    created() {
        let FWQ ={
            taskActorName:'第三方注册系统编码',
            actUserName:'调取地产云地址',
            opinionDate:'',
            opinionConfirm:'',
            opinionText:'Web接口',
        };
        this.$store.dispatch('GetSystemURL',FWQ).then(rotn=>{
            this.url = rotn.actUserName;
            this.taskActorName = rotn.taskActorName;
            this.opinionText = rotn.opinionText;
            this.$axios({
                url:this.opinionText+"/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.opinionText+"/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 = '';
            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);
            let GX ={//OA更新接口数据参数
                // registerCode:this.taskActorName,//注册编码
                // taskId:'',
                // state:'1',
                // subState:'0',            //致远OA参数
                syscode:this.taskActorName,//泛微OA参数
                flowid:'',
                requestname:'',
                workflowname:'地产业务事项', 
                receiver:this.userid,
            } 
            this.$axios({
                url:this.opinionText+"/WorkFlowBack?userCode=" + this.user +
                "&actCode=" + this.code +
                "&routerCode=" + this.liuXiangCode +
                "&taskActors=" + taskActors +
                "&opinion=" + encodeURIComponent(this.text) +
                "&auditValue=" + this.approve,
                method:'get',
                //发送格式为json
                headers:{
                'Content-Type': 'application/json'
                }
            }).then(r=>{
                // 取当前人的往OA传过去的待办id
                this.$axios({
                    url:this.opinionText+"/GetWorkFlowActListDone?userCode="+this.user+"&procedureName="+encodeURIComponent(procedureName),
                    method:'get',
                    //发送格式为json
                    headers:{
                    'Content-Type': 'application/json'
                    }
                }).then(rTaskId=>{
                    let tid = '';
                    for(let o of rTaskId.data.data){
                        if(o.flowNumber == flowNumber){
                            let kmid = o.actCode+o.fromDate;
                            tid = kmid.replace('-','').replace(' ','').replace(':','');
                            GX.requestname = o.title;
                        }
                    }
                    GX.flowid = tid.replace('-','');
                    taskActors += this.taskActorCode;
                    if(r.data.msg=='退回成功'){
                        this.$Message.success(r.data.msg);
                        let taskId = '';//生成唯一
                        let titleS= '';
                        let lastActCode = '';
                        //let XXSZ = []; //致远OA消息
                        let DBSZ = [];
                        let lastuserid = '';
                        let createdatetime = '';
                        for(let opop of this.taskActor2){
                            let uservm = {
                                actUserName:opop.name,
                            }
                            this.$store.dispatch('GetUserVM',uservm).then(r4=>{
                                lastuserid = r4.userID;})
                            // 取下个人的往OA传过去的待办id
                            this.$axios({
                                url:this.opinionText+"/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
                                        taskId = ksid.replace('-','').replace(' ','').replace(':','');
                                        createdatetime = o.fromDate;
                                    }
                                }
                                var nowDate = new Date();
                                //当前日期
                                let date = nowDate.getFullYear()+'-'+((nowDate.getMonth()+1)<10?'0'+(nowDate.getMonth()+1):(nowDate.getMonth()+1))+'-'+(nowDate.getDate()<10?'0'+nowDate.getDate():nowDate.getDate())+' '+(nowDate.getHours()<10?'0'+nowDate.getHours():nowDate.getHours())+':'+(nowDate.getMinutes()<10?'0'+nowDate.getMinutes():nowDate.getMinutes())+':'+(nowDate.getSeconds()<10?'0'+nowDate.getSeconds():nowDate.getSeconds());
                                let casecode = '';
                                this.$axios({
                                    url:this.opinionText+"/GetAttachmentFileList?actcode="+lastActCode,
                                    method:'get',
                                    //发送格式为json
                                    headers:{
                                    'Content-Type': 'application/json'
                                    }
                                }).then(res=>{
                                    casecode = res.data.data.casecode
                                    let applicantion = '';
                                    this.$store.dispatch('GetWorkFlowCaseVM',casecode).then(reo=>{
                                        if(reo.applicationCode==null){
                                            applicantion = '没有获取到applicantion';
                                        }
                                        else{
                                            applicantion = reo.applicationCode
                                        }
                                        // 第一个参数:加密的字.第二个参数:加密需要用到的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
                                        let encrypted4 = CryptoJS.AES.encrypt(casecode, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});//加密caseCode
                                        let encrypted5 = CryptoJS.AES.encrypt(applicantion, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});//加密caseCode
                                        // let DB ={//致远OA待办接口数据参数
                                        //     registerCode: this.taskActorName,
                                        //     taskId: taskId.replace('-',''),
                                        //     title: this.title,
                                        //     senderName: this.userName,
                                        //     state: "0",
                                        //     thirdSenderId: this.user,
                                        //     thirdReceiverId: opop.name,
                                        //     creationDate: date,
                                        //     url: `${this.url}reception?&type=1&userCode=${encrypted1.toString()}&frameType=info&caseCode=${encrypted4.toString()}&actCode=${encrypted2.toString()}&applicantion=${encrypted5.toString()}&PName=${encodeURIComponent(procedureName)}`,
                                        //     h5url: `${this.url}reception?&type=0&userCode=${encrypted1.toString()}&actCode=${encrypted2.toString()}&un=${encrypted3.toString()}&PName=${encodeURIComponent(procedureName)}`,
                                        // }
                                        // DBSZ.push(DB);
                                        // let XX ={//OA消息接口数据参数
                                        //     thirdpartyMessageId:taskId.replace('-',''),
                                        //     thirdpartyRegisterCode:this.taskActorName,
                                        //     messageContent:titleS,
                                        //     thirdpartySenderId:this.user,
                                        //     thirdpartyReceiverId:opop.name,
                                        //     creation_date:date,
                                        //     downloadUrl:'',
                                        //     messageURL:`${this.url}reception?&type=1&userCode=${encrypted1.toString()}&frameType=info&caseCode=${encrypted4.toString()}&actCode=${encrypted2.toString()}&applicantion=${encrypted5.toString()}&PName=${encodeURIComponent(procedureName)}`,
                                        //     messageH5URL:`${this.url}reception?&type=0&userCode=${encrypted1.toString()}&actCode=${encrypted2.toString()}&un=${encrypted3.toString()}&PName=${encodeURIComponent(procedureName)}`,
                                        //     noneBindingSender:'',//发送人/登录名称/人员编码/手机号/电子邮件
                                        //     noneBindingReceiver:'',//接收人/登录名称/人员编码/手机号/电子邮件
                                        // } 
                                        // XXSZ.push(XX);
                                        // if(DBSZ.length==this.taskActor2.length&&XXSZ.length==this.taskActor2.length){
                                        //     this.$store.dispatch('getGengXin',GX);//存到vuex
                                        //     this.$store.dispatch('getDaiBan',DBSZ);//存到vuex
                                        //     //this.$store.dispatch('getXiaoXi',XXSZ);//存到vuex
                                        //     this.$router.push('/transmit'); 
                                        let DB ={//泛微OA待办接口数据参数
                                            syscode: this.taskActorName,
                                            flowid: taskId.replace('-',''),
                                            requestname: titleS,
                                            workflowname: '地产业务事项',
                                            nodename: procedureName,
                                            pcurl: `${this.url}reception?&amp;type=1&amp;userCode=${encrypted1.toString()}&amp;frameType=info&amp;caseCode=${encrypted4.toString()}&amp;actCode=${encrypted2.toString()}&amp;applicantion=${encrypted5.toString()}&amp;PName=${encodeURIComponent(procedureName)}`,
                                            appurl: `${this.url}reception?&amp;type=0&amp;userCode=${encrypted1.toString()}&amp;actCode=${encrypted2.toString()}&amp;un=${encrypted3.toString()}&amp;PName=${encodeURIComponent(procedureName)}`,
                                            creator: this.userid,
                                            createdatetime: createdatetime,
                                            receiver: lastuserid,
                                            receivedatetime: date,

                                        }
                                        DBSZ.push(DB);
                                        if(DBSZ.length==this.taskActor2.length){
                                            this.$store.dispatch('getGengXin',GX);//存到vuex
                                            this.$store.dispatch('getDaiBan',DBSZ);//存到vuex
                                            
                                            this.$router.push('/transmitfw'); 
                                        }
                                    })
                                })
                            })
                        }
                        // this.$router.push('/zaiban')
                    }else{
                        this.$Message.error(r.data.msg);
                    }
                })  
            })
                     
        },
        // 选择流向
        updateliuXiang(i){
            this.taskActor1 = [];
            this.liuXiangCode = i.RouterCode;
            this.$axios({
                url:this.opinionText+"/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>
