<template>
    <div class="hc-task-form">
        <!-- 基础表单 -->
        <hc-card-item>
            <el-form ref="baseFormRef" :model="baseForm" :rules="baseFormRules" label-position="left" label-width="auto">
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item label="计量期：">
                            <el-select v-model="baseForm.contractPeriodId" placeholder="选择计量期" disabled block>
                                <el-option v-for="item in periods" :key="item.id" :label="item.periodNumber" :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="计量编号：">
                            <el-input v-model="baseForm.meterNumber" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="业务日期：">
                            <el-date-picker v-model="baseForm.businessDate" class="block" format="YYYY-MM-DD" type="date" value-format="YYYY-MM-DD" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="16">
                        <el-form-item label="工程划分：">
                            <el-input v-model="baseForm.engineerDivide" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="部位名称：">
                            <el-input v-model="baseForm.partName" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="交工证书：">
                            <el-input v-model="baseForm.certificateNumber" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="计量金额：">
                            <el-input v-model="baseForm.meterMoney" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="变更令：">
                            <el-input v-model="baseForm.changeTokenNumber" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="收方单：">
                            <el-input v-model="baseForm.debitFormId" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="16">
                        <el-form-item label="草图文件：">
                            <div class="form-item-div">暂无文件</div>
                            <!-- hc-form-upload :src="baseForm.pictureUrl" @upload="formUpload" @change="formUploadChange" / -->
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="计算式：">
                            <el-input v-model="baseForm.calculateFormula" :autosize="{ minRows: 4, maxRows: 8 }" type="textarea" disabled />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </hc-card-item>
        <!-- 收方清单列表 -->
        <hc-card-item class="mt-3">
            <template #header>
                <span class="mr-3 text-[13px]">计量清单</span>
                <span class="text-[13px] text-orange font-400">温馨提示：超计,累计计量量> 变更后数量，分解清单超计整行红色</span>
            </template>
            <template #extra>
                <el-link type="primary" @click="addBillModalClick">
                    <hc-icon name="add" />
                    <span>添加清单</span>
                </el-link>
            </template>
            <hc-table is-new :index-style="{ width: 60 }" :is-stripe="false" :column="tableColumn" :datas="tableData" :row-style="tableRowStyle">
                <template #currentPrice="{ row }">
                    <hc-table-input v-model="row.currentPrice" disabled />
                </template>
                <template #changeTotal="{ row }">
                    <hc-table-input v-model="row.changeTotal" disabled />
                </template>
                <template #currentMeterTotal="{ row }">
                    <hc-table-input v-model="row.currentMeterTotal" @blur="currentMeterTotalBlur(row)" />
                </template>
                <template #containChangeTotal="{ row }">
                    <hc-table-input v-model="row.containChangeTotal" @blur="containChangeTotalBlur(row)" />
                </template>
                <template #currentMeterMoney="{ row }">
                    <hc-table-input v-model="row.currentMeterMoney" disabled />
                </template>
                <template #allMeterTotal="{ row }">
                    <hc-table-input v-model="row.allMeterTotal" disabled />
                </template>
                <template #action="{ index }">
                    <el-link type="danger" @click="delAddTableClick(index)">删除</el-link>
                </template>
            </hc-table>
        </hc-card-item>
        <!-- 附件列表 -->
        <hc-card-item class="mt-3" title="附件列表">
            <template #extra>
                <span class="text-[13px] text-orange font-400">可上传 图片(png、jpg、jpeg)、Excel(xls、xlsx)、PDF、Word(doc、docx)文件</span>
            </template>
            <el-form :model="baseForm" label-position="left" label-width="auto">
                <el-form-item label="上传附件">
                    <hc-form-upload type="list" :src="baseForm.fileList" :h-props="uploadFormProps" @upload="attachmentUpload" @change="attachmentUploadChange" />
                </el-form-item>
            </el-form>
        </hc-card-item>
        <!-- 添加分解清单 -->
        <HcBillBaseModal v-model="addBillBaseModalShow" :ids="billBaseIds" :idn="nodeId" :period-id="baseForm.contractPeriodId" :contract-id="cid" @finish="finishChange" />
        <!-- 文件上传组件 -->
        <hc-upload-file ref="uploadFileRef" :echo-params="uploadFileParams" :options="uploadFileOptions" @success="uploadFileSuccess" />
    </div>
</template>

<script setup>
import { getArrValue } from 'js-fast-way'
import { onMounted, ref, watch } from 'vue'
import HcBillBaseModal from '~src/views/debit-pay/admin/components/middlepay/addBillBaseModal.vue'
import BigNumber from 'bignumber.js'
import { getHeader } from 'hc-vue3-ui'

import mainApi from '~api/tasks/hc-data'

const props = defineProps({
    isEdit: {
        type: Boolean,
        default: true,
    },
    type: {
        type: [String, Number],
        default: '',
    },
})

//双向绑定
// eslint-disable-next-line no-undef
const rowInfo = defineModel('modelValue', {
    default: () => ({}),
})

//监听可否编辑
const isEdits = ref(props.isEdit)
watch(() => props.isEdit, (val) => {
    isEdits.value = val
}, { immediate: true, deep: true })

//监听类型
const isType = ref(props.type)
watch(() => props.type, (val) => {
    isType.value = val
}, { immediate: true, deep: true })

//监听数据
watch(() => rowInfo, (val) => {
    setTaskInfo(val.value)
}, { deep: true })

//渲染完成
onMounted(() => {
    setTaskInfo(rowInfo.value)
})

//设置任务信息
const taskInfo = ref({})
const setTaskInfo = async (info) => {
    taskInfo.value = info
    getAllPeriod(info.contractId).then()
}

//获取合同计量期列表
const periods = ref([])
const getAllPeriod = async (cid) => {
    if (cid === '') return
    const { data } = await mainApi.getAllPeriod({
        contractId: cid,
        type: 1,
    })
    periods.value = getArrValue(data)
}

//基础表单
const baseFormRef = ref(null)
const baseForm = ref({ fileList: [], pictureUrl: '' })
const baseFormRules = {}

//清单表格
const tableColumn = ref([
    { key: 'formNumber', name: '清单编号', width: '120' },
    { key: 'formName', name: '清单名称', minWidth: '280' },
    { key: 'currentPrice', name: '单价(元)', width: '100' },
    { key: 'resolveTotal', name: '分解数量', width: '100' },
    { key: 'changeTotal', name: '变更后数量', width: '100' },
    { key: 'currentMeterTotal', name: '本期计量数量', width: '140' },
    { key: 'containChangeTotal', name: '含变更数量', width: '100' },
    { key: 'currentMeterMoney', name: '本期计量金额', width: '120' },
    { key: 'allMeterTotal', name: '累计计量量', width: '100' },
    { key: 'action', name: '操作', fixed: 'right', width: 80, align: 'center' },
])
const tableData = ref([])

//清单样式
const tableRowStyle = () => {

}

const currentMeterTotalBlur = (row) => {

}

const containChangeTotalBlur = (row) => {

}

//删除清单
const delAddTableClick = (index) => {

}

//添加计量清单
const billBaseIds = ref('')
const nodeId = ref('')
const cid = ref('')
const addBillBaseModalShow = ref(false)
const addBillModalClick = () => {

}

const finishChange = () => {

}


const uploadFormProps = ref({})

const attachmentUpload = () => {

}

const attachmentUploadChange = () => {

}

//文件上传
const uploadFileRef = ref(null)
const uploadFileParams = ref({})
const uploadFileOptions = ref({})
const uploadFileSuccess = () => {

}
</script>

<style lang="scss" scoped>
.hc-task-form {

}
</style>
