<template>
    <!-- 风机风门-->
    <div class="l_Dialog bou tablebk">
      <div class="common-layout" style="margin: 0">
        <el-aside
          width="2.125rem"
          class="L_aside L_aside1 asideg asidegbg leftbgimg1"

        >
          <div class="demo-collapse">
            <el-collapse v-model="coolactiveName1">
                <el-collapse-item name="1" class="imgneon">
                <template #title>
                  <el-icon class="iconimg Frame3" fit="contain"></el-icon>
                风机<el-icon class="header-icon"> </el-icon>
                </template> 
                <div class="asides_content">
                  <div class="jc_content tablecolor">
                    <div class="jc_padding font12">
                      <div class="xian btncolor tablefocus bmar">
                        <el-table
                          :data="fjtableData"
                          style="width: 100%"
                          :max-height="tableHeight"
                          :highlight-current-row="currentrow"
                          :row-class-name="tableRowClassName"
                          @row-click="handlerow($event)"
                          :header-cell-style="{ background: 'rgba(13, 22, 57, 0) ' }"
                        >
                        <template v-slot:empty>
                          <p>暂无数据</p>
                          </template>
                          <el-table-column prop="fanname" label="风机名称" />
                          <el-table-column prop="pname" label="选择巷道" />
                        <el-table-column prop="site" label="相对位置" />
                        <el-table-column prop="pa" >
                            <template v-slot:header>
                            <span >增压</span><br>
                            <span >(kpa)</span>
                          </template>
                        </el-table-column>
                          <el-table-column prop="onoff" label="开启状态">
                          <template #default="scope">
                            {{
                              scope.row.onoff == "0"
                                ? "否"
                                : scope.row.onoff == "1"
                                ? "是"
                                : "是"
                            }}
                          </template>
                        </el-table-column>
                        </el-table>
                        <div class="asdis_btn">
                          <div class="flex_a" >
                            <div
                              class="btns"
                              @click="fjloadclick();sdialog.fjloaddialog = true;"
                            >
                              <span class="spantext">加载</span>
                            </div>
                          </div>
                          <div class="flex_a" >
                            <div
                              class="btns"
                              @click="fjclick();sdialog.fjdialog = true;"
                            >
                              <span class="spantext">添加</span>
                            </div>
                          </div>
                          <div class="flex_a" >
                            <div class="btns" @click="xiugaiclick()">
                              <span class="spantext">修改</span>
                            </div>
                          </div>
                          <div class="flex_a">
                            <div class="btns" @click="delonclock('风机')" >
                              <span class="spantext">删除</span>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </el-collapse-item> 
            </el-collapse>
            <el-collapse v-model="coolactiveName1"  style="margin-top: 10px;">
                <el-collapse-item name="2" class="imgneon">
                <template #title>
                  <el-icon class="iconimg Frame3" fit="contain"></el-icon>
                风门<el-icon class="header-icon"> </el-icon>
                </template> 
                <div class="asides_content">
                  <div class="jc_content tablecolor">
                    <div class="jc_padding font12">
                      <div class="xian btncolor tablefocus bmar">
                        <el-table
                          :data="fmtableData"
                          style="width: 100%"
                          :max-height="tableHeight"
                          :highlight-current-row="currentrow"
                          :row-class-name="tableRowClassName"
                          @row-click="handlerow2($event)"
                          :header-cell-style="{ background: 'rgba(13, 22, 57, 0) ' }"
                        >
                        <template v-slot:empty>
                          <p>暂无数据</p>
                          </template>
                          <el-table-column prop="doorname" label="风门名称" />
                          <el-table-column prop="pname" label="选择巷道" />
                        <el-table-column prop="site" label="相对位置" />
                        <el-table-column prop="dragcoe" label="阻力系数" />
                        <el-table-column prop="onoff" label="开启状态">
                          <template #default="scope">
                            {{
                              scope.row.onoff == 0
                                ? "否"
                                : scope.row.onoff == 1
                                ? "是"
                                : "是"
                            }}
                          </template>
                        </el-table-column>
                        </el-table>
                        <div class="asdis_btn">
                          <div class="flex_a" >
                            <div
                              class="btns"   @click="fmload();sdialog.fmloaddialog=true"
                            >
                              <span class="spantext">加载</span>
                            </div>
                          </div>
                          <div class="flex_a" >
                            <div
                              class="btns"   @click="fmadd();sdialog.fmdialog=true"
                            >
                              <span class="spantext">添加</span>
                            </div>
                          </div>
                          <div class="flex_a" >
                            <div class="btns" @click="fmxiugai()">
                              <span class="spantext">修改</span>
                            </div>
                          </div>
                          <div class="flex_a">
                            <div class="btns" @click="fmdel('风门')" >
                              <span class="spantext">删除</span>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </el-collapse-item>
            </el-collapse>
          </div>
        </el-aside>
      </div>
      <!-- 风机的加载 -->
      <el-dialog
      v-model="sdialog.fjloaddialog"
      width="400"
      align-center
      :modal="false"
      :append-to-body="true"
      :z-index='1000'
      :close-on-click-modal="false"
      :fullscreen="false"
      :modal-append-to-body="false"
      modal-class="summary-dlg"
      class="dialog_class bgcolor tianjia"
      draggable
    >
      <template #header="{ titleId, titleClass }">
        <div class="my-header">
          <el-image :src="icon" fit="contain"></el-image>
          <h4 :id="titleId" :class="titleClass">加载</h4>
        </div>
      </template>
      <div class="demo-input-suffix firsttitle leftbgimg2">
        <el-table
          :row-class-name="tableRowClassName"
          :max-height="tableHeight"
          ref="tabledatafjjzref"
          type="selection"
          :data="tableDatafjjz"
          style="width: 100%"
          
          :header-cell-style="{ background: 'rgba(13, 22, 57, 0) ' }"
        >
        <template v-slot:empty>
                        <p>暂无数据</p>
                        </template>
          <el-table-column type="selection" width="55" />
          <!-- <el-table-column property="snId" label="开始节点" />
          <el-table-column property="enId" label="结束节点" /> -->
          <el-table-column type="index" label="编号" width="70" />
          <el-table-column property="fanname" label="风机名称" />
          <el-table-column property="pname" label="巷道名称"  />
          <el-table-column property="site" label="相对位置" />
          <el-table-column prop="pa" >
                            <template v-slot:header>
                            <span >增压</span><br>
                            <span >(kpa)</span>
                          </template>
                        </el-table-column>
          <el-table-column prop="onoff" label="开启状态">
                          <template #default="scope">
                            {{
                              scope.row.onoff == "0"
                                ? "否"
                                : scope.row.onoff == "1"
                                ? "是"
                                : "是"
                            }}
                          </template>
          </el-table-column>
        </el-table>

        <div class="dialog-footer footer_div l_btn">
          <div class="footerbtn flex1">
            <div class="borderimg">
              <el-button @click="sdialog.fjloaddialog = false">取消</el-button>
            </div>
          </div>
          <div class="footerbtn flex1">
            <div class="borderimg">
              <el-button
                @click="
                  fjloadsave();
                  sdialog.fjloaddialog = false;
                "
              >
                确定
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
<!-- 风机的添加 -->
<el-dialog
      v-model="sdialog.fjdialog"
      width="400"
      align-center
      :modal="false"
      :append-to-body="true"
      :z-index='1000'
      :close-on-click-modal="false"
      :fullscreen="false"
      :modal-append-to-body="false"
      modal-class="summary-dlg"
      class="dialog_class bgcolor tianjia"
      draggable
      @close='closeDialog'
    >
      <template #header="{ titleId, titleClass }">
        <div class="my-header">
          <el-image :src="icon" fit="contain"></el-image>
          <h4 :id="titleId" :class="titleClass">{{ zhtext }}</h4>
        </div>
      </template>
      <el-form >
        <el-form-item label="编码" :label-width="formLabelWidth6">
            <el-input v-model="fjobj.fancode"  maxlength="18"  @input="fjobj.fancode = fjobj.fancode.replace(/[\u4e00-\u9fa5\s]/g, '')"    class="w-50 m-2" placeholder="请输入" />
        </el-form-item>
      <el-form-item label="风机名称" :label-width="formLabelWidth6">
            <el-input v-model="fjobj.fanname"  maxlength="18" oninput ="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/g,'')"  class="w-50 m-2" placeholder="请输入" />
        </el-form-item>
      <div class="demo-input-suffix firsttitle magintop">
        <el-form-item label="巷道名称" :label-width="formLabelWidth6">
          <el-input
            v-model="source.selectstr"
            disabled
            class="w-50 m-2"
            placeholder="巷道名称"
          />
        </el-form-item>
        <div class="btn2">
          <el-button
            type="primary"
            @click="
              fireclick('fj');
              sdialog.dialogVisiblenode = true;
            "
            >选择巷道</el-button
          >
        </div>
        <div class="btn2" style="padding-left: 10px">
          <el-button type="primary" @click="Dclick('fj')">3D巷道选择</el-button>
        </div>
      </div>
      <div class="input">
        <el-form-item label="相对位置" :label-width="formLabelWidth6">
          <el-input-number
            v-model="source.site"
            :min="0"
            :max="1"
            :precision="2"
            :step="0.1"
            controls-position="right"
            class="w-50 m-2"
            placeholder="相对位置"
            @change="inputnuberChange"
          />
        </el-form-item>
      </div>
      <div style="display: flex;" >
                  <span class="switchppm">开启状态</span>
                <el-switch 
                v-model="fjobj.onoff"
                inline-prompt
                active-text="是"
                inactive-text="否"
                :active-value="1"
                :inactive-value="0"
                @change="handleSwitchChange"
              />
            </div>
      <el-form-item label="增压(kpa)" :label-width="formLabelWidth6">
            <el-input v-model="fjobj.pa"  maxlength="10" oninput ="value=value.replace(/[^0-9.]/g,'')" class="w-50 m-2"  placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div class="dialog-footer footer_div l_btn">
        <div class="footerbtn flex1">
          <div class="borderimg">
            <el-button @click="closeDialog(); sdialog.fjdialog=false">取消</el-button>
          </div>
        </div>
        <div class="footerbtn flex1">
          <div class="borderimg">
            <el-button
              :disabled="isSubmitting"
              @click="addrequest(); "
            >
              确定
            </el-button>
          </div>
        </div>
      </div>
    </el-dialog>
    <!-- 风门的加载 -->
    <el-dialog
      v-model="sdialog.fmloaddialog"
      width="400"
      align-center
      :modal="false"
      :append-to-body="true"
      :z-index='1000'
      :close-on-click-modal="false"
      :fullscreen="false"
      :modal-append-to-body="false"
      modal-class="summary-dlg"
      class="dialog_class bgcolor tianjia"
      draggable
    >
      <template #header="{ titleId, titleClass }">
        <div class="my-header">
          <el-image :src="icon" fit="contain"></el-image>
          <h4 :id="titleId" :class="titleClass">加载</h4>
        </div>
      </template>
      <div class="demo-input-suffix firsttitle leftbgimg2">
        <el-table
          :row-class-name="tableRowClassName"
          :max-height="tableHeight"
          ref="tabledatafmjzref"
          type="selection"
          :data="tableDatafmjz"
          style="width: 100%"
          
          :header-cell-style="{ background: 'rgba(13, 22, 57, 0) ' }"
        >
        <template v-slot:empty>
                        <p>暂无数据</p>
                        </template>
          <el-table-column type="selection" width="55" />
          <!-- <el-table-column property="snId" label="开始节点" />
          <el-table-column property="enId" label="结束节点" /> -->
          <el-table-column type="index" label="编号" width="70" />
          <el-table-column property="doorname" label="风门名称" />
          <el-table-column property="pname" label="巷道名称"  />
          <el-table-column property="site" label="相对位置" />
          <el-table-column prop="dragcoe" label="阻力系数" />
          <el-table-column prop="onoff" label="开启状态">
                          <template #default="scope">
                            {{
                              scope.row.onoff == "0"
                                ? "否"
                                : scope.row.onoff == "1"
                                ? "是"
                                : "是"
                            }}
                          </template>
          </el-table-column>
        </el-table>

        <div class="dialog-footer footer_div l_btn">
          <div class="footerbtn flex1">
            <div class="borderimg">
              <el-button @click="sdialog.fmloaddialog = false">取消</el-button>
            </div>
          </div>
          <div class="footerbtn flex1">
            <div class="borderimg">
              <el-button
                @click="
                  fmloadsave();
                  sdialog.fmloaddialog = false;
                "
              >
                确定
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
    <!-- 风门的添加 -->
