<template>
  <div class="bigBox">
    <MOSTY.FromPage :title="props.formData.title" @closeDialog="black">
      <template #content>
        <MOSTY.Assort :title="'基础信息'" />
        <el-form class="mosty-from-wrap" ref="ruleFormRef" label-width="140px" :model="fromData" :inline="true"
                 :rules="rules" :disabled="!props.formData.saveBtnShow">
          <el-form-item label="设备设施类型:" prop="sblx">
            <MOSTY.DictRidioAndSelect :dictData="sblxlist" v-model="fromData.sblx" selectType="radio"
                                      style='width:824px;' :disabled="!props.formData.saveBtnShow"
                                      @getselect="getselect" />
          </el-form-item>
          <el-form-item label="设备设施名称:" prop="name"
                        :rules="[{ required: true, message: '请输入名称', trigger: 'blur' }]">
            <el-input v-model="fromData.name" placeholder="请输入设备设施名称" clearable
                      style="width: 337px"></el-input>
          </el-form-item>
          <el-form-item label="类型:" prop="type" v-if="fromData.sblx == '200603000000'">
            <div style="width: 337px">
              <MOSTY.DictRidioAndSelect :dictData="D_BZ_BYQLX" v-model="fromData.type" selectType="select"
                                        :disabled="false" />
            </div>
          </el-form-item>
          <el-form-item label="容量:" prop="capacity" v-if="fromData.sblx == '200603000000'">
            <el-input v-model="fromData.capacity" type="number" placeholder="请输入容量" style="width: 337px">
              <template #append>KvA</template>
            </el-input>
          </el-form-item>
          <el-form-item label="功率:" prop="power" v-if="fromData.sblx == '200604000000'">
            <el-input v-model="fromData.power" type="number" placeholder="请输入功率" style="width: 337px">
              <template #append>千瓦</template>
            </el-input>
          </el-form-item>
          <el-form-item label="视在功率:" prop="apparentPower" v-if="fromData.sblx == '200606000000'">
            <el-input v-model="fromData.apparentPower" type="number" placeholder="请输入视在功率"
                      style="width: 337px">
              <template #append>VA</template>
            </el-input>
          </el-form-item>
          <el-form-item label="有功功率:" prop="activePower" v-if="fromData.sblx == '200606000000'">
            <el-input v-model="fromData.activePower" type="number" placeholder="请输入有功功率"
                      style="width: 337px">
              <template #append>W</template>
            </el-input>
          </el-form-item>
          <el-form-item label="品牌型号:" prop="brandModel">
            <el-input v-model="fromData.brandModel" style="width: 337px"></el-input>
          </el-form-item>
          <el-form-item label="生产厂家:" prop="sccj">
            <el-input v-model="fromData.sccj" style="width: 337px"></el-input>
          </el-form-item>
          <el-form-item label="投入使用日期:" prop="trsysj">
            <el-date-picker v-model="fromData.trsysj" type="date" value-format="YYYY-MM-DD"
                            style="width: 337px" placeholder="请选择投入使用日期" />
          </el-form-item>
          <el-form-item label="所属场所:" prop="sitename" v-if="['cs', 'jzw', 'lc', 'bw'].includes(addtype)">
            <el-input v-model="fromData.sitename" style="width: 337px" disabled />
          </el-form-item>
          <!-- <el-form-item label="所属场所:" prop="sitename" v-if="['bpdsb'].includes(addtype) && fromData.id">
              <el-input v-model="fromData.sitename" style="width: 337px" disabled />
          </el-form-item> -->
          <!-- && !fromData.id -->
          <el-form-item label="所属场所:" prop="sitename" v-if="['bpdsb'].includes(addtype)">
            <el-select v-model="fromData.sitename" @change="cahngecsmc" clearable style="width: 337px"
                       placeholder="请选择">
              <el-option v-for="item in csmclist" :key="item" :label="item.placename" :value="item.id" />
            </el-select>
          </el-form-item>
          <!-- && !fromData.id -->
          <el-form-item :label="fromData.publicarea == '1' ? '所属场区:' : '所属建筑物:'" prop="buildingname"
                        v-if="['cs', 'bpdsb'].includes(addtype)">
            <el-select v-model="fromData.buildingname" @change="cahngejzwmc" clearable style="width: 337px"
                       placeholder="请选择">
              <el-option v-for="item in jzwlist" :key="item" :label="item.buildingname"
                         :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item :label="fromData.publicarea == '1' ? '所属场区:' : '所属建筑物:'" prop="buildingname"
                        v-if="['jzw', 'lc', 'bw'].includes(addtype) && !fromData.id">
            <el-input v-model="fromData.buildingname" style="width: 337px" disabled />
          </el-form-item>
          <!-- ['cs', 'jzw', 'lc', 'bw', 'bpdsb'] -->
          <el-form-item :label="fromData.publicarea == '1' ? '所属场区:' : '所属建筑物:'" prop="buildingname"
                        v-if="['jzw', 'lc', 'bw'].includes(addtype) && fromData.id">
            <el-input v-model="fromData.buildingname" style="width: 337px" disabled />
          </el-form-item>
          <!-- && !fromData.id -->
          <el-form-item label="所属楼层:" prop="floorname"
                        v-if="['cs', 'jzw', 'bpdsb'].includes(addtype) && fromData.publicarea != '1'">
            <el-select v-model="fromData.floorname" @change="cahngelcmc" clearable style="width: 337px"
                       placeholder="请选择">
              <el-option v-for="item in lclist" :key="item" :label="item.floorname" :value="item.id" />
            </el-select>
          </el-form-item>
          <!-- 'cs', 'jzw', 'lc', 'bpdsb', 'bw' -->
          <el-form-item label="所属楼层:" prop="floorname"
                        v-if="['lc', 'bw'].includes(addtype) && fromData.id && fromData.publicarea != '1'">
            <el-input v-model.number="fromData.floorname" style="width: 337px" disabled />
          </el-form-item>
          <el-form-item label="所属楼层:" prop="floorname"
                        v-if="['lc', 'bw'].includes(addtype) && !fromData.id && fromData.publicarea != '1'">
            <el-input v-model.number="fromData.floorname" style="width: 337px" disabled />
          </el-form-item>
          <!-- && !fromData.id -->
          <el-form-item :label="fromData.publicarea == '1' ? '场区部位:' : '所属部位:'" prop="partname"
                        v-if="['cs', 'jzw', 'lc', 'bpdsb'].includes(addtype)">
            <el-select v-model="fromData.partname" @change="cahngebw" clearable style="width: 337px"
                       placeholder="请选择">
              <el-option v-for="item in bwlist" :key="item"
                         :label="item.partname ? item.partname : item.qtmc" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item :label="fromData.publicarea == '1' ? '场区部位:' : '所属部位:'" prop="partname"
                        v-if="['bw'].includes(addtype) && !fromData.id">
            <el-input v-model="fromData.partname" disabled style="width: 337px" />
          </el-form-item>
          <!-- 'cs', 'jzw', 'lc', 'bw', 'bpdsb' -->
          <el-form-item :label="fromData.publicarea == '1' ? '场区部位:' : '所属部位:'" prop="partname"
                        v-if="['bw',].includes(addtype) && fromData.id">
            <el-input v-model="fromData.partname" disabled style="width: 337px" />
          </el-form-item>
          <el-form-item label="安装位置:" prop="installlocation">
            <el-input v-model="fromData.installlocation" placeholder="请输入安装位置" clearable
                      style="width: 337px"></el-input>
          </el-form-item>
          <el-form-item v-if="fromData.id" label="状态:" prop="state">
            <div style="width: 337px;">
              <MOSTY.DictRidioAndSelect :dictData="D_BZ_SYZT" v-model="fromData.state" selectType="radio"
                                        :disabled="false" />
            </div>
          </el-form-item>
          <!-- 未确定字段 -->
          <el-form-item label="内部编号:" prop="nbbh" v-if="fromData.id">
            <el-input v-model="fromData.nbbh" disabled style="width: 337px"></el-input>
          </el-form-item>
          <el-form-item label="上次检测日期:" prop="sycjcrq">
            <el-date-picker v-model="fromData.sycjcrq" type="date" value-format="YYYY-MM-DD"
                            style="width: 337px" placeholder="请选择上次检测日期" :disabled-date="disablednowDate1" />
          </el-form-item>
          <el-form-item label="检测周期:" prop="time">
            <el-input v-model="fromData.time" type="number" style="width: 337px">
              <template #append>月</template>
            </el-input>
          </el-form-item>
          <el-form-item label="检查周期:" prop="inspectTime">
            <el-input v-model="fromData.inspectTime" type="number" style="width: 337px"
                      placeholder="请输入检查周期">
              <template #append>月</template>
            </el-input>
          </el-form-item>

          <el-form-item label="报废时间:" prop="scrapTime">
            <el-date-picker v-model="fromData.scrapTime" type="date" value-format="YYYY-MM-DD"
                            style="width: 337px" placeholder="请选择报废时间" />
          </el-form-item>
          <el-form-item label="维保单位:">
            <el-select v-model="fromData.wborgname" style="width: 337px" @change="changexgf" clearable
                       placeholder="请选择维保单位：">
              <el-option v-for="item in xgflist" :key="item.id" :label="item.label" :value="item" />
            </el-select>
          </el-form-item>
          <el-form-item label="维保电话:" prop="phone">
            <el-input v-model="fromData.phone" placeholder="请输入维保电话" clearable
                      style="width: 337px"></el-input>
          </el-form-item>
          <!-- 1.高压配电柜 - 2.低压配电柜 才有 电压、电流 -->
          <el-form-item v-if="['200601000000', '200602000000'].includes(fromData.sblx)" label="电压:"
                        prop="voltage">
            <el-input v-model="fromData.voltage" placeholder="请输入电压" clearable style="width: 337px">
              <template #append>V</template>
            </el-input>
          </el-form-item>
          <el-form-item v-if="['200601000000', '200602000000'].includes(fromData.sblx)" label="电流:"
                        prop="electricCurrent">
            <el-input v-model="fromData.electricCurrent" placeholder="请输入电流" clearable style="width: 337px">
              <template #append>A</template>
            </el-input>
          </el-form-item>
          <!-- 5.配电箱 才有 防护等级 -->
          <el-form-item v-if="['200605000000'].includes(fromData.sblx)" label="防护等级:" prop="protectionLevel">
            <el-select style="width: 337px" v-model="fromData.protectionLevel" placeholder="请选择防护等级"
                       clearable>
              <el-option v-for="item in D_BZ_FHDJ" :key="item.value" :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>

          </el-form-item>
          <el-form-item v-if="['200607000000'].includes(fromData.sblx)" label="内设控制开关组数:" prop="switchCount">
            <el-input v-model="fromData.switchCount" type="number" style="width: 337px"
                      placeholder="请输入内设控制开关组数">
              <template #append>组</template>
            </el-input>
          </el-form-item>

          <el-form-item label="设备资料:" style="width: 100%;" prop="deviceFile">
            <MOSTY.Upload :width="'100%'" :limit="5" :nopicer="true"
                          placeholder="请上传设备资料（pdf,ofd,doc,docx,xls,xlsx）" :isImg="false" ShowfileList
                          :disabled="!props.formData.saveBtnShow" v-model="fromData.deviceFile" :nameshow='true'
                          :gsgfList="['pdf', 'ofd', 'doc', 'docx', 'xls', 'xlsx']">
            </MOSTY.Upload>
          </el-form-item>
          <el-form-item label="其它资料:" style="width: 100%;" prop="otherFile">
            <MOSTY.Upload :width="'100%'" :limit="5" :nopicer="true" :isImg="false" ShowfileList
                          :disabled="!props.formData.saveBtnShow"
                          placeholder="请上传其他资料（pdf,ofd,doc,docx,xls,xlsx）"
                          v-model="fromData.otherFile" :nameshow='true'
                          :gsgfList="['pdf', 'ofd', 'doc', 'docx', 'xls', 'xlsx']">
            </MOSTY.Upload>
          </el-form-item>
          <el-form-item label="图片:" style="width:100%;">
            <div style='display:flex;'>
              <MOSTY.ErwmImg
                :url="qrcodeImg(props.formData.id, 'sbss', fromData.sitename, fromData.buildingname, `${fromData.name}(${fromData.nbbh})`)"
                :filename='"设备设施二维码"' v-if="props.formData.id" />
              <MOSTY.Upload :width="'100%'" :limit="5" :nopicer="false" :isImg="true"
                            :ShowfileList="false" v-model="fromData.picture" :nameshow='true'
                            :gsgfList="['jpg', 'png']" @filehandleChange="changeFiles">
              </MOSTY.Upload>
            </div>
          </el-form-item>
        </el-form>
        <MOSTY.Assort :title="'管理信息'" />
        <el-form class="mosty-from-wrap" ref="ruleFormRef2" label-width="130px" :model="fromData" :inline="true"
                 :rules="rules2" :disabled="!props.formData.saveBtnShow">
          <el-form-item label="使用单位:">
            <el-input v-model="fromData.userorgname" clearable readonly style="width: 337px">
              <template #append>
                <el-button class='cursor' @click="ChooesDdld('02')">选择</el-button>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="责任部门:" prop="responsibilitydeptname">
            <el-input v-model="fromData.responsibilitydeptname" readonly clearable style="width: 337px">
              <template #append>
                <el-button class='cursor' @click="ChooesDdld('03')">选择</el-button>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="责任人:" prop="responsibilitypersonname" style="display: flex;width:100%;">
            <el-input v-model="fromData.responsibilitypersonname" type="textarea" :rows="4" readonly
                      style="width: 93%" clearable>
            </el-input>
            <el-button class='cursor ml10' @click="ChooesDdld('01')">选择</el-button>
          </el-form-item>
          <el-form-item label="巡查相关方：" prop="xgfMc">
            <XiangGFSelect style="width: 337px" v-model="formData.xgfMc" v-model:id="formData.xgfId"
                           @change="xgfDwChange" @update:modelValue="updateModelValue" @update:id='updateId' />
          </el-form-item>
          <el-form-item label="相关方责任人：" prop="xgfRyMc">
            <XiangGFRen inputWidth="337px" v-model:ids="formData.xgfRyId" v-model:cName="formData.xgfRyMc" :relatedid="formData.xgfId"
                        :disabled="!formData.xgfId" @update:ids="updateIds" @update:cName="updateName" />
          </el-form-item>
        </el-form>
        <div v-if='props.formData.id'>
          <MOSTY.Assort :title="'关联信息(固定信息、动态信息)'" />
          <div>
            <div class='table-lable'>
              <span>固定信息</span>
              <img :src="getImgUrl('img/icon-jt.png')" style='cursor:pointer'
                   @click='isShowgd = !isShowgd' />
            </div>
            <div v-show='isShowgd'>
              <MOSTY.TabList :tablist='tablist' :obj='fromData' :showCount='true' :cardtype="'03'"
                             :isEdit="props.formData.saveBtnShow" @handleChange="changelicolor" />
            </div>
            <div class='table-lable'>
              <span>动态信息</span>
              <img :src="getImgUrl('img/icon-jt.png')" style='cursor:pointer'
                   @click='isShowdt = !isShowdt' />
            </div>
            <div v-show='isShowdt'>
              <MOSTY.TabList :tablist='tablist2' :type='"sb"' :obj='fromData' :cardtype="'04'"
                             :isEdit="props.formData.saveBtnShow" @handleChange="changelicolor2"
                             @resetChange="searchChange" />
            </div>
          </div>
        </div>
      </template>
      <template #footer>
        <el-button type="primary" @click="submit(ruleFormRef)" v-loading='saveloading'
                   v-if="props.formData.saveBtnShow">
          <el-icon>
            <DocumentChecked />
          </el-icon>
          保存
        </el-button>
        <el-button @click="black">
          <el-icon>
            <DocumentDelete />
          </el-icon>
          关闭
        </el-button>
      </template>
      <template #dwbox>
        <!-- 选择单位 -->
        <Dwhasbj v-if='showdwDept' :showDdld="showdwDept" @closezzjg="closezzjg" @listdata="getSonValue"
                 :zzlx="zzlx" :searchType="'02'" :Isstrictly="Isstrictly" />
        <!-- 选择部门 -->
        <Choosebm :showDdld="showDept" @closezzjg="closezzjg" @listdata="getSonValue" :zzlx="zzlx" />
        <ChooseUser v-model="chooseUserVisible" :Single="singleShow" :data="memberList"
                    :deptId="fromData.responsibilitydeptid" @choosedUsers="getSonValue"></ChooseUser>
      </template>
    </MOSTY.FromPage>
  </div>
