<template>
    <div class="relative h-full flex">
        <div :id="`hc_table_card_${uuid}`">
            <hc-new-card>
                <template #header>
                    <el-button hc-btn type="primary" @click="systemPaymentClick">引用项目支付项</el-button>
                </template>
                <hc-table :column="tableColumn" :datas="tableData" :loading="tableLoading" is-new :index-style="{ width: 60 }" is-current-row @row-click="viewRowClick">
                    <template #isDeduct="{ row }">
                        <span>{{ row?.isDeduct === 1 ? '是' : '否' }}</span>
                    </template>
                    <template #isTotalTerms="{ row }">
                        <span>{{ row?.isTotalTerms === 1 ? '是' : '否' }}</span>
                    </template>
                    <template #action="{ row }">
                        <el-link type="success" @click="editRowClick(row)">修改</el-link>
                        <el-link type="danger" @click="delRowClick(row)">删除</el-link>
                    </template>
                </hc-table>
                <template #action>
                    <hc-pages :pages="searchForm" @change="pageChange" />
                </template>
            </hc-new-card>
        </div>
        <div :id="`hc_info_card_${uuid}`" class="flex-1">
            <hc-new-card scrollbar>
                <infoTable :ids="rowViewId" />
                <hc-title title="汇总项">
                    <template #extra>
                        <el-link type="primary" @click="addColloect">添加汇总项</el-link>
                    </template>
                </hc-title>
                <hc-table :column="tableColumn1" :datas="tableData1" is-new :index-style="{ width: 60 }">
                    <template #isDeduct="{ row }">
                        <span>{{ row?.isDeduct === 1 ? '是' : '否' }}</span>
                    </template>
                    <template #action="{ row }">
                        <el-link type="danger" @click="delCollectRow(row)">删除</el-link>
                    </template>
                </hc-table>
            </hc-new-card>
        </div>

        <!-- 引用系统支付项 -->
        <systemPay v-model="systemPayShow" @finish="finishSystemPay" />

        <!-- 修改项目支付项 -->
        <rowDataInfo v-model="isEditRowShow" :ids="rowDataId" @finish="finishEditRow" />

        <!-- 添加汇总项 -->
        <hc-new-dialog v-model="isCollectShow" is-table widths="1200px" title="添加汇总项" @save="collectModalSave">
            <div class="hc-flex-column">
                <div class="hc-flex-shrink hc-alert-primary">
                    <span class="mr-1">当前表格已选择</span>
                    <span class="text-4">0</span>
                    <span class="ml-1">项</span>
                </div>
                <div class="hc-flex-content">
                    <hc-body padding="0px">
                        <hc-table
                            :column="tableCollectColumn" :datas="tableCollectData" :loading="tableCollectLoading"
                            is-new :check-style="{ width: 29 }" :index-style="{ width: 60 }"
                            is-check @selection-change="tableCollectCheckChange"
                        >
                            <template #isDeduct="{ row }">
                                <span>{{ row?.isDeduct === 1 ? '是' : '否' }}</span>
                            </template>
                            <template #isTotalTerms="{ row }">
                                <span>{{ row?.isTotalTerms === 1 ? '是' : '否' }}</span>
                            </template>
                        </hc-table>
                    </hc-body>
                </div>
            </div>
        </hc-new-dialog>
    </div>
</template>

<script setup>
import { nextTick, onMounted, ref } from 'vue'
import { getRandom } from 'js-fast-way'
import infoTable from './components/pay/info-table.vue'
import systemPay from './components/pay/system-pay.vue'
import rowDataInfo from './components/pay/row-data.vue'
import payApi from '~api/project/debit/contract/pay.js'
import { arrToId, getArrValue, getObjValue } from 'js-fast-way'
import { useAppStore } from '~src/store'
import { delMessageV2 } from '~com/message/index.js'
const useAppState = useAppStore()
const projectId = ref(useAppState.getProjectId)
const contractId = ref(useAppState.getContractId)
defineOptions({
    name: 'ProjectDebitProjectPay',
})

const uuid = getRandom(4)

//渲染完成
onMounted(() => {
    setSplitRef()
    getTableData()
})

//初始化设置拖动分割线
const setSplitRef = () => {
    //配置参考： https://split.js.org/#/?direction=vertical&snapOffset=0
    nextTick(() => {
        window.$split(['#hc_table_card_' + uuid, '#hc_info_card_' + uuid], {
            sizes: [70, 30],
            snapOffset: 0,
            minSize: [50, 300],
        })
    })
}

//搜索表单
const searchForm = ref({ current: 1, size: 10, total: 0 })

//分页
const pageChange = ({ current, size }) => {
    searchForm.value.current = current
    searchForm.value.size = size
}