<el-dialog
      v-model="sdialog.fmdialog"
      width="400"
      align-center
      :modal="false"
      :append-to-body="true"
      :z-index='1000'
      :close-on-click-modal="false"
      :fullscreen="false"
      :modal-append-to-body="false"
      modal-class="summary-dlg"
      class="dialog_class bgcolor tianjia"
      draggable
      @close='closeDialog'
    >
      <template #header="{ titleId, titleClass }">
        <div class="my-header">
          <el-image :src="icon" fit="contain"></el-image>
          <h4 :id="titleId" :class="titleClass">{{ zhtext }}</h4>
        </div>
      </template>
      <el-form >
        <!--  只能输入引文oninput ="value=value.replace(/[^a-zA-Z]/g, '');" -->
        <el-form-item label="编码" :label-width="formLabelWidth6">
            <el-input v-model="fmobj.doorcode"  maxlength="18"  @input="fmobj.doorcode = fmobj.doorcode.replace(/[\u4e00-\u9fa5\s]/g, '')"   class="w-50 m-2" placeholder="请输入" />
        </el-form-item>
      <el-form-item label="风门名称" :label-width="formLabelWidth6">
            <el-input v-model="fmobj.doorname"  maxlength="18" oninput ="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/g,'')"  class="w-50 m-2" placeholder="请输入" />
        </el-form-item>
      <div class="demo-input-suffix firsttitle magintop">
        <el-form-item label="巷道名称" :label-width="formLabelWidth6">
          <el-input
            v-model="source.selectstr"
            disabled
            class="w-50 m-2"
            placeholder="巷道名称"
          />
        </el-form-item>
        <div class="btn2">
          <el-button
            type="primary"
            @click="
              fireclick('fm');
              sdialog.dialogVisiblenode = true;
            "
            >选择巷道</el-button
          >
        </div>
        <div class="btn2" style="padding-left: 10px">
          <el-button type="primary" @click="Dclick('fm')">3D巷道选择</el-button>
        </div>
      </div>
      <div class="input">
        <el-form-item label="相对位置" :label-width="formLabelWidth6">
          <el-input-number
            v-model="source.site"
            :min="0"
            :max="1"
            :precision="2"
            :step="0.1"
            controls-position="right"
            class="w-50 m-2"
            placeholder="相对位置"
            @change="inputnuberChange1"
          />
        </el-form-item>
      </div>
      <div style="display: flex;" >
                  <span class="switchppm">开启状态</span>
                <el-switch 
                v-model="fmobj.onoff"
                inline-prompt
                active-text="是"
                inactive-text="否"
                :active-value="1"
                :inactive-value="0"
                @change="handleSwitchChange"
              />
            </div>
      <el-form-item label="阻力系数" :label-width="formLabelWidth6">
            <el-input v-model="fmobj.dragcoe"  maxlength="10" oninput ="value=value.replace(/[^0-9.]/g,'')" class="w-50 m-2"  placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div class="dialog-footer footer_div l_btn">
        <div class="footerbtn flex1">
          <div class="borderimg">
            <el-button @click="closeDialog(); sdialog.fmdialog=false">取消</el-button>
          </div>
        </div>
        <div class="footerbtn flex1">
          <div class="borderimg">
            <el-button
              :disabled="isSubmitting"
              @click="addxiufm(); "
            >
              确定
            </el-button>
          </div>
        </div>
      </div>
    </el-dialog>
      <!-- 选择巷道 -->
      <pipelines ref="pipeline" :selectstr="source.selectstr" @pipelineapi="pipelineapi" />
  
      <!-- 删除 -->
      <el-dialog
        v-model="sdialog.dialogsgdelect"
        width="400"
        class="dialog_class bgcolor tianjia"
      >
        <template #header="{ titleId, titleClass }">
          <div class="my-header">
            <el-image :src="icon" fit="contain"></el-image>
            <h4 :id="titleId" :class="titleClass">删除框</h4>
          </div>
        </template>
        <h4 class="delecttitle">
          是否确认删除 <span class="spanclad">{{ zqname }}</span>
        </h4>
        <div class="dialog-footer footer_div l_btn">
        <div class="footerbtn flex1">
          <div class="borderimg">
            <el-button @click="sdialog.dialogsgdelect=false">取消</el-button>
          </div>
        </div>
        <div class="footerbtn flex1">
          <div class="borderimg">
            <el-button
              @click="deletefun(); "
            >
              确定
            </el-button>
          </div>
        </div>
      </div>
        <!-- <div class="dialog-footer footer_div l_btn">
          <div class="footerbtn flex1">
            <div class="borderimg">
              <el-button @click="sdialog.dialogsgdelect=false">取消</el-button>
              <el-button @click="deletefun()">确定</el-button>
            </div>
          </div>
       
        </div> -->
      </el-dialog>
      <selfdialogs ref="dialogtrue" :site="dsite"  @Dtext="Dtext"></selfdialogs>
    </div>
  </template>
  <script setup>
  import { computed, ref, onMounted, reactive } from "vue";
  import { request, uploadFile } from "@/utils/request";
  import icon from "@/assets/img/icon.png";
  import closeimg from "@/assets/img/colse.png";
  import { ElMessage, ElButton, ElDialog,ElConfigProvider } from "element-plus";
  import { message } from '@/utils/message';
  import zhCn from 'element-plus/es/locale/lang/zh-cn'
  import { Calendar, Search } from "@element-plus/icons-vue";
  // import selfdialogs from "./SetNode.vue";
  import selfdialogs from "./SetPipe.vue";
  import pipelines from "./PipeIine.vue"
  import { vtkmodel } from "@/control/vtkModel.js";
  let zqname=ref();
  let tableHeight=ref(200);
  let dsite=ref();
  let currentrow=ref();
  let pipeline = ref();
  let dialogtrue = ref();
  let isSubmitting = false;
  let sdialog=ref({
    dialogsgdelect:false,
    dialogVisiblenode:false,
    fjdialog:false,
    fjloaddialog:false,
    fmdialog:false,
    fmloaddialog:false
  })
  let delte=ref();
  let state=ref();
  let formLabelWidth6=ref(120)
  let source = ref({
    selectstr: '',
    site:null
})
// let selectstr=ref();
// let firepid=ref();
let aid=ref()//事故id
  let pid=ref();// 巷道id
  let site=ref();
  let fjobj=ref({
    fanid:null,
    fanname:"",
    fancode:'',
    pa:null,
    onoff:0
})
let handobj=ref({});
let fmobj=ref({
  doorid:null,
  doorname:"",
  dragcoe:null,
  doorcode:'',
  onoff:0
})
let handobjfm=ref({});
const props = defineProps({
    aid: {
    type: String,
  },

});
  let zhtext=ref('');
  let tishi=ref('');
  let fjtableData=ref([]);
  let tableDatafjjz=ref([]);
  let tabledatafjjzref = ref();
  let fmtableData=ref([]);
  let tableDatafmjz=ref([]);
  let tabledatafmjzref = ref();

