<template>
  <div class="bigBox">
    <MOSTY.FromPage :title="data.config.title" @closeDialog="black">
      <template #content>
        <MOSTY.Assort :title="'基本信息'" />
        <el-form ref="ruleFormRef" :model="data.fromObj" :rules="rules" :disabled="!data.config.saveBtnShow"
          label-width="186px" class="demo-ruleForm" status-icon :inline="true">
         <el-form-item label="相关方类型:" prop="type">j
            <MOSTY.DictRidioAndSelect placeholder="请选择相关方类型" style='width:337px'
              :dictData="data.config.zdlist.D_TB_RELATED_TYPE" v-model="data.fromObj.type" :disabled="data.fromObj.id"
              selectType="select" @getselect="handleSelectType" />
          </el-form-item>
          <el-form-item label="相关方名称:" prop="relatedname">
            <el-input v-model="data.fromObj.relatedname" style='width:337px' placeholder="请输入相关方名称" clearable
              class="input-with-select" @blur="nameBlur" @clear="clearXgf">
              <template #append>
               <el-button v-if="xgflist.length > 0" @click="chooseXgf">选择</el-button>
                <span v-else>输入</span>
                <!-- <span>输入</span> -->
              </template>
            </el-input>
            <!-- <el-select v-if="xgflist.length > 0" v-model="data.fromObj.relatedname" filterable
              :disabled="data.fromObj.id" @clear="dataFilter" ref="selectName" allow-create @change="namechange"
              @blur="nameBlur" style="width: 337px" clearable placeholder="请选择相关方">
              <el-option v-for="item in xgflist" :key="item.id" :label="item.relatedname" @click="changecgx(item)"
                :value="item.id" />
            </el-select>
            <el-input v-else v-model="data.fromObj.relatedname" placeholder="请输入相关方名称" clearable
              :disabled="data.fromObj.id" style="width: 337px"></el-input> -->
          </el-form-item>
          <el-form-item label="归口责任部门:" prop="responsibilitydeptid">
            <el-input v-if="disabledMain" v-model="data.fromObj.responsibilitydeptidData" :disabled="disabledMain"
              style="width:337px" placeholder="请选择归口责任部门" clearable></el-input>
            <MOSTY.Department v-else style="width: 337px" :searchType="'04'" placeholder="请选择归口责任部门" clearable
              :deptType="'01'" v-model="data.fromObj.responsibilitydeptidData"
              :deptId="data.fromObj.id ? props.editObj.orgid : data.fromObj.orgid" @depitem="handleSelectStatusRe"
              :disabled="disabledMain" />
          </el-form-item>
          <el-form-item label="归口责任人:" prop="responsibilitypersonname">
            <el-input v-model="data.fromObj.responsibilitypersonname" :disabled="disabledMain" readonly clearable
              style="width: 337px">
              <template #append>
                <el-button class='cursor' @click="ChooesDdld('02')" :disabled="disabledMain">选择</el-button></template>
            </el-input>
          </el-form-item>
          <el-form-item label="属地管理部门:" prop="belongDeptId">
            <el-input v-if="disabledMain" v-model="data.fromObj.belongDeptId" :disabled="disabledMain"
              style="width:337px" placeholder="请选择属地管理部门" clearable></el-input>
            <el-cascader v-else style="width: 337px" class="el-cascader-zj" :show-all-levels="false"
              placeholder='请选择属地管理部门' :options="putUnderDeptData" v-model="data.fromObj.belongDeptId"
              @change="handleSelectStatus" :props="cascaderProps" clearable />
          </el-form-item>
          <el-form-item label="属地管理责任人:" prop="belongResponsibilityName">
            <el-input v-model="data.fromObj.belongResponsibilityName" readonly clearable style="width: 337px"
              :disabled="disabledMain">
              <template #append>
                <el-button class='cursor' @click="ChooesDdld('01')" :disabled="disabledMain">选择</el-button></template>
            </el-input>
          </el-form-item>
          <el-form-item label="相关方地址:" prop="relatedadd">
            <el-input v-model="data.fromObj.relatedadd" style="width:890px" placeholder="请输入相关方地址" clearable></el-input>
          </el-form-item>
          <el-form-item label="相关方业务类型:" prop="relatedtype">
            <MOSTY.DictRidioAndSelect placeholder="请选择相关方业务类型" style='width:890px'
              :dictData="data.config.zdlist.D_TB_RELATED" v-model="data.fromObj.relatedtype" :multiple="true"
              selectType="select" @onChange="relatedChange" />
          </el-form-item>
          <el-form-item label="进场总人数:" prop="personNum">
            <el-input v-model="data.fromObj.personNum" type="number" style="width: 337px" />
          </el-form-item>
          <!-- :prop="`related_${index}`" -->
          <el-form-item v-for="(label, index) in relatedLabel" :label="label + '进场人数'">
            <el-input v-model="relatedval[index]" type="number" style="width: 337px" />
          </el-form-item>
          <el-form-item label="信用代码或营业证号:" prop="businesslicense" v-if="data.fromObj.type == '01'">
            <el-input v-model="data.fromObj.businesslicense" placeholder="请输入信用代码或营业证号" clearable
              :disabled="data.fromObj.xgfid && data.fromObj.id" style="width: 337px" @blur="businessBlur"></el-input>
          </el-form-item>
          <el-form-item label="法人姓名:" prop="legalperson" v-if="data.fromObj.type == '01'">
            <el-input v-model="data.fromObj.legalperson" placeholder="请输入法人姓名" clearable
              :disabled="data.fromObj.xgfid && data.fromObj.id" style="width: 337px"></el-input>
          </el-form-item>
          <el-form-item label="法人身份证号:" v-if="data.fromObj.type == '01'">
            <el-input v-model="data.fromObj.legalpersonidnumber" :disabled="data.fromObj.xgfid && data.fromObj.id"
              placeholder="请输入法人身份证号" clearable style="width: 337px"></el-input>
          </el-form-item>
          <el-form-item label="业务联系人姓名:" prop="contactsname" v-if="data.fromObj.type == '01'">
            <el-input v-model="data.fromObj.contactsname" placeholder="请输入业务联系人姓名" clearable
              style="width: 337px"></el-input>
          </el-form-item>
          <el-form-item label="业务联系人联系电话:" prop="contactsphone" v-if="data.fromObj.type == '01'">
            <el-input v-model="data.fromObj.contactsphone" placeholder="请输入业务联系人联系电话" clearable @input="inputChange"
              style="width: 337px"></el-input>
          </el-form-item>
          <el-form-item label="相关方联系电话:" prop="legalpersonphone" v-if="data.fromObj.type == '02'">
            <el-input v-model="data.fromObj.legalpersonphone" placeholder="请输入相关方联系电话" clearable
              :disabled="data.fromObj.xgfid && data.fromObj.id" style="width: 337px"></el-input>
          </el-form-item>
          <el-form-item label="相关方身份证号:" v-if="data.fromObj.type == '02'">
            <el-input v-model="data.fromObj.idCard" :disabled="data.fromObj.xgfid && data.fromObj.id"
              placeholder="请输入相关方身份证号" clearable style="width: 337px"></el-input>
          </el-form-item>
          <el-form-item label="是否签订安全协议:" prop="signSecurityProtocol">
            <MOSTY.DictRidioAndSelect placeholder="请选择是否签订安全协议" style='width:337px'
              :dictData="data.config.zdlist.D_BZ_SF" v-model="data.fromObj.signSecurityProtocol" selectType="select" />
          </el-form-item>
          <el-form-item label="合作开始时间:" prop="cooperationTime">
            <el-date-picker v-model="data.fromObj.cooperationTime" type="date" value-format="YYYY-MM-DD"
              style="width: 337px" placeholder="请选择合作开始时间" />
          </el-form-item>
          <el-form-item label="合作结束时间:" prop="cooperationEndTime">
            <el-date-picker v-model="data.fromObj.cooperationEndTime" type="date" value-format="YYYY-MM-DD"
              style="width: 337px" placeholder="请选择合作结束时间" />
          </el-form-item>
          <el-form-item label="是否常驻相关方:" prop="residentStakeholders">
            <MOSTY.DictRidioAndSelect placeholder="请选择是否常驻相关方" style='width:337px'
              :dictData="data.config.zdlist.D_BZ_SF" v-model="data.fromObj.residentStakeholders" selectType="select" />
          </el-form-item>
          <el-form-item label="合作频次:" prop="cooperationfrequency">
            <MOSTY.DictRidioAndSelect placeholder="请选择合作频次" style='width:337px'
              :dictData="data.config.zdlist.D_TB_RELATED_HZPC" v-model="data.fromObj.cooperationfrequency"
              selectType="select" />
          </el-form-item>
          <el-form-item label="是否涉及危险作业:" prop="involveDanger">
            <MOSTY.DictRidioAndSelect placeholder="请选择是否涉及危险作业" style='width:337px'
              :dictData="data.config.zdlist.D_BZ_SF" v-model="data.fromObj.involveDanger" selectType="select" />
          </el-form-item>
          <el-form-item label="是否具备相应资质:" prop="zzFlag">
            <MOSTY.DictRidioAndSelect placeholder="请选择是否具备相应资质" style='width:337px'
              :dictData="data.config.zdlist.D_BZ_SF" v-model="data.fromObj.zzFlag"
              :disabled="data.fromObj.xgfid && data.fromObj.id" selectType="select" />
          </el-form-item>
          <el-form-item label="营业执照：" :prop="data.fromObj.type == '02' ? '' : ''" style='width:100%;'>
            <MOSTY.Upload :width="'100%'" :limit="2" :nopicer="false" :isImg="true" :ShowfileList="false"
              v-model="data.fromObj.yyzzfile" :nameshow='true' :gsgfList="['jpg', 'png']"
              :disabled="data.fromObj.xgfid && data.fromObj.id" @filehandleChange="changeFiles">
            </MOSTY.Upload>
          </el-form-item>
          <el-form-item label="相关方资质附件:" :prop="data.fromObj.zzFlag == '1' ? 'zzfj' : ''">
            <MOSTY.Upload width="100%" :widthNumber="437" :limit="5" :nopicer="true" :isImg="false" :ShowfileList="true"
              :disabled="data.fromObj.xgfid && data.fromObj.id" v-model="data.fromObj.zzfj" :showinput="true"
              :gsgfList="['pdf', 'ofd', 'doc', 'docx', 'xls', 'xlsx', 'wps']" :delrow="delrow">
            </MOSTY.Upload>
          </el-form-item>
          <el-form-item label="法人身份证照片(正反面)：" style='width:100%;'>
            <MOSTY.Upload :width="'100%'" :limit="2" :nopicer="false" :isImg="true" :ShowfileList="false"
              v-model="data.fromObj.idcardfile" :nameshow='true' :gsgfList="['jpg', 'png']"
              @filehandleChange="changeFiles">
            </MOSTY.Upload>
          </el-form-item>
        </el-form>
        <div v-if='data.fromObj.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' :editObj="props.editObj" :obj="data.fromObj" :showCount="false"
                :isEdit="props.editObj.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='data.fromObj' @handleChange="changelicolor2" />
            </div>
          </div>
        </div>
      </template>
      <template #footer>
        <el-button type="primary" @click="submit(ruleFormRef)" v-loading='loadingshow'
          v-if="props.editObj.saveBtnShow"><el-icon>
            <DocumentChecked />
          </el-icon>保存</el-button>
        <el-button @click="black"><el-icon>
            <DocumentDelete />
          </el-icon>关闭</el-button>
      </template>
    </MOSTY.FromPage>
    <div v-if="comprehdjlShow" class="dialog-box">
      <Activity :title="actitle" :xgfid="data.fromObj.id" :rowId="rowId" :xgfywlx="data.fromObj.relatedtype"
        :zdlist="{ D_TB_RELATED }" :savebtn="showacbtn" @closePage="closePage" :type="'wxxfz'" />
    </div>
    <!-- 选择责任人 -->
    <ChooseUser v-model="chooseUserVisible" :Single="singleShow" :data="memberList" :leaderid="leaderid"
      :deptId="chooseDept" @choosedUsers="getSonValue"></ChooseUser>

    <!-- 相关方 -->
    <!-- <XgfDialog v-if="isShowXgfDialog" :ids="data.fromObj.id" :cName="data.fromObj.relatedname" :ssbmid="dept.ssbmid"
      :xgfType="data.fromObj.type" v-model:ids="data.fromObj.xgfid" @change="changecgx"
      @close="isShowXgfDialog = false" /> -->
    <MOSTY.Choosexgf v-if="isShowXgfDialog" v-model="isShowXgfDialog" :Single="singleShow" :type="data.fromObj.type"
      @confirm="changecgx" :ids="data.fromObj.xgfid">
    </MOSTY.Choosexgf>
  </div>