//表格数据
const tableLoading = ref(false)
const tableColumn = ref([
    { key: 'payNumber', name: '支付项编号', width: 140 },
    { key: 'payName', name: '支付项名称' },
    { key: 'payTypeName', name: '支付项类型', width: 160 },
    { key: 'isDeduct', name: '是否为扣款项', width: 100 },
    { key: 'isTotalTerms', name: '是否合计项', width: 100 },
    { key: 'payApplicableTypeName', name: '适用类型', width: 100 },
    { key: 'action', name: '操作', width: 100 },
])
const tableData = ref([])
const getTableData = async () => {
    tableLoading.value = true
    const { error, code, data } = await payApi.getProListPage({
       ...searchForm.value,
       contractId:contractId.value,
    })
    tableLoading.value = false
    if (!error && code === 200) {
        tableData.value = getArrValue(data['records'])
        if (tableData.value.length > 0) {
            getDetail(tableData.value[0].id)
            rowViewId.value = tableData.value[0].id
         
        }
       
      
    } else {
        tableData.value = []
  
    }
}

//引用系统支付项
const systemPayShow = ref(false)
const systemPaymentClick = () => {
    systemPayShow.value = true
}

const finishSystemPay = ()=>{
    getTableData()
    systemPayShow.value = false

}
//汇总项
const tableColumn1 = ref([
    { key: 'payNumber', name: '支付项编号' },
    { key: 'payName', name: '支付项名称' },
    { key: 'payTypeName', name: '支付项类型' },
    { key: 'contractFormulaIds', name: '合同计算公式' },
    { key: 'updateFormulaIds', name: '变更计算公式' },
    { key: 'currentFormulaIds', name: '本期计算公式' },
    { key: 'isDeduct', name: '是否为扣款项' },
    { key: 'action', name: '操作', width: 50 },
])
const tableData1 = ref([])

//修改
const rowDataId = ref('')
const rowViewId = ref('')
const isEditRowShow = ref(false)
const editRowClick = (row) => {
    isEditRowShow.value = true
    rowDataId.value = row.id
   
}
//获取节点详情
const getDetail = async (id)=>{
    const { error, code, data } = await payApi.getProDetail({
       id,
    })
    if (!error && code === 200) {
        tableData1.value = getObjValue(data)['summaryItemList']
   
    } else {
        tableData1.value = []
    }
}
const viewRowClick = ({ row })=>{
    rowViewId.value = row.id
    getDetail(row.id)
}
const finishEditRow = ()=>{
    isEditRowShow.value = false
    getTableData()
}
const delRowClick = async (row)=>{
    delMessageV2(async (action, instance, done) => {
            if (action === 'confirm') {
                instance.confirmButtonLoading = true
                removeProPay(row.id)
                instance.confirmButtonLoading = false
                done()
            } else {
                done()
            }
    })
}
const removeProPay = async (id) => {
    const { error, code } = await payApi.removeProList({
        id: id, 
    })
    if (!error && code === 200) {
        window?.$message?.success('删除成功')
       getTableData()
       rowViewId.value = ''
    }
}
//添加汇总项
const isCollectShow = ref(false)
const collectSaveLoading = ref(false)
const collectModalSave = async () => {

    collectSaveLoading.value = true
    const { error, code, msg } = await payApi.addCollect({
        bindIds	:selectTableCollectIds.value,
        id:rowViewId.value,
       
        })
        //判断状态
        collectSaveLoading.value = false
        if (!error && code === 200) {
            window?.$message?.success(msg)
            
         
        }
        getDetail(rowViewId.value)
    isCollectShow.value = false
}
const addColloect = ()=>{
    isCollectShow.value = true
    getTableCollectData()
}
//删除汇总项
const delCollectRow = (row)=>{
    delMessageV2(async (action, instance, done) => {
            if (action === 'confirm') {
                instance.confirmButtonLoading = true
                removeCollectPay(row.id)
                instance.confirmButtonLoading = false
                done()
            } else {
                done()
            }
    })
}
const removeCollectPay = async (id) => {
    const { error, code } = await payApi.removeCollect({
        id: rowViewId.value,
        bindId:id,
    })
    if (!error && code === 200) {
        window?.$message?.success('删除成功')
        getDetail(rowViewId.value)
    }
}
//添加汇总项
const tableCollectColumn = ref([
    { key: 'payNumber', name: '支付项编号' },
    { key: 'payName', name: '支付项名称' },
    { key: 'payTypeName', name: '支付项类型' },
    { key: 'isDeduct', name: '是否为扣款项' },
    { key: 'isTotalTerms', name: '是否合计项' },
    { key: 'payApplicableTypeName', name: '支付项适用类型' },
])
const tableCollectData = ref([])
const tableCollectLoading = ref(false)
const getTableCollectData = async () => {
    tableCollectLoading.value = true
    const { error, code, data } = await payApi.getProListPage({
       type:'1',
       contractId:contractId.value,
       current: 1, size: 100000,
    })
    tableCollectLoading.value = false
    if (!error && code === 200) {
        tableCollectData.value = getArrValue(data['records'])
      
    } else {
        tableCollectData.value = []
  
    }
}
const selectTableCollectIds = ref('')
const tableCollectCheckChange = (list) => {
    selectTableCollectIds.value = arrToId(list)
}
</script>
