<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="变更令编号：" prop="changeNumber">
                            <el-input v-model="baseForm.changeNumber" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="变更令名称：" prop="changeName">
                            <el-input v-model="baseForm.changeName" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="变更发起单位：">
                            <el-input v-model="baseForm.changeUnit" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="业务日期：" prop="businessDate">
                            <el-date-picker v-model="baseForm.businessDate" class="block" format="YYYY-MM-DD" type="date" value-format="YYYY-MM-DD" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="变更类型：">
                            <el-select v-model="baseForm.changeType" filterable block>
                                <el-option v-for="item in typeData" :key="item.value" :label="item.label" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="延长工期：">
                            <el-input v-model="baseForm.lengthenDays" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="变更申请金额：">
                            <el-input v-model="baseForm.changeMoney" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="设计完成时间：" prop="designDate">
                            <el-date-picker v-model="baseForm.designDate" class="block" format="YYYY-MM-DD" type="date" value-format="YYYY-MM-DD" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实际变更桩号：">
                            <el-input v-model="baseForm.realityChangeNumber" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="变更归类：">
                            <el-select v-model="baseForm.changeClassify" filterable block>
                                <el-option v-for="item in classifyData" :key="item.value" :label="item.label" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="变更批复文号：">
                            <el-input v-model="baseForm.changeApprovalNumber" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="变更批复日期：" prop="changeApprovalDate">
                            <el-date-picker v-model="baseForm.changeApprovalDate" class="block" format="YYYY-MM-DD" type="date" value-format="YYYY-MM-DD" />
                        </el-form-item>
                    </el-col>
                    <!-- el-col :span="24">
                        <el-form-item class="input-link-item" label="引用预变更：">
                            <el-input v-model="baseForm.key6" disabled />
                            <el-link type="primary" @click="quoteClick">引用</el-link>
                            <el-link type="danger">删除引用</el-link>
                        </el-form-item>
                    </el-col -->
                    <el-col :span="24">
                        <el-form-item label="变更原因：">
                            <el-input v-model="baseForm.changeCause" :autosize="{ minRows: 4, maxRows: 8 }" type="textarea" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </hc-card-item>
        <!-- 变更申请部位 -->
        <hc-card-item title="变更申请部位" class="mt-3">
            <hc-table :column="tableColumn" :datas="tableData" is-new is-current-row :index-style="{ width: 60 }" @row-click="rowChangeNodeClick">
                <template #contractPicture="{ row }">
                    <hc-table-input v-model="row.contractPicture" />
                </template>
                <template #action="{ index }">
                    <el-link type="danger" @click="delChangeNode(index)">删除</el-link>
                </template>
            </hc-table>
        </hc-card-item>
        <!-- 变更申请清单 -->
        <hc-card-item title="变更申请清单" class="mt-3">
            <template #extra>
                <el-link type="primary" @click="changeShowClick">添加</el-link>
            </template>
            <div class="hc-table-ref-box no-border">
                <el-table class="w-full" :data="tableData[tableIndex]?.formList" row-key="id" height="100%" highlight-current-row border>
                    <el-table-column type="index" label="序号" />
                    <el-table-column prop="formNumber" label="清单编号" />
                    <el-table-column prop="formName" label="清单名称" />
                    <el-table-column prop="currentPrice" label="单价" />
                    <el-table-column label="数量" align="center">
                        <el-table-column prop="contractTotal" label="变更前" />
                        <el-table-column prop="currentChangeTotal" label="变更增减">
                            <template #default="{ row }">
                                <hc-table-input v-model="row.currentChangeTotal" @blur="currentChangeTotalBlur(row)" />
                            </template>
                        </el-table-column>
                        <el-table-column prop="changeTotal" label="变更后" />
                    </el-table-column>
                    <el-table-column label="金额" align="center">
                        <el-table-column prop="contractMoney" label="变更前" />
                        <el-table-column prop="currentChangeMoney" label="变更增减" />
                        <el-table-column prop="changeMoney" label="变更后" />
                    </el-table-column>
                    <el-table-column prop="action" label="操作" width="80" align="center">
                        <template #default="scope">
                            <el-link type="danger" @click="tableFormListDel(scope.$index)">删除</el-link>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </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>
        <!-- 变更申请清单 -->
        <ChangeRequest v-model="isChangeShow" :ids="changeIds" :tree-id="changeNodeItem.id" :contract-id="contractId" @finish="changeNodeFinish" />
        <!-- 文件上传组件 -->
        <hc-upload-file ref="uploadFileRef" :echo-params="uploadFileParams" :options="uploadFileOptions" @success="uploadFileSuccess" />
    </div>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue'
