<template>
    <div class="hc-layout-box">
        <div class="hc-layout-left-box" :style="'width:' + leftWidth + 'px;'">
            <div class="hc-project-box">
                <div class="hc-project-icon-box">
                    <HcIcon name="stack"/>
                </div>
                <div class="ml-2 project-name-box">
                    <span class="text-xl text-cut project-alias">{{projectInfo['projectAlias']}}</span>
                    <div class="text-xs text-cut project-name">{{projectInfo['name']}}</div>
                </div>
            </div>
            <div class="hc-tree-box">
                <el-scrollbar>
                    <WbsTree :autoExpandKeys="treeAutoExpandKeys" :projectId="projectId" :contractId="contractId" @nodeTap="wbsElTreeClick" :classifyType="classifyType"/>
                </el-scrollbar>
            </div>
            <!--左右拖动-->
            <div class="horizontal-drag-line" @mousedown="onmousedown"/>
        </div>
        <div class="hc-chart-content-box" v-loading="isLoading">
            <BarChart ref="barChartRef" :datas="barChartDatas"/>
        </div>
    </div>
</template>

<script setup>
import {ref, watch} from 'vue'
import {useAppStore} from "~src/store";
import WbsTree from "../data-fill/components/WbsTree.vue"
import {getStoreData, setStoreData} from '~src/utils/storage'
import BarChart from "./components/echarts/BarChart.vue"
import DataApi from "~api/schedule/data"
import {getArrValue} from "vue-utils-plus"

//变量
const useAppState = useAppStore()
const projectId = ref(useAppState.getProjectId);
const contractId = ref(useAppState.getContractId);
const projectInfo = ref(useAppState.getProjectInfo);
const isCollapse = ref(useAppState.getCollapse)
const contractInfo = ref(useAppState.getContractInfo);
const {contractType} = contractInfo.value;
const classifyType = ref(contractType === 2 ? '2' : '1')
//监听
watch(() => [
    useAppState.getCollapse
], ([Collapse]) => {
    isCollapse.value = Collapse
    barChartRef.value?.onResize()
})

//自动展开缓存
const treeAutoExpandKeys = ref(getStoreData('scheduleTreeKeys') || [])
const barChartRef = ref(null)

//树被点击
const treeItemInfo = ref({})
const wbsElTreeClick = ({data, keys}) => {
    treeItemInfo.value = data
    //缓存自动展开
    treeAutoExpandKeys.value = keys
    setStoreData('scheduleTreeKeys',keys)
    //获取数据
    queryNeiWaiYeProgress()
}

//获取数据
const isLoading = ref(false)
const barChartDatas = ref([])
const queryNeiWaiYeProgress = async () => {
    //获取数据
    isLoading.value = true
    const { primaryKeyId, contractIdRelation } = treeItemInfo.value
    const { error, code, data } = await DataApi.queryNeiWaiYeProgress({
        projectId:projectId.value,
        contractId: contractIdRelation ? contractIdRelation : contractId.value,
        contractIdRelation,
        primaryKeyId,
    });
    //处理数据
    isLoading.value = false
    if (!error && code === 200) {
        barChartDatas.value = getArrValue(data);
    } else {
        barChartDatas.value = [];
    }
}

//左右拖动，改变树形结构宽度
const leftWidth = ref(382)
const onmousedown = () => {
    const leftNum = isCollapse.value ? 142 : 272
    document.onmousemove = (ve) => {
        const diffVal = ve.clientX - leftNum;
        if(diffVal >= 310 && diffVal <= 900) {
            leftWidth.value = diffVal;
        }
    }
    document.onmouseup = () => {
        barChartRef.value?.onResize()
        document.onmousemove = null;
        document.onmouseup = null;
    }
}
</script>

<style lang="scss" scoped>
@import '../../styles/schedule/write.scss';
</style>
