<template>
    <div :class="ui" class="hc-report-tasks-user-box">
        <div class="tag-user-list" @click="userShowModal">
            <template v-for="(item, index) in userData" :key="index">
                <el-tag>{{ item.userName }}</el-tag>
                <hc-icon v-if="(userData.length - 1) > index" name="arrow-right" ui="arrow-icon-tag" />
            </template>
            <div v-if="userData.length <= 0" class="tasks-placeholder">
                <span v-if="!isChangePopele"> 点击这里选择任务人</span>
                <span v-else> 点击这里选择整改人</span>
            </div>
        </div>
        <!-- 选择任务人 -->
        <HcUserModal v-model="isUserModalShow" :data="userData" :datas="dataInfo" :fixed-flow-link-type-val="fixedFlowLinkTypeVal" @finish="fixedUserFinish" />
    </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { arrToKey, deepClone, getObjValue } from 'js-fast-way'
import HcUserModal from './user-modal.vue'
import { checkCustomFlowUserIsEVisaPermissions, checkCustomFlowUserIsEVisaPermissions3, checkCustomFlowUserIsEVisaPermissionsquery } from '~api/other'

//参数
const props = defineProps({
    ui: {
        type: String,
        default: '',
    },
    data: {
        type: Object,
        default: () => ({}),
    },
  
    //选中的用户数组
    users: {
        type: Array,
        default: () => ([]),
    },
    projectId: {
        type: [String, Number],
        default: '',
    },
    contractId: {
        type: [String, Number],
        default: '',
    },
    type: { //first,log,wbs
        type: [String, Number],
        default: '',
    },
    typeData: {
        type: [String, Number, Array, Object],
        default: '',
    },
    classifyType: {
        type: [String, Number],
        default: '',
    },
    tableOwner: {
        type: [String, Number],
        default: '',
    },
    nodeId: {
        type: [String, Number],
        default: '', //选中节点nodeid
    },
    infoIds:{
        type: [String, Number],
        default: '', //上报任务ID
    },
    disabled:{
        type:Boolean,
        default:false,
    },
    isChangePopele:{
        type:Boolean,
        default:false, //显示整改人还是任务人
    },
    fixedFlowLinkTypeVal: {
        type: [String, Number],
        default: '', //流程审批1，平行审批2
    },
})




//事件
const emit = defineEmits(['change'])
const projectId = ref(props.projectId)
const contractId = ref(props.contractId)
const isTypes = ref(props.type)
const typeDatas = ref(props.typeData)
const classifyType = ref(props.classifyType)
const tableOwner = ref(props.tableOwner)
const nodeId = ref(props.nodeId)
const infoIds = ref(props.infoIds)
const disabled = ref(props.disabled)
const isChangePopele = ref(props.isChangePopele)
const fixedFlowLinkTypeVal = ref(props.fixedFlowLinkTypeVal)
//监听
watch(() => [
    props.users,
    props.projectId,
    props.contractId,
    props.type,
    props.typeData,
    props.classifyType,
    props.tableOwner,
    props.nodeId,
    props.infoIds,
    props.disabled,
    props.isChangePopele,
    props.fixedFlowLinkTypeVal,
], ([users, pid, cid, type, data, cla, tab, noid, infoid, disa, isChan, typeVal]) => {
    userData.value = users
    projectId.value = pid
    contractId.value = cid
    isTypes.value = type
    typeDatas.value = data

    classifyType.value = cla
    tableOwner.value = tab
    nodeId.value = noid
    infoIds.value = infoid
    disabled.value = disa
    isChangePopele.value = isChan
    fixedFlowLinkTypeVal.value = typeVal
})
//监听基础数据
const dataInfo = ref(props.data)
watch(() => props.data, (data) => {
    dataInfo.value = getObjValue(data)
}, { deep: true, immediate: true })

//展开弹窗
const isUserModalShow = ref(false)
const userShowModal = () => {
    isUserModalShow.value = true
}

