<!--
@ 作者: 秦东
@ 时间: 2023-03-21 13:04:13
@ 备注: 执行节点选择栏
-->
<template>
    <el-drawer :append-to-body="true" :title="nodeTitle" :visible.sync="approverDrawer" direction="rtl" class="set_promoter" size="550px" :before-close="saveApprover"> 
        <div class="demo-drawer__content">
            <div class="drawer_content">
                <!--选择处理界限-->
                <div class="approver_content">
                    <el-radio-group v-model="approverConfig.settype" class="clear" @change="changeType">
                        <el-radio :label="1">指定成员</el-radio>
                        <el-radio :label="2">主管</el-radio>
                        <el-radio :label="3">行政岗位</el-radio>
                        <el-radio :label="4">发起人自选</el-radio>
                        <el-radio :label="5">发起人自己</el-radio>
                        <el-radio :label="7">连续多级主管</el-radio>
                        <el-radio :label="8" v-if="approverConfig.type == 3">指定审批节点自选</el-radio>
                    </el-radio-group>
                    <el-button type="primary" @click="addApprover" v-if="approverConfig.settype==1">添加/修改成员</el-button>
                    <p class="selected_list" v-if="approverConfig.settype==1">
                        <span v-for="(item,index) in approverConfig.nodeUserList" :key="index">{{item.name}}
                            <img src="@/assets/images/add-close1.png" @click="$func.removeEle(approverConfig.nodeUserList,item,'targetId')">
                        </span>
                        <a v-if="approverConfig.nodeUserList.length!=0" @click="approverConfig.nodeUserList=[]">清除</a>
                    </p>
                </div>
                <!--主管选项-->
                <div class="approver_manager" v-if="approverConfig.settype==2">
                    <p>
                        <span>发起人的：</span>
                        <select v-model="approverConfig.directorLevel">
                            <option v-for="item in directorMaxLevel" :value="item" :key="item">{{item==1?'直接':'第'+item+'级'}}主管</option>
                        </select>
                    </p>
                    <p class="tip">找不到主管时，由上级主管代审批</p>
                </div>

                <div class="approver_manager" v-if="approverConfig.settype==3">
                    <el-button type="primary" @click="addApproverPost">添加/修改岗位</el-button>
                    
                    <p class="selected_list" style="margin-top:30px">
                        <span v-for="(item,index) in approverConfig.nodeUserList" :key="index">{{item.name}}
                            <img src="@/assets/images/add-close1.png" @click="$func.removeEle(approverConfig.nodeUserList,item,'targetId')">
                        </span>
                        <a v-if="approverConfig.nodeUserList.length!=0&&approverConfig.selectRange!=1" @click="approverConfig.nodeUserList=[]">清除</a>
                    </p>
                </div>
                <!--发起人自选-->
                <div class="approver_self_select" v-show="approverConfig.settype==4">
                    <el-radio-group v-model="approverConfig.selectMode" style="width: 100%;">
                        <el-radio :label="1">选一个人</el-radio>
                        <el-radio :label="2">选多个人</el-radio>
                    </el-radio-group>
                    <h3>选择范围</h3>
                    <el-radio-group v-model="approverConfig.selectRange" style="width: 100%;" @change="changeRange">
                        <el-radio :label="1">全公司</el-radio>
                        <el-radio :label="4">本部门</el-radio>
                        <el-radio :label="2">指定成员</el-radio>
                        <el-radio :label="3">指定角色</el-radio>
                    </el-radio-group>
                    <el-button type="primary" @click="addApprover" v-if="approverConfig.selectRange==2">添加/修改成员</el-button>
                    <el-button type="primary" @click="addRoleApprover" v-if="approverConfig.selectRange==3">添加/修改角色</el-button>
                    <p class="selected_list" v-if="approverConfig.selectRange==2||approverConfig.selectRange==3">
                        <span v-for="(item,index) in approverConfig.nodeUserList" :key="index">{{item.name}}
                            <img src="@/assets/images/add-close1.png" @click="$func.removeEle(approverConfig.nodeUserList,item,'targetId')">
                        </span>
                        <a v-if="approverConfig.nodeUserList.length!=0&&approverConfig.selectRange!=1" @click="approverConfig.nodeUserList=[]">清除</a>
                    </p>
                </div>
                <!--发起人自选-->
                <div class="approver_self" v-if="approverConfig.settype==5">
                    <p>该审批节点设置“发起人自己”后，审批人默认为发起人</p>
                </div>
                <div class="approver_manager" v-if="approverConfig.settype==7">
                    <p>审批终点</p>
                    <p style="padding-bottom:20px">
                        <span>发起人的：</span>
                        <select v-model="approverConfig.examineEndDirectorLevel">
                            <option v-for="item in directorMaxLevel" :value="item" :key="item">{{item==1?'最高':'第'+item}}层级主管</option>
                        </select>
                    </p>
                </div>
                <!--指定审批节点自选-->
                <div class="approver_some" v-if="approverConfig.settype==8">
                    <p>可选节点列表</p>
                    <el-radio-group v-model="approverConfig.customNode" class="clear">
                        <el-radio v-for="item in nodeOptional" :label="item.nodeNumber" :key="item.nodeNumber" class="nodeGroupRadio">{{ item.nodeName }}（编号：{{ item.nodeNumber }}）</el-radio>
                        
                    </el-radio-group>
                </div>



                <div class="approver_some" v-if="(approverConfig.settype==1&&approverConfig.nodeUserList.length>1)||approverConfig.settype==2||approverConfig.settype==3||(approverConfig.settype==4&&approverConfig.selectMode==2)">
                    <p>多人审批时采用的审批方式</p>
                    <el-radio-group v-model="approverConfig.examineMode" class="clear">
                        <el-radio :label="1">依次审批</el-radio>
                        <br/>
                        <el-radio :label="2" v-if="approverConfig.settype!=2">会签(须所有审批人同意)</el-radio>
                        <br/>
                        <el-radio :label="3" v-if="approverConfig.settype!=2">非会签(有一位审批人同意即可)</el-radio>
                    </el-radio-group>
                </div>
                <div class="approver_some" v-if="approverConfig.settype==2||approverConfig.settype==7">
                    <p>审批人为空时</p>
                    <el-radio-group v-model="approverConfig.noHanderAction" class="clear">
                        <el-radio :label="1">自动审批通过/不允许发起</el-radio>
                        <br/>
                        <el-radio :label="2">转交给审核管理员</el-radio>
                    </el-radio-group>
                </div>

                <div class="approver_some"  v-if="approverConfig.type == 3">
                    <div class="demo-input-suffix">
                        <p>任务执行页面</p>
                        <el-input
                          placeholder="请输入URL(例：execut/page?key={val})  {val}为地址参数值占位符"
                          suffix-icon="el-icon-s-promotion"
                          v-model="approverConfig.executionaddress"
                          clearable>
                        </el-input>
                      </div>
                </div>

                <div class="approver_some">
                    <p>退回设置</p>
                    <el-radio-group v-model="approverConfig.sendBackNode" class="clear">
                        <el-radio label="beginnode" class="nodeGroupRadio">发起人</el-radio>
                        <el-radio v-for="item in nodeOptional" :label="item.nodeNumber" :key="item.nodeNumber" class="nodeGroupRadio">{{ item.nodeName }}（编号：{{ item.nodeNumber }}）</el-radio>
                        
                    </el-radio-group>
                </div>
                

            </div>

            <div class="demo-drawer__footer clear">
                <el-button type="primary" @click="saveApprover">确 定</el-button>
                <el-button @click="closeDrawer">取 消</el-button>
            </div>
            <employeesDialog
                :visible.sync="approverVisible"
                :data.sync="checkedList"
                @change="sureApprover"
            />
            <roleDialog
                :visible.sync="approverRoleVisible"
                :data.sync="checkedRoleList"
                @change="sureRoleApprover"
            />
            <positionDialog
                :visible.sync="appPosistonVisible"
                :data.sync="checkedPostList"
                @change="surePostApprover"
            />
        </div>
    </el-drawer>
