<!-- 地层维护-->
<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="98px">
            <!-- <el-form-item label="选择邻井" prop="jh">
                <el-input v-model="queryParams.jh" placeholder="请输入井号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item> -->
            <el-form-item style="margin-top: -10px;">
                <!-- <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
            </el-form-item>
        </el-form>
        <el-table border :data="jsbbList" @selection-change="handleSelectionChange"
            style="width: 100%; flex: 1;margin-top: -5px" height="100%">
            <el-table-column label="井号" align="center" prop="jh" min-width="90" show-overflow-tooltip />
            <el-table-column label="地质时代名称" align="center" prop="dzsdmc" min-width="120" show-overflow-tooltip />
            <el-table-column label="层位" align="center" prop="cw" min-width="90" show-overflow-tooltip />
            <!-- <el-table-column label="设计底界深度" align="center" prop="sjdjsd" min-width="110" show-overflow-tooltip /> -->
            <el-table-column label="设计底界深度m" align="center" prop="sjdjsd2" min-width="110" show-overflow-tooltip />
            <!-- <el-table-column label="设计厚度" align="center" prop="sjhd" min-width="90" show-overflow-tooltip /> -->
            <el-table-column label="设计厚度m" align="center" prop="sjhd2" min-width="90" show-overflow-tooltip />
            <el-table-column label="主要岩性描述" align="center" prop="zyyxms" min-width="130" show-overflow-tooltip />
            <el-table-column label="地层走向" align="center" prop="dczx" min-width="90" show-overflow-tooltip />
            <el-table-column label="地层倾角" align="center" prop="dcqj" min-width="90" show-overflow-tooltip />
            <el-table-column label="备注" align="center" prop="bz" min-width="150" show-overflow-tooltip />
            <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit"
                        @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete"
                        @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" :page-sizes="[20, 50, 100]" />
        <!-- 添加或修改地层数据对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
            <el-form ref="mainForm" :model="form" :rules="rules" label-width="120px">
                <el-row :gutter="10">
                    <el-col :span="12">
                        <el-form-item label="井号" prop="jh">
                            <el-input v-model="form.jh" placeholder="请输入井号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="地质时代名称" prop="dzsdmc">
                            <el-input v-model="form.dzsdmc" placeholder="请输入地质时代名称" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10">
                    <el-col :span="12">
                        <el-form-item label="层位" prop="cw">
                            <el-input v-if="!showCascader" v-model="form.cw" readonly style="width: 100%;">
                                <template slot="append">
                                    <el-button @click="openCascader">选择</el-button>
                                </template>
                            </el-input>
                            <el-cascader v-if="showCascader" ref="cascader" v-model="cascaderValue" @change="change"
                                @click="handleCascaderClick" :options="mdcOptions" :props="mdc" placeholder="请选择层位"
                                style="width: 200px;" popper-class="cascader-popper">
                            </el-cascader>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="设计底界深度m" prop="sjdjsd2">
                            <el-input v-model="form.sjdjsd2" placeholder="请输入实际底界深度"
                                @input="handleDecimalInput(form.sjdjsd2, 'sjdjsd2')" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10">
                    <el-col :span="12">
                        <el-form-item label="设计厚度m" prop="sjhd2">
                            <el-input v-model="form.sjhd2" placeholder="请输入实际厚度"
                                @input="handleDecimalInput(form.sjhd2, 'sjhd2')" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="主要岩性描述" prop="zyyxms">
                            <el-input v-model="form.zyyxms" placeholder="请输入主要岩性描述" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10">
                    <el-col :span="12">
                        <el-form-item label="地层走向" prop="dczx">
                            <el-input v-model="form.dczx" placeholder="请输入地层走向" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="地层倾角" prop="dcqj">
                            <el-input v-model="form.dcqj" placeholder="请输入地层倾角" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="备注" prop="bz">
                            <el-input v-model="form.bz" placeholder="请输入备注" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div class="param-box">
                <el-form ref="paramForm" :model="form" :rules="rules" label-width="60px">
                    <!-- 横波时差 -->
                    <el-row :gutter="10">
                        <el-col :span="3" class="title-col">
                            <el-form-item>
                                <template slot="label">横波时差</template>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A1" prop="hbsca1">
                                <el-input v-model="form.hbsca1" placeholder="A1" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A2" prop="hbsca2">
                                <el-input v-model="form.hbsca2" placeholder="A2" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A3" prop="hbsca3">
                                <el-input v-model="form.hbsca3" placeholder="A3" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- 泥质含量 -->
                    <el-row :gutter="10">
                        <el-col :span="3" class="title-col">
                            <el-form-item>
                                <template slot="label">泥质含量</template>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A1" prop="nzhla1">
                                <el-input v-model="form.nzhla1" placeholder="A1" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- 地层密度 -->
                    <el-row :gutter="10">
                        <el-col :span="3" class="title-col">
                            <el-form-item>
                                <template slot="label">地层密度</template>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A1" prop="dcmda1">
                                <el-input v-model="form.dcmda1" placeholder="A1" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A2" prop="dcmda2">
                                <el-input v-model="form.dcmda2" placeholder="A2" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- 泊松比 -->
                    <el-row :gutter="10">
                        <el-col :span="3" class="title-col">
                            <el-form-item>
                                <template slot="label">泊松比</template>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A1" prop="bsba1">
                                <el-input v-model="form.bsba1" placeholder="A1" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="B1" prop="bsbb1">
                                <el-input v-model="form.bsbb1" placeholder="B1" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="B2" prop="bsbb2">
                                <el-input v-model="form.bsbb2" placeholder="B2" />
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <!-- 弹性模量 -->
                    <el-row :gutter="10">
                        <el-col :span="3" class="title-col">
                            <el-form-item>
                                <template slot="label">弹性模量</template>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="7">
                            <el-form-item label="A1" prop="txmla1">
                                <el-input v-model="form.txmla1" placeholder="A1" />
                            </el-form-item>
                        </el-col> -->
                        <el-col :span="7">
                            <el-form-item label="B1" prop="txmlb1">
                                <el-input v-model="form.txmlb1" placeholder="B1" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="B2" prop="txmlb2">
                                <el-input v-model="form.txmlb2" placeholder="B2" />
                            </el-form-item>
                        </el-col>
                    </el-row>



                    <!-- 可钻性极值 -->
                    <el-row :gutter="10">
                        <el-col :span="3" class="title-col">
                            <el-form-item>
                                <template slot="label">可钻性极值</template>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A1" prop="kzxjza1">
                                <el-input v-model="form.kzxjza1" placeholder="A1" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A2" prop="kzxjza2">
                                <el-input v-model="form.kzxjza2" placeholder="A2" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A3" prop="kzxjza3">
                                <el-input v-model="form.kzxjza3" placeholder="A3" />
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="4">
                        <el-form-item label="B1" prop="kzxjzb1">
                            <el-input v-model="form.kzxjzb1" placeholder="B1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B2" prop="kzxjzb2">
                            <el-input v-model="form.kzxjzb2" placeholder="B2" />
                        </el-form-item>
                    </el-col> -->
                    </el-row>

                    <!-- 研磨系数 -->
                    <el-row :gutter="10">
                        <el-col :span="3" class="title-col">
                            <el-form-item>
                                <template slot="label">研磨系数</template>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A1" prop="ymxsa1">
                                <el-input v-model="form.ymxsa1" placeholder="A1" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A2" prop="ymxsa2">
                                <el-input v-model="form.ymxsa2" placeholder="A2" />
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="4">
                        <el-form-item label="A3" prop="ymxsa3">
                            <el-input v-model="form.ymxsa3" placeholder="A3" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B1" prop="ymxsb1">
                            <el-input v-model="form.ymxsb1" placeholder="B1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B2" prop="ymxsb2">
                            <el-input v-model="form.ymxsb2" placeholder="B2" />
                        </el-form-item>
                    </el-col> -->
                    </el-row>

                    <!-- 粘聚力 -->
                    <el-row :gutter="20">
                        <el-col :span="3" class="title-col">
                            <el-form-item>
                                <template slot="label">粘聚力</template>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A1" prop="njla1">
                                <el-input v-model="form.njla1" placeholder="A1" />
                            </el-form-item>
                        </el-col>

                    </el-row>


                    <!-- 岩石抗拉强度 -->
                    <!-- <el-row :gutter="20">
                    <el-col :span="4">
                        <el-form-item>
                            <span
                                style="line-height: 32px; font-weight: bold; text-align: right; display: block;">岩石抗拉强度</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="A1" prop="ysklqda1">
                            <el-input v-model="form.ysklqda1" placeholder="A1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="A2" prop="ysklqda2">
                            <el-input v-model="form.ysklqda2" placeholder="A2" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="A3" prop="ysklqda3">
                            <el-input v-model="form.ysklqda3" placeholder="A3" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B1" prop="ysklqdb1">
                            <el-input v-model="form.ysklqdb1" placeholder="B1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B2" prop="ysklqdb2">
                            <el-input v-model="form.ysklqdb2" placeholder="B2" />
                        </el-form-item>
                    </el-col>
                </el-row> -->

                    <!-- 内摩擦角 -->
                    <!-- <el-row :gutter="20">
                    <el-col :span="4">
                        <el-form-item>
                            <span
                                style="line-height: 32px; font-weight: bold; text-align: right; display: block;">内摩擦角</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="A1" prop="nmcja1">
                            <el-input v-model="form.nmcja1" placeholder="A1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="A2" prop="nmcja2">
                            <el-input v-model="form.nmcja2" placeholder="A2" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="A3" prop="nmcja3">
                            <el-input v-model="form.nmcja3" placeholder="A3" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B1" prop="nmcjb1">
                            <el-input v-model="form.nmcjb1" placeholder="B1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B2" prop="nmcjb2">
                            <el-input v-model="form.nmcjb2" placeholder="B2" />
                        </el-form-item>
                    </el-col>
                </el-row> -->

                    <!-- 均质性系数 -->
                    <el-row :gutter="10">
                        <el-col :span="3" class="title-col">
                            <el-form-item>
                                <template slot="label">均质性系数</template>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A1" prop="cjzsa1">
                                <el-input v-model="form.cjzsa1" placeholder="A1" />
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="4">
                        <el-form-item label="A2" prop="cjzsa2">
                            <el-input v-model="form.cjzsa2" placeholder="A2" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="A3" prop="cjzsa3">
                            <el-input v-model="form.cjzsa3" placeholder="A3" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B1" prop="cjzsb1">
                            <el-input v-model="form.cjzsb1" placeholder="B1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B2" prop="cjzsb2">
                            <el-input v-model="form.cjzsb2" placeholder="B2" />
                        </el-form-item>
                    </el-col> -->
                    </el-row>
                    <!-- 钻头选型指数 A1、A2 -->
                    <el-row :gutter="10">
                        <el-col :span="3" class="title-col">
                            <el-form-item>
                                <template slot="label">钻头选型指数</template>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A1" prop="ztxxzsa1">
                                <el-input v-model="form.ztxxzsa1" placeholder="A1" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="A2" prop="ztxxzsa2">
                                <el-input v-model="form.ztxxzsa2" placeholder="A2" />
                            </el-form-item>
                        </el-col>
                    </el-row>


                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listSjJsbb, getSjJsbb, delSjJsbb, addSjJsbb, updateSjJsbb } from "@/api/system/sjJsbb";
