<template>
    <global-popup v-model="showPopup" mode="right" :width="580" background="#F7F8FA" title="选择责任单位" :round="32">
        <view class="list">
            <view class="item" v-for="item in arrangeWorkExtendList" :key="item.id" @tap="handleClick(item)">
                {{ getCompanyName(item) }}
            </view>
        </view>
    </global-popup>
</template>

<script setup>
import { ref } from 'vue'
import useAssignWorkStore from 'mocp/store/assign-work'
import { storeToRefs } from 'pinia'

const assignWorkStore = useAssignWorkStore()
const { arrangeWorkExtendList, details, selectList } = storeToRefs(assignWorkStore)
const showPopup = ref(false)
const open = () => {
    showPopup.value = true
}
//切换责任公司
const handleClick = (row) => {
    showPopup.value = false
    assignWorkStore.setState('arrangeWorkExtend', row)
    uni.$mocpMessage.success('已切换成' + getCompanyName(row))
}
//递归查找责任单位名称
const findRelatedLabelOfEnetOrg = (items, id) => {
    let res = ''
    for (const item of items) {
        if (item.nodeId == id) {
            res = item.fullName
            break
        }
        if (Array.isArray(item.nodeList) && item.nodeList.length > 0) {
            res = findRelatedLabelOfEnetOrg(item.nodeList, id)
            if (res) {
                break
            }
        }
    }
    return res
}
//去除前缀
const removeText = (str) => {
    return str.replace('海航航空技术有限公司-', '')
}
const getCompanyName = (v) => {
    return removeText(
        details.value.tag && details.value.tag * 1
            ? findRelatedLabelOfEnetOrg(selectList.value.EnetOrgTree, v.company)
            : findRelatedLabelOfEnetOrg(selectList.value.EnetOrgTree, v.company.slice(0, -1))
    )
}
defineExpose({
    open
})
</script>
<style lang="scss" scoped>
.list {
    padding: 18rpx 32rpx;
    .item {
        color: $mocp-text-4;
        font-size: 30rpx;
        background: #fff;
        margin-bottom: 24rpx;
        padding: 16rpx 24rpx;
    }
}
</style>
