<!--
@ 作者: 秦东
@ 时间: 2025-02-18 09:22:13
@ 备注: 设置调班锚点
-->
<script lang="ts" setup>
import { gaveAllApp, ginTeamRule, setupRulerForm } from "@/api/hr/people/index";

const props = defineProps({
  open: {
    type: Boolean,
    default: false,
  },
});
const emits = defineEmits(["update:open"]);
const formContent = reactive({
  appKey: "",
  tableKey: "",
});
const sendData = reactive({
  source_id: "",
  appid: 0,
  dataBaseName: "",
  ip: "",
  port: 0,
  password: "",
  data_source: "",
  sqlType: "",
  table_key: "",
  table_name: "",
  userName: "",
});
const appList = ref<any>([]);
const tableList = ref<any>([]);
/**
 * 弹窗显示控制
 */
const isShow = computed({
  get: () => {
    if (props.open) {
    }
    return props.open;
  },
  set: (val) => {
    emits("update:open", val);
  },
});
/**
@ 作者: 秦东
@ 时间: 2025-02-18 09:29:28
@ 功能: 关闭表单
*/
const closeDialog = () => {
  emits("update:open", false);
};

/**
@ 作者: 秦东
@ 时间: 2025-02-18 10:44:01
@ 功能: 获取app列表应用
*/
const gaveAllAppList = () => {
  gaveAllApp()
    .then(({ data }) => {
      // console.log(" 获取app列表应用", data);
      appList.value = data;
    })
    .finally(() => {
      ginTeamRule().then(({ data }) => {
        // console.log(" 获取app列表应用=====>", data);
        if (data.appid != 0) {
          formContent.appKey = data.appid.toString();
        }
        formContent.sdatableKey = data.tableKey.toString();
      });
    });
};

watch(
  () => formContent.appKey,
  (val: any) => {
    if (appList && appList.value.length > 0) {
      appList.value.forEach((iten: any) => {
        if (iten.app_id == val) {
          tableList.value = iten.list;
          // console.log(" 获取app列表应用==选中的===>", iten);
        }
      });
    }
  },
  {
    deep: true,
  }
);
/**
@ 作者: 秦东
@ 时间: 2025-02-18 11:29:53
@ 功能: 提交调班配置表
*/
const onSubmitPick = () => {
  if (appList && appList.value.length > 0) {
    appList.value.forEach((iten: any) => {
      if (iten.app_id == formContent.appKey) {
        sendData.appid = iten.app_id;
      }
    });
  }
  if (tableList.value && appList.value.length > 0) {
    tableList.value.forEach((iten: any) => {
      // console.log(
      //   " 获取app列表应用==路边内需===>",
      //   iten.table_key,
      //   "---------->",
      //   formContent.sdatableKey
      // );

      if (iten.table_key == formContent.sdatableKey) {
        sendData.dataBaseName = iten.dataBaseName;
        sendData.ip = iten.ip;
        sendData.port = iten.port;
        sendData.password = iten.password;
        sendData.data_source = iten.data_source;
        sendData.sqlType = iten.sqlType;
        sendData.table_key = iten.table_key;
        sendData.table_name = iten.table_name;
        sendData.userName = iten.userName;
        sendData.source_id = iten.source_id;
      }
    });
  }
  // console.log("提交调班配置表", sendData);
  setupRulerForm(sendData).then((data: any) => {
    // console.log("提交调班配置表===========234===========>", data);
    closeDialog();
  });
};

onMounted(() => {
  gaveAllAppList();
});
</script>
<template>
  <el-dialog
    v-model="isShow"
    width="400"
    title="设置调班表单"
    append-to-body
    :before-close="closeDialog"
  >
    <el-form :model="formContent" label-width="auto">
      <el-form-item label="关联App">
        <el-select v-model="formContent.appKey" placeholder="请选择关联App">
          <el-option
            v-for="item in appList"
            :label="item.app_name"
            :value="item.app_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关联流程表单">
        <el-select v-model="formContent.sdatableKey" placeholder="请选择关联App">
          <el-option
            v-for="item in tableList"
            :label="item.table_name"
            :value="item.table_key"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmitPick">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<style lang="scss" scoped></style>