</template>

<script setup>
import { getImgUrl } from "@/utils/tools.js"
import { ref, getCurrentInstance, defineAsyncComponent, onMounted } from "vue"
import * as MOSTY from "@/components/MyComponents/index"
import ChooseUser from "@/components/MyComponents/ChooseUser/Leader.vue"
import Choosebm from "@/components/MyComponents/ChooseDept/index.vue"
import Dwhasbj from "@/components/MyComponents/ChooseDept/dwhasbj.vue"
import { ElMessage } from "element-plus"
import { getApi, postApi } from "@/api/tobAcco_api.js"
import { qrcodeImg, disablednowDate1 } from "@/utils/tools.js"
import * as rule from "@/utils/rules.js"
import { getItem } from "@/utils/storage"
import XiangGFSelect from "@/components/dialog/component/XiangGFSelect.vue"
import XiangGFRen from "@/components/dialog/component/XiangGFRen.vue"

const { proxy } = getCurrentInstance()
const {
  D_BZ_SYZT,
  D_BZ_BYQLX,
  D_BZ_FHDJ
} = proxy.$dict("D_BZ_SYZT", "D_BZ_BYQLX", "D_BZ_FHDJ")
const rules = {
  sitename: [{
    required: true,
    message: "请选择所场所",
    trigger: "blur"
  }],
  buildingname: [{
    required: true,
    message: "请选择所属建筑物",
    trigger: "blur"
  }],
  floorname: [{
    required: true,
    message: "请选择所属楼层",
    trigger: "blur"
  }],
  partname: [{
    required: true,
    message: "请选择所属部位",
    trigger: "blur"
  }],
  sblx: [{
    required: true,
    message: "请选择设备设施类型",
    trigger: "blur"
  }],
  nbbh: [{
    required: true,
    message: "请选择内部编号",
    trigger: "blur"
  }],
  ...rule.numberzzsRule({
    validator: true,
    require: false,
    num: 120
  }, "time"),
  ...rule.phoneRule({
    require: false,
    validator: true
  }, "phone"),
  trsysj: [{
    required: true,
    message: "请选择投入使用日期",
    trigger: "change"
  }],
  installlocation: [{
    required: true,
    message: "请输入安装位置",
    trigger: "change"
  }],
  ...rule.znumberRule({
    validator: true,
    require: true
  }, "power"),
  ...rule.znumberRule({
    validator: true,
    require: true
  }, "capacity"),
  ...rule.znumberRule({
    validator: true,
    require: true
  }, "apparentPower"),
  ...rule.znumberRule({
    validator: true,
    require: true
  }, "activePower"),
  state: [{
    required: true,
    message: "请选择状态",
    trigger: "change"
  }],
  type: [{
    required: true,
    message: "请选择类型",
    trigger: "change"
  }],
  ...rule.numberzzsRule({
    validator: true,
    require: false,
    num: 120
  }, "inspectTime"),
  ...rule.numberzzsRule({
    validator: true,
    require: true,
    num: 9999
  }, "switchCount"),
  voltage: [{
    required: true,
    message: "请输入电压",
    trigger: "blur"
  }],
  electricCurrent: [{
    required: true,
    message: "请输入电流",
    trigger: "blur"
  }],
  protectionLevel: [{
    required: true,
    message: "请选择防护等级",
    trigger: "blur"
  }]
}
const rules2 = {
  responsibilitypersonname: [{
    required: true,
    message: "请选择责任人",
    trigger: "change"
  }],
  userorgname: [{
    required: true,
    message: "请选择使用单位",
    trigger: "change"
  }],
  responsibilitydeptname: [{
    required: true,
    message: "请选择责任部门",
    trigger: "change"
  }]
}
const isShowgd = ref(true)
const saveloading = ref(false)
const showdwDept = ref(false)
const showDept = ref(false)
const props = defineProps({
  formData: {
    type: Object,
    default: {}
  },
  type: {
    default: "",
    type: String
  }
})
const Isstrictly = ref(false)
const addtype = ref("")
const fromData = ref({ state: "01" })
const isShowdt = ref(true)
const bwlist = ref([])
const sblxlist = ref([])
const jzwlist = ref([])
const xgflist = ref([])
const lclist = ref([])
const emit = defineEmits("changePage", 'createPage')
const ruleFormRef = ref()
const ruleFormRef2 = ref()
const chooseUserVisible = ref(false) //选择领导弹窗
const singleShow = ref(true) //单选多选
const memberList = ref([])
const zzlx = ref(null)
// 01.设备附件(原来的) 02.预防性实验 03.防护装置 04.保护装置; type: 类型 01 设备附件 02 预防性实验附件 03 防护装置 04 保护装置
const tablist = ref([
  {
    title: "风险告知卡",
    num: 0,
    icon: "sbss2",
    actives: true,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Fxgzk.vue"))
  },
  {
    title: "设备附件列表",
    icon: "sbss2",
    actives: false,
    opt: { type: "01" },
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Bpdfile.vue"))
  },
  {
    title: "预防性实验附件列表",
    icon: "sbss2",
    actives: false,
    opt: { type: "02" },
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Bpdfile.vue"))
  },
  {
    title: "防护装置附件列表",
    icon: "sbss2",
    actives: false,
    opt: { type: "03" },
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Bpdfile.vue"))
  },
  {
    title: "保护装置附件列表",
    icon: "sbss2",
    actives: false,
    opt: { type: "04" },
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Bpdfile.vue"))
  }
])
const tablist2 = ref([
  {
    title: "检查标准",
    num: 0,
    icon: "sbss2",
    sblx: "200600000000",
    actives: true,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Synthesize.vue"))
  },
  {
    title: "检查记录",
    num: 0,
    icon: "sbss2",
    actives: false,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Standard.vue"))
  },
  {
    title: "风险清单",
    num: 0,
    icon: "sbss2",
    actives: false,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Risklist.vue"))
  },
  {
    title: "隐患清单",
    num: 0,
    icon: "sbss2",
    actives: false,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Pitfall.vue"))
  },
  {
    title: "维保记录",
    num: 0,
    icon: "sbss2",
    actives: false,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Maintenance.vue"))
  },
  {
    title: "检测记录",
    num: 0,
    icon: "sbss2",
    actives: false,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Testing.vue"))
  }
])
onMounted(() => {
  addtype.value = props.formData.type ? props.formData.type : "bpdsb"
  fromData.value.xgfRyId = props.formData.xgfRyId ? props.formData.xgfRyId : null;
  fromData.value.xgfRyMc = props.formData.xgfRyMc ? props.formData.xgfRyMc : null;
  fromData.value.xgfMc = props.formData.xgfMc ? props.formData.xgfMc : null;
  fromData.value.xgfId = props.formData.xgfId ? props.formData.xgfId : null;

  fromData.value.siteid = props.formData.siteid ? props.formData.siteid : null
  fromData.value.sitename = props.formData.sitename ? props.formData.sitename : null
  fromData.value.buildingid = props.formData.buildingid ? props.formData.buildingid : null
  fromData.value.buildingname = props.formData.buildingname ? props.formData.buildingname : null
  fromData.value.floorid = props.formData.floorid ? props.formData.floorid : null
  fromData.value.floorname = props.formData.floorname ? props.formData.floorname : null
  fromData.value.partid = props.formData.partid ? props.formData.partid : null
  fromData.value.partname = props.formData.partname ? props.formData.partname : null
  fromData.value.publicarea = props.formData.publicarea ? props.formData.publicarea : null
  fromData.value.sfcq = props.formData.sfcq ? props.formData.sfcq : null

  fromData.value.xgfRyId = props.formData.xgfRyId ? props.formData.xgfRyId : null
  fromData.value.xgfRyMc = props.formData.xgfRyMc ? props.formData.xgfRyMc : null
  fromData.value.xgfMc = props.formData.xgfMc ? props.formData.xgfMc : null
  fromData.value.xgfId = props.formData.xgfId ? props.formData.xgfId : null

  fromData.value.saveBtnShow = props.formData.saveBtnShow

  getsblb()
  getxgflist()
  if (props.formData.id) {
    getmain(props.formData.id)
  } else {
    if (props.formData.type == "cs") {
      getjzwlist(props.formData.siteid)
    } else if (props.formData.type == "jzw") {
      if (props.formData.publicarea == "1") {
        getbw2data(props.formData.buildingid)
      } else {
        getlcdata(props.formData.buildingid)
      }
    } else if (props.formData.type == "lc") {
      getbwdata(props.formData.floorid)
    } else if (["bpdsb"].includes(addtype.value)) {
      getcsData()
    }
  }

})

