<template>
    <HcCard action-ui="text-center" :title="detailData?.projectName">
        <template #extra>
            <HcNewSwitch :datas="tabTab" :keys="tabKey" :round="false" @change="tabChange" />
        </template>
        <template #search>
            <div class="hc-program-project-form-radio-group">
                <el-radio-group v-model="radioType" size="large">
                    <el-radio-button label="1">
                        市场部
                    </el-radio-button>
                    <el-radio-button label="2">
                        研发部
                    </el-radio-button>
                    <el-radio-button label="3">
                        实施部
                    </el-radio-button>
                    <el-radio-button label="4">
                        维护部
                    </el-radio-button>
                    <el-radio-button label="5">
                        管理支出
                    </el-radio-button>
                    <el-radio-button label="6">
                        外包劳务
                    </el-radio-button>
                </el-radio-group>
            </div>
        </template>
        <HcTable :is-index="false" :column="tableColumn" :datas="tableData" :row-style="tableRowStyle" has-children="hasChildren1" children="childrenList">
            <template #projectProcessValue="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <span class="text-hover">{{ row.projectProcessValue }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>{{ row.projectProcessValue }}</span>
            </template>

            <template #budgetTypeValue="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <span class="text-hover">{{ row.budgetTypeValue }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>{{ row.budgetTypeValue }}</span>
            </template>
            <template #taskDetailValue="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <span class="text-hover">{{ row.taskDetailValue }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>{{ row.taskDetailValue }}</span>
            </template>
            <template #planTaskType="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <el-select v-if="row.isEdit" v-model="row.planTaskType" clearable>
                                <el-option v-for="item in taskTypeList" :key="item.id" :label="item.dictName" :value="item.id" />
                            </el-select>
                            <span v-else>{{ row?.planTaskTypeValue }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>
                    <el-select v-if="row.isEdit" v-model="row.planTaskType" clearable>
                        <el-option v-for="item in taskTypeList" :key="item.id" :label="item.dictName" :value="item.id" />
                    </el-select>
                    <span v-else>{{ row?.planTaskTypeValue }}</span>
                </span>
            </template>
            <template #planTaskDesc="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <el-input v-if="row.isEdit" v-model="row.planTaskDesc" clearable />
                            <span v-else>{{ row.planTaskDesc }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>
                    <el-input v-if="row.isEdit" v-model="row.planTaskDesc" clearable />
                    <span v-else>{{ row.planTaskDesc }}</span>
                </span>
            </template>
            <template #planTarget="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <el-input v-if="row.isEdit" v-model="row.planTarget" clearable />
                            <span v-else>{{ row.planTarget }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>
                    <el-input v-if="row.isEdit" v-model="row.planTarget" clearable />
                    <span v-else>{{ row.planTarget }}</span>  
                </span>
            </template>
            <template #key7="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <HcDatePicker v-if="row.isEdit" :dates="[row.planStartTime, row.planEndTime]" clearable @change="betweenTimeUpdate($event, row)" />
                            <span v-else>
                                <span>{{ row.planStartTime ? row.planStartTime : '' }}</span>
                                <span v-if="row.planEndTime">~</span>
                                <span>{{ row.planEndTime ? row.planEndTime : '' }}</span>
                            </span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>
                    <HcDatePicker v-if="row.isEdit" :dates="[row.planStartTime, row.planEndTime]" clearable @change="betweenTimeUpdate($event, row)" />
                    <span v-else>
                        <span>{{ row.planStartTime ? row.planStartTime : '' }}</span>
                        <span v-if="row.planEndTime">~</span>
                        <span>{{ row.planEndTime ? row.planEndTime : '' }}</span>
                    </span>  
                </span>
            </template>
            <!-- <template #planDays="{row,index}">
                <el-input v-model="row.planDays" v-if="row.isEdit"  />
                <span v-else>{{row.planDays}}</span>
            </template> -->
            <template #planDays="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <el-input v-if="row.isEdit" v-model="row.planDays" disabled clearable />
                            <span v-else>{{ row.planDays }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>
                    <el-input v-if="row.isEdit" v-model="row.planDays" disabled clearable />
                    <span v-else>{{ row.planDays }}</span>   
                </span>
            </template>
            <template #postTypeValue="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <span class="text-hover">{{ row.postTypeValue }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>{{ row.postTypeValue }}</span>
            </template>
            <template #staffCount="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <span class="text-hover">{{ row.staffCount }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>{{ row.staffCount }}</span>
            </template>
            <template #budgetStaffCost="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <span class="text-hover">{{ row.budgetStaffCost }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>{{ row.budgetStaffCost }}</span>
            </template>
            <template #budgetTravelExpense="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <span class="text-hover">{{ row.budgetTravelExpense }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>{{ row.budgetTravelExpense }}</span>
            </template>
            <template #outsourceUnitPrice="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <span class="text-hover">{{ row.outsourceUnitPrice }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>{{ row.outsourceUnitPrice }}</span>
            </template>
            <template #outsourcePeopleCount="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <span class="text-hover">{{ row.outsourcePeopleCount }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>{{ row.outsourcePeopleCount }}</span>
            </template>
            <template #outsourceCountMoney="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <span class="text-hover">{{ row.outsourceCountMoney }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>{{ row.outsourceCountMoney }}</span>
            </template>
            <template #otherBudgetMoney="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <span class="text-hover">{{ row.otherBudgetMoney }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>{{ row.otherBudgetMoney }}</span>
            </template>
            <template #budgetCountMoney="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <span class="text-hover">{{ row.budgetCountMoney }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>{{ row.budgetCountMoney }}</span>
            </template>
            <template #budgetRemark="{ row }">
                <template v-if="row.taskFinishedStatus === 1">
                    <el-popover placement="top-start" title="" :width="200" :hide-after="0" :content="row?.outMoneyTips">
                        <template #reference>
                            <span class="text-hover">{{ row.budgetRemark }}</span>
                        </template>
                    </el-popover>
                </template>
                <span v-else>{{ row.budgetRemark }}</span>
            </template>
            
            <template #action="{ row }">
                <el-button v-if="row.isEdit" size="small" type="success" @click="updateMainPlanClick(row)">
                    保存
                </el-button>
                <el-button v-else size="small" type="primary" :disabled="row?.isShowEdit === 0 || row.parentId !== 0" @click="row.isEdit = true">
                    编辑
                </el-button>
                <el-button size="small" type="warning" :disabled="row.parentId !== 0" @click="relatedModalShow(row)">
                    关联回款
                </el-button>
                <el-button size="small" type="danger" :disabled="row?.isShowChildren == 0 || row.parentId !== 0" @click="subplanModalShow(row)">
                    分解子计划
                </el-button>
            </template>
        </HcTable>
        <template #action>
            <el-button size="large" type="info" hc-btn @click="goBackClick">
                <HcIcon name="arrow-go-back" />
                <span>取消并返回</span>
            </el-button>
            <el-button size="large" type="primary" hc-btn :loading="saveLoaing" @click="saveClick">
                <HcIcon name="check-double" />
                <span>提交保存</span>
            </el-button>
        </template>

        <!-- 分解子计划 -->
        <HcDialog
            bg-color="#ffffff" is-to-body is-table is-row-footer
            title="分解子计划" widths="80%"
            save-text="保存"
            :show="subplanModal"
            @close="subplanCloseClick"
            @save="updateChildPlanClick"
        >
            <template #extra>
                <el-button size="small" type="primary" @click="addplan">
                    <HcIcon name="add" />
                    新增
                </el-button>
            </template>
            <HcTable :is-index="false" :column="tableSubplanColumn" :datas="tableSubplanData">
                <template #planTaskType="{ row }">
                    <el-select v-model="row.planTaskType" clearable :disabled="row?.isShowDelete === 0" @change="changePlanTaskType($event, row)">
                        <el-option v-for="item in taskTypeList" :key="item.id" :label="item.dictName" :value="item.id" />
                    </el-select>
                </template>
                <template #planTaskDesc="{ row }">
                    <el-input v-model="row.planTaskDesc" clearable :disabled="row?.isShowDelete === 0" />
                </template>
                <template #planTarget="{ row }">
                    <el-input v-model="row.planTarget" clearable :disabled="row?.isShowDelete === 0" />
                </template>
                <template #key7="{ row }">
                    <HcDatePicker :dates="[row.planStartTime, row.planEndTime]" clearable :disabled="row?.isShowDelete === 0" @change="subbetweenTimeUpdate($event, row)" />
                </template>
                <template #planDays="{ row }">
                    <el-input v-model="row.planDays" disabled clearable />
                </template>
                <template #key9="{ row }">
                    <el-input v-model="row.key9" clearable :disabled="row?.isShowDelete === 0" />
                </template>
                <template #action="{ row, index }">
                    <el-button size="small" type="danger" :disabled="row?.isShowDelete === 0" @click="delSubplan(row, index)">
                        <HcIcon name="delete-bin" />
                        删除
                    </el-button>
                </template>
            </HcTable>
            <template #footer>
                <el-button type="primary" size="large" :loading="updateChildPlanLoading" @click="updateChildPlanClick">
                    <HcIcon name="save" />
                    保存
                </el-button>
            </template>
        </HcDialog>

        <!-- 关联回款里程碑 -->
        <HcDialog bg-color="#ffffff" is-to-body is-table :footer="false" :show="relatedModal" title="关联回款里程碑" widths="70%" @close="relatedCloseClick">
            <HcTable :column="tableRelatedColumn" :datas="tableRelatedData">
                <template #action="{ row }">
                    <el-button v-if="row.isRelation == 1" size="small" type="success" @click="relation(row, 0)">
                        取消关联
                    </el-button>
                    <el-button v-else size="small" type="primary" @click="relation(row, 1)">
                        关联
                    </el-button>
                </template>
            </HcTable>
        </HcDialog>
    </HcCard>
</template>

<script setup>
import { useRoute, useRouter } from 'vue-router'
import { onActivated, ref, watch } from 'vue'
import projectApi from '~api/program/project.js'
import contractApi from '~api/project/project-contract.js'
import { getArrValue, getObjValue } from 'js-fast-way'
import { getTaskType, getuserList } from '~api/other'
import { useAppStore } from '~src/store'
const useAppState = useAppStore()

//初始变量
const router = useRouter()
const useRoutes = useRoute()
const dataId = ref(useRoutes?.query?.id ?? '')
const dataType = ref(useRoutes?.query?.type ?? '')

//缓存页面被激活时
onActivated(() => {
    dataId.value = useRoutes?.query?.id ?? ''
    dataType.value = useRoutes?.query?.type ?? ''
    getUserDict()
    gettaskTypeList()
    if (dataType.value !== 'add') {
        getPlanByProjectId()
    
    }

 
})

const detailData = ref({})
const constructionData = ref([])
const buildData = ref([])
const supervisorUnitData = ref([])
//获取详情
const getPlanByProjectId = async ()=>{
    const { error, code, data } = await projectApi.getPlanByProjectId({ id: dataId.value })
    if (!error && code === 200) {
        console.log(getObjValue(data), '详情')
        detailData.value = getObjValue(data)
        constructionData.value = detailData.value?.constructUnit || []
        buildData.value = detailData.value?.buildUnit || []
        supervisorUnitData.value = detailData.value?.supervisorUnit || []
        tabKey.value = 'construction'
        radioType.value = 1
     
        // milestoneData.value=getArrValue(data)
       

    } else {
        // milestoneData.value=[]
    }
}
//获取所有员工
const userList = ref([])
//获取部门人员列表
const getUserDict = async ()=>{
    const { error, code, data } = await getuserList({ tenantId:useAppState.tenantId })
    if (!error && code === 200) {
        userList.value = getArrValue(data)

    } else {
        userList.value = []

    }
}
//获取所有任务类型
const taskTypeList = ref([])
//获取部门人员列表
const gettaskTypeList = async ()=>{
    const { error, code, data } = await getTaskType()
    if (!error && code === 200) {
        taskTypeList.value = getArrValue(data['records'])

    } else {
        taskTypeList.value = []

    }
}
//类型tab数据和相关处理
const tabKey = ref('')
const tabTab = ref([
    { key: 'build', name: '施工单位成本' },
    { key: 'supervision', name: '监理单位成本' },
    { key: 'construction', name: '建设单位成本' },
])
const originTableData = ref([ {}])

const tabChange = ({ key }) => {
    tabKey.value = key
    if (key === 'supervision') {
        tableData.value = supervisorUnitData.value && supervisorUnitData.value[radioType.value]?.length > 0 ? supervisorUnitData.value : originTableData.value
        detailData.value.supervisorUnit = tableData.value
    } else if (key === 'construction') {
        tableData.value = constructionData.value[radioType.value]?.length > 0 ? constructionData.value : originTableData.value
        detailData.value.constructUnit = tableData.value
      
      
    } else if (key === 'bulid') {
        tableData.value = buildData.value[radioType.value]?.length > 0 ? buildData.value : originTableData.value
        detailData.value.buildUnit = tableData.value
       
    }
}

const radioType = ref('')
//深度监听
watch(() => [
radioType.value,
tabKey.value,
], ([radioType]) => {
    if (tabKey.value === 'construction') {
        console.log(constructionData.value[radioType], 'constructionData.value')
        tableData.value = constructionData.value[radioType]
    } else if (tabKey.value === 'build') {
        console.log(buildData.value[radioType], 'buildData.value')
        tableData.value = buildData.value[radioType]
    } else if (tabKey.value === 'supervision') {
        console.log(supervisorUnitData.value[radioType], 'supervisorUnitData.value')
        tableData.value = supervisorUnitData.value[radioType]
    }
}, { deep: true })
//表格
const tableColumn = [
    { key: 'projectProcessValue', name: '项目环节', width: '160', align: 'center' },
    { key: 'budgetTypeValue', name: '预算类型', width: '160', align: 'center' },
    { key: 'taskDetailValue', name: '任务明细', width: '160', align: 'center' },
    { key: 'planTaskType', name: '任务类型', width: '160', align: 'center' },
    { key: 'planTaskDesc', name: '任务描述', minWidth: '200', align: 'center', isTooltip: true },
    { key: 'planTarget', name: '完成指标', minWidth: '200', align: 'center', isTooltip: true },
    { key: 'key7', name: '计划起止日期', width: '280', align: 'center' },
    { key: 'planDays', name: '预计工作量（小数/整数/天）', width: '160', align: 'center' },
    { key: 'postTypeValue', name: '投入岗位类型(日单价)', width: '160', align: 'center' },
    { key: 'staffCount', name: '投入人员数量', width: '160', align: 'center' },
    { key: 'budgetStaffCost', name: '预计人工成本（元）', width: '160', align: 'center' },
    { key: 'budgetTravelExpense', name: '预计差旅费（元）', width: '160', align: 'center' },
    { key: 'outsourceUnitPrice', name: '外包单价', width: '160', align: 'center' },
    { key: 'outsourcePeopleCount', name: '外包数量', width: '160', align: 'center' },
    { key: 'outsourceCountMoney', name: '外包金额', width: '160', align: 'center' },
    { key: 'otherBudgetMoney', name: '其他预算额', width: '160', align: 'center' },
    { key: 'budgetCountMoney', name: '总预算金额', width: '160', align: 'center' },
    { key: 'budgetRemark', name: '测算备注', width: '160', align: 'center' },
    { key: 'returnedValue', name: '关联回款里程碑', minWidth: '200', isTooltip: true },
    { key: 'action', name: '操作', width: '280', align: 'center', fixed: 'right' },
]
const tableData = ref([

])

//表格行样式
const tableRowStyle = ({ row }) => {
    if (row.taskFinishedStatus === 1) {
        return {
            'background-color': '#E99D42',
            '--el-fill-color-lighter': '#E99D42',
            '--el-table-row-hover-bg-color': '#E99D42',
        }
    } else if (row.taskFinishedStatus === 2 ) {
        return {
            'background-color': '#7e9559',
            '--el-fill-color-lighter': '#7e9559',
            '--el-table-row-hover-bg-color': '#7e9559',
        }
    }
}

//日期时间被选择

const betweenTimeUpdate = ({ arr }, item) => {
    item.planStartTime = arr[0]
    item.planEndTime = arr[1]
    item.betweenTime = arr
    // if(arr.length>0){
    //     item.planDays=null
    //      getWorkDays(item)
    // }else{
    //     item.planDays=null
    // }
}
//日期时间被选择

const subbetweenTimeUpdate = ({ arr }, item) => {
    console.log(item, 'item')
    item.planStartTime = arr[0]
    item.planEndTime = arr[1]
    item.subbetweenTime = arr
    if (arr.length > 0) {
        item.planDays = null
         getWorkDays(item)
    } else {
        item.planDays = null
    }
}

const subplanModal = ref(false)
const subPlanItem = ref({})
const subplanModalShow = (row) => {
    console.log(row, 'row')
    subPlanItem.value = row
    subplanModal.value = true
    tableSubplanData.value = row?.childrenList || []

}
//表格
const tableSubplanColumn = [
    { key: 'projectProcessValue', name: '项目环节', width: '160', align: 'center' },
    { key: 'budgetTypeValue', name: '预算类型', width: '160', align: 'center' },
    { key: 'taskDetailValue', name: '任务明细', width: '160', align: 'center' },
    { key: 'planTaskType', name: '任务类型', width: '160', align: 'center' },
    { key: 'planTaskDesc', name: '任务描述', minWidth: '200', align: 'center', isTooltip: true },
    { key: 'planTarget', name: '完成指标', minWidth: '200', align: 'center', isTooltip: true },
    { key: 'key7', name: '计划起止日期', width: '280', align: 'center' },
    { key: 'planDays', name: '预计工作量（小数/整数/天）', width: '160', align: 'center' },
    { key: 'action', name: '操作', width: '180', align: 'center', fixed: 'right' },
]
const tableSubplanData = ref([

])

const subplanCloseClick = () => {
    const arr = tableSubplanData.value
    for (let i = arr.length - 1; i >= 0; i--) {
        if (!arr[i].planTaskType || !arr[i].planTaskDesc || !arr[i].planTarget || !arr[i].planStartTime || !arr[i].planEndTime || !arr[i].planDays) {
            arr.splice(i, 1)
        }
    }
    subplanModal.value = false

    
}

const addplan = ()=>{
    tableSubplanData.value.push({
        projectProcessValue:subPlanItem.value?.projectProcessValue,
        budgetTypeValue:subPlanItem.value?.budgetTypeValue,
        taskDetailValue:subPlanItem.value?.taskDetailValue,
        planTaskType:subPlanItem.value?.planTaskType,
        isEdit:true,


    })
}
const changePlanTaskType = (val)=>{
    tableSubplanData.value.forEach((ele)=>{
        ele.planTaskType = val
        getWorkDays(ele)
    })
}
const delSubplan = (row, index)=>{
    tableSubplanData.value.splice(index, 1)
}

const relatedModal = ref(false)
const rePlanid = ref('')
const relatedModalShow = (row) => {
    relatedModal.value = true
    rePlanid.value = row.id
    getListByProjectId(row.projectId)
   
}
//保存获取工作时长
const getWorkDays = async (row)=>{
        if (row?.planStartTime && row?.planEndTime) {
            row.planDays = 0
            const { error, code, data } = await projectApi.getWorkDays( {
                startDate:row.planStartTime,
                endDate:row.planEndTime,
                planTaskType:row.planTaskType,
            
            })
            if (!error && code === 200) {
                    row.planDays = data
            }
        }
    
  
 
}

//编辑主计划
const updateMainPlanClick = async (row)=>{
    const { planTaskType } = row
    taskTypeList.value.forEach((ele)=>{
        if (ele.id === planTaskType) {
            row.planTaskTypeValue = ele.dictName
        }
    })
    const { error, code, data, msg } = await projectApi.updateMainPlan( 
        row,
    )
    if (!error && code === 200) {
        
       if (data) {
            row.taskFinishedStatus = data.taskFinishedStatus
            row.outMoneyTips = data.outMoneyTips
            row.planDays = data.planDays
            row.isShowEdit = data.isShowEdit
            row.isShowChildren = data.isShowChildren
            row.planTaskType = data.planTaskType
            row.planTaskTypeValue = data.planTaskTypeValue
            row.isShowChildren = data.isShowChildren
            row.planStartTime = data.planStartTime
            row.planEndTime = data.planEndTime
            row.isEdit = false
       }
       
    }

}
const updateChildPlanLoading = ref(false)
//编辑子计划传入父级
const updateChildPlanClick = async ()=>{
    subPlanItem.value.childrenList = tableSubplanData.value
    updateChildPlanLoading.value = true
    const { error, code, data, msg } = await projectApi.updateChildrenPlan( 
        subPlanItem.value,
    )
    updateChildPlanLoading.value = false
    if (!error && code === 200) {
        window.$message.success(msg)
        tableData.value.forEach((ele)=>{
            if (ele.id === subPlanItem.value.id) {
                ele.childrenList = tableSubplanData.value
                ele.planTarget = data.planTarget
                ele.planTaskDesc = data.planTaskDesc
                ele.taskFinishedStatus = data.taskFinishedStatus
                ele.planDays = data.planDays
                ele.outMoneyTips = data.outMoneyTips
                ele.isShowEdit = data.isShowEdit
                ele.isShowChildren = data.isShowChildren
                ele.planTaskType = data.planTaskType
                ele.planTaskTypeValue = data.planTaskTypeValue
                ele.planStartTime = data.planStartTime
                ele.planEndTime = data.planEndTime
                //子计划不显示保存按钮，且其他按钮禁用
                ele.childrenList.forEach((item)=>{
                    item.isEdit = false
                })
                
            }
        })
        subplanModal.value = false
    }
   

}
//关联回款
const relation = async (row, type)=>{
    const { error, code, data, msg } = await projectApi.relationPlanAndReturned( {
        planId:rePlanid.value,
        returnedId:row.id,
        type,
        projectId:row.projectId,
    })
    if (!error && code === 200) {
       window.$message.success(msg)
       getListByProjectId(row.projectId).then()
    } 
}
//表格
const tableRelatedColumn = [
    { key: 'returnedCondition', name: '回款条件', minWidth: '260' },
    { key: 'shouldReturnedTime', name: '应收回款时间', width: '160', align: 'center' },
    { key: 'shouldReturnedMoney', name: '应收回款金额(元)', width: '160', align: 'center' },
    { key: 'reminderUserName', name: '催款执行人', width: '160', align: 'center' },
    { key: 'action', name: '操作', width: '130', align: 'center' },
]
const tableRelatedData = ref([
    { id: 1, key1: 'xx', key2: 'xx', key3: 'xx', key4: 'xx', key5: 'xx', key6: 'xx', key7: 'xx', key8: 'xx', key9: 'xx', key10: 'xx' },
    { id: 2, key1: 'xx', key2: 'xx', key3: 'xx', key4: 'xx', key5: 'xx', key6: 'xx', key7: 'xx', key8: 'xx', key9: 'xx', key10: 'xx' },
    { id: 3, key1: 'xx', key2: 'xx', key3: 'xx', key4: 'xx', key5: 'xx', key6: 'xx', key7: 'xx', key8: 'xx', key9: 'xx', key10: 'xx' },
    { id: 4, key1: 'xx', key2: 'xx', key3: 'xx', key4: 'xx', key5: 'xx', key6: 'xx', key7: 'xx', key8: 'xx', key9: 'xx', key10: 'xx' },
    { id: 5, key1: 'xx', key2: 'xx', key3: 'xx', key4: 'xx', key5: 'xx', key6: 'xx', key7: 'xx', key8: 'xx', key9: 'xx', key10: 'xx' },
    { id: 6, key1: 'xx', key2: 'xx', key3: 'xx', key4: 'xx', key5: 'xx', key6: 'xx', key7: 'xx', key8: 'xx', key9: 'xx', key10: 'xx' },
])
//合同里程碑

const getListByProjectId = async (projectId, planId)=>{
    const { error, code, data } = await contractApi.getListByProjectId({ projectId, planId:rePlanid.value })
    if (!error && code === 200) {
        tableRelatedData.value = getArrValue(data)
       

    } else {
        tableRelatedData.value = []
    }
}

const relatedCloseClick = () => {
    relatedModal.value = false
}
//修改合同
const updatePlan = async (obj)=>{
    console.log(obj, '编辑')
    saveLoaing.value = true
    const { error, code, data, msg } = await projectApi.updatePlan( obj)
    saveLoaing.value = false
    if (!error && code === 200) {
       window.$message.success(msg)
       getPlanByProjectId()
    } 

}
const isEmptyObj = (obj)=> {
    let arr = Object.keys(obj)
    return (arr.length == 0)

}    
//批量保存
const saveLoaing = ref(false)
const saveClick = () => {
   console.log(tableData.value, 'tableData.value')
   console.log( detailData.value, ' detailData.value')
       //取消空对象提交
       if (detailData.value?.supervisorUnit?.length > 0) {
        let suisnullObj = isEmptyObj(detailData.value?.supervisorUnit[0]) 
        if (suisnullObj === true) {
            detailData.value.supervisorUnit = null
        }
    } 
    if (detailData.value?.constructUnit?.length > 0) {
        let coisnullObj = isEmptyObj(detailData.value?.constructUnit[0]) 
        if (coisnullObj) {
            detailData.value.constructUnit = null
        }
    }
    if (detailData.value?.buildUnit?.length > 0) {
        let buisnullObj = isEmptyObj(detailData.value?.buildUnit[0]) 
        if (buisnullObj) {
            detailData.value.buildUnit = null
        }
    }
   updatePlan(detailData.value)
}
//取消并返回
const goBackClick = () => {
    router.back()
}
</script>

<style lang="scss">
.hc-program-project-form-radio-group {
    position: relative;
    width: 100%;
    .el-radio-group, .el-radio-group .el-radio-button .el-radio-button__inner {
        width: 100%;
    }
    .el-radio-group .el-radio-button {
        flex: 1;
    }
}
</style>
