<template>
    <HcCard action-ui="text-center">
        <el-scrollbar ref="listItemScrollRef">
            <CollapseForm
                v-if="ListItemDatas.length > 0"
                ref="ListItemRef"
                v-loading="ListItemLoading"
                :classify="dataType"
                :contract-id="0"
                :datas="ListItemDatas"
                :primary-key-id="0"
                :status="0"
                :area-id="areaId"
                :agreement-id="agreementId"
                @renew="updateGetTablesData(agreementId)"
            />
            <HcStatus v-else text="暂无表单" />
        </el-scrollbar>
        <template #action>
            <el-button size="large" type="primary" hc-btn :loading="tableFormSaveLoading" @click="tableFormSaveClick">
                <HcIcon name="check-double" />
                <span>提交保存</span>
            </el-button>
            <el-button size="large" type="primary" hc-btn>
                <HcIcon name="draft" />
                <span>一键生成协议</span>
            </el-button>
            <el-button size="large" type="warning" hc-btn :loading="bussPdfsLoading" @click="bussPdfsClick">
                <HcIcon name="eye" />
                <span>预览</span>
            </el-button>
            <el-button size="large" type="success" hc-btn @click="addFile">
                <HcIcon name="upload" />
                <span>上传附件协议</span>
            </el-button>
            <el-button size="large" type="info" hc-btn @click="goBackClick">
                <HcIcon name="arrow-go-back" />
                <span>取消并返回</span>
            </el-button>
        </template>
        <HcDialog is-to-body :show="addModal" title="上传附件协议" @close="testModalClose">
            <HcUpload :datas="uploadData" :file-list="fileListData" :is-canupload="false" action="/api/blade-manager/exceltab/add-bussfile-node" accept="application/pdf" accept-tip="允许格式：pdf" @change="uploadChange" />
        </HcDialog>
    </HcCard>
</template>

<script setup>
import { onActivated, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import CollapseForm from './collapse-form/index.vue'
import landApi from '~api/agree/land.js'
import { useAppStore } from '~src/store'
import { getArrValue, getObjVal } from 'js-fast-way'
import HcUpload from './components/HcUpload.vue'

const useAppState = useAppStore()
const projectId = ref(useAppState.getProjectId)
const router = useRouter()
const useRoutes = useRoute()

//初始变量
const dataType = ref(useRoutes?.query?.type ?? 'view')
const agreementId = ref(useRoutes?.query?.id ?? '')
const areaId = ref(useRoutes?.query?.areaId ?? '')

//缓存页面被激活时
onActivated(() => {
    dataType.value = useRoutes?.query?.type ?? 'view'
    agreementId.value = useRoutes?.query?.id ?? ''
    areaId.value = useRoutes?.query?.areaId ?? ''
    if (agreementId.value.length > 0) {
        updateGetTablesData( agreementId.value)
    } else {
        searchNodeAllTable()
    }
    
    
})

const listItemScrollRef = ref(null)

//返回
const goBackClick = () => {
    router.back()

}

//获取数据列表
const ListItemsRef = ref(null)
const ListItemDatas = ref([])
const ListItemLoading = ref(false)

//新增获取表单列表id
const searchNodeAllTable = async () => {
    const { error, code, data } = await landApi.addGetTables({
        projectId: projectId.value,
        type: 2,
        areaId:areaId.value,
    
    })
    //处理数据

    if (!error && code === 200) {
        agreementId.value = data
        updateGetTablesData(data)
    } else {
        agreementId.value = ''
    }
}
//编辑获取表单列表
const updateGetTablesData = async (id) => {
    ListItemDatas.value = []
    ListItemLoading.value = true
    const { error, code, data } = await landApi.updateGetTables({
        agreementId:id,
    
    })
    //处理数据
    ListItemLoading.value = false
    if (!error && code === 200) {
        ListItemDatas.value = getArrValue(data)
    } else {
        ListItemDatas.value = []
    }
}

//保存
const ListItemRef = ref(null)
const tableFormSaveLoading = ref(false)
const tableFormSaveClick = async () => {
    //获取数据
    let FormData = []
        FormData = await ListItemRef.value?.getFormData()
    //效验数据
    if (FormData.length > 0) {
        tableFormSaveLoading.value = true
        FormData.forEach(ele => {
            delete ele['pkeyId']
        })
        const { error, code, data } = await landApi.saveBussData({
            dataInfo: { orderList: FormData },
        })
        tableFormSaveLoading.value = false
        if (!error && code === 200) {
            window?.$message?.success('保存成功')
            dataType.value = 2
             await bussPdfsClick()
             updateGetTablesData(agreementId.value)
        }
    } else {
        console.log('预览')
        // await bussPdfsClick()
    }
}
//多表预览
const bussPdfsLoading = ref(false)
const bussPdfsClick = async () => {
    bussPdfsLoading.value = true
    const { error, code, data } = await landApi.getBussPdfs({
        agreementId:agreementId.value,
    })
    tableFormSaveLoading.value = false
    bussPdfsLoading.value = false
    if (!error && code === 200) {
        window.open(data, '_blank')
    } else {
        window.$message?.warning('获取PDF失败')
    }
}
//上传附件
const addModal = ref(false)
const testModalClose = ()=>{
    addModal.value = false
}
const addFile = ()=>{
    addModal.value = true
}
const uploadData = ref({})
const fileListData = ref([])
//上传文件
const uploadChange = async ({ type }) => {
    if (type === 'success') {
        // getBussFileList(primaryKeyId.value)
    } else if (type === 'del') {
        // getBussFileList(primaryKeyId.value)
    }
}
</script>

<style lang="scss" scoped>

</style>