const xgfDwChange = () => {

}

// 获取相关方责任人id
const updateIds = (ids) => {
  fromData.value.xgfRyId = ids;
}
// 获取相关方人员列表
const updateName = (names) => {
  fromData.value.xgfRyMc = names;
}
// 获取检查方名称
const updateModelValue = (ModelValue) => {
  fromData.value.xgfMc = ModelValue;
}
// 获取检查方id
const updateId = (id) => {
  fromData.value.xgfId = id;
}
const searchChange = () => {
  gettotal()
}
//获取相关方
//获取统计
function gettotal() {
  postApi({
    bizId: fromData.value.id,
    bizType: fromData.value.sblx
  }, "/mosty-jcgl/deviceUpkeepRecord/statistics").then(res => {
    tablist2.value.forEach(item => {
      if (item.title.includes(["维保记录"])) {
        item.num = res ? res : 0
      }
    })
  })
}

function getxgflist() {
  let orgid = getItem("deptId").ssdwid
  getApi({ orgid }, "/mosty-jcgl/relatedpartyinfo/getListByOrg").then(res => {
    if (res) {
      xgflist.value = res.map(item => {
        return {
          label: item.relatedname,
          value: item.id,
          phone: item.contactsphone
        }
      })
    }

  })
}

function changexgf(val) {
  fromData.value.wborgcode = val.value
  fromData.value.wborgname = val.label
  fromData.value.phone = val.phone
}

