<template>
  <div>
    <MOSTY.FromPage :title="props.formData.title" @closeDialog="closeDialog">
      <template #content>
        <MOSTY.Assort :title="'基础信息'" />
        <el-form :model="FromData" ref="ruleFormRef" label-position="right" :rules="rules" label-width="172px"
          :disabled='!props.formData.saveBtnShow'>
          <el-form-item label="特种设备类型:" prop="flx" style='width:100%;'>
            <el-radio-group v-model="FromData.flx" @change="changelx" :disabled='FromData.id'>
              <el-radio v-for="it in tablist" :key="it" :label="it.lable">{{ it.title }}</el-radio>
            </el-radio-group>
          </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>
          <!--  && !FromData.id -->
          <el-form-item label="所属场所:" prop="sitename" v-if="['tzsb'].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>
          <!-- <el-form-item label="所属场所:" prop="sitename" v-if="['tzsb'].includes(addtype) && FromData.id">
            <el-input v-model="FromData.sitename" style="width: 337px" disabled />
          </el-form-item> -->
          <!-- && !FromData.id -->
          <el-form-item :label="FromData.publicarea == '1' ? '所属场区:' : '所属建筑物:'" prop="buildingname"
            v-if="['cs', 'tzsb'].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>
          <!-- 'cs', 'jzw', 'lc', 'bw', 'tzsb' -->
          <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>
          <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>
          <template v-if="Number(FromData.flx > 2)">
            <!--  && !FromData.id -->
            <el-form-item label="所属楼层:" prop="floorname"
              v-if="['cs', 'jzw', 'tzsb'].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', 'bw', 'tzsb', -->
            <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', 'tzsb'].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" :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', 'tzsb' -->
            <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>
          </template>
          <component :is="pageData.componentObj" :csmclist="csmclist" :formData="FromData" :xgflist="xgflist"
            :getfrom="isgetFrom" :keys='keys' @saveSuccess="savechange" />
        </el-form>
        <MOSTY.Assort :title="'管理信息'" />
        <el-form class="mosty-from-wrap" ref="ruleFormRef2" label-width="172px" :model="FromData" :inline="true"
          :rules="rules2" :disabled="!props.formData.saveBtnShow">
          <el-form-item label="使用单位:" prop="userorgname" style='margin-right:0px !important;'>
            <el-input v-model="FromData.userorgname" readonly clearable 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" style='margin-right:0px !important;'>
            <el-input v-model="FromData.responsibilitydeptname" style="width: 337px">
              <template #append>
                <el-button class='cursor' @click="ChooesDdld('05')">选择</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: 900px" clearable>
            </el-input>
            <el-button class='cursor ml10' @click="ChooesDdld('01')">选择</el-button>
          </el-form-item>
          <el-form-item label="相关方巡查人员：" prop="xgfRyMc">
            <XiangGFRen :inputWidth="'900px'" :inputHeight="true" v-model:ids="formData.xgfRyId"
              v-model:cName="formData.xgfRyMc" :relatedid="'xgfid111'" @update:ids="updateIds"
              @update:cName="updateName" />
          </el-form-item>
          <!-- <el-form-item label="巡查相关方：" prop="xgfMc">
            <XiangGFSelect style="width: 337px" v-model="formData.xgfMc" v-model:id="formData.xgfId"
               @update:modelValue="updateModelValue" @update:id='updateId' />
          </el-form-item> -->
        </el-form>
        <div v-if='FromData.id && props.formData.title != "特种设备复制"'>
          <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='tablist1' :obj='FromData' :showCount='false' @handleChange="changelicolor"
                :cardtype="'05'" :isEdit="props.formData.saveBtnShow" />
            </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' @handleChange="changelicolor2"
                @resetChange="searchChange" />
            </div>
          </div>
        </div>
      </template>
      <template #dwbox>
        <ChooseUser v-model="chooseUserVisible" :Single="singleShow" :deptId='FromData.responsibilitydeptid'
          :data="memberList" @choosedUsers="getSonValue"></ChooseUser>
        <!-- 部门 -->
        <Choosebm :showDdld="showDept" @closezzjg="closezzjg" @listdata="getSonValue" :zzlx="zzlx" :searchType="'01'"
          :deptId='deptIdbm' :Isstrictly="Isstrictly" />
        <Dwhasbj :showDdld="showdwDept" @closezzjg="closezzjg" @listdata="getSonValue" :zzlx="zzlx" :searchType="'03'"
          :Isstrictly="Isstrictly" />
      </template>
      <template #footer>
        <el-button type="primary" v-loading="saveloading" @click="saveData(ruleFormRef)"
          v-if='props.formData.saveBtnShow'>
          <el-icon>
            <DocumentChecked />
          </el-icon>
          保存
        </el-button>
        <el-button @click="closeDialog()">
          <el-icon>
            <DocumentDelete />
          </el-icon>
          关闭
        </el-button>
      </template>
    </MOSTY.FromPage>
  </div>
