<template>
  <div>
    <el-dialog v-model="props.shownewwb" :title="props.wbobjtitle" width="600px" :before-close="closeaddwb">
      <div class="frombox">
        <el-form ref="ruleFormRef2" :model="fromData" label-width="130px" status-icon :inline="true"
          :disabled="!props.saveBtnShow" style="display:block">
          <!-- <el-form-item label="分管领导:" v-if="props.type == 'dw'">
            <el-input v-model="fromData.fgldname" clearable readonly :disabled="true">
              <template #append>
                <el-button class='cursor' @click="ChooesDdld('03')" :disabled="true">选择</el-button></template>
</el-input>
</el-form-item> -->
          <!-- <el-form-item label="安全部门负责人:" v-if="props.type == 'dw'">
            <el-input v-model="fromData.principalheadname" clearable readonly :disabled="true">
              <template #append>
                <el-button class='cursor' @click="ChooesDdld('04')" :disabled="true">选择</el-button></template>
            </el-input>
          </el-form-item> -->
          <!-- <el-form-item label="车队队长:" v-if="props.type == 'dw'">
            <el-input v-model="fromData.cdzname" clearable readonly>
              <template #append>
                <el-button class='cursor' @click="ChooesDdld('05')">选择</el-button></template>
            </el-input>
          </el-form-item> -->
          <el-form-item label="车辆管理部门负责人:">
            <el-input v-model="fromData.cdzname" clearable readonly>
              <template #append>
                <el-button class='cursor' @click="ChooesDdld('07')">选择</el-button></template>
            </el-input>
          </el-form-item>
          <el-form-item label="车管员:">
            <el-input v-model="fromData.cgyname" clearable readonly>
              <template #append>
                <el-button class='cursor' @click="ChooesDdld('06')">选择</el-button></template>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeaddwb"> <el-icon>
              <DocumentDelete />
            </el-icon>
            <span>关闭</span></el-button>
        </span>
      </template>
    </el-dialog>

  </div>
  <ChooseUser v-model="chooseUserVisible" :Single="singleShow" :leaderid="leaderid" @choosedUsers="getSonValue"
    :deptId="deptId">
  </ChooseUser>
</template>

<script setup>
import {
  ref,
  onMounted
} from "vue";
import ChooseUser from "@/components/MyComponents/ChooseUser/Leader.vue";
import { getApi, postApi } from "@/api/tobAcco_api.js";
import { ElMessage, ElMessageBox } from "element-plus";
const props = defineProps({
  shownewwb: Boolean,
  saveBtnShow: Boolean,
  wbobjtitle: String,
  type: String,
  obj: Object

});
const fromData = ref({})
const deptId = ref('')
const leaderid = ref("");
const singleShow = ref(true);
const zzlx = ref(null);
const chooseUserVisible = ref(false);
const emit = defineEmits("changeclose");

onMounted(() => {
  if (props.obj) {
    fromData.value = props.obj
  }
});


function ChooesDdld(val) {
  zzlx.value = val;
  leaderid.value = "";
  deptId.value = null
  if (val == "03") {
    deptId.value = fromData.value.orgid;
    leaderid.value = fromData.value.fgid;
    singleShow.value = true;
    chooseUserVisible.value = true;
  } else if (val == "04") {
    // 获取安全部门下的人员id
    getApi({ id: fromData.value.orgid }, '/mosty-base/deptFeign/getAqbmId').then(res => {
      deptId.value = res;
      leaderid.value = fromData.value.principalheadid;
      singleShow.value = true;
      chooseUserVisible.value = true;
    })

  } else if (val == "05") {
    deptId.value = fromData.value.orgid;
    leaderid.value = fromData.value.cdzid;
    singleShow.value = true;
    chooseUserVisible.value = true;
  } else if (val == "06") {
    deptId.value = fromData.value.orgid;
    leaderid.value = fromData.value.cgyid;
    singleShow.value = true;
    chooseUserVisible.value = true;
  } else if (val == "07") {
    deptId.value = fromData.value.orgid;
    leaderid.value = fromData.value.cgyid;
    singleShow.value = true;
    chooseUserVisible.value = true;
  }

}
const getSonValue = (value) => {

  if (zzlx.value == "03") {
    fromData.value.fgldname = value[0].userName;
    fromData.value.fgldid = value[0].id;
    // 保存分管领导
    xgfgld()
  }
  if (zzlx.value == "04") {
    fromData.value.principalheadname = value[0].userName;
    fromData.value.principalheadid = value[0].id;
    //修改安全部门负责人
    xgaqbnfzr()

  }
  if (zzlx.value == "05") {
    fromData.value.cdzname = value[0].userName;
    fromData.value.userid2 = value[0].id;
    //单位车队队长
    xgaqbmfzr()
  }
  if (zzlx.value == "06") {
    fromData.value.cgyname = value[0].userName;
    fromData.value.userid = value[0].id;
    //修改单位车管员
    xgdwcgy()
  }
  if (zzlx.value == "07") {
    fromData.value.cdzname = value[0].userName;
    fromData.value.userid = value[0].id;
    //修改单位车辆管理部门负责人
    xgdwclglbmfzr()
  }
};
//修改分管领导
function xgfgld() {
  postApi({ orgid: fromData.value.orgid, fgldid: fromData.value.fgldid, fgldname: fromData.value.fgldname },
    '/mosty-jcgl/securitychild/updateZgldById').then(res => {
      ElMessage.success("分管领导保存成功");
    })
}
//修改单位车队队长
function xgaqbmfzr() {
  postApi({ orgcode: fromData.value.orgid, userid: fromData.value.userid2, sptype: "3", username: fromData.value.cdzname },
    '/mosty-jcgl/sprole/update').then(res => {
      ElMessage.success("单位车队队长保存成功");
    })
}
// 修改单位车辆管理部门负责人
function xgdwclglbmfzr() {
  let prams = {}
  prams = {
    orgid: fromData.value.orgcode, orgcode: fromData.value.orgid, userid: fromData.value.userid, sptype: "3", username: fromData.value.cdzname
  }
  postApi(prams,
    '/mosty-jcgl/sprole/update').then(res => {
      ElMessage.success("单位车辆管理部门负责人保存成功");
    })
}
//修改单位车管员
function xgdwcgy() {
  let prams = {}
  if (props.type == 'dw') {
    prams = {
      orgcode: fromData.value.orgid, userid: fromData.value.userid, sptype: "2", username: fromData.value.cgyname
    }
  } else {
    prams = {
      orgid: fromData.value.orgcode, orgcode: fromData.value.orgid, userid: fromData.value.userid, sptype: "2", username: fromData.value.cgyname
    }
  }
  postApi(prams,
    '/mosty-jcgl/sprole/update').then(res => {
      ElMessage.success("单位车管员保存成功");
    })
}
//修改安全部门负责人
function xgaqbnfzr() {
  postApi({ orgid: fromData.value.orgid, fgldid: fromData.value.principalheadid, fgldname: fromData.value.principalheadname },
    '/mosty-jcgl/securitychild/updateZgldFzById').then(res => {
      ElMessage.success("安全部门负责人保存成功");
    })
}
function closeaddwb() {
  emit("changeclose");
}

</script>

<style lang="scss" scoped>
::v-deep .el-cascader,
::v-deep .el-select {
  width: 100%;
}

.Box {
  border: 1px solid var(--el-color-primary);
  margin-left: 150px;
  width: calc(100% - 300px);
  color: black;
  font-size: 20px;
}
</style>