<!-- 地层 -->
<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 label="层位" prop="cw">
        <el-input v-model="queryParams.cw" placeholder="请输入层位" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <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"
          v-hasPermi="['system:jsbb:add']">新增</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:jsbb:export']">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table border v-loading="loading" :data="jsbbList" @selection-change="handleSelectionChange"
      style="width: 100%; flex: 1;" height="100%">
      <el-table-column label="井号" align="center" prop="jh" min-width="80" show-overflow-tooltip />
      <el-table-column label="地质时代名称" align="center" prop="dzsdmc" min-width="130" show-overflow-tooltip />
      <el-table-column label="层位" align="center" prop="cw" min-width="80" show-overflow-tooltip />
      <el-table-column label="设计底界深度m" align="center" prop="sjdjsd" min-width="90" show-overflow-tooltip />
      <el-table-column label="实际底界深度" align="center" prop="sjdjsd2" min-width="90" show-overflow-tooltip />
      <el-table-column label="设计厚度m" align="center" prop="sjhd" min-width="90" show-overflow-tooltip />
      <el-table-column label="实际厚度" 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="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:jsbb:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-setting" @click="handleCalcParams(scope.row)"
            v-hasPermi="['system:jsbb:edit']">计算参数</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:jsbb:remove']">删除</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="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <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="cw">
              <el-input v-model="form.cw" placeholder="请输入层位" />
            </el-form-item>
          </el-col> -->
          <!-- </el-row>
        <el-row :gutter="20"> -->
          <!-- <el-col :span="12">
            <el-form-item label="地质时代名称" prop="dzsdmc">
              <el-input v-model="form.dzsdmc" placeholder="选择目的层后自动生成" readonly />
            </el-form-item>
          </el-col> -->
          <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>
              <!-- 使用v-if来条件渲染el-cascader -->
              <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-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="设计底界深度m" prop="sjdjsd">
              <el-input v-model="form.sjdjsd" placeholder="请输入设计底界深度"
                @input="handleDecimalInput(form.sjdjsd, 'sjdjsd')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际底界深度" 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="sjhd">
              <el-input v-model="form.sjhd" placeholder="请输入设计厚度" @input="handleDecimalInput(form.sjhd, 'sjhd')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际厚度" prop="sjhd2">
              <el-input v-model="form.sjhd2" placeholder="请输入实际厚度" @input="handleDecimalInput(form.sjhd2, 'sjhd2')" />
            </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 :gutter="10">
          <el-col :span="12">
            <el-form-item label="主要岩性描述" prop="zyyxms">
              <el-input v-model="form.zyyxms" placeholder="请输入主要岩性描述" />
            </el-form-item>
          </el-col>
          <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 slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 计算参数弹窗 -->
    <el-dialog title="计算参数" :visible.sync="calcParamsOpen" width="60%" append-to-body>
      <div class="param-box">
        <el-form ref="calcParamsForm" :model="calcParamsForm" :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="calcParamsForm.hbsca1" placeholder="A1" />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="A2" prop="hbsca2">
                <el-input v-model="calcParamsForm.hbsca2" placeholder="A2" />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="A3" prop="hbsca3">
                <el-input v-model="calcParamsForm.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="calcParamsForm.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="calcParamsForm.dcmda1" placeholder="A1" />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="A2" prop="dcmda2">
                <el-input v-model="calcParamsForm.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="calcParamsForm.bsba1" placeholder="A1" />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="B1" prop="bsbb1">
                <el-input v-model="calcParamsForm.bsbb1" placeholder="B1" />
              </el-form-item>
            </el-col>

            <el-col :span="7">
              <el-form-item label="B2" prop="bsbb2">
                <el-input v-model="calcParamsForm.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="B1" prop="txmlb1">
                <el-input v-model="calcParamsForm.txmlb1" placeholder="B1" />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="B2" prop="txmlb2">
                <el-input v-model="calcParamsForm.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="calcParamsForm.kzxjza1" placeholder="A1" />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="A2" prop="kzxjza2">
                <el-input v-model="calcParamsForm.kzxjza2" placeholder="A2" />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="A3" prop="kzxjza3">
                <el-input v-model="calcParamsForm.kzxjza3" placeholder="A3" />
              </el-form-item>
            </el-col>
            <!-- <el-col :span="4">
                        <el-form-item label="B1" prop="kzxjzb1">
                            <el-input v-model="calcParamsForm.kzxjzb1" placeholder="B1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B2" prop="kzxjzb2">
                            <el-input v-model="calcParamsForm.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="calcParamsForm.ymxsa1" placeholder="A1" />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="A2" prop="ymxsa2">
                <el-input v-model="calcParamsForm.ymxsa2" placeholder="A2" />
              </el-form-item>
            </el-col>
            <!-- <el-col :span="4">
                        <el-form-item label="A3" prop="ymxsa3">
                            <el-input v-model="calcParamsForm.ymxsa3" placeholder="A3" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B1" prop="ymxsb1">
                            <el-input v-model="calcParamsForm.ymxsb1" placeholder="B1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B2" prop="ymxsb2">
                            <el-input v-model="calcParamsForm.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="calcParamsForm.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="calcParamsForm.ysklqda1" placeholder="A1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="A2" prop="ysklqda2">
                            <el-input v-model="calcParamsForm.ysklqda2" placeholder="A2" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="A3" prop="ysklqda3">
                            <el-input v-model="calcParamsForm.ysklqda3" placeholder="A3" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B1" prop="ysklqdb1">
                            <el-input v-model="calcParamsForm.ysklqdb1" placeholder="B1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B2" prop="ysklqdb2">
                            <el-input v-model="calcParamsForm.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="calcParamsForm.nmcja1" placeholder="A1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="A2" prop="nmcja2">
                            <el-input v-model="calcParamsForm.nmcja2" placeholder="A2" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="A3" prop="nmcja3">
                            <el-input v-model="calcParamsForm.nmcja3" placeholder="A3" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B1" prop="nmcjb1">
                            <el-input v-model="calcParamsForm.nmcjb1" placeholder="B1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B2" prop="nmcjb2">
                            <el-input v-model="calcParamsForm.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="calcParamsForm.cjzsa1" placeholder="A1" />
              </el-form-item>
            </el-col>
            <!-- <el-col :span="4">
                        <el-form-item label="A2" prop="cjzsa2">
                            <el-input v-model="calcParamsForm.cjzsa2" placeholder="A2" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="A3" prop="cjzsa3">
                            <el-input v-model="calcParamsForm.cjzsa3" placeholder="A3" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B1" prop="cjzsb1">
                            <el-input v-model="calcParamsForm.cjzsb1" placeholder="B1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="B2" prop="cjzsb2">
                            <el-input v-model="calcParamsForm.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="calcParamsForm.ztxxzsa1" placeholder="A1" />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="A2" prop="ztxxzsa2">
                <el-input v-model="calcParamsForm.ztxxzsa2" placeholder="A2" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCalcParams">确 定</el-button>
        <el-button @click="cancelCalcParams">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listJsbb, getJsbb, delJsbb, addJsbb, updateJsbb } from "@/api/system/jsbb";