</template>

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

const { proxy } = getCurrentInstance()
const { D_BZ_SYZT } = proxy.$dict("D_BZ_SYZT")
const rules = {
  flx: [{
    required: true,
    message: "请选择特种设备类型",
    trigger: "blur"
  }],
  sitename: [{
    required: true,
    message: "请选择所属场所",
    trigger: "blur"
  }],
  buildingname: [{
    required: true,
    message: "请选择所属建筑物",
    trigger: "blur"
  }],
  floorname: [{
    required: true,
    message: "请选择所属楼层",
    trigger: "blur"
  }],
  partname: [{
    required: true,
    message: "请选择所属部位",
    trigger: "blur"
  }]
}
const rules2 = {
  responsibilitypersonname: [{
    required: true,
    message: "请选择设备负责人",
    trigger: "change"
  }],
  responsibilitydeptname: [{
    required: true,
    message: "请选择责任部门",
    trigger: "change"
  }],
  userorgname: [{
    required: true,
    message: "请选择使用单位",
    trigger: "change"
  }],
  aqorgname: [{
    required: true,
    message: "请选择安全管理部门",
    trigger: "change"
  }],
  aqusername: [{
    required: true,
    message: "请选择安全管理人员",
    trigger: "change"
  }],
  ...rule.phoneRule({
    validator: true,
    require: true
  }, "aqphone")
}
const showDept = ref(false)
const showdwDept = ref(false)
const saveloading = ref(false)
const chooseUserVisible = ref(false) //选择领导弹窗
const singleShow = ref(true) //单选多选
const memberList = ref([])
const zzlx = ref(null)
const keys = ref(1)
const deptId = ref(null)
const deptIdbm = ref(null)
const isgetFrom = ref(false)
const Isstrictly = ref(false)
const ruleFormRef = ref()
const ruleFormRef2 = ref()
const isShowdt = ref(true)
const pageData = reactive({
  componentObj: null
})
const xgflist = ref([])
const isShowgd = ref(true)
const csmclist = ref([])
const jzwlist = ref([])
const lclist = ref([])
const bwlist = ref([])
const FromData = ref({
  state: "01",
  saveBtnShow: true,
  flx: "1"
})

// 01 安全装置 02 防护装置 03 保护装置 04 安全附件
const tablist1 = ref([
  {
    title: "风险告知卡",
    num: 0,
    icon: "sbss2",
    actives: true,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Fxgzk.vue"))
  },
  {
    title: "安全装置",
    num: 0,
    icon: "sbss2",
    opt: { type: "01" },
    actives: false,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Area.vue"))
  },
  {
    title: "防护装置",
    num: 0,
    icon: "sbss2",
    opt: { type: "02" },
    actives: false,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Area.vue"))
  },
  {
    title: "保护装置",
    num: 0,
    icon: "sbss2",
    opt: { type: "03" },
    actives: false,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Area.vue"))
  },
  {
    title: "安全附件",
    num: 0,
    icon: "sbss2",
    opt: { type: "04" },
    actives: false,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Area.vue"))
  },
  {
    title: "特种设备操作人员",
    num: 0,
    icon: "sbss2",
    actives: false,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Tzsbczry.vue"))
  }
])
const tablist = ref([
  {
    title: "电梯",
    lable: "1",
    components: defineAsyncComponent(() => import("@/components/dialog/addcoment/Adddt.vue"))
  }, // 电梯
  {
    title: "锅炉",
    lable: "2",
    components: defineAsyncComponent(() => import("@/components/dialog/addcoment/Addgl.vue"))
  }, // 锅炉
  {
    title: "压力容器",
    lable: "3",
    components: defineAsyncComponent(() => import("@/components/dialog/addcoment/Addylrq.vue"))
  }, // 压力容器
  {
    title: "压力管道",
    lable: "4",
    actives: false,
    components: defineAsyncComponent(() => import("@/components/dialog/addcoment/Addylgd.vue"))
  }, // 压力管道
  {
    title: "场内机动车",
    lable: "5",
    components: defineAsyncComponent(() => import("@/components/dialog/addcoment/Addcljdc.vue"))
  }, // 场内机动车
  {
    title: "起重机械",
    lable: "6",
    components: defineAsyncComponent(() => import("@/components/dialog/addcoment/Addqzjx.vue"))
  }, // 起重机械
  {
    title: "工业气瓶",
    lable: "7",
    components: defineAsyncComponent(() => import("@/components/dialog/addcoment/Addgyqp.vue"))
  } // 工业气瓶

])
const tablist2 = ref([
  {
    title: "检查标准",
    num: 0,
    icon: "sbss2",
    sblx: "200500000000",
    actives: true,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Synthesize.vue"))
  },
  {
    title: "检测记录",
    num: 0,
    icon: "sbss2",
    actives: false,
    components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Testing.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/Standard.vue"))
  }
])