//根据操作人 查询 场所
function getcsData() {
  getApi({}, "/mosty-jcgl/site/getSiteListByUser").then((res) => {
    csmclist.value = res
  })
}

//获取场所选择
const cahngecsmc = (val) => {
  csmclist.value.forEach((item) => {
    if (item.id == val) {
      fromData.value.siteid = item.id
      fromData.value.sitename = item.placename
      fromData.value.buildingname = ""
      fromData.value.buildingid = ""
      fromData.value.publicarea = ""
      fromData.value.floorname = ""
      fromData.value.floorid = ""
      fromData.value.partname = ""
      fromData.value.partid = ""
      getjzwlist(item.id)

    }
  })
}

//获取当前部位
const cahngebw = (val) => {
  bwlist.value.forEach((item) => {
    if (item.id == val) {
      fromData.value.partid = item.id
      fromData.value.partname = item.partname ? item.partname : item.qtmc
    }
  })
}

//	根据placeid，查询建筑物
function getjzwlist(placeid) {
  getApi({ placeid }, "/mosty-jcgl/building/queryBuildingByPlaceid").then(res => {
    jzwlist.value = res ? res : []
  })
}

//获取建筑物名称
function cahngejzwmc(val) {
  jzwlist.value.forEach((item) => {
    if (item.id == val) {
      fromData.value.buildingid = item.id
      fromData.value.buildingname = item.buildingname
      fromData.value.publicarea = item.publicarea
      fromData.value.floorname = ""
      fromData.value.floorid = ""
      fromData.value.partname = ""
      fromData.value.partid = ""
      if (item.publicarea == "1") {
        getbw2data(item.id)
      } else {
        getlcdata(item.id)
      }

    }
  })
}

