<!--
@ 作者: 秦东
@ 时间: 2025-03-19 13:27:12
@ 备注: 岗位选择
-->
<script lang="ts" setup>
import { basis_org_postList,getgovcont, getpositioncont,getOrgTreeList } from "@/api/hr/org/index";

import { orgInfo } from "@/api/hr/org/type";

const props = withDefaults(
  defineProps<{
    modelValue?: string;
    disabled?: boolean;
    type?: number;
    data?: object;
  }>(),
  {}
);
const emits = defineEmits<{
  (e: "update:modelValue", value: string): void;
}>();
const value = computed({
  get: () => {
    if (props.modelValue != "" && props.modelValue != undefined) {
      console.log("初始结果", props.modelValue);
      let ads = props.modelValue.split("#@#");
      console.log("初始结果", ads);
      if (ads.length >= 2) {
        orgPostInfo.orgId = ads[0] * 1;
        orgPostInfo.postId = ads[1] * 1;
        orgChange(ads[0] * 1);
      } else if (ads.length == 1) {
        orgPostInfo.orgId = ads[0] * 1;
        orgChange(ads[0] * 1);
      } else {
        orgPostInfo.orgId = "";
        orgPostInfo.postId = "";
      }
      //   if (props.type == 4) {
      //     writeInfo(props.modelValue);
      //     return valueHtml;
      //   } else {
      //     return props.modelValue;
      //   }
      return props.modelValue;
    } else {
      return props.modelValue;
    }
  },
  set: (newVal: any) => {
    emits("update:modelValue", newVal);
  },
});
const postOptions = ref([]);
const valueHtml = ref("");
const valueHtmlPost = ref("");
const orgTreeList = ref<orgInfo[]>();
const orgTreeLoading = ref(false); //加载行政组织树
const orgTreeProps = {
  children: "child",
  label: "name",
}; //行政组织树对照值
const orgPostTreeLoading = ref(false);
const orgPostInfo = reactive({
  orgId: "",
  postId: "",
});

function haveOrgTreeInfo() {
  orgTreeLoading.value = true;
  getOrgTreeList({ orgid: 309 })
    .then(({ data }) => {
      // console.log("行政组织树对照值", data);
      orgTreeList.value = data;
    })
    .finally(() => {
      orgTreeLoading.value = false;
    });
}

onBeforeMount(() => {
  haveOrgTreeInfo();
});

onMounted(() => {
  // console.log("value---1--》",value.value,props.modelValue)
  nextTick(() => {
    haveOrgTreeInfo();
    if (props.type == 4) {
      console.log("value---1--》", props.modelValue);
    }
  });
});
/**
@ 作者: 秦东
@ 时间: 2025-03-20 14:43:57
@ 功能: 改变行政组织获取相应岗位
*/
const orgChange = (val: any) => {
  //   console.log("改变行政组织获取相应岗位", val);
  orgPostTreeLoading.value = true;
  basis_org_postList({ id: [val] })
    .then(({ data }) => {
      console.log("改变行政组织获取相应岗位", data);
      postOptions.value = data;
    })
    .finally(() => {
      orgPostTreeLoading.value = false;
    });
};
watch(
  () => orgPostInfo,
  (val: any) => {
    if (val.orgId != "") {
      value.value = val.orgId;
      if (val.postId != "") {
        value.value = val.orgId + "#@#" + val.postId;
        if (props.type == 4) {
          if (postOptions.value && postOptions.value.length > 0) {
            orgPostInfo.postId.forEach((item: any) => {
              if (item.id == val.postId) {
                value.value = value.value + item.name;
              }
            });
          }
        }
      } else {
        value.value = "";
      }
    } else {
      value.value = "";
    }
  },
  {
    deep: true,
  }
);
/**
@ 作者: 秦东
@ 时间: 2025-03-20 16:06:01
@ 功能: 获取行政组织基本信息
*/
const gainOrgCont = (val: string) => {
  let name = "";
  getgovcont({ id: val * 1, idstr: val })
    .then(({ data }) => {
      console.log("选择行政组织-3->", data, data.name);
      // orgName.value =  data.name
      valueHtml.value = data.name;
    })
    .finally(() => {});
  return name;
};

/**
@ 作者: 秦东
@ 时间: 2025-03-20 16:36:03
@ 功能: 获取岗位
*/
const getPostInfo = (val: string) => {
  getpositioncont({ id: val * 1, idstr: val }).then(({ data }) => {
    console.log("选择行政组织----4->", data, data.name);
    valueHtmlPost.value = data.name;
  });
};

/**
@ 作者: 秦东
@ 时间: 2025-03-20 16:15:48
@ 功能: 输出结果
*/
const writeInfo = (val: any) => {
  console.log("输出结果-3->", val);
  if (val != "" && val != undefined) {
    let ads = val.split("#@#");
    if (ads.length >= 2) {
      gainOrgCont(ads[0]);
      getPostInfo(ads[1]);
    } else if (ads.length == 1) {
      gainOrgCont(ads[0]);
    }
  }
  let sendInfo = valueHtml.value;
  if (valueHtmlPost.value != "") {
    sendInfo = sendInfo + "-" + valueHtmlPost.value;
  }
  return sendInfo;
};
</script>
<template>
  <el-row v-if="props.type != 4">
    <el-col :span="24">
      <el-tree-select
        v-bind="$props"
        v-model="orgPostInfo.orgId"
        v-loading="orgTreeLoading"
        node-key="id"
        :props="orgTreeProps"
        :data="orgTreeList"
        check-strictly
        b
        :render-after-expand="false"
        placeholder="请选择行政组织"
        @change="orgChange"
        clearable
      />
    </el-col>
    <el-col :span="24">
      <el-select
        v-model="orgPostInfo.postId"
        v-loading="orgPostTreeLoading"
        placeholder="请选择岗位"
        clearable
      >
        <el-option
          v-for="item in postOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
    </el-col>
    <el-input style="display: none" v-model="value" placeholder="请选择" clearable />
  </el-row>
  <div v-else v-html="writeInfo(value)"></div>
</template>
<style lang="scss" scoped>
.el-row {
  width: 100%;
}
</style>
