<template>
    <div>
        <div class="tv-container">
            <div class="container-box">
                <div class="el-label"><i class="el-icon-collection-tag"></i>基本信息</div>
                <div class="el-line"></div>
                <baseTable v-if="woNo" :itemId="itemId" :woNo="woNo" :taskName="taskName"/>

                <div class="el-line"></div>
                <div class="el-label"><i class="el-icon-collection-tag"></i>审批轨迹</div>
                <div class="el-line"></div>
                <el-table v-if="rovaList" class="com-table" ref="multipleTable" :data="rovaList" tooltip-effect="dark" size="small"
                    border style="width: 100%">
                    <el-table-column prop="procId" label="编号">
                    </el-table-column>
                    <el-table-column prop="taskName" label="环节">
                    </el-table-column>
                    <el-table-column prop="opName" label="处理人">
                    </el-table-column>
                    <el-table-column prop="opNo" label="处理人工号">
                    </el-table-column>
                    <el-table-column prop="createTime" label="处理时间">
                    </el-table-column>
                    <el-table-column prop="remark" label="审批意见">
                    </el-table-column>
                </el-table>

                <div class="el-line"></div>
                <div class="el-label"><i class="el-icon-collection-tag"></i>{{taskName}}</div>
                <div class="el-line"></div>
                <div>
                  <el-row>
                    <el-col :span="6">&nbsp;</el-col>
                    <el-col :span="12">
                        <el-form ref="form" :model="form" label-width="80px">
                          <el-form-item label="审核结果" v-if="hasAuditResult==='Y'">
                              <el-radio v-model="form.auditResult" label="Y">通过</el-radio>
                              <el-radio v-model="form.auditResult" label="N">未通过</el-radio>
                          </el-form-item>
                          <el-form-item label="是否上线" v-if="showPublish==='Y'">
                              <el-radio v-model="form.publish" label="1">上线</el-radio>
                              <el-radio v-model="form.publish" label="0">未上线</el-radio>
                          </el-form-item>

                          <!--选择下步处理人-->
                          <el-form-item label="审批人" v-if="showUserTreeDlg===true">
                            <div style="display:none;">
                            <el-input type="hidden" placeholder="请选择教师" v-model="assigneeNo"/>
                            </div>
                            <el-input type="input" placeholder="请选择审批人" v-model="assigneeName">
                                <el-button slot="append" icon="el-icon-search" @click="handleShowUserTreeDlg"></el-button>
                            </el-input>
                          </el-form-item>
                          
                          <el-form-item label="备注信息">
                            <el-input type="textarea" v-model="form.note"></el-input>
                          </el-form-item>
                        </el-form>
                    </el-col>
                    <el-col :span="6">&nbsp;</el-col>
                  </el-row>
                </div>
                <div class="el-line"></div>
                <div style="text-align:right">
                  <el-button type="primary" @click="handleAudit">确定</el-button>
                </div>
            </div>
        </div>


        <el-dialog
          title="请选择"
          :visible.sync="userDlgVisual"
          width="30%"
          :modal-append-to-body="false"
          :close-on-click-modal="false"
          :before-close="handleClose">
        <div>
            <el-radio  v-for="user in userList " v-model="assigneeNo" :key="user.loginNoStr" :label="user.loginNoStr">{{user.loginNameStr}}</el-radio>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="userDlgVisual = false">取 消</el-button>
            <el-button type="primary" @click="checkSelectUser">确 定</el-button>
        </span>
      </el-dialog>


        <userDlg :visible="userTreeDlgVisual" @handleSelect="handleUserSelect" @close="closeUserSelect"/>

    </div>
</template>