//根据建筑物查询楼层
function getlcdata(id) {
  getApi({ id }, "/mosty-jcgl/floor/queryFloorByBuildId").then(res => {
    lclist.value = res ? res : []
  })
}

//获取楼层名称
function cahngelcmc(val) {
  lclist.value.forEach((item) => {
    if (item.id == val) {
      fromData.value.floorid = item.id
      fromData.value.floorname = item.floorname
      fromData.value.partname = ""
      fromData.value.partid = ""
      getbwdata(item.id)
    }
  })
}

//获取设备设施类型
function getsblb() {
  getApi({
    zdbh: "D_TB_JCYWLX",
    dm: "2006",
    level: 2
  }, "/mosty-base/sysDict/getJcyelxDictTree").then(res => {
    sblxlist.value = res[0].itemList

  })
}

// (新)根据楼层id查询部位
function getbwdata(id) {
  getApi({ id }, "/mosty-jcgl/part/getListByFloorid").then((res) => {
    bwlist.value = res ? res : []
  })
}

//(新)根据建筑物id查询部位
function getbw2data(id) {
  getApi({ id }, "/mosty-jcgl/part/getListByBuildid").then((res) => {
    bwlist.value = res ? res : []
  })
}

function changelicolor(val) {
  tablist.value.forEach(item => {
    if (item.title == val.title) {
      item.actives = true
    } else {
      item.actives = false
    }
  })
}