import { getMdcByQkid } from "@/api/scientificDrill/schemeOptimization";

export default {
    name: "FormationMaintenance",
    props: {
        wellInfo: {
            type: Object,
            default: () => ({})
        }
    },
    data() {
        return {
            loading: true,
            ids: [],
            single: true,
            multiple: true,
            showSearch: true,
            total: 0,
            jsbbList: [],
            title: "",
            open: false,
            isAdd: false,
            showCascader: false,
            mdcOptions: [],
            oilOptions: [],
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                jh: "",
                cw: null,
                sjdjsd: null,
                sjdjsd2: null,
                sjhd: null,
                sjhd2: null,
                zyyxms: null,
                dczx: null,
                dcqj: null,
                bz: null
            },
            form: {},
            mdc: {
                lazy: true,
                lazyLoad: this.loadTreeNode,
                checkStrictly: true,
                multiple: false,
                expandTrigger: 'click',
                emitPath: true,
                maxLevel: 5
            },
            rules: {
                sjdjsd2: [
                    { required: true, message: "设计底界深度不能为空", trigger: "blur" }
                ],
                jh: [
                    { required: true, message: "井号不能为空", trigger: "blur" }
                ],
                sjhd2: [
                    { required: true, message: "设计厚度不能为空", trigger: "blur" }
                ],
                cw: [
                    { required: true, message: "层位不能为空", trigger: "blur" }
                ],
                // 横波时差
                hbsca1: [{ required: true, message: "A1不能为空", trigger: "blur" }],
                hbsca2: [{ required: true, message: "A2不能为空", trigger: "blur" }],
                hbsca3: [{ required: true, message: "A3不能为空", trigger: "blur" }],
                // 泥质含量
                nzhla1: [{ required: true, message: "A1不能为空", trigger: "blur" }],
                // 地层密度
                dcmda1: [{ required: true, message: "A1不能为空", trigger: "blur" }],
                dcmda2: [{ required: true, message: "A2不能为空", trigger: "blur" }],
                // 泊松比
                bsba1: [{ required: true, message: "A1不能为空", trigger: "blur" }],
                bsbb1: [{ required: true, message: "B1不能为空", trigger: "blur" }],
                bsbb2: [{ required: true, message: "B2不能为空", trigger: "blur" }],
                // 弹性模量
                txmla1: [{ required: true, message: "A1不能为空", trigger: "blur" }],
                txmlb1: [{ required: true, message: "B1不能为空", trigger: "blur" }],
                txmlb2: [{ required: true, message: "B2不能为空", trigger: "blur" }],
                // 弹性模量
                txmla1: [{ required: true, message: "A1不能为空", trigger: "blur" }],
                txmlb1: [{ required: true, message: "B1不能为空", trigger: "blur" }],
                txmlb2: [{ required: true, message: "B2不能为空", trigger: "blur" }],
                // 可钻性极值
                kzxjza1: [{ required: true, message: "A1不能为空", trigger: "blur" }],
                kzxjza2: [{ required: true, message: "A2不能为空", trigger: "blur" }],
                kzxjza3: [{ required: true, message: "A3不能为空", trigger: "blur" }],
                // 研磨系数
                ymxsa1: [{ required: true, message: "A1不能为空", trigger: "blur" }],
                ymxsa2: [{ required: true, message: "A2不能为空", trigger: "blur" }],
                // 粘聚力
                njla1: [{ required: true, message: "A1不能为空", trigger: "blur" }],
                // 均质性系数
                cjzsa1: [{ required: true, message: "A1不能为空", trigger: "blur" }],
                // 钻头选型指数
                ztxxzsa1: [{ required: true, message: "A1不能为空", trigger: "blur" }],
                ztxxzsa2: [{ required: true, message: "A2不能为空", trigger: "blur" }],
            },
            cascaderValue: []
        };
    },
    created() {
        this.initMdcOptions();
    },
    watch: {
        'wellInfo.jh': {
            immediate: true,
            handler(newJh) {
                if (newJh) {
                    this.queryParams.jh = newJh;
                    this.getList();
                }
            }
        }
    },
    methods: {
        initMdcOptions() {
            const params = { dclevel: 1, pid: null };
            getMdcByQkid(params).then(response => {
                const options = response.data.map(item => ({
                    value: item.id,
                    label: item.mc,
                    leaf: false,
                    level: 1
                }));
                this.mdcOptions = options;
            }).catch(() => {
                this.mdcOptions = [];
            });
        },
        getList() {
            this.loading = true;
            listSjJsbb(this.queryParams).then(response => {
                this.jsbbList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        cancel() {
            this.open = false;
            this.reset();
        },
        reset() {
            this.form = {
                jh: this.queryParams.jh,
                dzsdmc: null,
                cw: null,
                cwid: null,
                mdcid: null,
                sjdjsd: 1,
                sjdjsd2: 1,
                sjhd: 1,
                sjhd2: 1,
                zyyxms: null,
                dczx: null,
                dcqj: null,
                bz: null,
                // 横波时差
                hbsca1: 0.6,
                hbsca2: -0.0014,
                hbsca3: 0.25,
                // 泥质含量
                nzhla1: 2,
                // 地层密度
                dcmda1: 0.32,
                dcmda2: 0.25,
                // 泊松比
                bsba1: 2,
                bsbb1: 0.9,
                bsbb2: 0.035,
                // 弹性模量
                // txmla1: 1,
                txmlb1: 0.9,
                txmlb2: 0.035,
                // 可钻性极值
                kzxjza1: 1.1908,
                kzxjza2: 0.0003,
                kzxjza3: -0.1427,
                // 研磨系数
                ymxsa1: 0.75,
                ymxsa2: 1.15,
                // 粘聚力
                njla1: 0.000003326,
                // 均质性系数
                cjzsa1: 0.75,
                // 钻头选型指数
                ztxxzsa1: 1,
                ztxxzsa2: 1.25
            };
            // 重置两块表单的校验状态
            this.resetForm && this.resetForm("mainForm");
            this.resetForm && this.resetForm("paramForm");
            this.showCascader = false;
            this.cascaderValue = [];
        },
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        resetQuery() {
            this.resetForm("queryForm");
            // 保留井号（默认取父页传递的井号）
            this.queryParams.jh = this.wellInfo.jh || this.queryParams.jh;
            this.handleQuery();
        },
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.jh)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        handleAdd() {
            this.reset();
            this.open = true;
            this.isAdd = true;
            this.title = "添加地层数据";
        },
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            console.log(row, '22222');
            this.isAdd = false;
            getSjJsbb(id).then(response => {
                this.form = response.data;
                if (this.form.mdcid) {
                    this.cascaderValue = [this.form.mdcid];
                }
                this.open = true;
                this.title = "修改地层数据";
            });
        },

        submitForm() {
            const validateMain = () => new Promise(resolve => {
                if (this.$refs.mainForm) {
                    this.$refs.mainForm.validate(valid => resolve(valid));
                } else {
                    resolve(true);
                }
            });
            const validateParam = () => new Promise(resolve => {
                if (this.$refs.paramForm) {
                    this.$refs.paramForm.validate(valid => resolve(valid));
                } else {
                    resolve(true);
                }
            });
            Promise.all([validateMain(), validateParam()]).then(([mainOk, paramOk]) => {
                if (mainOk && paramOk) {
                    if (!this.isAdd) {
                        updateSjJsbb(this.form).then(() => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addSjJsbb(this.form).then(() => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        handleDelete(row) {
            const id = row.id || this.ids;
            const name = row.jh || id;
            this.$modal.confirm('是否确认删除编号为"' + name + '"的数据项？').then(() => {
                return delSjJsbb(id);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        handleDecimalInput(value, key) {
            if (value === '') {
                this.form[key] = '';
                return;
            }
            value = value.toString().replace(/[^\d.]/g, '');
            const parts = value.split('.');
            if (parts.length > 2) {
                value = parts[0] + '.' + parts.slice(1).join('');
            }
            if (parts.length === 2 && parts[1].length > 2) {
                value = parts[0] + '.' + parts[1].slice(0, 2);
            }
            this.form[key] = value;
        },
        openCascader() {
            this.showCascader = true;
            this.$nextTick(() => {
                if (this.$refs.cascader && this.form.mdcid) {
                    this.cascaderValue = [this.form.mdcid];
                }
            });
        },
        handleCascaderClick() { },
        change(val) {
            if (val && val.length > 0) {
                const selectedValue = val[val.length - 1];
                this.form.mdcid = selectedValue;
                this.getSelectedLabel(val, selectedValue);
                setTimeout(() => {
                    this.showCascader = false;
                    this.$forceUpdate();
                }, 100);
            } else {
                this.showCascader = false;
            }
        },
        async getSelectedLabel(path, selectedValue) {
            for (let i = path.length - 1; i >= 0; i--) {
                const currentValue = path[i];
                const level = i + 1;
                try {
                    const params = { dclevel: level, pid: i === 0 ? null : path[i - 1] };
                    const response = await getMdcByQkid(params);
                    const nodes = response.data;
                    const selectedNode = nodes.find(item => item.id === currentValue);
                    if (selectedNode) {
                        this.form.cw = selectedNode.mc;
                        this.form.cwid = selectedNode.id;
                        this.buildDzsdmc(path, selectedNode);
                        this.$set(this.form, 'cw', selectedNode.mc);
                        this.$set(this.form, 'cwid', selectedNode.id);
                        this.$set(this.form, 'mdcid', selectedValue);
                        break;
                    }
                } catch (error) { }
            }
        },
        async buildDzsdmc(path, selectedNode) {
            if (path.length >= 2) {
                try {
                    const eraPath = [];
                    for (let i = 1; i < path.length - 1; i++) {
                        const currentValue = path[i];
                        const level = i + 1;
                        const pid = i === 1 ? path[0] : path[i - 1];
                        const params = { dclevel: level, pid: pid };
                        const response = await getMdcByQkid(params);
                        const nodes = response.data;
                        const currentNode = nodes.find(item => item.id === currentValue);
                        if (currentNode) {
                            eraPath.push(currentNode.mc);
                        }
                    }
                    const dzsdmc = eraPath.join('/');
                    this.form.dzsdmc = dzsdmc;
                    this.$set(this.form, 'dzsdmc', dzsdmc);
                } catch (error) { }
            }
        },
        loadTreeNode(node, resolve) {
            if (node.level >= 5) {
                resolve([]);
                return;
            }
            const params = { dclevel: node.level + 1, pid: node.value };
            getMdcByQkid(params).then(response => {
                const oilOptions = response.data;
                if (Array.isArray(oilOptions) && oilOptions.length > 0) {
                    const nodes = oilOptions.map(item => ({
                        value: item.id,
                        label: item.mc,
                        leaf: node.level + 1 >= 5,
                        level: node.level + 1
                    }));
                    resolve(nodes);
                } else {
                    resolve([]);
                }
            }).catch(() => {
                resolve([]);
            });
        }
    }
}
</script>

<style lang="scss" scoped>
.app-container {
    height: calc(100vh - 180px);
    display: flex;
    flex-direction: column;
    padding: 5px;
}

::v-deep .el-table__cell>.cell {
    font-weight: normal;
}

::v-deep .el-table--medium .el-table__cell {
    padding: 3px 0 !important;
}

.pagination-container {
    padding: 3px !important;
    margin: 0;
}

/* 参量标题列右对齐，与表单label对齐 */
::v-deep .title-col {
    /* 抵消 param-box 左内边距，使标题与外层表单右缘对齐 */
    margin-left: -13px;
}

/* 仅标题使用与外层一致的标签宽度 */
::v-deep .title-col .el-form-item__label {
    width: 110px !important;
    padding-right: 12px;
    /* 与 Element UI 默认一致 */
    white-space: nowrap;
}

::v-deep .title-col .el-form-item__content {
    display: flex;
    justify-content: flex-end;
    text-align: right;
    padding-right: 4px;
}

/* 参量区域外框样式 */
.param-box {
    border: 1px solid #e4e7ed;
    border-radius: 6px;
    background: #fafafa;
    padding: 12px 16px;
    margin-top: 8px;
}

::v-deep .cascader-popper {
    z-index: 3000 !important;
}

::v-deep .el-cascader {
    width: 100%;
}

::v-deep .el-cascader__dropdown {
    z-index: 3000 !important;
}

// 减小表单项之间的间距
::v-deep .el-form-item {
    margin-bottom: 10px;
}

// 减小对话框内的行间距
::v-deep .el-dialog__body {
    padding: 10px 20px;
}

::v-deep .el-dialog .el-row {
    margin-bottom: 5px;
}
</style>