<script>
import baseTable from  "./base.vue"
import userDlg from "../userDlg"
// 
export default {
    components:{ baseTable ,userDlg},
    data(){
        return{
            itemId : "",
            taskId:"",
            woNo :"",
            taskName:"",
            stepId:"",
            procId:"",
            procVersion:"",
            rovaList:"",
            form:{
                note:"",
                auditResult:"Y",
                publish:"1"
            },
            hasAuditResult:"N",
            showPublish:"N",
            userDlgVisual:false,
            userList:[],

            assigneeNo:"",
            assigneeName:"",
            infoApply:"",

            showUserTreeDlg:false,
            userTreeDlgVisual:false
        }
    },
    methods:{
        dialogCheck() {
            this.$http({
                url: '/bpm/api/taskInit',
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                },
                data: {
                    taskId: this.taskId,
                    woNo: this.woNo
                },
            }).then((res) => {
                this.rovaList = res.data.taskList;
                this.uploadstatus = true;
            });
        },
        submitInfo(u, v) {
            let _this = this;
            this.$http({
                url: u,
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                },
                data: v,
            }).then((res) => {
                if (res.data.result === 1) {
                    _this.$message({
                        message: res.data.desc,
                        type: 'error'
                    });
                } else {
                    _this.$message({
                        message: '成功',
                        type: 'success'
                    });
                    _this.$router.push({"path":"/magicboxAudit"})

                }

            });
        },
        getLeader(loginNoStr,type,level){
            //
            if(loginNoStr===""){
                let userInfo = window.sessionStorage.userInfo;
                if(null === userInfo){
                    this.$message({ showClose: true, message: "数据异常",type: 'error'});
                    return 
                }
                userInfo = JSON.parse(userInfo);
                loginNoStr = userInfo.loginNoStr;
            }
            //
            return new Promise((resolve, reject) => {
            this.$http({
                url: "/market/api/user/info/leader2",
                method: "post",
                headers: {"Content-Type": "application/json"},
                data:{"loginNoStr":loginNoStr,"level":type,"type":level}                
                }).then((res) => {
                if(res.data.result==0){
                    resolve(res.data.body)
                }else{
                    this.$message({ showClose: true, message: '系统异常',type: 'error'});
                }
                }); 
            });
        }
        ,
        completeTask(paramsInfo){
            let remark = "";
            if(this.form.auditResult==='Y'){
                remark ="通过";
            }else if(this.form.auditResult==='N'){
                remark ="未通过";
            }else{
                remark = ""
                if(this.form.publish==='1'){
                    remark ="上线";
                }else if(this.form.publish==='0'){
                    remark ="未上线";
                }else{
                    remark = ""
                }
            }
            //

            //
            //console.log({remark})
            if(remark===""){
                if(this.form.note!==""){
                    remark = this.form.note
                }
            }else{
                if(this.form.note!==""){
                    remark = remark  + " , "+this.form.note;
                } 
            }
            //
            //console.log({remark})
            let userInfo = JSON.parse(window.sessionStorage.userInfo);
            let opTime = this.$formatDate(new Date(), "YYYY-MM-DD HH:mm:ss")
            let params = {
                dealType: '0',
                dealTypeDesc: '提交',
                procId: this.procId,
                procVersion: this.procVersion,
                remark: remark,
                stepId: this.stepId,
                taskId: this.taskId,
                taskName: this.taskName,
                opName: userInfo.loginNoName,
                opNo: userInfo.loginNoStr,
                opTime: opTime,
                params: paramsInfo,
                woNo: this.woNo
            }
            this.submitInfo("/bpm/api/submitTask",params);
        }
        ,
        handleAudit(){
            let paramsInfo={}
            if(this.taskName==="其他部门科室主任审批"){
                //部门领导审批
                paramsInfo = {"result":this.form.auditResult}
                if(!this.checkSelectUserX()){
                    return false
                }
                if(this.form.auditResult==='Y'){
                    // 选领导
                    paramsInfo.assigneeNo = this.assigneeNo
                    paramsInfo.assigneeName =this.assigneeName
                    this.form.publish ="";
                    this.completeTask(paramsInfo)
                    /*
                    this.getLeader("","2","1").then(val => {
                        if(val.length==1){
                            paramsInfo.assigneeNo = val[0].loginNoStr
                            paramsInfo.assigneeName =val[0].loginNameStr
                            this.form.publish ="";
                            this.completeTask(paramsInfo)
                        }else{
                            this.userList = val;
                            this.userDlgVisual = true;
                        }
                    });
                    */
                }else if(this.form.auditResult==="N"){
                    this.over()
                }
                
            }else if(this.taskName==="其他部门部门领导审批"){
                if(this.form.auditResult==="Y"){
                    paramsInfo = {"result":this.form.auditResult}
                    this.form.publish ="";
                    this.completeTask(paramsInfo)
                }else if(this.form.auditResult==="N"){
                     this.over()
                }
            }else if(this.taskName==="自有业务素材审核" || this.taskName==="传播管理素材审核" || this.taskName==="法务部素材风险审核"){
                this.form.auditResult = "";
                this.form.publish ="";
                this.getFinishFlag().then(val=>{
                    paramsInfo.finishFlag = val;
                    this.completeTask(paramsInfo)
                });
            }else if(this.taskName==="播控审核"){
                this.form.auditResult = "";
                this.form.publish ="";
                this.completeTask(paramsInfo)
            }else if(this.taskName==="科室主任审批"){
                if(this.form.auditResult==="Y"){
                    this.form.publish ="";
                    paramsInfo = {"result":this.form.auditResult}
                    this.completeTask(paramsInfo)
                }else{
                    this.over()
                }
            }else if(this.taskName==="主管副总终审"){
                if(this.form.auditResult==="Y"){
                    this.form.publish ="";
                    paramsInfo = {"result":this.form.auditResult}
                    this.completeTask(paramsInfo)
                }else{
                    this.over()
                }
            }else if(this.taskName==="录入是否执行"){
                this.form.auditResult = "";
                paramsInfo.publish = this.form.publish
                paramsInfo.woNo = this.woNo
                this.completeTask(paramsInfo)
            }else if(this.taskName==="市场部科室主任审批"){
                if(this.form.auditResult==="Y"){
                    this.form.publish ="";
                    paramsInfo = {"result":this.form.auditResult}
                    this.completeTask(paramsInfo)
                }else if(this.form.auditResult==="N"){
                    this.over()
                }
            }else if(this.taskName==="地市公司科室主任审批"){
                if(this.form.auditResult==="Y"){
                    paramsInfo = {"result":this.form.auditResult}
                    //
                    if(!this.checkSelectUserX()){
                        return false
                    }                    
                    paramsInfo.assigneeNo = this.assigneeNo
                    paramsInfo.assigneeName =this.assigneeName
                    this.form.publish ="";
                    this.completeTask(paramsInfo)
                    /*
                    this.getLeader("","2","1").then(val => {
                        if(val.length==1){
                            paramsInfo.assigneeNo = val[0].loginNoStr
                            paramsInfo.assigneeName =val[0].loginNameStr
                            this.form.publish ="";
                            this.completeTask(paramsInfo)
                        }else{
                            this.userList = val;
                            this.userDlgVisual = true;
                        }
                    });*/
                }else if(this.form.auditResult==="N"){
                    this.over()
                }
            }else if(this.taskName==="地市公司部门领导审批"){
                if(this.form.auditResult==="Y"){
                    paramsInfo = {"result":this.form.auditResult}
                    //
                    if(!this.checkSelectUserX()){
                        return false
                    }   
                    paramsInfo.assigneeNo = this.assigneeNo
                    paramsInfo.assigneeName =this.assigneeName
                    this.form.publish ="";
                    this.completeTask(paramsInfo)
                    /*
                    this.getLeader("","3","2").then(val => {
                        if(val.length==1){
                            paramsInfo.assigneeNo = val[0].loginNoStr
                            paramsInfo.assigneeName =val[0].loginNameStr
                            this.form.publish ="";
                            this.completeTask(paramsInfo)
                        }else{
                            if(this.form.auditResult==='Y'){
                                this.userList = val;
                                this.userDlgVisual = true;
                            }else if(this.form.auditResult==='N'){
                                this.over()
                            }
                        }
                    });*/
                }else if(this.form.auditResult==="N"){
                    this.over()
                }
            }else if(this.taskName==="公司主管副总审批"){
                if(this.form.auditResult==='Y'){
                    paramsInfo = {"result":this.form.auditResult}
                    this.form.publish ="";
                    this.completeTask(paramsInfo)
                }else if(this.form.auditResult==="N"){
                    this.over()
                }
            }
        },
        auditResultShow(){
            if(this.taskName==="其他部门科室主任审批"  || this.taskName==="其他部门部门领导审批"
            || this.taskName==="市场部科室主任审批"
            || this.taskName==="地市公司科室主任审批" || this.taskName==="地市公司部门领导审批" || this.taskName==="公司主管副总审批"
            || this.taskName==="科室主任审批" || this.taskName==="主管副总终审"){
                this.hasAuditResult="Y";
            }else if( this.taskName==="录入是否执行"){
                this.showPublish = "Y";
            }

            if(this.taskName==="其他部门科室主任审批"  || this.taskName==="地市公司科室主任审批" || this.taskName==="地市公司部门领导审批"){
                this.showUserTreeDlg = true;
            }

            //
        },
        getFinishFlag() {
            return new Promise((resolve, reject) => {
                this.$http({
                url: "/market/mbAudit/queryAllFinishFlag",
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                },
                    data: {"woNo":this.woNo},
                }).then((res) => {
                    resolve(res.data)
                });
            });
        },
        over(){
            let paramsInfo = {"result":this.form.auditResult}
            paramsInfo.assigneeNo = this.infoApply.createLogin
            paramsInfo.assigneeName = this.infoApply.createLoginName
            this.form.publish ="";
            paramsInfo.result = this.form.auditResult;

            this.$http({
            url: "/market/mbh/over",
            method: "post",
            headers: {
                "Content-Type": "application/json",
            },
                data: {"woNo":this.woNo},
            }).then((res) => {
                if(res.data.result==0){
                    this.completeTask(paramsInfo)
                }else{
                    this.$message({ showClose: true, message: '系统异常',type: 'error'});
                }
            });
        },
        checkSelectUser(){
            if(this.assigneeNo===''){
                this.$message.error('请选择下一步处理人');
                return
            }
            //
            this.userList.forEach((e)=>{
                if(e.loginNoStr === this.assigneeNo){
                    this.assigneeName = e.loginNameStr
                }
            });
            //
            this.userDlgVisual = false
            let paramsInfo = {"result":this.form.auditResult}
            paramsInfo.assigneeNo = this.assigneeNo;
            paramsInfo.assigneeName = this.assigneeNo;
            this.form.publish ="";
            this.completeTask(paramsInfo)
        },
        handleClose(){
            this.userList = []
            this.userDlgVisual = false;
        },
        loadItemData(){
            this.$http({
                url: "/market/mbh/list",
                method: "post",
                headers: {"Content-Type": "application/json"},
                data: {"itemId":this.itemId,"woNo":this.woNo}
            }).then((res) => {
                if(res.data.result==0){
                    this.infoApply = res.data.body[0]
                }else{
                    this.$message({ showClose: true, message: '系统异常',type: 'error'});
                }
            });
        },
   
        handleUserSelect(v){
            this.assigneeNo = v.leaderAuditNo
            this.assigneeName = v.leaderAuditName
            this.closeUserSelect()
        },
        checkSelectUserX(){
            if(this.assigneeNo===''){
                this.$message.error('请选择审批人');
                return false
            }
            if(this.assigneeName===''){
                this.$message.error('请选择审批人');
                return false
            }
            return true;
        },
        handleShowUserTreeDlg(){
            this.userTreeDlgVisual= true
        },
        closeUserSelect(){
            this.userTreeDlgVisual = false;
        }
    },
    mounted(){
        this.itemId = this.$route.query.itemId
        this.woNo = this.$route.query.woNo
        this.taskName = this.$route.query.taskName
        this.taskId = this.$route.query.taskId
        //
        this.stepId = this.$route.query.stepId
        this.procId = this.$route.query.procId
        this.procVersion = this.$route.query.procVersion
        //
        this.dialogCheck();
        this.auditResultShow();
        this.loadItemData();
    }
}
</script>

<style scoped lang="scss">
.tv-container{
  background-color: white;
  margin:15px 15px 15px 15px;
  padding:20px ;

}
.el-label{
  color: #606266;
  border-bottom:1px dotted #bfc1c5;
  padding-bottom:5px;
}
.el-line{
  margin: 0px 0px 20px 0px;
}
</style>
