<template>
    <div class="hc-layout-box">
        <HcNewCard :scrollbar="false" action-size="lg">
            <template #header>
                <HcTooltip keys="gauge-bezier-add">
                    <el-button :disabled="searchForm.partId.length < 1" hc-btn type="primary" @click="addLineEleModal">
                        <HcIcon name="add-box" />
                        <span>添加线元</span>
                    </el-button>
                </HcTooltip>
                <HcTooltip keys="gauge-bezier-to">
                    <el-button hc-btn @click="showToModalClick">
                        <HcIcon name="repeat" />
                        <span>转换坐标</span>
                    </el-button>
                </HcTooltip>
                <HcTooltip keys="gauge-bezier-pile">
                    <el-button hc-btn @click="pileModalClick">
                        <HcIcon name="settings-6" />
                        <span>逐桩坐标</span>
                    </el-button>
                </HcTooltip>
                <HcTooltip keys="gauge-bezier-import">
                    <el-button hc-btn @click="importModalClick">
                        <HcIcon name="upload-cloud" />
                        <span>导入</span>
                    </el-button>
                </HcTooltip>
                <HcTooltip keys="gauge-bezier-export">
                    <el-button :loading="downloadLoading" hc-btn @click="exportModalClick">
                        <HcIcon name="download" />
                        <span>导出</span>
                    </el-button>
                </HcTooltip>
            </template>
            <template #extra>
                <div class="w-60">
                    <el-select v-model="searchForm.partId" block clearable placeholder="片段查询" @change="searchClick">
                        <el-option v-for="item in partData" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                </div>
                <HcTooltip keys="gauge-bezier-add-part">
                    <div class="hc-extra-icon-btn" @click="AddPartClick">
                        <HcIcon name="add-circle" />
                    </div>
                </HcTooltip>
                <HcTooltip keys="gauge-bezier-admin-part">
                    <div class="hc-extra-icon-btn" @click="AdminPartClick">
                        <HcIcon name="list-settings" />
                    </div>
                </HcTooltip>
            </template>

            <HcTable :column="tableColumn" :datas="tableData" :loading="tableLoading" is-new :index-style="{ width: 60 }">
                <template #name="{ row }">{{ row.valueStr }}~{{ row.valueEndStr }}</template>
                <template #type="{ row }">{{ getTypeName(row?.type) }}</template>
                <!-- <template #angle="{row}">
                    {{row['du']}}°{{row['fen']}}′{{row['miao']}}″
                </template> -->
                <template #action="{ row }">
                    <HcTooltip keys="gauge-station-edit">
                        <el-button size="small" text type="primary" @click="handleTableEdit(row)">编辑</el-button>
                    </HcTooltip>
                    <HcTooltip keys="gauge-station-del">
                        <el-button size="small" text type="danger" @click="handleTableDelete(row)">删除</el-button>
                    </HcTooltip>
                </template>
            </HcTable>
            <template #action>
                <HcPages :pages="searchForm" @change="pageChange" />
            </template>
        </HcNewCard>
        <!-- 管理片段弹框 -->
        <hc-new-dialog v-model="showAdminPartModal" title="管理片段" widths="47rem">
            <div class="admin-part-data-table">
                <el-scrollbar>
                    <div class="hc-table-ref-box">
                        <el-table :data="AdminPartTableData" hc border>
                            <el-table-column label="片段名称" prop="name">
                                <template #default="scope">
                                    <el-input v-if="scope.row.isEdit" v-model="scope.row.name" placeholder="片段名称" />
                                    <div v-else>{{ scope.row.name }}</div>
                                </template>
                            </el-table-column>
                            <el-table-column label="桩号前缀" prop="prefix">
                                <template #default="scope">
                                    <el-input v-if="scope.row.isEdit" v-model="scope.row.prefix" placeholder="桩号前缀" />
                                    <div v-else>{{ scope.row.prefix }}</div>
                                </template>
                            </el-table-column>
                            <el-table-column align="center" label="操作" width="130">
                                <template #default="scope">
                                    <el-button v-if="scope.row.isEdit" size="small" text type="primary" @click="handleAdminPartSave(scope.row)">保存</el-button>
                                    <HcTooltip v-else keys="gauge-station-edit">
                                        <el-button size="small" text type="primary" @click="handleAdminPartEdit(scope.row)">编辑</el-button>
                                    </HcTooltip>
                                    <HcTooltip keys="gauge-station-del">
                                        <el-button size="small" text type="danger" @click="handleAdminPartDelete(scope.row, scope.$index)">删除</el-button>
                                    </HcTooltip>
                                </template>
                            </el-table-column>
                        </el-table>
                    </div>
                </el-scrollbar>
            </div>
            <template #footer>
                <div class="dialog-footer">
                    <el-button size="large" @click="showAdminPartModal = false">取消</el-button>
                    <el-button hc-btn type="primary" @click="AddAdminPart">新增</el-button>
                </div>
            </template>
        </hc-new-dialog>
        <!-- 新增片段弹框 -->
        <hc-new-dialog v-model="showAddPartModal" title="新增片段" widths="38rem">
            <el-form ref="partFormRef" :model="partForm" :rules="partRules" label-width="auto" size="large">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="partForm.name" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="桩号前缀" prop="prefix">
                    <el-input v-model="partForm.prefix" placeholder="只允许输入大写的英文字母" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button size="large" @click="showAddPartModal = false">取消</el-button>
                    <el-button :loading="savePartLoading" hc-btn type="primary" @click="savePartInfo">保存</el-button>
                </div>
            </template>
        </hc-new-dialog>
        <!-- 添加/编辑线元弹框 -->
        <hc-new-dialog v-model="showLineEleModal" :title="(formLineEleValue.id === -1 || !formLineEleValue.id) ? '添加线元' : '编辑线元'" widths="40rem">
            <el-form :model="formLineEleValue" label-width="auto" size="large">
                <el-form-item label="类型">
                    <el-radio-group v-model="formLineEleValue.type">
                        <el-radio v-for="(item, index) in lineElementType" :key="index" :label="item.value">{{ item.label }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="开始里程">
                    <el-input v-model="formLineEleValue.value" placeholder="请输入开始里程" />
                </el-form-item>
                <el-form-item label="X">
                    <el-input v-model="formLineEleValue.x" placeholder="请输入X" />
                </el-form-item>
                <el-form-item label="Y">
                    <el-input v-model="formLineEleValue.y" placeholder="请输入Y" />
                </el-form-item>
                <el-form-item v-if="formLineEleValue.type !== 0" label="半径">
                    <el-input v-model="formLineEleValue.r" placeholder="请输入半径" />
                </el-form-item>
                <el-form-item v-if="formLineEleValue.type === 4" label="结束半径">
                    <el-input v-model="formLineEleValue.r2" placeholder="请输入半径" />
                </el-form-item>
                <el-form-item v-if="formLineEleValue.type !== 0" label="转向">
                    <el-radio-group v-model="formLineEleValue.swing">
                        <el-radio label="左转">左转</el-radio>
                        <el-radio label="右转">右转</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="线长度">
                    <el-input v-model="formLineEleValue.l" placeholder="请输入线长度" />
                </el-form-item>
                <div class="flex">
                    <el-form-item class="flex-1" label="方位角">
                        <el-input v-model="formLineEleValue.du" placeholder="输入度">
                            <template #suffix>度</template>
                        </el-input>
                    </el-form-item>
                    <el-form-item class="flex-1 ml-4" no-label>
                        <el-input v-model="formLineEleValue.fen" placeholder="输入分">
                            <template #suffix>分</template>
                        </el-input>
                    </el-form-item>
                    <el-form-item class="flex-1 ml-4" no-label>
                        <el-input v-model="formLineEleValue.miao" placeholder="输入秒">
                            <template #suffix>秒</template>
                        </el-input>
                    </el-form-item>
                </div>
                <el-form-item v-if="formLineEleValue.type !== 3" label="断链前里程">
                    <el-input v-model="formLineEleValue.dlq" placeholder="请输入断链前里程" />
                </el-form-item>
                <el-form-item v-if="formLineEleValue.type !== 3" label="断链后里程">
                    <el-input v-model="formLineEleValue.dlh" placeholder="请输入断链后里程" />
                </el-form-item>
                <el-form-item label="顺序">
                    <el-input v-model="formLineEleValue.orderNum" placeholder="请输入顺序" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button size="large" @click="showLineEleModal = false">取消</el-button>
                    <el-button size="large" @click="refreshable">刷新</el-button>
                    <el-button :loading="saveLoading" hc-btn type="primary" @click="saveLineEleClick">保存</el-button>
                </div>
            </template>
        </hc-new-dialog>
        <!-- 里程转换坐标 -->
        <hc-new-dialog v-model="showToModal" title="里程转换坐标" widths="38rem">
            <el-form :model="formToValue" label-width="auto" size="large">
                <el-form-item label="里程">
                    <el-input v-model="formToValue.value" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="偏移">
                    <el-input v-model="formToValue.b" placeholder="请输入偏移" />
                </el-form-item>
                <el-form-item label="夹角">
                    <el-input v-model="formToValue.jj" placeholder="请输入夹角" />
                </el-form-item>
                <el-form-item label="X">
                    <div class="form-item-div">{{ formToValue.x }}</div>
                </el-form-item>
                <el-form-item label="Y">
                    <div class="form-item-div">{{ formToValue.y }}</div>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button size="large" @click="showToModal = false">取消</el-button>
                    <el-button hc-btn type="primary" @click="queryPsChange">转换</el-button>
                </div>
            </template>
        </hc-new-dialog>
        <!-- 逐桩坐标弹框 -->
        <hc-new-dialog v-model="showPileModal" title="逐桩坐标" widths="62rem">
            <el-form :model="formPileValue" inline label-position="top" label-width="auto" size="large">
                <el-form-item label="开始桩号">
                    <el-input v-model="formPileValue.x" placeholder="开始桩号" />
                </el-form-item>
                <el-form-item label="结束桩号">
                    <el-input v-model="formPileValue.y" placeholder="结束桩号" />
                </el-form-item>
                <el-form-item label="桩距">
                    <el-input v-model="formPileValue.value" placeholder="桩距" />
                </el-form-item>
                <el-form-item label="偏距">
                    <el-input v-model="formPileValue.b" placeholder="偏距" />
                </el-form-item>
                <el-form-item label=" ">
                    <el-button :loading="generateLoading" attr-type="button" type="primary" @click="generateClick">生成</el-button>
                </el-form-item>
            </el-form>
            <div class="admin-part-data-table">
                <el-scrollbar>
                    <div class="hc-table-ref-box">
                        <el-table :data="pileTable" hc border>
                            <el-table-column label="序号" prop="num" width="80">
                                <template #default="scope">{{ scope.$index + 1 }}</template>
                            </el-table-column>
                            <el-table-column label="里程" prop="valueStr" />
                            <el-table-column label="偏距" prop="b" />
                            <el-table-column label="X" prop="xxStr" />
                            <el-table-column label="Y" prop="yyStr" />
                        </el-table>
                    </div>
                </el-scrollbar>
            </div>
        </hc-new-dialog>
        <!-- 导入弹框 -->
        <hc-new-dialog v-model="showImportModal" title="导入线元" widths="32rem">
            <div class="text-center">
                <el-upload
                    ref="uploadRef" v-model:file-list="fileList" :accept="accept" :action="action"
                    :auto-upload="false" :data="addition" :headers="getTokenHeader()" :limit="1"
                    :on-error="handleUploadError" :on-exceed="handleUploadExceed"
                    :on-progress="handleUploadProgress" :on-success="handleUploadFinish"
                >
                    <template #trigger>
                        <el-button :loading="importLoading" type="primary">选择文件</el-button>
                    </template>
                </el-upload>
            </div>
            <template #footer>
                <div class="lr-dialog-footer">
                    <div class="left">
                        <el-button size="large" @click="tmportTmpClick">下载导入模板</el-button>
                    </div>
                    <div class="right">
                        <el-button size="large" @click="showImportModal = false">取消</el-button>
                        <el-button :disabled="fileList.length <= 0" :loading="importLoading" hc-btn type="primary" @click="handleImportClick">确认导入</el-button>
                    </div>
                </div>
            </template>
        </hc-new-dialog>
    </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { useAppStore } from '~src/store'
import bezierApi from '~api/gauge/bezier'
import { getTokenHeader } from '~src/api/request/header'
import { deepClone, downloadBlob, formValidate, getArrValue, getObjValue, isObjNull } from 'js-fast-way'
import { genFileId } from 'element-plus'
import { delMessageV2 } from '~com/message/index.js'

//初始变量
const useAppState = useAppStore()
const projectId = ref(useAppState.getProjectId)
const contractId = ref(useAppState.getContractId)

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

//获取下拉列表
const partData = ref([])
const queryPartList = async () => {
    const { error, code, data } = await bezierApi.queryPartList({
        projectId: projectId.value,
        contractId: contractId.value,
    })
    if (!error && code === 200) {
        let records = getArrValue(data)
        partData.value = records
        AdminPartTableData.value = deepClone(records)
        if (records.length > 0 && !searchForm.value.partId) {
            searchForm.value.partId = records[0].id
            partNo.value = records[0].prefix
            getTableData()
        }
    } else {
        partData.value = []
        AdminPartTableData.value = []
        searchForm.value.partId = null
    }
}

//搜索表单
const searchForm = ref({ partId: '', current: 1, size: 20, total: 0 })
const partNo = ref('')
//重新搜索数据
const searchClick = (id) => {
    partData.value.forEach((ele) => {
        if (ele.id === id) {
            partNo.value = ele.prefix
        }
    })
    searchForm.value.current = 1
    getTableData()
}

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

//获取数据
const tableColumn = ref([
    { key: 'name', name: '里程' },
    { key: 'orderNum', name: '排序' },
    { key: 'l', name: '长度' },
    { key: 'angleStr', name: '方位角' },
    { key: 'type', name: '类型' },
    { key: 'swing', name: '转向' },
    { key: 'xxStr', name: 'X' },
    { key: 'yyStr', name: 'Y' },
    { key: 'r', name: '半径' },
    { key: 'action', name: '操作', width: '130' },
])
const tableData = ref([])
const tableLoading = ref(false)
const getTableData = async () => {
    tableLoading.value = true
    const { error, code, data } = await bezierApi.queryListData(searchForm.value)
    tableLoading.value = false
    if (!error && code === 200) {
        tableData.value = getArrValue(data['records'])
        searchForm.value.total = data['total'] || 0
    } else {
        tableData.value = []
        searchForm.value.total = 0
    }
}

//编辑表格
const handleTableEdit = (row) => {
    const form = deepClone(row)
    formLineEleValue.value['id'] = form['id']
    formLineEleValue.value['type'] = form['type']
    formLineEleValue.value['projectId'] = projectId.value
    formLineEleValue.value['contractId'] = contractId.value
    formLineEleValue.value['partId'] = searchForm.value.partId
    let mile = ['x', 'y', 'fen', 'miao', 'du', 'value', 'l', 'dlq', 'dlh', 'swing', 'r', 'r2', 'orderNum']
    mile.forEach((key) => {
        formLineEleValue.value[key] = form[key] + ''
    })
    saveLoading.value = false
    showLineEleModal.value = true
}
//删除表格
const handleTableDelete = (row) => {
    delMessageV2(async (action, instance, done) => {
            if (action === 'confirm') {
                instance.confirmButtonLoading = true
                saveDelMileage(row.id)
                instance.confirmButtonLoading = false
                done()
            } else {
                done()
            }
    })
}
//确认删除
const saveDelMileage = async (rid) => {
    const { error, code } = await bezierApi.saveDelMileage({ id: rid }, false)
    //判断状态
    if (!error && code === 200) {
        window?.$message?.success('删除成功')
        getTableData()
    } else {
        window?.$message?.error('删除失败')
    }
}

//线元类型
const showLineEleModal = ref(false)
const formLineEleValue = ref({ type: 0 })
const lineElementType = ref([
    { value: 0, label: '直线段' },
    { value: 1, label: '圆曲线' },
    { value: 2, label: '入缓和曲线' },
    { value: 3, label: '出缓和曲线' },
    { value: 4, label: '不完整缓和曲线' },
])
//获取类型名称
const getTypeName = (val) => {
    if (val !== -1 || val >= 0) {
        return lineElementType.value[val].label || ''
    } else {
        return '-'
    }
}
//添加线元
const addLineEleModal = () => {
    formLineEleValue.value = { type: 0 }
    saveLoading.value = false
    showLineEleModal.value = true
    getNextMileagexy({partId:searchForm.value.partId})
}
//刷新
const refreshable = () => {
    getNextMileagexy({
        orderNum: formLineEleValue.value['orderNum'],
        partId:searchForm.value.partId
    })
}
//保存
const saveLoading = ref(false)
const saveLineEleClick = async () => {
    let form = formLineEleValue.value
    form.partId = searchForm.value.partId
    if (form.id === -1 || !form.id) {
        saveLoading.value = true
        const { error, code } = await bezierApi.saveAddMileage(form)
        saveLoading.value = false
        if (!error && code === 200) {
            window?.$message?.success('保存成功')
            showLineEleModal.value = false
            getTableData()
        }
    } else {
        saveLoading.value = true
        const { error, code } = await bezierApi.saveUpdateMileage(form)
        saveLoading.value = false
        if (!error && code === 200) {
            window?.$message?.success('保存成功')
            showLineEleModal.value = false
            getTableData()
        }
    }
}
//获取数据
const getNextMileagexy = async (obj = {}) => {
    const { error, code, data } = await bezierApi.getNextMileagexy({
        ...obj,
        projectId: projectId.value,
        contractId: contractId.value,
    })
    if (!error && code === 200) {
        const res = getObjValue(data)
        if (!isObjNull(res)) {
            let mile = ['x', 'y', 'fen', 'miao', 'du', 'value', 'l', 'dlq', 'dlh', 'swing', 'r', 'r2', 'orderNum']
            mile.forEach((key) => {
                formLineEleValue.value[key] = res[key] + ''
            })
            formLineEleValue.value['type'] = res['type']
            formLineEleValue.value['projectId'] = projectId.value
            formLineEleValue.value['contractId'] = contractId.value
            formLineEleValue.value['partId'] = searchForm.value.partId
        } else {
            formLineEleValue.value = { type: 0 }
            formLineEleValue.value['projectId'] = projectId.value
            formLineEleValue.value['contractId'] = contractId.value
            formLineEleValue.value['partId'] = searchForm.value.partId
        }
    }
}

//转换坐标
const showToModal = ref(false)
const formToValue = ref({ value: '', b: '0', jj: '90', x: '', y: '' })
const showToModalClick = () => {
    formToValue.value = { value: '', b: '0', jj: '90', x: '', y: '' }
    showToModal.value = true
}
//转换坐标
const queryPsChange = async () => {
    const { error, code, data } = await bezierApi.queryPsChange({
        ...formToValue.value,
        projectId: projectId.value,
        partId: searchForm.value.partId,
    })
    //判断状态
    if (!error && code === 200) {
        const res = getObjValue(data)
        formToValue.value['x'] = res?.x + ''
        formToValue.value['y'] = res?.y + ''
        window?.$message?.success('转换成功')
    }
}

//逐桩坐标
const showPileModal = ref(false)
const formPileValue = ref({})
const pileModalClick = () => {
    formPileValue.value = { x: '0', y: '0', value: '0', b: '0' }
    pileTable.value = []
    generateLoading.value = false
    showPileModal.value = true
}
//确认生成
const generateLoading = ref(false)
const pileTable = ref([])
const generateClick = async () => {
    generateLoading.value = true
    const { error, code, data } = await bezierApi.getZzList({
        ...formPileValue.value,
        projectId: projectId.value,
        partId: searchForm.value.partId,
    })
    //判断状态
    generateLoading.value = false
    if (!error && code === 200) {
        pileTable.value = getArrValue(data)
    } else {
        pileTable.value = []
    }
}

//导入配置
const showImportModal = ref(false)
const importLoading = ref(false)

//上传配置
const uploadRef = ref(null)
const addition = ref({})
const fileList = ref([])
const action = '/api/blade-business/mileage/import'
const accept = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel'

//导入
const importModalClick = () => {
    addition.value = {
        projectId: projectId.value,
        contractId: contractId.value,
        partId: searchForm.value.partId,
    }
    importLoading.value = false
    showImportModal.value = true
}

//下载导入模板
const tmportTmpClick = () => {
    window.open('https://bladex-chongqing-info.oss-cn-hangzhou.aliyuncs.com//upload/20230817/063231e1b4813060e67d872cc732e82d.xlsx', '_blank')

}
//上传一个文件时，重置
const handleUploadExceed = (files) => {
    uploadRef.value?.clearFiles()
    const file = files[0]
    file.uid = genFileId()
    uploadRef.value?.handleStart(file)
}

//确认导入
const handleImportClick = () => {
    uploadRef.value?.submit()
}
//上传中
const handleUploadProgress = () => {
    importLoading.value = true
}
//上传完成
const handleUploadFinish = (res) => {
    importLoading.value = false
    showImportModal.value = false
    if (res.code === 200) {
        window?.$message?.success(res.msg)
        uploadRef.value?.clearFiles()
        getTableData()
    } else {
        window?.$message?.error(res.msg)
    }
}
//上传失败
const handleUploadError = (res) => {
    importLoading.value = false
    window?.$message?.error(res.msg || '导入失败')
}

//导出
const exportModalClick = () => {
    window?.$messageBox?.alert('请确认当前查询条件，系统将以当前筛选条件导出数据', '导出数据', {
        showCancelButton: true,
        confirmButtonText: '确定导出',
        cancelButtonText: '取消',
        callback: (action) => {
            if (action === 'confirm') {
                getExportExcel()
            }
        },
    })
}
//确定导出
const downloadLoading = ref(false)
const getExportExcel = async () => {
    //批量下载
    downloadLoading.value = true
    const { error, disposition, res } = await bezierApi.getExportExcel({
        projectId: projectId.value,
        partId: searchForm.value.partId,
        partNo: partNo.value,
    })
    //处理数据
    downloadLoading.value = false
    if (!error) {
        if (disposition) {
            downloadBlob(res, disposition)
        } else {
            window.$message?.error('数据异常')
        }
    }
}


//片段管理表单
const partFormRef = ref(null)
const partForm = ref({ name: '', prefix: '' })
const partRules = {
    name: {
        required: true,
        trigger: 'blur',
        message: '请输入名称',
    },
    prefix: {
        required: true,
        validator: (rule, value, callback) => {
            const reg = /^[A-Z]+$/
            if (value && !reg.test(value)) {
                callback(new Error('只允许输入大写的英文字母'))
            } else {
                callback()
            }
        },
        trigger: 'blur',
    },
}

//管理片段
const showAdminPartModal = ref(false)
const AdminPartTableData = ref([])
const AdminPartClick = () => {
    AdminPartTableData.value = deepClone(partData.value)
    showAdminPartModal.value = true
}

//新增片段弹框
const showAddPartModal = ref(false)
const AddPartClick = () => {
    partForm.value = { name: '', prefix: '' }
    savePartLoading.value = false
    showAddPartModal.value = true
}
//新增保存
const savePartLoading = ref(false)
const savePartInfo = async () => {
    const validate = await formValidate(partFormRef.value)
    if (validate) {
        savePartLoading.value = true
        const { error, code } = await bezierApi.savePartAdd({
            ...partForm.value,
            projectId: projectId.value,
            contractId: contractId.value,
        })
        savePartLoading.value = false
        if (!error && code === 200) {
            window?.$message?.success('新增成功')
            showAddPartModal.value = false
            queryPartList()
        }
    }
}

//新增
const AddAdminPart = () => {
    AdminPartTableData.value.push({
        id: '', name: '', prefix: '', isEdit: true,
    })
}
//编辑
const handleAdminPartEdit = (row) => {
    row.isEdit = true
}

//保存
const handleAdminPartSave = async (row) => {
    //严格模式/^[A-Z]+((#\d+)?|\d+)$/
    const reg = /^[A-Z#-\d]+$/
    if (!row.name) {
        window?.$message?.warning('请输入名称')
    } else if (!row.prefix && row.name.indexOf('主线') < 0) {
        //没有前缀，且片段名称中不包含‘主线’
        window?.$message?.warning('请输入桩号前缀')
    } else if (!reg.test(row.prefix) && row.name.indexOf('主线') < 0) {
        window?.$message?.warning('桩号前缀,只允许输入大写的英文字母')
    } else if (row.id) {
        const { error, code } = await bezierApi.savePartUpdate({
            ...row,
            projectId: projectId.value,
            contractId: contractId.value,
        })
        if (!error && code === 200) {
            window?.$message?.success('保存成功')
            row.isEdit = false
            queryPartList()
        }
    } else if (!row.id) {
        const { error, code, data } = await bezierApi.savePartAdd({
            ...row,
            projectId: projectId.value,
            contractId: contractId.value,
        })
        if (!error && code === 200) {
            window?.$message?.success('保存成功')
            row.isEdit = false
            alert(222)
            row.id = data.id
            partData.value.push(data)
            //queryPartList()不要刷新
        }
    }
}

//删除
const handleAdminPartDelete = (row, index) => {
    if (row.id) {
        window?.$messageBox?.alert('关联平曲线一起删除', '是否删除当前数据？', {
            showCancelButton: true,
            confirmButtonText: '确定删除',
            cancelButtonText: '取消',
            callback: (action) => {
                if (action === 'confirm') {
                    handleAdminPartDeleteSub(row, index)
                }
            },
        })
    } else {
        AdminPartTableData.value.splice(index, 1)
    }
}

const handleAdminPartDeleteSub = async (row, index) => {
    const { error, code } = await bezierApi.delPartData({
        ids: row.id,
    })
    if (!error && code === 200) {
        window?.$message?.success('删除成功')
        queryPartList()
    }

}
</script>

<style lang="scss" scoped>
@import '../../styles/gauge/bezier.scss';
</style>