</template>

<script setup>
import { getImgUrl } from "@/utils/tools.js"
import { getItem } from "@/utils/storage";
import { reactive, ref, watchEffect, getCurrentInstance, onMounted, defineAsyncComponent } from "vue";
import * as MOSTY from "@/components/MyComponents/index";
import * as rule from "@/utils/rules.js";
import Activity from './Activity.vue'
import { ElMessage } from "element-plus";
import { getApi, postApi } from "@/api/tobAcco_api.js";
import XgfDialog from './XgfDialog.vue'
import ChooseUser from "@/components/MyComponents/ChooseUser/Leader.vue";
const { proxy } = getCurrentInstance();
const { D_TB_RELATED, } = proxy.$dict("D_TB_RELATED");
const keys = ref(0)
const props = defineProps({
  addDiaShow: {
    type: Boolean,
    default: false
  },
  editObj: {
    type: Object,
    default: () => { }
  },
  configobj: { type: Object, default: () => { } }
});
const rowId = ref(null)
const actitle = ref('作业活动新增')
const isShowXgfDialog = ref(false)
const comprehdjlShow = ref(false)
const showacbtn = ref(false)
const listQuery = reactive({
  pageCurrent: 1,
  pageSize: 5
});
const pageConfiger = ref({
  pageCurrent: 1,
  pageSize: 5,
  total: 0
})
const chooseDept = ref('')
const isShowgd = ref(true)
const isShowdt = ref(true)
const tablist = ref([
  { title: '作业人员', icon: 'sbss2', actives: true, components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Xgfzyry.vue")) },
  { title: '安全告知交底', icon: 'sbss2', actives: false, filetype: '01', components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/tab/addfj.vue")) },
  { title: '安全协议', icon: 'sbss2', actives: false, filetype: '02', components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/tab/aqgzjd.vue")) },
  { title: '风险管控清单', icon: 'sbss2', filetype: '03', actives: false, components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/tab/addfj.vue")) },
  { title: '其他', icon: 'sbss2', actives: false, filetype: '04', components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/tab/addfj.vue")) },
])
const tablist2 = ref([
  { title: '检查标准', icon: 'sbss2', actives: true, sblx: '900000000000', components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Synthesize.vue")) },
  { title: '作业监督记录', icon: 'sbss2', actives: false, components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/Standard.vue")) },
  { title: '评价资料', icon: 'sbss2', actives: false, components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/tab/xgfpjzl.vue")) },
  { title: '危险作业记录', icon: 'sbss2', actives: false, components: defineAsyncComponent(() => import("@/components/MyComponents/TabList/tab/xgfwxzyjl.vue")) },
])

const dataList = ref([])
const loadingshow = ref(false)
const chooseUserVisible = ref(false); //选择负责人弹窗
const memberList = ref([]); //成员选择的id回显
const leaderid = ref("");
const singleShow = ref(true); //单选多选
const delrow = ref({}); //删除文件列表
const dept = getItem('deptId')
const emit = defineEmits("changePage");
const xgflist = ref([])
const ruleFormRef = ref();
const data = reactive({
  formData: {},
  fromObj: {}
});
const zzlx = ref(null);
const rules = reactive({
  ...rule.phoneRule({ require: true, validator: true, type: '02', text: '座机号或手机号' }, "contactsphone"),
  ...rule.phoneRule({ require: true, validator: true, type: '02', text: '座机号或手机号' }, "legalpersonphone"),
  ...rule.identityCardRule({ require: false, validator: true }, "legalpersonidnumber"),
  ...rule.identityCardRule({ require: true, validator: true }, "idCard"),
  ...rule.NoTexts({ require: true, validator: true }, "businesslicense"),
  ...rule.intfsRule({ require: true, validator: true }, "personNum"),
  ...rule.intfsRule({ require: true, validator: true }, "related_0"),
  ...rule.intfsRule({ require: true, validator: true }, "related_1"),
  ...rule.intfsRule({ require: true, validator: true }, "related_2"),
  ...rule.intfsRule({ require: true, validator: true }, "related_3"),
  ...rule.intfsRule({ require: true, validator: true }, "related_4"),
  ...rule.intfsRule({ require: true, validator: true }, "related_5"),
  ...rule.intfsRule({ require: true, validator: true }, "related_6"),
  ...rule.intfsRule({ require: true, validator: true }, "related_7"),
  ...rule.intfsRule({ require: true, validator: true }, "related_8"),
  ...rule.intfsRule({ require: true, validator: true }, "related_9"),
  ...rule.intfsRule({ require: true, validator: true }, "related_10"),
  ...rule.intfsRule({ require: true, validator: true }, "related_11"),
  ...rule.intfsRule({ require: true, validator: true }, "related_12"),
  ...rule.intfsRule({ require: true, validator: true }, "related_13"),
  ...rule.intfsRule({ require: true, validator: true }, "related_14"),
  ...rule.intfsRule({ require: true, validator: true }, "related_15"),
  ...rule.intfsRule({ require: true, validator: true }, "related_16"),
  ...rule.intfsRule({ require: true, validator: true }, "related_17"),
  ...rule.intfsRule({ require: true, validator: true }, "related_18"),
  ...rule.intfsRule({ require: true, validator: true }, "related_19"),
  relatedname: [{ required: true, message: "请输入相关方名称", trigger: "blur" }],
  type: [{ required: true, message: "请选择相关方类型", trigger: "blur" }],
  relatedtype: [{ required: true, message: "请选择相关方业务类型", trigger: "blur" }],
  contactsname: [{ required: true, message: "请填写业务联系人", trigger: "blur" }],
  cooperationfrequency: [{ required: true, message: "请选择合作频次", trigger: "blur" }],
  belongResponsibilityName: [{ required: true, message: "请选择属地管理责任人", trigger: "input" }],
  belongDeptId: [{ required: true, message: "请选择属地管理部门", trigger: "input" }],
  responsibilitypersonname: [{ required: true, message: "请选择归口责任人", trigger: "input" }],
  responsibilitydeptid: [{ required: true, message: "请选择归口部门", trigger: "input" }],
  relatedadd: [{ required: true, message: "相关方地址", trigger: "blur" }],
  legalperson: [{ required: true, message: "法人姓名", trigger: "blur" }],
  // legalpersonidnumber: [{ required: true, message: "法人身份证号", trigger: "blur" }],
  signSecurityProtocol: [{ required: true, message: "是否签订安全协议", trigger: "blur" }],
  zzFlag: [{ required: true, message: "是否具备相应资质", trigger: "blur" }],
  cooperationTime: [{ required: true, message: "开始合作时间", trigger: "blur" }],
  cooperationEndTime: [{ required: true, message: "开始合作时间", trigger: "blur" }],
  residentStakeholders: [{ required: true, message: "是否常驻相关方", trigger: "blur" }],
  involveDanger: [{ required: true, message: "是否涉及危险作业", trigger: "blur" }],
  yyzzfile: [{ required: true, message: "请上传营业执照", trigger: "blur" }],
  zzfj: [{ required: true, message: "请上传相关方资质附件", trigger: "blur" }],
  // idcardfile: [{ required: true, message: "请上传法人身份证照片(正反面)", trigger: "blur" }],
});
onMounted(() => {
  data.config = props.editObj
  // data.fromObj.belongDeptId = dept.ssbmid
  // data.fromObj.belongDeptName = dept.ssbmmc
  getmain(props.editObj.id);
  getAvtivityList()
  getPutUnderDept()
});
watchEffect(() => {
  if (props.editObj) {
    data.config = props.editObj;
    // data.fromObj.belongDeptName = dept.ssbmmc
  }
});
const putUnderDeptData = ref([])
const cascaderProps = {
  value: 'id',
  label: 'orgname',
  children: 'children'
}
function getPutUnderDept() {
  // getApi({}, '/mosty-base/deptFeign/getPutUnderDept')
  getApi({ type: '01' }, '/mosty-base/deptFeign/getDeptAndJgbm')
    .then(res => {
      putUnderDeptData.value = res
    })
}
function inputChange(val) {
  data.fromObj.contactsphone = val && val.replace(/[^\d-]/g, '');
}
function handleSelectStatus(val) {
  const selectedId = val[val.length - 1];
  const selectedNode = findNodeById(putUnderDeptData.value, selectedId);
  if (selectedNode) {
    data.fromObj.belongDeptId = selectedNode.id
    data.fromObj.belongDeptName = selectedNode.orgname
    // 部门变化时 清空 属地责任人,
    data.fromObj.belongResponsibilityName = ''
    data.fromObj.belongResponsibilityId = ''
  } else {
    data.fromObj.belongDeptId = ''
    data.fromObj.belongDeptName = ''
    // 部门变化时 清空 属地责任人,
    data.fromObj.belongResponsibilityName = ''
    data.fromObj.belongResponsibilityId = ''
  }
}
function handleSelectStatusRe(val) {
  data.fromObj.responsibilitydeptid = val.orgCode;
  data.fromObj.responsibilitydeptname = val.orgName;
  // 部门变化时 清空 归口责任人,
  data.fromObj.responsibilitypersonname = '';
  data.fromObj.responsibilitypersonid = '';
}
// 递归在树形数据中根据id查找节点
function findNodeById(tree, id) {
  for (let node of tree) {
    if (node.id === id) {
      return node;
    }
    if (node.children && node.children.length > 0) {
      const found = findNodeById(node.children, id);
      if (found) {
        return found;
      }
    }
  }
  return null;
}
function handleSelectType(val) {
  clearXgf()
  getxgflist(val.value)
}
function clearXgf() {
  // if (data.fromObj.xgfid) {
  data.fromObj.relatedname = ''
  data.fromObj.relatedadd = null
  data.fromObj.relatedtype = null
  data.fromObj.businesslicense = null
  data.fromObj.legalperson = null
  data.fromObj.legalpersonidnumber = null
  data.fromObj.contactsname = null
  data.fromObj.contactsphone = null
  data.fromObj.signSecurityProtocol = null
  data.fromObj.cooperationTime = null
  data.fromObj.personNum = null
  data.fromObj.residentStakeholders = null
  data.fromObj.cooperationfrequency = null
  data.fromObj.involveDanger = null
  data.fromObj.legalpersonphone = null
  data.fromObj.yyzzfile = null
  data.fromObj.zzFlag = null
  data.fromObj.zzfj = null
  data.fromObj.idcardfile = null
  data.fromObj.xgfid = null
  // }
}
function dataFilter(val) {
  if (data.fromObj.xgfid) {
    data.fromObj.relatedname = val
    data.fromObj.relatedadd = null
    data.fromObj.relatedtype = null
    data.fromObj.businesslicense = null
    data.fromObj.legalperson = null
    data.fromObj.legalpersonidnumber = null
    data.fromObj.contactsname = null
    data.fromObj.contactsphone = null
    data.fromObj.signSecurityProtocol = null
    data.fromObj.cooperationTime = null
    data.fromObj.personNum = null
    data.fromObj.residentStakeholders = null
    data.fromObj.cooperationfrequency = null
    data.fromObj.involveDanger = null
    data.fromObj.legalpersonphone = null
    data.fromObj.yyzzfile = null
    data.fromObj.zzFlag = null
    data.fromObj.zzfj = null
    data.fromObj.idcardfile = null
    data.fromObj.xgfid = null
  }

}
/** 打开相关方弹框 */
function chooseXgf() {
  isShowXgfDialog.value = true
}
function nameBlur(e) {
  let value = e.target.value; // 输入框值
  if (value) {
    // this.$set向响应式对象添加一个属性和值，这个就是在this.formTerminal中添加name属性和值，值就是输入框的内容
    data.fromObj.relatedname = value
    if (data.fromObj.xgfid) {
      dataFilter(value)
    }
  }
}

//获取相关方
function getxgflist(type) {
  getApi({ deptid: dept.ssbmid, type }, '/mosty-jcgl/relatedpartyinfo/getXgfList').then(res => {
    xgflist.value = res.records ? res.records : []
  })
}
/** 删除空格 */
function removeEmpty(str) {
  if (typeof str !== 'string') return ''
  return str.replace(/\s+/g, '')
}
function businessBlur() {
  data.fromObj.businesslicense = removeEmpty(data.fromObj.businesslicense)
}

function changecgx(val) {
  data.fromObj.relatedadd = val.relatedadd
  data.fromObj.relatedtype = val.relatedtype ? val.relatedtype.split(',') : []
  data.fromObj.businesslicense = val.businesslicense ? val.businesslicense.replace(/\s+/g, '') : ''
  data.fromObj.legalperson = val.legalperson
  data.fromObj.legalpersonidnumber = val.legalpersonidnumber
  data.fromObj.contactsname = val.contactsname
  data.fromObj.contactsphone = val.contactsphone
  data.fromObj.relatedname = val.relatedname
  data.fromObj.signSecurityProtocol = val.signSecurityProtocol
  data.fromObj.cooperationTime = val.cooperationTime
  data.fromObj.personNum = val.personNum
  data.fromObj.residentStakeholders = val.residentStakeholders
  data.fromObj.cooperationfrequency = val.cooperationfrequency
  data.fromObj.involveDanger = val.involveDanger
  data.fromObj.legalpersonphone = val.legalpersonphone
  data.fromObj.yyzzfile = val.yyzzfile
  data.fromObj.zzFlag = val.zzFlag
  data.fromObj.zzfj = val.zzfj
  data.fromObj.idcardfile = val.idcardfile
  data.fromObj.xgfid = val.id
}
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;
  leaderid.value = "";
  if (val == "01") {
    if (data.fromObj.belongDeptId) {
      leaderid.value = data.fromObj.belongResponsibilityId;
      chooseDept.value = data.fromObj.belongDeptId
      singleShow.value = true;
      chooseUserVisible.value = true;
    } else {
      ElMessage.error("请先选择属地管理部门！");
    }
  } else if (val == "02") {
    if (data.fromObj.responsibilitydeptid) {
      leaderid.value = data.fromObj.responsibilitypersonid;
      chooseDept.value = data.fromObj.responsibilitydeptid
      singleShow.value = true;
      chooseUserVisible.value = true;
    } else {
      ElMessage.error("请先选择归口管理部门！");
    }
  }
}
const getSonValue = (value) => {
  if (zzlx.value == "01") {
    data.fromObj.belongResponsibilityName = value[0].userName;
    data.fromObj.belongResponsibilityId = value[0].id;
  } else if (zzlx.value == "02") {
    data.fromObj.responsibilitypersonname = value[0].userName;
    data.fromObj.responsibilitypersonid = value[0].id;
    data.fromObj.belongDeptId = data.fromObj.responsibilitydeptid
    data.fromObj.belongDeptName = data.fromObj.responsibilitydeptname
    tablist.value[0].data = [data.fromObj.responsibilitydeptid, data.fromObj.responsibilitydeptname]
    data.fromObj.belongResponsibilityName = value[0].userName;
    data.fromObj.belongResponsibilityId = value[0].id;
  }
};
// 保存
const submit = async (formEl) => {
  if (!formEl) return
  let dynamicValid = true;
  for (let i = 0; i < relatedval.value.length; i++) {
    const val = relatedval.value[i];
    if (val === '' || val === null || val === undefined || val < 0) {
      ElMessage.error(`请正确填写${relatedLabel.value[i]}进场人数`);
      dynamicValid = false;
      break;
    }
  }
  if (!dynamicValid) return;
  const total = relatedval.value.reduce((sum, val) => sum + (Number(val) || 0), 0);
  if (total !== Number(data.fromObj.personNum)) {
    ElMessage.error('各项进场人数之和应等于进场总人数');
    return;
  }
  await formEl.validate((valid, fields) => {
    if (valid) {
      loadingshow.value = true
      data.fromObj.relatedtype = data.fromObj.relatedtype ? data.fromObj.relatedtype.toString() : "";
      data.fromObj.yyzzfile = data.fromObj.yyzzfile ? data.fromObj.yyzzfile.toString() : "";
      data.fromObj.zzfj = data.fromObj.zzfj ? data.fromObj.zzfj.toString() : "";
      data.fromObj.idcardfile = data.fromObj.idcardfile ? data.fromObj.idcardfile.toString() : "";
      data.fromObj.personChildNums = relatedval.value.join();
      data.fromObj.status = null
      if (belongDeptIdOld.value) {
        data.fromObj.belongDeptId = belongDeptIdOld.value
      }
      if (data.fromObj.id) {
        postApi(data.fromObj, "/mosty-jcgl/relatedpartyinfo/update").then(() => {
          ElMessage.success("修改成功");
          emit("changePage", "Main");
        }).finally(() => {
          loadingshow.value = false
          data.fromObj.relatedtype = data.fromObj.relatedtype ? data.fromObj.relatedtype.split(',') : "";
        })
      } else {
        postApi(data.fromObj, "/mosty-jcgl/relatedpartyinfo/add").then(() => {
          ElMessage.success("新增成功");
          emit("changePage", "Main");
        }).finally(() => {
          loadingshow.value = false
          data.fromObj.relatedtype = data.fromObj.relatedtype ? data.fromObj.relatedtype.split(',') : "";
        })
      }
    } else {
      ElMessage.error("请检查表单!");
    }
  });
};
const disabledMain = ref(false)
const belongDeptIdOld = ref('')
const relatedtypeData = ref([{ "label": "建筑施工和拆除", "value": "01", "elTagType": null }, { "label": "房屋修缮和装修装潢", "value": "02", "elTagType": null }, { "label": "设备设施安装拆除和维修保养", "value": "03", "elTagType": null }, { "label": "食堂运行", "value": "04", "elTagType": null }, { "label": "污水处理运行", "value": "05", "elTagType": null }, { "label": "绿化保洁", "value": "06", "elTagType": null }, { "label": "外墙清洗", "value": "07", "elTagType": null }, { "label": "杀虫作业", "value": "08", "elTagType": null }, { "label": "危害物品供应和运输", "value": "09", "elTagType": null }, { "label": "生产性业务外包", "value": "10", "elTagType": null }, { "label": "保安服务", "value": "11", "elTagType": null }, { "label": "消防服务", "value": "12", "elTagType": null }, { "label": "物业服务", "value": "13", "elTagType": null }, { "label": "房屋承租", "value": "14", "elTagType": null }, { "label": "物流运输", "value": "15", "elTagType": null }, { "label": "车辆维修及保养", "value": "16", "elTagType": null }, { "label": "咨询服务", "value": "17", "elTagType": null }, { "label": "其他", "value": "99", "elTagType": null }])
//获取详情
function getmain(id) {
  if (id) {
    getApi({ id }, "/mosty-jcgl/relatedpartyinfo/getInfo").then((res) => {
      if (res) {
        data.fromObj = res
        let orgid = getItem("deptId").ssbmid
        if (orgid === res.orgid) {
          data.fromObj.belongDeptId = res.belongDeptId
          data.fromObj.responsibilitydeptidData = res.responsibilitydeptid
          disabledMain.value = false
        } else {
          belongDeptIdOld.value = res.belongDeptId
          data.fromObj.belongDeptId = res.belongDeptName
          data.fromObj.responsibilitydeptidData = res.responsibilitydeptname
          disabledMain.value = true
        }
        res.relatedtype = res.relatedtype ? res.relatedtype.split(',') : []
        if (res.relatedtype && res.relatedtype.length > 0) {
          let labels = []
          res.relatedtype.forEach(values => {
            relatedtypeData.value.forEach(item => {
              if (item.value == values) {
                labels.push(item.label)
              }
            });
          });
          relatedChange(res.relatedtype, labels);
        }
        if (data.fromObj.type) {
          getxgflist(data.fromObj.type)
        }
      }
    });
  }
}
const relatedLabel = ref()
const relatedval = ref()
const relatedChange = (val, label) => {
  relatedLabel.value = label
  relatedval.value = new Array(label.length).fill(0)
  if (label.length === 1) {
    relatedval.value[0] = parseInt(data.fromObj.personNum)
  } else {
    relatedval.value = data.fromObj.personChildNums ? data.fromObj.personChildNums?.split(',') : relatedval.value
  }
}
const black = () => {
  if (props.editObj.saveBtnShow) {
    proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？')
      .then(() => {
        // 关闭上一次的表单校验
        ruleFormRef.value.clearValidate();
        emit("changePage", "Main");
      })
      .catch(() => {
        // catch error
      })
  } else {
    // 关闭上一次的表单校验
    ruleFormRef.value.clearValidate();
    emit("changePage", "Main");
  }

};

// 切换分页
const changeActivityNo = (val) => {
  listQuery.pageCurrent = val;
  pageConfiger.value.pageCurrent = val;
  getAvtivityList();
};
// 切换每页显示
const changeActivitySize = (val) => {
  listQuery.pageSize = val;
  pageConfiger.value.pageSize = val;
  getAvtivityList();
};
// 添加记录
const addActivity = () => {
  actitle.value = "作业活动新增"
  rowId.value = null
  showacbtn.value = true
  comprehdjlShow.value = true
}
//修改记录
function xguser(row) {
  actitle.value = "作业活动修改"
  rowId.value = row.id
  showacbtn.value = true
  comprehdjlShow.value = true
}
//查看记录
function userxq(row) {
  actitle.value = "作业活动详情"
  rowId.value = row.id
  showacbtn.value = false
  comprehdjlShow.value = true
}
//删除记录
function deldetery(row) {
  postApi({}, `/mosty-jcgl/activity/del/${row.id}`).then(res => {
    ElMessage.success("删除成功");
    getAvtivityList()
  })
}
function closePage(params) {
  actitle.value = ""
  rowId.value = null
  showacbtn.value = false
  comprehdjlShow.value = false
  getAvtivityList()
}
// 查询作业活动记录
const getAvtivityList = () => {
  listQuery.relatedId = props.editObj.id
  getApi(listQuery, "/mosty-jcgl/activity/getPageList").then((res) => {
    if (res) {
      dataList.value = res.records
      pageConfiger.value.total = res.total
    } else {
      dataList.value = [];
    }
  });
}

</script>

<style lang="scss" scoped>
.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-image {
  margin: 0;
  padding: 1px;
}



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

::v-deep .el-form-item__content {
  margin-left: 10px !important;
}

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

.bigBox {
  display: block !important;
  padding-right: 14px;
  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;
  }
}

.headBox {
  display: flex;
  justify-content: end;
  width: 100%;
  margin-bottom: 10px;
}

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