//选择完成
const userData = ref(props.users)
const fixedUserFinish = (data) => {
    const res = deepClone(data)
    userData.value = res
    const userIds = arrToKey(res, 'userId', ',')
    let userIdsArr = userIds.split(',')
    sureSignUserClick(userIdsArr, userData.value )
}

//确认选择
const sureSignUserLoading = ref(false)
const sureSignUserClick = (userIds, userData) => {
    let type = isTypes.value, flowJson = {}
    

    if (userIds.length > 0) {
        sureSignUserLoading.value = true
        //判断类型
        if (type === 'first') {
            flowJson['firstId'] = typeDatas.value
        } else if (type === 'log') {
            // flowJson['theLogPrimaryKeyId'] = typeDatas.value
            flowJson['theLogPrimaryKeyId'] = nodeId.value
        } else if (type === 'wbs') {
            flowJson['privatePKeyId'] = typeDatas.value
        } else if (type === 'query') {
            flowJson['privatePKeyId'] = typeDatas.value
        }
  
        //效验人员
        if (type === 'wbs') {
            getCheckCustomFlowUserIsEVisaPermissions(flowJson, userIds)
        } else if (type === 'first' || type === 'log' ) {
            getCheckCustomFlowUserIsEVisaPermissions3(flowJson, userIds)
        } else if (type === 'query') {
            getCheckCustomFlowUserIsEVisaPermissionsquery(flowJson, userIds)
        } else {
        
            sureSignUserLoading.value = false
            emit('change', userData)
        }
    } else {
        window.$message?.warning('请先选择任务人员，或点击取消')
    }
}
//检查所选的流程环节处理人是否具有审批权限（三大填报页、日志列表的批量上报、首件列表的批量上报）
const getCheckCustomFlowUserIsEVisaPermissions = async (flowJson, newUserId) => {
    console.log( dataInfo.value, ' dataInfo.value')
    
    const { error, code, data, msg, success } = await checkCustomFlowUserIsEVisaPermissions({
        projectId: dataInfo.value.projectId,
        contractId:  dataInfo.value.contractId,
        customFlowUserList: newUserId,
        ...flowJson,
        classifyType:classifyType.value,
        tableOwner:tableOwner.value,
        nodeId:nodeId.value,

    })
    //处理数据
    sureSignUserLoading.value = false
    
    if (!error && code === 200 && data === true) {
       
        emit('change', userData.value)
        isUserModalShow.value = false
    } else {
        // window.$message.error(msg)
        userData.value = []
        emit('change', [])
    }
}
//资料查询页面
const getCheckCustomFlowUserIsEVisaPermissionsquery = async (flowJson, newUserId) => {
    const { error, code, data, msg } = await checkCustomFlowUserIsEVisaPermissionsquery({
        projectId: projectId.value,
        contractId: contractId.value,
        customFlowUserList: newUserId,
        ...flowJson,
        nodeId:nodeId.value,
        classifyType:classifyType.value,
        tableOwner:tableOwner.value,
        infoIds:infoIds.value,
    })
    //处理数据
    sureSignUserLoading.value = false
    if (!error && code === 200 && data === true) {
        isUserModalShow.value = false
        emit('change', userData.value)
    } else {
        // window.$message.error(msg)
        userData.value = []
        emit('change', [])
    }
}
//日志和首件页面
const getCheckCustomFlowUserIsEVisaPermissions3 = async (flowJson, newUserId) => {
    const { error, code, data, msg } = await checkCustomFlowUserIsEVisaPermissions3({
        projectId: projectId.value,
        contractId: contractId.value,
        customFlowUserList: newUserId,
        ...flowJson,
        // nodeId:nodeId.value,
        classifyType:classifyType.value,
        tableOwner:tableOwner.value,
        infoIds:infoIds.value,
    })
    //处理数据
    sureSignUserLoading.value = false
    if (!error && code === 200 && data === true) {
        isUserModalShow.value = false
        emit('change', userData.value)
    } else {
        // window.$message.error(msg)
        userData.value = []
         emit('change', [])
    }
}
</script>

<style lang="scss">
@import './index.scss';
</style>