import { getMdcByQkid } from "@/api/scientificDrill/schemeOptimization";

export default {
  name: "Jsbb",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 地质简介地层及岩性描述数据表格数据
      jsbbList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示计算参数弹出层
      calcParamsOpen: false,
      // 是否为新增操作
      isAdd: false,
      // 当前操作的行数据
      currentRow: null,
      // 计算参数表单
      calcParamsForm: {},
      // 目的层相关
      showCascader: false,
      mdcOptions: [],
      oilOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        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" }],
        // 可钻性极值
        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: [] // 用于绑定 el-cascader 的 v-model
    };
  },
  created() {
    this.getList();
    this.initMdcOptions();
  },
  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(error => {
        console.error('初始化目的层选项失败:', error);
        this.mdcOptions = [];
      });
    },
    /** 查询地质简介地层及岩性描述数据列表 */
    getList() {
      this.loading = true;
      listJsbb(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: null,
        dzsdmc: null,
        cw: null,
        cwid: null, // 层位ID
        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("form");
      this.showCascader = false;
      this.cascaderValue = []; // 重置 cascaderValue
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      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 jh = row.jh || this.ids;
      const sjdjsd = row.sjdjsd;
      this.isAdd = false;
      getJsbb({ jh: jh, sjdjsd: sjdjsd }).then(response => {
        this.form = response.data;
        // 如果有目的层ID，需要设置级联选择器的值
        if (this.form.mdcid) {
          this.cascaderValue = [this.form.mdcid];
        }
        this.open = true;
        this.title = "修改地质简介地层及岩性描述数据";
      });
    },
    /** 计算参数按钮操作 */
    handleCalcParams(row) {
      this.currentRow = row;
      const jh = row.jh || this.ids;
      const sjdjsd = row.sjdjsd;
      getJsbb({ jh: jh, sjdjsd: sjdjsd }).then(response => {
        // 初始化计算参数表单，只包含计算参数相关的字段
        this.calcParamsForm = {
          jh: response.data.jh,
          sjdjsd: response.data.sjdjsd,
          // 横波时差
          hbsca1: response.data.hbsca1 || 0.6,
          hbsca2: response.data.hbsca2 || -0.0014,
          hbsca3: response.data.hbsca3 || 0.25,
          // 泥质含量
          nzhla1: response.data.nzhla1 || 2,
          // 地层密度
          dcmda1: response.data.dcmda1 || 0.32,
          dcmda2: response.data.dcmda2 || 0.25,
          // 泊松比
          bsba1: response.data.bsba1 || 2,
          bsbb1: response.data.bsbb1 || 0.9,
          bsbb2: response.data.bsbb2 || 0.035,
          // 弹性模量
          txmlb1: response.data.txmlb1 || 0.9,
          txmlb2: response.data.txmlb2 || 0.035,
          // 可钻性极值
          kzxjza1: response.data.kzxjza1 || 1.1908,
          kzxjza2: response.data.kzxjza2 || 0.0003,
          kzxjza3: response.data.kzxjza3 || -0.1427,
          // 研磨系数
          ymxsa1: response.data.ymxsa1 || 0.75,
          ymxsa2: response.data.ymxsa2 || 1.15,
          // 粘聚力
          njla1: response.data.njla1 || 0.000003326,
          // 均质性系数
          cjzsa1: response.data.cjzsa1 || 0.75,
          // 钻头选型指数
          ztxxzsa1: response.data.ztxxzsa1 || 1,
          ztxxzsa2: response.data.ztxxzsa2 || 1.25
        };
        this.calcParamsOpen = true;
      });
    },
    /** 提交计算参数 */
    submitCalcParams() {
      this.$refs.calcParamsForm.validate(valid => {
        if (valid) {
          // 合并计算参数到完整表单数据
          const updateData = {
            ...this.calcParamsForm
          };
          updateJsbb(updateData).then(response => {
            this.$modal.msgSuccess("计算参数保存成功");
            this.calcParamsOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 取消计算参数弹窗 */
    cancelCalcParams() {
      this.calcParamsOpen = false;
      this.calcParamsForm = {};
      this.currentRow = null;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (!this.isAdd) {
            updateJsbb(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addJsbb(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jhs = row.jh || this.ids;
      const sjdjsd = row.sjdjsd;
      this.$modal.confirm('是否确认删除地质简介地层及岩性描述数据编号为"' + jhs + '"的数据项？').then(function () {
        return delJsbb({ jh: jhs, sjdjsd: sjdjsd });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/jsbb/export', {
        ...this.queryParams
      }, `jsbb_${new Date().getTime()}.xlsx`)
    },
    /** 限制只能输入数字且保留两位小数 */
    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) {
          console.log('级联选择器已初始化');
          // 设置初始值
          if (this.form.mdcid) {
            this.cascaderValue = [this.form.mdcid];
          }
        }
      });
    },
    // 处理级联选择器点击
    handleCascaderClick() {
      console.log('级联选择器被点击');
    },
    change(val) {
      console.log('选择变化:', val); // 调试信息

      if (val && val.length > 0) {
        // 获取最后选择的节点值
        const selectedValue = val[val.length - 1];

        // 设置选中层的 id 到表单中
        this.form.mdcid = selectedValue;

        // 直接通过级联选择器获取选中的标签
        this.getSelectedLabel(val, selectedValue);

        // 延迟关闭级联选择器，确保值已经设置
        setTimeout(() => {
          this.showCascader = false;
          // 强制更新视图
          this.$forceUpdate();
        }, 100);
      } else {
        this.showCascader = false;
      }
    },
    // 获取选中节点的标签
    async getSelectedLabel(path, selectedValue) {
      console.log('获取选中标签:', 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) {
            console.log('找到选中节点:', selectedNode);

            // 设置层位信息
            this.form.cw = selectedNode.mc; // 层位名称
            this.form.cwid = selectedNode.id; // 层位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) {
          console.error('获取节点信息失败:', error);
        }
      }
    },
    // 构建地质时代名称
    async buildDzsdmc(path, selectedNode) {
      console.log('构建地质时代名称:', 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]; // 父级ID

            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('/');
          console.log('构建的地质时代名称:', dzsdmc);
          this.form.dzsdmc = dzsdmc;
          this.$set(this.form, 'dzsdmc', dzsdmc);
        } catch (error) {
          console.error('构建地质时代名称失败:', error);
        }
      }
    },
    loadTreeNode(node, resolve) {
      // 检查是否已达到最大层级（5级）
      if (node.level >= 5) {
        console.log('已达到最大层级，不再加载子节点');
        resolve([]);
        return;
      }

      const params = {
        dclevel: node.level + 1,
        pid: node.value,
      };
      console.log('加载节点参数:', params); // 调试信息

      // 发起请求获取节点数据
      getMdcByQkid(params).then(response => {
        const oilOptions = response.data; // 获取到的数据
        console.log('加载节点数据:', oilOptions); // 调试信息

        if (Array.isArray(oilOptions) && oilOptions.length > 0) {
          // 将数据转换为符合 Cascader 要求的格式
          const nodes = oilOptions.map(item => ({
            value: item.id,
            label: item.mc,
            leaf: node.level + 1 >= 5, // 第五级设置为叶子节点，不允许继续展开
            level: node.level + 1 // 设置节点层级
          }));
          console.log('转换后的节点:', nodes); // 调试信息
          resolve(nodes);
        } else {
          // 如果没有数据，返回空数组
          console.log('没有找到子节点数据');
          resolve([]);
        }
      }).catch(error => {
        // 处理请求失败的情况
        console.error('加载节点数据失败:', error);
        resolve([]);
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.app-container {
  height: calc(100vh - 100px);
  display: flex;
  flex-direction: column;
  padding: 10px;
}

/* 参量标题列右对齐，与表单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 .el-table__cell>.cell {
  font-weight: normal;
}

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

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

// 级联选择器样式
::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>