let opendialogname = '';//用于区分风机风门打开的弹窗


  const handlerow = (val) => {
    vtkmodel.delfan();
    currentrow.value=true; 
    handobj.value=val;
    console.log(val);
    vtkmodel.delAll();
    vtkmodel.addfan(val.pid,val.site);
};
const handlerow2 = (val) => {
  vtkmodel.deldoor();
    currentrow.value=true; 
    handobjfm.value=val;
    vtkmodel.delAll();
    vtkmodel.adddoor(val.pid,val.site);

};
let coolactiveName1 = ref(["1", "2"]);
let coolactiveName2 = ref( "2");
// 表格变色
const tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 2 != 0) {
    return "evenRow";
  }
  return "oddRow";
};
//
const initisshow=()=>{
  sdialog.value.dialogsgdelect=false;
  sdialog.value.dialogVisiblenode=false;
  sdialog.value.fjdialog=false;
  sdialog.value.fjloaddialog=false;
  sdialog.value.fmdialog=false;
  dialogtrue.value.dialoglog = false;
  pipeline.value.dialogVisiblenode = false;
  closeDialog();
}

// 3D弹屏
const Dclick = (val) => {
  opendialogname = val;
  dialogtrue.value.dialoglog = true;
  vtkmodel.clearJgAddMode();
  vtkmodel.clearpipfan();
  vtkmodel.clearpipdoor();
  
  vtkmodel.selectPipes();
  dialogtrue.value.jgSelect();
  console.log(dialogtrue.value.dialoglog);
};