function changelicolor2(val) {
  tablist2.value.forEach(item => {
    if (item.title == val.title) {
      item.actives = true
    } else {
      item.actives = false
    }
  })
}

//点击领导选择按钮
function ChooesDdld(val) {
  zzlx.value = val
  memberList.value = []
  Isstrictly.value = false
  if (val == "01") {
    //设备负责人：
    if (fromData.value.responsibilitydeptid) {
      memberList.value = fromData.value.responsibilityperson ? fromData.value.responsibilityperson.split(",") : []
      singleShow.value = false
      chooseUserVisible.value = true
    } else {
      ElMessage.error("请先选择责任部门！!")
    }

  } else if (val == "02") {
    //使用单位：
    Isstrictly.value = true
    showdwDept.value = true
  } else {
    //责任部门
    showDept.value = true
  }

}

const getSonValue = (value) => {
  // 获取责任人
  if (zzlx.value == "01") {
    fromData.value.responsibilitypersonname = value.map(item => {
      if (item.mobile) {
        return item.userName + "（" + item.mobile + "）"
      } else {
        return item.userName
      }
    }).toString()
    fromData.value.responsibilityperson = value.map(item => {
      return item.id
    }).toString()
    // 获取责任单位
  } else if (zzlx.value == "02") {
    fromData.value.userorgname = value.orgName
    fromData.value.userorgcode = value.id

  } else {
    fromData.value.responsibilitydeptname = value.orgName
    fromData.value.responsibilitydeptid = value.id
  }

}

