<!--
@ 作者: 秦东
@ 时间: 2025-08-12 15:48:52
@ 备注: 编辑排班记录
-->
<script lang="ts" setup>
import { geiOrgAllPeople, saveEditDutyInfo, getDutyCont } from "@/api/hr/paiban/index";
import { get_org } from "@/api/opk/zxy/news/api";
const props = defineProps({
  isOpen: {
    type: Boolean,
    default: false,
  },
  cumpanyId: {
    type: Number,
    default: "",
  },
  dutyCont: {
    type: Object,
    default() {
      return {};
    },
  },
});
const loading = ref(false);
const emits = defineEmits(["update:isOpen", "pickRefresh"]);
const isShow = computed({
  get() {
    // console.log("对话框状态",props.isOpen);

    return props.isOpen;
  },
  set(val: boolean) {
    // console.log("对话框状态",props.isOpen);
    emits("update:isOpen", val);
  },
});
const currtOrg = ref(props.cumpanyId);
const saveInfo = ref(props.dutyCont);
const orgPeopleList = ref([]);
//关闭比阿尼系统
const closeSavePage = () => {
  emits("update:isOpen", false);
};
//监听
watch(
  () => props.isOpen,
  (val: boolean) => {
    if (val) {
      getOrgPeople();
    }
  },
  {
    deep: true,
  }
);
//获取行政组织人员信息
const getOrgPeople = () => {
  loading.value = true;

  getDutyCont({ id: saveInfo.value.id.toString() })
    .then((res) => {
      console.log("获取单一排版信息", res);
      saveInfo.value.allDay = res.data.allDayAry;
      saveInfo.value.baiTian = res.data.baiTianAry;
      saveInfo.value.night = res.data.nightAry;
      saveInfo.value.isCompany = 1;
    })
    .finally(() => {
      console.log("------------>", saveInfo.value.isCompany);
      if (saveInfo.value.isCompany == 1) {
        get_org({
          id: props.cumpanyId.toString(),
          all: 1,
        })
          .then((res: any) => {
            console.log("获取行政组织人员信息get_org", res);
            orgPeopleList.value = res.data;
            loading.value = false;
          })
          .finally(() => {
            loading.value = false;
          });
      } else {
        geiOrgAllPeople({
          orgId: saveInfo.value.orgid * 1,
          companyId: currtOrg.value * 1,
          isAll: saveInfo.value.isCompany * 1,
        })
          .then((res: any) => {
            console.log("获取行政组织人员信息geiOrgAllPeople", res);
            orgPeopleList.value = res.data;
            loading.value = false;
          })
          .finally(() => {
            loading.value = false;
          });
      }
    });
};
//提交数据
const saveCenter = () => {
  console.log("提交数据", saveInfo.value);
  saveInfo.value.holiday = saveInfo.value.holiday.toString();
  saveEditDutyInfo(saveInfo.value).then((res: any) => {
    closeSavePage();
    emits(
      "pickRefresh",
      props.cumpanyId,
      saveInfo.value.years,
      saveInfo.value.month,
      saveInfo.value.orgid
    );
  });
};
const orgTreeProps = {
  children: "child",
  label: "name",
}; //行政组织树对照值
</script>
<template>
  <el-dialog
    v-model="isShow"
    :title="
      saveInfo.years + '年' + saveInfo.month + '月' + saveInfo.days + '日编辑值班信息'
    "
    width="500"
    :before-close="closeSavePage"
  >
    <el-form ref="ruleFormRef" :model="saveInfo">
      <el-form-item label="行政组织" prop="orgname">
        <el-input v-model="saveInfo.orgname" disabled />
      </el-form-item>
      <el-form-item label="节假日" prop="holiday">
        <el-switch
          v-model="saveInfo.holiday"
          active-value="1"
          inline-prompt
          inactive-value="2"
          active-text="是"
          inactive-text="否"
        />
      </el-form-item>
      <el-form-item v-if="saveInfo.holiday == 1" label="白天" prop="baiTian">
        <el-tree-select
          v-if="saveInfo.isCompany == 1"
          v-model="saveInfo.baiTian"
          :data="orgPeopleList"
          multiple
          clearable
          filterable
          :props="orgTreeProps"
          node-key="id"
          :render-after-expand="false"
          style="width: 300px"
          v-loading="loading"
        />
        <el-tree-select
          v-else
          v-model="saveInfo.baiTian"
          :data="orgPeopleList"
          multiple
          clearable
          filterable
          :props="orgTreeProps"
          node-key="userKey"
          :render-after-expand="false"
          style="width: 300px"
          v-loading="loading"
        />
      </el-form-item>
      <el-form-item v-if="saveInfo.holiday == 1" label="夜晚" prop="night">
        <el-tree-select
          v-if="saveInfo.isCompany == 1"
          v-model="saveInfo.night"
          :data="orgPeopleList"
          multiple
          clearable
          filterable
          :props="orgTreeProps"
          node-key="id"
          :render-after-expand="false"
          style="width: 300px"
          v-loading="loading"
        />
        <el-tree-select
          v-else
          v-model="saveInfo.night"
          :data="orgPeopleList"
          multiple
          clearable
          filterable
          :props="orgTreeProps"
          node-key="userKey"
          :render-after-expand="false"
          style="width: 300px"
          v-loading="loading"
        />
      </el-form-item>
      <el-form-item v-else label="值班" prop="allDay">
        <el-tree-select
          v-if="saveInfo.isCompany == 1"
          v-model="saveInfo.allDay"
          :data="orgPeopleList"
          multiple
          clearable
          filterable
          :props="orgTreeProps"
          node-key="id"
          :render-after-expand="false"
          style="width: 300px"
          v-loading="loading"
        />
        <el-tree-select
          v-else
          v-model="saveInfo.allDay"
          :data="orgPeopleList"
          multiple
          clearable
          filterable
          :props="orgTreeProps"
          node-key="userKey"
          :render-after-expand="false"
          style="width: 300px"
          v-loading="loading"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeSavePage">取消</el-button>
        <el-button type="primary" @click="saveCenter"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style lang="scss" scoped></style>