// 选择巷道
const fireclick = (val) => {
    opendialogname = val;
    pipeline.value.searchtaggd='';
    pipeline.value.dialogVisiblenode = true;
    pipeline.value.pipelinedata('');
  
}
const closeDialog=()=>{
state.value=sessionStorage.getItem("state");
  if(state.value=='1'){
    vtkmodel.clearModeAddJg();
  }
  //节点图形
  vtkmodel.clearpipfan();//清除选择巷道的位置
  vtkmodel.clearpipdoor();
  vtkmodel.selectNoting();
  dialogtrue.value.timintclaer();
  sdialog.value.fjdialog=false
}
//点击确定
const pipelineapi = (row) => {
    console.log(row);
    pid.value = row.id;
    source.value.selectstr = row.name;

    vtkmodel.selectByPipeId(row.id);
    if(opendialogname === "fj"){
      vtkmodel.addpipfan(row.id, source.value.site|| 0.0);
    }else {
      vtkmodel.addpipdoor(row.id, source.value.site|| 0.0);
    }
    
}

//风机加载
const fjloadclick = () => {
  console.log("目标：",fjtableData.value)

  tableDatafjjz.value = [];

  let temptable = [];
  
  fjtableData.value.forEach(row => {
    if (row.fanbaseid !== 0) {
      temptable.push(row);
    }
  });

  const params = {
    transCode: "D50007",
    // aid: aid.value,
  };
  request(params)
    .then((res) => {
      // 使用 concat() 将新数据添加到现有数据后面
      res.rows.forEach(newRow => {
        const exists = temptable.some(existingRow => existingRow.fanbaseid === newRow.fanbaseid);
        if (!exists) {
          temptable.push(newRow);  // 只有在不存在时才追加
        }
      });
      tableDatafjjz.value = temptable;

      nextTick(() => {
        fjtableData.value.forEach(row => {
          if (row.fanbaseid !== 0) {  // 确保只选中符合条件的行
            // 在 tableData3 中查找与 tableDatpump 中对应的行
            const rowIndex = tableDatafjjz.value.findIndex(item => item.fanbaseid === row.fanbaseid);
            if (rowIndex !== -1) {
              // 找到对应的行后，选中该行
              tabledatafjjzref.value.toggleRowSelection(tableDatafjjz.value[rowIndex], true);  // 选中该行
              console.log("选中的行：", tableDatafjjz.value[rowIndex]);
            }
          }
        });
      });
    })
    .catch((err) => {
      message.error(err.returnMsg);
    });
}

