<template>
    <HcTabsSimple :cur="tabsKey" :datas="tabsData" @tabClick="tabsClick">
        <template #tab-base>
            <baseTabCard :datas="infoData" :disabled="dataType === 'view'" v-if="tabsKey === 'base'" @saveClick="saveClick" :saveLoaing="saveLoaing" :contractType="contractType" :projectList="projectList"/>
        </template>
        <template #tab-file>
            <fileTabCard :datas="infoDataFile" :disabled="dataType === 'view'" v-if="tabsKey === 'file'" @saveClick="saveClick"  :saveLoaing="saveLoaing"/>
        </template>
        <template #tab-milestone>
            <milestoneTab :datas="milestoneData" :disabled="dataType === 'view'" v-if="tabsKey === 'milestone'" @saveClick="saveClick"  :saveLoaing="saveLoaing"/>
        </template>
        <template #tab-record>
            <recordTabCard :datas="recordData" v-if="tabsKey === 'record'"/>
        </template>
        
    </HcTabsSimple>
</template>

<script setup>
import {onActivated, ref} from "vue";
import {useRouter, useRoute} from 'vue-router'
import baseTabCard from "./components/base.vue";
import fileTabCard from "./components/file.vue";
import milestoneTab from "./components/milestone.vue";
import recordTabCard from "./components/record.vue";
import {arrIndex,getObjValue,getArrValue} from "js-fast-way";
import contractApi from '~api/project/project-contract.js';

//初始变量
const router = useRouter()
const useRoutes = useRoute()
const dataId = ref(useRoutes?.query?.id ?? '')
const dataType = ref(useRoutes?.query?.type ?? '')
const infoData = ref({})
//缓存页面被激活时
onActivated(() => {
    dataId.value = useRoutes?.query?.id ?? ''
    dataType.value = useRoutes?.query?.type ?? ''
    setTabsData()
    getContractType()
    if(dataType.value!=='add'){
        getContractInfoById()
        getListByContractId()
        getProjectList(2)
    }else if(dataType.value=='add'){
        infoData.value={}
        infoDataFile.value={}
        milestoneData.value=[{}]
        getProjectList(1)
    }

 
})
const infoDataFile=ref({pdfUrl:''})
//获取详情
const getContractInfoById=async()=>{
    const {error, code, data} = await contractApi.getContractInfoById({id: dataId.value})
    if (!error && code === 200) {
        infoData.value=getObjValue(data)
        infoDataFile.value.pdfUrl=infoData.value.contractAccessory

    } else {
        infoData.value={}
    }
}
//设置选项卡数据
const setTabsData = () => {
    const index = arrIndex(tabsData.value, 'key', 'record')
    if (dataType.value === 'view') {
        //预览模式下，索引不存在，就添加此选项
        if (index === -1)  {
            tabsData.value.push({
                icon: 'file-history',
                label: '合同开票记录',
                key: 'record'
            })
        }
    } else {
        //编辑模式下，索引存在，就删除此选项
        if (index !== -1) {
            tabsData.value.splice(index, 1)
        }
    }
}

//选项卡
const tabsKey = ref('base')
const tabsData = ref([
    {icon: 'file-list-3', label: '合同基础信息', key: 'base'},
    {icon: 'file-word', label: '合同附件', key: 'file'},
    {icon: 'creative-commons-sa', label: '合同回款里程碑', key: 'milestone'},
])
const tabsClick = (key) => {
    tabsKey.value = key
    if(tabsKey.value=='base'){
        console.log(infoData.value,'基础信息');
    }

}


const isNoDropShadow = ref(false)
//合同里程碑
const milestoneData=ref([])
const getListByContractId=async()=>{
    const {error, code, data} = await contractApi.getListByContractId({contractId: dataId.value})
    if (!error && code === 200) {
        milestoneData.value=getArrValue(data)
       

    } else {
        milestoneData.value=[]
    }
}
//合同开票记录
const recordData=ref([])
const saveLoaing=ref(false)
//保存
const saveClick=(info,type)=>{
  console.log(infoData.value,'indo11111');
  console.log(infoDataFile.value,'infoDataFile');
  console.log(milestoneData.value,'milestoneData');
  let saveobj={...infoData.value}
  saveobj.contractAccessory=infoDataFile.value.pdfUrl
  saveobj.list=milestoneData.value

    if (dataId.value.length>0) {
        saveobj.id=dataId.value
        updateContractInfo(saveobj)
    }else{
        addContractInfo(saveobj)
    }

}
//新增合同
const addContractInfo=async(obj)=>{
    console.log(obj,'新增');
    saveLoaing.value=true;
    const {error, code, data,msg} = await contractApi.addContractInfo( obj)
    saveLoaing.value=false;
    if (!error && code === 200) {
        window.$message.success(msg)
    } 

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

}
//获取合同类型
const contractType=ref([])
const getContractType=async()=>{
    const {error, code, data} = await contractApi.getContractTypeDict()
    if (!error && code === 200) {
        contractType.value=getArrValue(data)
    } else {
        contractType.value=[]
    }
}
//所属项目projectList
const projectList=ref([])
const getProjectList=async(type)=>{
    const {error, code, data} = await contractApi.getNoConnectionProject({type})
    if (!error && code === 200) {
        projectList.value=getArrValue(data)
    } else {
        projectList.value=[]
    }
}
</script>