const emits = defineEmits("saveSuccess", "createPage")
// 接收子组件传来的
const props = defineProps({
  formData: {
    type: Object,
    default: {}
  },
  type: {
    default: "",
    type: String
  }
})
const addtype = ref("tzsb")
onMounted(() => {
  addtype.value = props.formData.type ? props.formData.type : "tzsb"
  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.zbid = 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.qtmc = props.formData.qtmc ? props.formData.qtmc : 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
  getxgflist()
  if (props.formData.id) {
    getaqzzcount()
    if (props.formData.flx == "1") {
      getmain(`/mosty-jcgl/dt/getDtById/${props.formData.id}`)
    } else if (props.formData.flx == "2") {
      getmain(`/mosty-jcgl/gl/getGlById/${props.formData.id}`)
    } else if (props.formData.flx == "3") {
      getmain(`/mosty-jcgl/ylrq/getYlrqById/${props.formData.id}`)
    } else if (props.formData.flx == "4") {
      getmain(`/mosty-jcgl/ylgd/getYlgdById/${props.formData.id}`)
    } else if (props.formData.flx == "5") {
      getmain(`/mosty-jcgl/cnjdc/getCnjdcById/${props.formData.id}`)
    } else if (props.formData.flx == "6") {
      getmain(`/mosty-jcgl/qzjx/getQzjxById/${props.formData.id}`)
    } else if (props.formData.flx == "7") {
      getmain(`/mosty-jcgl/gyqp/getGyqpById/${props.formData.id}`)
    }
  } else {
    pageData.componentObj = tablist.value[0].components
    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 (["tzsb"].includes(addtype.value)) {
      getcsData()
    }
  }
})

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

function change(type) {
  switch (type) {
    case "ccrq":
      formData.value.azsj = ""
      formData.value.trsysj = ""
      break
    case "azsj":
      formData.value.trsysj = ""
      break
    default:
      break
  }
}

// 获取相关方责任人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
}

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

  })
}

//安全装置总数
function getaqzzcount() {
  getApi({ zbid: props.formData.id }, "/mosty-jcgl/SbglAqzz/countTbSbglAqzz").then(res => {
    tablist1.value[1].num = res ? res : 0
  })
}

//根据操作人 查询 场所
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
    }
  })
}

//	根据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)
    }
  })
}

// (新)根据楼层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 changelx(val) {
  tablist.value.forEach(item => {
    if (item.lable == val) {
      pageData.componentObj = item.components
    }
  })
}