//风机加载保存
const fjloadsave = () => {
  let values = [];
  const selectedRows = tabledatafjjzref.value.getSelectionRows();
  console.log( '选中行:',selectedRows);

  if (selectedRows.length === 0) {
    return; 
  }

  selectedRows.forEach(row => {
    // 获取每一行的 fanbaseid
    if (row.fanbaseid) {
      values.push(row.fanbaseid);
    }
  });

  const params = {
    transCode: "D50014",
    aid: aid.value,
    fanbaseids:values.join(',')
  };
  request(params)
    .then((res) => {
      message.success({
        message: "加载成功"
      });
      getrequest();
    })
    .catch((err) => {
      message.error(err.returnMsg);
    });
}

//风机的添加
const fjclick=()=>{
    zhtext.value='添加';
    currentrow.value=false;
    tishi.value= zhtext.value;
    handobj.value={};
    fjobj.value.fanid='';
    fjobj.value.fancode='';
    fjobj.value.fanname='';
    fjobj.value.onoff='';
    fjobj.value.pa='';
    source.value.site='';
    source.value.selectstr='';
    pid.value='';
    source.value.site=='';

    vtkmodel.clearJgAddMode();
    vtkmodel.delAll();
}
// 风机的修改
const xiugaiclick=()=>{
  zhtext.value='修改';
  if (JSON.stringify(handobj.value) == "{}"|| currentrow.value==false) {
    message.error("你还没有选中修改的项目");
  } else {
    sdialog.value.fjdialog = true;
    aid.value=handobj.value.aid;
    fjobj.value.fanid=handobj.value.fanid;
    fjobj.value.fancode=handobj.value.fancode;
    fjobj.value.fanname=handobj.value.fanname;
    fjobj.value.onoff=handobj.value.onoff;
    fjobj.value.pa=handobj.value.pa;
    source.value.selectstr=handobj.value.pname;
    source.value.site=handobj.value.site;
    pid.value=handobj.value.pid;
    source.value.site==handobj.value.site;

    vtkmodel.clearJgAddMode();
    vtkmodel.delAll();
    vtkmodel.selectByPipeId(pid.value);
    vtkmodel.addpipfan(pid.value, source.value.site|| 0.0);
  }
}
//风机删除
const delonclock=(val)=>{
  if (JSON.stringify(handobj.value) == "{}"|| currentrow.value==false) {
    message.error("你还没有选中删除的项目");
  } else {
    delte.value=val;
    sdialog.value.dialogsgdelect=true;
    zqname.value=handobj.value.pname;
  }
}
const deletefun=()=>{
  console.log(delte.value);
  if(delte.value=="风机"){
  const params = {
    transCode: "D20009",
    fanid: handobj.value.fanid,
  };
  request(params)
    .then((res) => {
      message.success({
        message:"删除风机成功",
      });
      sdialog.value.dialogsgdelect = false;
      getrequest();
    })

    .catch((err) => {
      message.error(err.returnMsg);
    });
  }else if(delte.value=="风门"){
    const params = {
    transCode: "D20012",
    doorid:handobjfm.value.doorid,
  };
  request(params)
    .then((res) => {
      message.success({
        message:"删除风门成功",
      });
      sdialog.value.dialogsgdelect = false;
      fmrequest();
    })

    .catch((err) => {
      message.error(err.returnMsg);
    });
  }else{
    
  }
  }