//获取设备类型名称
function getselect(val) {
  fromData.value.name = val.zdmc
}

//关闭弹窗
const closezzjg = (val) => {
  showDept.value = false
  showdwDept.value = false
}
// 保存
const submit = async (ruleFormRef) => {
  saveloading.value = true
  if (!ruleFormRef) return
  await ruleFormRef.validate((valid) => {
    if (valid) {
      ruleFormRef2.value.validate((valid) => {
        if (valid) {
          // fromData.value.file = fromData.value.file ? fromData.value.file.toString() : null;
          fromData.value.picture = fromData.value.picture ? fromData.value.picture.toString() : null
          fromData.value.deviceFile = fromData.value.deviceFile?.toString()
          fromData.value.otherFile = fromData.value.otherFile?.toString()
          fromData.value.switchCount = fromData.value.switchCount ? Number(fromData.value.switchCount) : null
          if (fromData.value) {
            if (fromData.value.id) {
              postApi(fromData.value, "/mosty-jcgl/sbglBpdsb/updateTbSbglBpdsbOne")
                .then((res) => {
                  ElMessage.success("修改成功")
                  emit("changePage")
                }).finally(() => {
                saveloading.value = false
              })
            } else {
              postApi(fromData.value, "/mosty-jcgl/sbglBpdsb/addTbSbglBpdsbOne")
                .then((res) => {
                  ElMessage.success("新增成功")
                  // emit("changePage", res)
                  emit("createPage", res)
                }).finally(() => {
                saveloading.value = false
              })
            }

          }
        } else {
          ElMessage.error("请输入必填项!")
          saveloading.value = false
        }
      })
    } else {
      ElMessage.error("请输入必填项!")
      saveloading.value = false
    }
  })
}