import ChangeRequest from '~src/views/alter/admin/components/order/changeRequest.vue'
import { getDictionaryData, isNumberReg } from '~uti/tools'
import { arrToKey, isArray, isNullES } from 'js-fast-way'
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 contractId = ref('')

//监听可否编辑
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
    getMeterChangeClassify().then()
    getMeterChangeType().then()
}

//计量变更归类
const classifyData = ref([])
const getMeterChangeClassify = async () => {
    classifyData.value = await getDictionaryData('meter_change_classify')
}

//计量变更类型
const typeData = ref([])
const getMeterChangeType = async () => {
    typeData.value = await getDictionaryData('meter_change_type')
}

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

//清单表格
const tableColumn = ref([
    { key: 'nodeName', name: '工程名称' },
    { key: 'nodeUrl', name: '节点路径' },
    { key: 'contractPicture', name: '合同图号' },
    { key: 'changeMoney', name: '变更后金额(元)' },
    { key: 'isSupplementName', name: '是否增补' },
    { key: 'action', name: '操作', width: 80, align: 'center' },
])
const tableData = ref([])

//删除变更申请部位
const delChangeNode = (index) => {
    tableData.value.splice(index, 1)
    tableIndex.value = -1
}

//变更申请部位 行被点击
const changeNodeItem = ref({})
const tableIndex = ref(-1)
const rowChangeNodeClick = ({ row, index }) => {
    if (!isArray(row.formList)) {
        tableData.value[index].formList = []
    }
    tableIndex.value = index
    changeNodeItem.value = row
}

//删除变更申请清单
const tableFormListDel = (index) => {
    tableData.value[tableIndex.value].formList.splice(index, 1)
    getFormList()
}
//变更清单的添加弹窗
const isChangeShow = ref(false)
const changeIds = ref('')
const changeShowClick = () => {
    if (isNullES(changeNodeItem.value['id'])) {
        window.$message.warning('请先选择变更申请部位')
        return false
    }
    changeIds.value = arrToKey(tableData.value[tableIndex.value].formList, 'id')
    isChangeShow.value = true
}

//确认选择完成
const changeNodeFinish = (data) => {
    tableData.value[tableIndex.value].formList.push(...data)
    getFormList()
}

//变更清单增减
const currentChangeTotalBlur = (row) => {
    //如果为空
    let val = row.currentChangeTotal
    const isMeter = isNumberReg(val)
    if (isNullES(val) || !isMeter) {
        val = 0
    }
}

//获取变更清单总额
const getFormList = () => {

}

//附件上传
const uploadFormProps = {
    url: 'fileUrl',
    name: 'fileName',
}

const attachmentUpload = () => {
    //uploadFileRef.value?.selectFile()
}
const attachmentUploadChange = (a, b, fileList) => {
    //baseForm.value.fileList = getArrValue(fileList)
}

//文件上传
const uploadFileRef = ref(null)
const uploadFileParams = ref({})
const uploadFileOptions = ref({
    headers: getHeader(),
    multiple: false,
})

// 文件上传成功的回调
const uploadFileSuccess = ({ resData }) => {
    baseForm.value.fileList.push({
        /*contractId: contractId.value,
        fileName: resData.originalName ?? '',
        filePdfUrl: resData.pdfUrl ?? '',
        fileUrl: resData.link ?? '',*/
    })
    //uploadFileRef.value?.setModalShow(false)
}
</script>

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

}
</style>