const inputnuberChange = (number) => {
    source.value.site=number;
    dsite.value=number;
    dialogtrue.value.jgSelenum();
    
    vtkmodel.addpipfan(pid.value,number)
    
}

const inputnuberChange1 = (number) => {
    source.value.site=number;
    dsite.value=number;
    dialogtrue.value.jgSelenum();
   
    vtkmodel.addpipdoor(pid.value,number)
}

const handleSwitchChange=(value)=> {
    fjobj.value.onoff=value;
    }
//初始化
const getquery=(id)=>{
  aid.value=id;
  getrequest();
  fmrequest();
}
//  风机查询
const getrequest=()=>{
  const params = {
    transCode: "D20007",
    aid: aid.value,
  };
  request(params)
    .then((res) => {
        console.log(res)
        vtkmodel.delfan();
        fjtableData.value=res.rows;
        currentrow.value=false;
        console.log( currentrow.value);
    })
    .catch((err) => {
      message.error(err.returnMsg);
    });
  }
// 确定添加
const addrequest=()=>{
  if (isSubmitting) return;
  if(fjobj.value.fancode==null||fjobj.value.fancode==''){
    message.error("风机编码不能为空")
    return
  }
  if(fjobj.value.fanname==null||fjobj.value.fanname==''){
    message.error("风机名称不能为空")
    return
  }
  if(pid.value==null||pid.value==""){
    message.error("巷道不能为空")
  }else{
    isSubmitting = true; // 开始提交
  const params = {
    transCode: "D20008",
    fanid:fjobj.value.fanid,
    fancode:fjobj.value.fancode,
    pid:pid.value,
    fanname:fjobj.value.fanname,
    site:source.value.site,
    pa:parseFloat(fjobj.value.pa),
    onoff:fjobj.value.onoff,
    aid:aid.value,

  };
  request(params)
    .then((res) => {
      message.success({
        message: zhtext.value+"风机成功",
      });
      sdialog.value.fjdialog = false;
      getrequest();
      // getrequest();
      fun3D();
    })
    .catch((err) => {
      const regex = /字段\d*/g;
        let Msg=err.returnMsg.replace(regex, '');
         message.error(Msg)
    })
    .finally(() => {
      isSubmitting = false; // 提交结束
    });
  }

}
//风门查询 
const fmrequest=()=>{
    console.log(aid.value)
  const params = {
    transCode: "D20010",
    aid: aid.value,
  };
  request(params)
    .then((res) => {
      vtkmodel.deldoor();
        fmtableData.value=res.rows;
        currentrow.value=false;
    })
    .catch((err) => {
      message.error(err.returnMsg);
    });
  }

  //风门加载
  const fmload = () => {
    console.log("目标：",fmtableData.value)

    tableDatafmjz.value = [];

    let temptable = [];
  
    fmtableData.value.forEach(row => {
      if (row.doorbaseid !== 0) {
        temptable.push(row);
      }
    });

    const params = {
      transCode: "D50010",
      // aid: aid.value,
    };
    request(params)
      .then((res) => {
        // 使用 concat() 将新数据添加到现有数据后面
        res.rows.forEach(newRow => {
        const exists = temptable.some(existingRow => existingRow.doorbaseid === newRow.doorbaseid);
        if (!exists) {
          temptable.push(newRow);  // 只有在不存在时才追加
        }
      });
        tableDatafmjz.value = temptable;

        nextTick(() => {
          fmtableData.value.forEach(row => {
          if (row.doorbaseid !== 0) {  // 确保只选中符合条件的行
            // 在 tableData3 中查找与 tableDatpump 中对应的行
            const rowIndex = tableDatafmjz.value.findIndex(item => item.doorbaseid === row.doorbaseid);
            if (rowIndex !== -1) {
              // 找到对应的行后，选中该行
              tabledatafmjzref.value.toggleRowSelection(tableDatafmjz.value[rowIndex], true);  // 选中该行
              console.log("选中的行：", tableDatafmjz.value[rowIndex]);
            }
          }
        });
      });
      })
      .catch((err) => {
        message.error(err.returnMsg);
      });
  }

  const fmloadsave = () => {
  let values = [];
  const selectedRows = tabledatafmjzref.value.getSelectionRows();
  console.log( '选中行:',selectedRows);

  if (selectedRows.length === 0) {
    return; 
  }

  selectedRows.forEach(row => {
    // 获取每一行的 doorbaseid
    if (row.doorbaseid) {
      values.push(row.doorbaseid);
    }
  });

  const params = {
    transCode: "D50015",
    aid: aid.value,
    doorbaseids:values.join(',')
  };
  request(params)
    .then((res) => {
      message.success({
        message: "加载成功"
      });
      fmrequest();
    })
    .catch((err) => {
      message.error(err.returnMsg);
    });
}

  //风门的添加
  const  fmadd=()=>{
    zhtext.value='添加';
    currentrow.value=false;
    tishi.value= zhtext.value;
    handobj.value={};
    fmobj.value.doorid=null;
    fmobj.value.doorcode='';
    fmobj.value.doorname='';
    fmobj.value.onoff=0;
    fmobj.value.dragcoe=null;
    source.value.site=null;
    source.value.selectstr='';
    pid.value=null;


    vtkmodel.clearJgAddMode();
    vtkmodel.delAll();

  }
  //风门的修改
  const  fmxiugai=()=>{
    zhtext.value='修改';
  if (JSON.stringify(handobjfm.value) == "{}"|| currentrow.value==false) {
    message.error("你还没有选中修改的项目");
  } else {
    sdialog.value.fmdialog = true;
    aid.value=handobjfm.value.aid;
    fmobj.value.doorid=handobjfm.value.doorid;
    fmobj.value.doorcode=handobjfm.value.doorcode;
    fmobj.value.doorname=handobjfm.value.doorname;
    fmobj.value.onoff=handobjfm.value.onoff;
    fmobj.value.dragcoe=handobjfm.value.dragcoe;
    source.value.selectstr=handobjfm.value.pname;
    source.value.site=handobjfm.value.site;
    pid.value=handobjfm.value.pid;
    source.value.site==handobjfm.value.site;


    vtkmodel.clearJgAddMode();
    vtkmodel.delAll();
    vtkmodel.selectByPipeId(pid.value);
    vtkmodel.addpipdoor(pid.value, source.value.site|| 0.0);

  }
  }
  // 删除
 
  const  fmdel=(val)=>{
    if (JSON.stringify(handobjfm.value) == "{}"|| currentrow.value==false) {
    message.error("你还没有选中删除的项目");
  } else {
    delte.value=val;
    sdialog.value.dialogsgdelect=true;
    zqname.value=handobjfm.value.pname;
  }
  }
  //添加接口
 