//获取详情
function getmain(id) {
  if (id) {
    getApi({ id: id }, "/mosty-jcgl/sbglBpdsb/queryTbSbglBpdsbById").then((res) => {
      if (res) {
        res.responsibilitypersonname = res.responsibilitypersonname ? res.responsibilitypersonname.replace(/（null）/g, "") : ""
        fromData.value = res
        fromData.value.saveBtnShow = props.formData.saveBtnShow
        getlcxq(fromData.value.buildingid)
        gettotal()
        if (props.formData.type == "cs") {
          getjzwlist(res.siteid)
        } else if (props.formData.type == "lc") {
          getbwdata(res.floorid)
        } else if (["bpdsb"].includes(addtype.value)) {
          getcsData()
        }
        if (res.partid) {
          getApi({ id: res.partid }, "/mosty-jcgl/part/queryScenePartById").then(res => {
            if (res) {
              fromData.value.partname = res.partname ? res.partname : res.qtmc
            }
          })
        }

      }
    })
  }
}

//获取建筑物详情
function getlcxq(id) {
  getApi({}, `/mosty-jcgl/building/getBuildingById/${id}`).then(res => {
    fromData.value.publicarea = res.publicarea ? res.publicarea : "2"
    if (fromData.value.publicarea == "1") {
      getbw2data(id)
    } else {
      getlcdata(id)
    }
  })
}

const black = () => {
  if (props.formData.saveBtnShow) {
    proxy.$modal.confirm("离开会丢失页面中修改的内容，确认离开吗？")
      .then(() => {
        // 关闭上一次的表单校验
        ruleFormRef.value.clearValidate()
        emit("changePage")
      })
      .catch(() => {
        // catch error
      })
  } else {
    // 关闭上一次的表单校验
    ruleFormRef.value.clearValidate()
    emit("changePage")
  }

}
const csmclist = ref([])


</script>

<style lang="scss" scoped>
::v-deep .el-form-item__content {
  margin-left: 0px !important;
}

.table-lable {
  margin: 10px;
  color: var(--el-color-primary);
}

::v-deep ::-webkit-scrollbar {
  display: none !important;
}

::v-deep .el-pagination {
  justify-content: center !important;
}


::v-deep .el-form {
  margin: auto;
  padding: 10px;
  margin-top: 10px;
  flex-wrap: wrap;
  justify-content: start;
}

.el-form-item {
  display: flex;
  margin-right: 20px;
}

.bigBox {
  display: block !important;
  padding: 0px !important;
  box-sizing: border-box;
}


::v-deep .el-table .cell {
  padding: 0 0 !important;
}

::v-deep .el-table--border .el-table__cell .cell {
  padding-left: 0px !important;

  > input {
    width: 100%;
    height: 33px;
  }
}


::v-deep .el-upload-list {
  width: 50%;
}
</style>
