<template>
    <HcTable ui="no-border" is-new :column="tableColumn" :datas="tableData" :loading="tableLoading" :is-index="false">
        <template #integrality="{ row }">
            <div v-if="row.isEdit" class="table-score-input-box">
                <el-input-number v-model="row.integrality" placeholder="" :controls="false" style="width: 100%;" :precision="0" />
            </div>
            <span v-else>{{ row.integrality || '-' }}</span>
        </template>
        <template #integralityDeduction="{ row }">
            <div v-if="row.isEdit" class="table-score-input-box">
                <el-input-number v-model="row.integralityDeduction" placeholder="" :controls="false" style="width: 100%;" :precision="0" />
            </div>
            <span v-else>{{ row.integralityDeduction || '-' }}</span>
        </template>
        <template #normativeDeduction="{ row }">
            <div v-if="row.isEdit" class="table-score-input-box">
                <el-input-number v-model="row.normativeDeduction" placeholder="" :controls="false" style="width: 100%;" :precision="0" />
            </div>
            <span v-else>{{ row.normativeDeduction || '-' }}</span>
        </template>
        <template #normative="{ row }">
            <div v-if="row.isEdit" class="table-score-input-box">
                <el-input-number v-model="row.normative" placeholder="" :controls="false" style="width: 100%;" :precision="0" />
            </div>
            <span v-else>{{ row.normative || '-' }}</span>
        </template>
        <template #action="{ row }">
            <el-button v-if="row.isEdit" type="success" size="small" @click="saveClick(row)">
                保存
            </el-button>
            <el-button v-else type="primary" size="small" @click="editClick(row)">
                编辑
            </el-button>
        </template>
    </HcTable>
</template>

<script setup>
import { onActivated, ref, watch } from 'vue'
import { getArrValue } from 'js-fast-way'
import writeApi from '~api/transfer/write-conclusion'


//参数
const props = defineProps({
    projectId: {
        type: [String, Number],
        default: '',
    },
    contractId: {
        type: [String, Number],
        default: '',
    },
    type: {
        type: [String, Number],
        default: '',
    },
})

//变量
const projectId = ref(props.projectId)
const contractId = ref(props.contractId)
const typeId = ref(props.type)

//监听
watch(() => [
    props.type,
], ([type]) => {
    typeId.value = type
    getTotalData()
})

//渲染完成
onActivated(() => {
    getTotalData()
})

//表格数据
const tableColumn = ref([
    { key:'scoreItem', name: '项目' },
    { key:'integrality', name: '完整性', width: 80, align: 'center' },
    { key:'integralityDeduction', name: '扣分', width: 80, align: 'center' },
    { key:'normative', name: '规范性', width: 80, align: 'center' },
    { key:'normativeDeduction', name: '扣分', width: 80, align: 'center' },
    { key:'action', name: '操作', width: 80, align: 'center' },
])
const tableData = ref([
   
])

const tableLoading = ref(false)
const getTotalData = async ()=>{
    tableLoading.value = true
    const { error, code, data } = await writeApi.getItemByUnit({
        projectId: projectId.value,
        unitType:typeId.value,
    })
    tableLoading.value = false
    if (!error && code === 200) {
        tableData.value = getArrValue(data)
     
    } else {
        tableData.value = []
     
    }
}
//编辑
const editClick = (row) => {
    row.isEdit = true
}
//保存
const saveClick = async (row) => {

    const { error, code, msg } = await writeApi.updateExpertScore({
      ...row,
    })
    if (!error && code === 200) {
        window.$message.success(msg)
        row.isEdit = false
     
    } 
}
</script>

<style lang="scss" scoped>
.table-score-input-box {
    position: relative;
    text-align: left;
}
</style>