const addxiufm = () => {
  if (isSubmitting) return; // 如果正在提交，则不再继续

  if (fmobj.value.doorcode == null || fmobj.value.doorcode == "") {
    message.error("风门编码不能为空");
    return;
  }
  if (fmobj.value.doorname == null || fmobj.value.doorname == "") {
    message.error("风门名称不能为空");
    return;
  }
  if (pid.value == null || pid.value == "") {
    message.error("巷道不能为空");
    return;
  }

  isSubmitting = true; // 开始提交

  const params = {
    transCode: "D20011",
    doorid: fmobj.value.doorid,
    doorcode: fmobj.value.doorcode,
    pid: pid.value,
    doorname: fmobj.value.doorname,
    site: source.value.site,
    dragcoe: parseFloat(fmobj.value.dragcoe),
    onoff: fmobj.value.onoff,
    aid: aid.value,
  };

  request(params)
    .then((res) => {
      message.success({
        message: zhtext.value + "风门成功"
      });
      sdialog.value.fmdialog = false;
      fmrequest();
      fun3D();
    })
    .catch((err) => {
      const regex = /字段\d*/g;
      let Msg = err.returnMsg.replace(regex, "");
      message.error(Msg);
    })
    .finally(() => {
      isSubmitting = false; // 提交结束
    });
};

  
// 3D关闭弹窗
const fun3D = () => {
 state.value=sessionStorage.getItem("state");
  //console.log(state.value);
  if(state.value=='1'){
    vtkmodel.clearModeAddJg();
  }else{
    vtkmodel.selectNoting();
  }
  dialogtrue.value.timintclaer();
  vtkmodel.clearpipdoor();//清除选择巷道的位置
  vtkmodel.clearpipfan();
}
const Dtext=(val)=>{
  console.log('dayin',val);
  console.log('opendialogname',opendialogname);
  source.value.selectstr=val.name;
  pid.value=val.id;
  if(opendialogname === "fj"){
    vtkmodel.addpipfan(pid.value, source.value.site|| 0.0);
  }else {
    vtkmodel.addpipdoor(pid.value, source.value.site|| 0.0);
  }
}
defineExpose({getquery,initisshow});
</script>
