/**
 * 部门管理的额外需求
 * - 研报小程序5.0:ficc研究员配置;
 * - 研报后台5.9：部门管理优化
 */
import { departInterence } from '@/api/api.js';
import http from '@/api/http.js';
import AddUserDialog from '../components/addUserDialog.vue';
import{checkPassWord} from '@/utils/commonOptions';

export default {
    components:{AddUserDialog},
    data() {
        const validatePass = (rule,value,callback)=>{
            if(value===''){
                callback(new Error('请输入新密码'))
            }
            if(this.resetForm.check!==''){
                this.$refs.resetForm.validateField('check')
            }
            if(!checkPassWord(value)){
                callback(new Error('密码要求8位及以上，包含数字、大写字母、小写字母、特殊字符中的三个类型'))
            }else{
                callback()
            }
        }
        const validateCheck = (rule,value,callback)=>{
            if(value===''){
                callback(new Error('请输入确认密码'))
            }else if(value!==this.resetForm.password){
                callback(new Error('两次输入的密码不一致'))
            }else{
                callback()
            }
        }
        return {
            isExtraUserFormItemShow: false, //是否显示额外的用户表单项
            form_directProp: {
                /* value: 'research_group_id',
                label: 'research_group_name',
                children: 'children', */
                value:'tag_id',
                label:'tag_name',
                children:'tags',
                multiple: true
            }, //研究方向选择配置
            uploadloading: false,
            researchGroup:[],

            isResetPasswordShow:false,
            isMoveDepartShow:false,
            resetRules:{
                password:[{validator: validatePass,trigger: 'blur'}],
                check:[{validator: validateCheck,trigger: 'blur'}]
            },
            resetForm:{
                password:'',
                check:''
            },
            resetDepart:'',
            modifyAdminId:0,
            modifyAdminName:'',


            areaCode:[]
        }
    },
    methods: {
        //上传图片
        fileSelected() {
            this.uploadloading = true;
            let hostfile = document.getElementById('file').files[0];
            console.log(hostfile)
            let form = new FormData();
            form.append('file', hostfile);
            console.log(form)
            setTimeout(() => {
                this.uploadloading = false
            }, 1000)
        },
        //模拟上传图片input被点击
        clickinput() {
            $("#file").click();
        },
        /* 获取研究方向分组列表 */
		getResearchGroup(){
			/* departInterence.getResearchGroupList().then(res=>{
                if(res.Ret===200){
                    this.researchGroup = res.Data
                }
            }) */
            departInterence.getTagTree().then(res=>{
                if(res.Ret===200){
                    this.researchGroup = res.Data
                }
            })
		},
        //转换研究员研究方向分组数据,用于编辑的回显
        changeResearchGroupIdsToDirect(ResearchGroupIds,keyWord){
            let direct = []
            ResearchGroupIds.forEach(item=>{
                direct.push(this.getResearchGroupByKeyword(item,keyWord))
            })
            return direct
        },
        getResearchGroupByKeyword(researchGroupId, keyWord = "research_group_id") {
            const getResearchGroupOption = (list, value) => {
              for (let i in list) {
                  //排除掉一级
                if (list[i].tag_id == value&&!list[i].classify_name) {
                  return [list[i]];
                }
                if (list[i].tags) {
                  let node = getResearchGroupOption(list[i].tags, value);
                  if (node !== undefined) {
                    return node.concat(list[i]);
                  }
                }
              }
            };
            const answer = getResearchGroupOption(this.researchGroup, researchGroupId)||[];
            return answer.map((i) => i[keyWord]).reverse();
          },
        //节点是否可拖拽:所有节点均可拖拽
        checkAllowDrag(){return true},
        //节点是否可放入
        checkAllowDrop(draggingNode,dropNode,type){
            if(type==='inner'){
                /**
                 * 情况1 
                 * draggingNode.level==2 dropNode.level==1
                 * draggingNode.TopId == dropNode.DepartmentId
                 * 情况2
                 * draggingNode.level==3 dropNode.level==2
                 * draggingNode.GroupId == dropNode.DepartmentId
                 */
                const case_1 = draggingNode.level===2&&dropNode.level===1
                                && draggingNode.data.TopId === dropNode.data.DepartmentId
                const case_2 = draggingNode.level===3&&dropNode.level===2
                                && draggingNode.data.GroupId == dropNode.data.DepartmentId
                return case_1||case_2
            }else if(draggingNode.level===dropNode.level){
                /**
                 * 情况1
                 * draggingNode.level===dropNode.level===1
                 * 情况2
                 * draggingNode.level===dropNode.level
                 * draggingNode.TopId === dropNode.TopId||draggingNode.GroupId===dropNode.GroupId
                 */
                const case_1 = dropNode.level===1
                const case_2 = draggingNode.data.TopId&&dropNode.data.TopId&&draggingNode.data.TopId === dropNode.data.TopId
                        ||draggingNode.data.GroupId&&dropNode.data.GroupId&&draggingNode.data.GroupId===dropNode.data.GroupId
                return case_1||case_2
            }
            return false
        },
        //放入节点
        handleDropOver(draggingNode,dropNode,type,e){
            //获取当前移动节点的层级和id,parentId
            //获取同层级的ids
            const treeData = this.$refs.departTree.data
            const node = this.$refs.departTree.getNode(draggingNode.data.parentKey)||{data:{Child:[]}}
            let params = {
                DepartmentIds:[],
                GroupIds:[],
                TeamIds:[]
            }
            if(draggingNode.level===1){
                params.DepartmentIds = treeData.map(i=>i.DepartmentId)
            }else{
                const ids = node.data.Child.map(i=>i.DepartmentId)
                params.GroupIds = draggingNode.level===2?ids:[]
                params.TeamIds =draggingNode.level===3?ids:[]
            }
            departInterence.sortDepart(params).then(res=>{
                if(res.Ret!==200) return 
                this.$message.success('移动排序成功')
                this.getDepartArr()
            })
        },
        //重置密码
        resetPassWord(data,type="show"){
            if(type==='show'){
                this.$refs.resetForm&&this.$refs.resetForm.clearValidate()
                this.modifyAdminId = data.AdminId
                this.modifyAdminName=data.AdminName
                this.isResetPasswordShow = true
            }else{
                this.$refs.resetForm.validate((valid)=>{
                    if(valid){
                        departInterence.resetSysuserPass({
                            AdminId:Number(this.modifyAdminId),
                            Password:new http.Base64().encode(this.resetForm.password),
                            RePassword:new http.Base64().encode(this.resetForm.password)
                        }).then(res=>{
                            if(res.Ret!==200) return 
                            this.$message.success('重置密码成功')
                            this.resetForm = {
                                password:'',
                                check:''
                            }
                            this.isResetPasswordShow = false
                            this.getTableUser();
                        })
                        
                    }
                })
                
            }
        },
        //移动分组
        moveDepart(data,type="show"){
            if(type==='show'){
                let departArr = [];
                departArr.push(data.DepartmentId)
                data.GroupId?departArr.push(data.GroupId):''
                data.TeamId ?departArr.push(data.TeamId):''
                this.resetDepart = departArr
                this.modifyAdminId = data.AdminId
                this.modifyAdminName=data.AdminName
                this.isMoveDepartShow = true
            }else{
                //选择分组接口
                departInterence.moveSysuser({
                    AdminId:Number(this.modifyAdminId),
                    DepartmentId:Number(this.resetDepart[0]),
                    GroupId:Number(this.resetDepart[1]?this.resetDepart[1]:0),
                    TeamId:Number(this.resetDepart[2]?this.resetDepart[2]:0),
                }).then(res=>{
                    if(res.Ret!==200) return
                    this.$message.success('移动分组成功')
                    this.isMoveDepartShow = false
                    this.getTableUser();
                })
            }
        },
        getAreaCode(){
            departInterence.getPhoneAreaCode().then(res=>{
                if(res.Ret!==200) return 
                this.areaCode = res.Data||[]
            })
        }
    }

}