</template>
<script>
import { judgeOptionalNode,getAllParentNode } from "@/api/systemaccredit/systemapi";
import { mapState, mapMutations } from 'vuex'
import employeesDialog from '@/customworkflow/dialog/employeesDialog'
import roleDialog from '@/customworkflow/dialog/roleDialog'
import positionDialog from '@/customworkflow/dialog/positionDialog'
export default {
    components: { employeesDialog,roleDialog,positionDialog },
    props: ['directorMaxLevel','nodeConfig'],
    data() {
        return {
            nodeTitle:'审批人设置',
            approverConfig: {},         //节点数据
            approverVisible: false,     //添加成员弹窗
            checkedList: [],            //成员数据
            approverRoleVisible: false, //添加角色弹窗
            checkedRoleList: [],        //角色数据
            nodeOptional:[],            //节点可操作数据
            appPosistonVisible: false, //添加岗位弹窗
            checkedPostList: [],        //角色数据
        }
    },
    created(){
        // console.log("directorMaxLevel-->",this.directorMaxLevel)
        // console.log("nodeConfig-->",this.nodeConfig)
    },
    computed: {
        ...mapState(['approverConfig1', 'approverDrawer']),
    },
    watch:{
        //执行节点数据
        approverConfig1(val) {
            // console.log("执行节点数据-->",val.value)
            this.approverConfig = val.value;
            switch(this.approverConfig.type){
                case 3:
                    this.nodeTitle='执行人设置';
                    break;
                default:
                    this.nodeTitle='审批人设置';
                    break;
            }
            this.getAllFatchNode(val.value.fromNode,this.nodeAllVerify);
        },
        nodeConfig:{
            handler(val){
                // console.log("节点变化---》",val)
                this.nodeAllVerify=[]
                this.getAllNodeCont(val)
            },
            deep: true,
            immediate: true
        }
    },
    methods:{
        ...mapMutations(['setApproverConfig', 'setApprover']),
        //保存选中的项目
        saveApprover() {
            this.approverConfig.error = !this.$func.setApproverStr(this.approverConfig)
            this.setApproverConfig({
                value: this.approverConfig,
                flag: true,
                id: this.approverConfig1.id
            })
            this.$emit("update:nodeConfig", this.approverConfig);
            this.closeDrawer()
        },
        //关闭执行抽屉
        closeDrawer() {
            this.setApprover(false)
        },
        //判断是否显示（指定审批节点自选）选项及可选节点
        async getAllNodeCont(val){
            const res = await judgeOptionalNode(val)
            // console.log("判断是否显示（指定审批节点自选）选项及可选节点",res)
            if(res.code == 0){
                this.nodeAllVerify = res.data.allcont
            }
        },
        //获取所有父级审批节点
        async getAllFatchNode(key,listcont){
            let sendData = {
                id:key,
                allcont:listcont
            }
            const res = await getAllParentNode(sendData);
            // console.log("获取所有父级审批节点",res)
            if(res.code == 0){
                this.nodeOptional = res.data.allcont
                if(res.data.total > 0){
                    this.nodeOptionalButton = true
                }else{
                    this.nodeOptionalButton = false
                }
            }
        },
        //单选控制
        changeType(val) {
            this.approverConfig.nodeUserList = [];
            this.approverConfig.examineMode = 1;
            this.approverConfig.noHanderAction = 2;
            if (val == 2) {
                this.approverConfig.directorLevel = 1;
            } else if (val == 4) {
                this.approverConfig.selectMode = 1;
                this.approverConfig.selectRange = 1;
            } else if (val == 7) {
                this.approverConfig.examineEndDirectorLevel = 1
            }
        },
        //添加/修改成员
        addApprover() {
            this.approverVisible = true;
            this.checkedList = this.approverConfig.nodeUserList
            // console.log("nodeUserList--------->",this.approverConfig.nodeUserList)
        },
        //添加、修改成员数值变化
        sureApprover(data) {
            this.approverConfig.nodeUserList = data;
            this.approverVisible = false;
        },
        //发起人自选选择范围
        changeRange() {
            this.approverConfig.nodeUserList = [];
        },
        //添加角色
        addRoleApprover() {
            this.approverRoleVisible = true;
            this.checkedRoleList = this.approverConfig.nodeUserList
        },
        //角色变动（回写数据，关闭弹窗）
        sureRoleApprover(data) {
            this.approverConfig.nodeUserList = data;
            this.approverRoleVisible = false;
        },
        //岗位变动（回写数据，关闭弹窗）
        surePostApprover(data) {
            this.approverConfig.nodeUserList = data;
            this.appPosistonVisible = false;
            // console.log("岗位变动（回写数据，关闭弹窗）--------->",this.approverConfig.nodeUserList)
        },
        //添加/修改岗位
        addApproverPost() {
            this.appPosistonVisible = true;
            this.checkedPostList = this.approverConfig.nodeUserList
            // console.log("nodeUserList--------->",this.approverConfig.nodeUserList)
        },
    }
}
</script>
<style lang="less">
.nodeGroupRadio{
    display: block;
    width: 100%;
    margin: 0px 0 0 10px;
}
.set_promoter {
    .approver_content {
        padding-bottom: 10px;
        border-bottom: 1px solid #f2f2f2;
    }

    .approver_self_select .el-button,
    .approver_content .el-button {
        margin-bottom: 20px;
    }

    .approver_content .el-radio,
    .approver_some .el-radio,
    .approver_self_select .el-radio {
        width: 27%;
        margin-bottom: 20px;
    }

    .approver_manager p {
        line-height: 32px;
    }

    .approver_manager select {
        width: 420px;
        height: 32px;
        background: rgba(255, 255, 255, 1);
        border-radius: 4px;
        border: 1px solid rgba(217, 217, 217, 1);
    }

    .approver_manager p.tip {
        margin: 10px 0 22px 0;
        font-size: 12px;
        line-height: 16px;
        color: #f8642d;
    }

    .approver_self {
        padding: 28px 20px;
    }

    .approver_self_select,
    .approver_manager,
    .approver_content,
    .approver_some {
        padding: 20px 20px 0;
    }

    .approver_manager p:first-of-type,
    .approver_some p {
        line-height: 19px;
        font-size: 14px;
        margin-bottom: 14px;
    }

    .approver_self_select h3 {
        margin: 5px 0 20px;
        font-size: 14px;
        font-weight: bold;
        line-height: 19px;
    }
}
</style>