//点击领导选择按钮
function ChooesDdld(val) {
  zzlx.value = val
  memberList.value = []
  deptId.value = null
  Isstrictly.value = false
  deptIdbm.value = null
  if (val == "01") {
    //设备负责人：
    if (FromData.value.responsibilitydeptid) {
      deptId.value = 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 {
    Isstrictly.value = false
    deptIdbm.value = FromData.value.userorgcode
    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
    if (value.id) {
      getaqbm({ orgcode: value.id }).then((res) => {
        FromData.value.aqorgname = res.orgName
        FromData.value.aqorgcode = res.id
      })
    }
  } else if (zzlx.value == "03") {
    FromData.value.aqorgname = value.orgName
    FromData.value.aqorgcode = value.id
  } else if (zzlx.value == "04") {
    FromData.value.aqusername = value[0].userName
    FromData.value.aquserid = value[0].id
  } else if (zzlx.value == "05") {
    FromData.value.responsibilitydeptname = value.orgName
    FromData.value.responsibilitydeptid = value.id
  }
}

//关闭弹窗
const closezzjg = (val) => {
  showDept.value = val
  showdwDept.value = false
}

// 关闭弹窗
function closeDialog() {
  if (props.formData.saveBtnShow) {
    proxy.$modal.confirm("离开会丢失页面中修改的内容，确认离开吗？")
      .then(() => {
        ruleFormRef.value.clearValidate()
        ruleFormRef.value.resetFields()
        emits("saveSuccess")
      })
      .catch(() => {
        // catch error
      })
  } else {
    ruleFormRef.value.clearValidate()
    ruleFormRef.value.resetFields()
    emits("saveSuccess")
  }

}

//分类点击事件
function changelicolor(val) {
  tablist1.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 getmain(mosty) {
  getApi({}, mosty).then(res => {
    if (res) {
      res.responsibilitypersonname = res.responsibilitypersonname ? res.responsibilitypersonname.replace(/（null）/g, "") : ""
      FromData.value = res
      FromData.value.state = res.state ? res.state : "01"
      FromData.value.saveBtnShow = props.formData.saveBtnShow
      FromData.value.qtmc = props.formData.qtmc ? props.formData.qtmc : null
      FromData.value.zbid = props.formData.partid ? props.formData.partid : null
      FromData.value.flx = props.formData.flx
      getlcxq(FromData.value.buildingid)
      gettotal()
      if (props.formData.type == "cs") {
        getjzwlist(res.siteid)
      } else if (props.formData.type == "lc") {
        getbwdata(res.floorid)
      } else if (["tzsb"].includes(addtype.value)) {
        getcsData()
      }
      if (res.partid) {
        getApi({ id: res.partid }, "/mosty-jcgl/part/queryScenePartById").then(res => {
          if (res) {
            FromData.value.partname = res.qtmc ? res.qtmc : res.partname
            FromData.value.checktype = res.checktype
            FromData.value.qtmc = res.qtmc
          }
        })
      }
      tablist.value.forEach(item => {
        if (item.lable == props.formData.flx) {
          pageData.componentObj = item.components
        }
      })

    }

  })
}

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 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 saveData = async (formEl) => {
  keys.value++
  isgetFrom.value = false
  if (!formEl) return
  await formEl.validate((valid) => {
    if (valid) {
      ruleFormRef2.value.validate((v) => {
        if (v) {
          FromData.value.picture = FromData.value.picture ? FromData.value.picture.toString() : null
          FromData.value.deviceFile = FromData.value.deviceFile ? FromData.value.deviceFile.toString() : null
          FromData.value.otherFile = FromData.value.otherFile ? FromData.value.otherFile.toString() : null
          isgetFrom.value = false

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

//获取组件表单的内容
async function savechange(val) {
  let valid = await ruleFormRef.value.validate()
  if (!valid) return
  const valid2 = await ruleFormRef2.value.validate()
  if (!valid2) return
  isgetFrom.value = true
  let obj = { ...FromData.value, ...val }
  obj.picture = obj.picture ? obj.picture.toString() : ""
  obj.deviceFile = obj.deviceFile ? obj.deviceFile.toString() : ""
  obj.otherFile = obj.otherFile ? obj.otherFile.toString() : ""
  if (props.formData.title === "特种设备复制") {
    obj.id = ''
  }
  if (obj.id) {
    if (obj.flx == "1") {
      // 修改电梯
      updataData(obj, "/mosty-jcgl/dt/updateSbglDtOne")
    }
    if (obj.flx == "2") {
      updataData(obj, "/mosty-jcgl/gl/updateSbglBoilerOne")
    } else if (obj.flx == "3") {
      updataData(obj, "/mosty-jcgl/ylrq/updateYlrqOne")
    } else if (obj.flx == "4") {
      updataData(obj, "/mosty-jcgl/ylgd/updateYlgdOne")
    } else if (obj.flx == "5") {
      updataData(obj, "/mosty-jcgl/cnjdc/updateCnjdcOne")
    } else if (obj.flx == "6") {
      updataData(obj, "/mosty-jcgl/qzjx/updateQzjxOne")
    } else if (obj.flx == "7") {
      updataData(obj, "/mosty-jcgl/gyqp/updateGyqpOne")
    }
  } else {
    if (obj.flx == "1") {
      // 新增电梯
      addData(obj, "/mosty-jcgl/dt/addSbglDtOne")
    } else if (obj.flx == "2") {
      //新增锅炉
      addData(obj, "/mosty-jcgl/gl/addSbglBoilerOne")
    } else if (obj.flx == "3") {
      addData(obj, "/mosty-jcgl/ylrq/addYlrqOne")
    } else if (obj.flx == "4") {
      addData(obj, "/mosty-jcgl/ylgd/addYlgdOne")
    } else if (obj.flx == "5") {
      addData(obj, "/mosty-jcgl/cnjdc/addCnjdcOne")
    } else if (obj.flx == "6") {
      addData(obj, "/mosty-jcgl/qzjx/addQzjxOne")
    } else if (obj.flx == "7") {
      addData(obj, "/mosty-jcgl/gyqp/addGyqpOne")
    }
  }

}

//新增
function addData(obj, mosty) {
  postApi(obj, mosty)
    .then((res) => {
      ElMessage.success("新增成功")
      // emits("saveSuccess", res)
      emits("createPage", res)
    }).finally(() => {
      saveloading.value = false
    })
}

//修改
function updataData(obj, mosty) {
  saveloading.value = true
  postApi(obj, mosty)
    .then((res) => {
      ElMessage.success("修改成功")
      emits("saveSuccess")
    }).finally(() => {
      saveloading.value = false
    })
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  --el-dialog-margin-top: 10vh !important;
}

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

.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-item__content {
  margin-left: 0px !important;
}
</style>
